Dotclear

source: plugins/importExport/inc/flat/class.flat.backup.php @ 3874:ab8368569446

Revision 3874:ab8368569446, 3.3 KB checked in by franck <carnet.franck.paul@…>, 7 years ago (diff)

short notation for array (array() → [])

Line 
1<?php
2/**
3 * @brief importExport, a plugin for Dotclear 2
4 *
5 * @package Dotclear
6 * @subpackage Plugins
7 *
8 * @copyright Olivier Meunier & Association Dotclear
9 * @copyright GPL-2.0-only
10 */
11
12if (!defined('DC_RC_PATH')) {return;}
13
14class flatBackup
15{
16    protected $fp;
17    private $line_cols = [];
18    private $line_name;
19    private $line_num;
20
21    private $replacement = [
22        '/(?<!\\\\)(?>(\\\\\\\\)*+)(\\\\n)/u' => "\$1\n",
23        '/(?<!\\\\)(?>(\\\\\\\\)*+)(\\\\r)/u' => "\$1\r",
24        '/(?<!\\\\)(?>(\\\\\\\\)*+)(\\\\")/u' => '$1"',
25        '/(\\\\\\\\)/'                        => '\\'
26    ];
27
28    public function __construct($file)
29    {
30        if (file_exists($file) && is_readable($file)) {
31            $this->fp       = fopen($file, 'rb');
32            $this->line_num = 1;
33        } else {
34            throw new Exception(__('No file to read.'));
35        }
36    }
37
38    public function __destruct()
39    {
40        if ($this->fp) {
41            @fclose($this->fp);
42        }
43    }
44
45    public function getLine()
46    {
47        if (($line = $this->nextLine()) === false) {
48            return false;
49        }
50
51        if (substr($line, 0, 1) == '[') {
52            $this->line_name = substr($line, 1, strpos($line, ' ') - 1);
53
54            $line            = substr($line, strpos($line, ' ') + 1, -1);
55            $this->line_cols = explode(',', $line);
56
57            return $this->getLine();
58        } elseif (substr($line, 0, 1) == '"') {
59            $line = preg_replace('/^"|"$/', '', $line);
60            $line = preg_split('/(^"|","|(?<!\\\)\"$)/m', $line);
61
62            if (count($this->line_cols) != count($line)) {
63                throw new Exception(sprintf('Invalid row count at line %s', $this->line_num));
64            }
65
66            $res = [];
67
68            for ($i = 0; $i < count($line); $i++) {
69                $res[$this->line_cols[$i]] =
70                    preg_replace(array_keys($this->replacement), array_values($this->replacement), $line[$i]);
71            }
72
73            return new flatBackupItem($this->line_name, $res, $this->line_num);
74        } else {
75            return $this->getLine();
76        }
77    }
78
79    private function nextLine()
80    {
81        if (feof($this->fp)) {
82            return false;
83        }
84        $this->line_num++;
85
86        $line = fgets($this->fp);
87        $line = trim($line);
88
89        return empty($line) ? $this->nextLine() : $line;
90    }
91}
92
93class flatBackupItem
94{
95    public $__name;
96    public $__line;
97    private $__data = [];
98
99    public function __construct($name, $data, $line)
100    {
101        $this->__name = $name;
102        $this->__data = $data;
103        $this->__line = $line;
104    }
105
106    public function f($name)
107    {
108        return iconv('UTF-8', 'UTF-8//IGNORE', $this->__data[$name]);
109    }
110
111    public function __get($name)
112    {
113        return $this->f($name);
114    }
115
116    public function __set($n, $v)
117    {
118        $this->__data[$n] = $v;
119    }
120
121    public function exists($n)
122    {
123        return isset($this->__data[$n]);
124    }
125
126    public function drop(...$args)
127    {
128        foreach ($args as $n) {
129            if (isset($this->__data[$n])) {
130                unset($this->__data[$n]);
131            }
132        }
133    }
134
135    public function substitute($old, $new)
136    {
137        if (isset($this->__data[$old])) {
138            $this->__data[$new] = $this->__data[$old];
139            unset($this->__data[$old]);
140        }
141    }
142}
Note: See TracBrowser for help on using the repository browser.

Sites map