1 | <?php
|
---|
2 |
|
---|
3 | $base_path = getcwd();
|
---|
4 | if (strpos($base_path, 'greenstone') === false)
|
---|
5 | {
|
---|
6 | printError('Failed to determine base path...');
|
---|
7 | }
|
---|
8 |
|
---|
9 | // The dokuwiki directory is always in the same place, relative to the PHP
|
---|
10 | // directory.
|
---|
11 | $dokuwiki_path = fileCat(array($base_path,'..','packages','dokuwiki'));
|
---|
12 | $dokuwiki_url = "../packages/dokuwiki";
|
---|
13 |
|
---|
14 | // Similarly, the xml-source directory is also relative to the PHP directory.
|
---|
15 | $xml_source_path = fileCat(array($base_path, '..', 'manuals', 'xml-source'));
|
---|
16 |
|
---|
17 | // Keep track of the id's we've seen to ensure when generating new ones they
|
---|
18 | // are unique.
|
---|
19 | $seen_ids = array();
|
---|
20 |
|
---|
21 | /**
|
---|
22 | */
|
---|
23 | function fileCat($raw_parts)
|
---|
24 | {
|
---|
25 | // 1. Expand out all of the parts first (as some may contain nested
|
---|
26 | // directories)
|
---|
27 | $long_path = array();
|
---|
28 | foreach ($raw_parts as $raw_part)
|
---|
29 | {
|
---|
30 | $long_path = array_merge($long_path, preg_split('/[\\\\\/]/', $raw_part));
|
---|
31 | }
|
---|
32 | // 2. Resolve any '..' in the path
|
---|
33 | $path = array();
|
---|
34 | foreach ($long_path as $part)
|
---|
35 | {
|
---|
36 | if ($part == '..')
|
---|
37 | {
|
---|
38 | array_pop($path);
|
---|
39 | }
|
---|
40 | else
|
---|
41 | {
|
---|
42 | $path[] = $part;
|
---|
43 | }
|
---|
44 | }
|
---|
45 | return implode('/', $path);
|
---|
46 | }
|
---|
47 | /** fileCat() **/
|
---|
48 |
|
---|
49 | /** Given a dokuwiki namespace use the wikis metadata to determine the file
|
---|
50 | * path to the most recently approved version of the page (which may not be
|
---|
51 | * the active page in the 'pages' directory, but an older version in the
|
---|
52 | * attic somewhere).
|
---|
53 | * @param $namespace A namespace in the form part:part:part
|
---|
54 | */
|
---|
55 | function getApprovedVersionPath($namespace)
|
---|
56 | {
|
---|
57 | global $dokuwiki_path;
|
---|
58 | $path = '#ERROR#';
|
---|
59 | $dokuwiki_data_path = $dokuwiki_path . '/data';
|
---|
60 | $namespace_path = str_replace(':','/',$namespace);
|
---|
61 | // 1. Build the path to the pages metadata file
|
---|
62 | $metadata_path = $dokuwiki_data_path . '/meta/' . $namespace_path . '.meta';
|
---|
63 | if (file_exists($metadata_path))
|
---|
64 | {
|
---|
65 | // 2. Deserialize the file
|
---|
66 | $page_metadata = unserialize(file_get_contents($metadata_path));
|
---|
67 |
|
---|
68 | // 3. Look up the 'current' versions timestamp
|
---|
69 | $current_timestamp = $page_metadata['current']['date']['modified'];
|
---|
70 |
|
---|
71 | // 4. Now look up the timestamp of the most recently approved version
|
---|
72 | $approved_timestamp = $current_timestamp; // default to the 'live' page
|
---|
73 | // - note that if a page have never been approved the 'approval' array
|
---|
74 | // will not exist
|
---|
75 | if (isset($page_metadata['current']['approval']) && !empty($page_metadata['current']['approval']))
|
---|
76 | {
|
---|
77 | $approved_timestamp = max(array_keys($page_metadata['current']['approval']));
|
---|
78 | }
|
---|
79 |
|
---|
80 | // 5. If they are the same, we can simply return the path to the 'live'
|
---|
81 | // page in the 'pages' directory. Otherwise we construct a path to
|
---|
82 | // the 'approved' version, which lurks in the mirrored location in
|
---|
83 | // the 'attic' directory
|
---|
84 | if ($current_timestamp == $approved_timestamp)
|
---|
85 | {
|
---|
86 | $path = $dokuwiki_data_path . '/pages/' . $namespace_path . '.txt';
|
---|
87 | }
|
---|
88 | else
|
---|
89 | {
|
---|
90 | $path = $dokuwiki_data_path . '/attic/' . $namespace_path . '.' . $approved_timestamp . '.txt';
|
---|
91 | }
|
---|
92 | }
|
---|
93 | ///cho "[debug] Approved path: $path\n";
|
---|
94 | return $path;
|
---|
95 | }
|
---|
96 | /** getApprovedVersionPath() **/
|
---|
97 |
|
---|
98 | /** Given a text string generate a unique identifier.
|
---|
99 | */
|
---|
100 | function generateID($text)
|
---|
101 | {
|
---|
102 | global $seen_ids;
|
---|
103 | $text = preg_replace('/(<!--.*?-->|\(.*?\))/', '', $text);
|
---|
104 | $text = preg_replace('/\'s/', '', $text);
|
---|
105 | $text = strtolower(trim($text));
|
---|
106 | $poss_id = preg_replace('/\s+/', '_', $text);
|
---|
107 | $id = $poss_id;
|
---|
108 | $counter = 1;
|
---|
109 | while (isset($seen_ids[$id]))
|
---|
110 | {
|
---|
111 | $id = $poss_id . '_' . $counter;
|
---|
112 | $counter++;
|
---|
113 | }
|
---|
114 | $seen_ids[$id] = true;
|
---|
115 | return $id;
|
---|
116 | }
|
---|
117 | /** generateID($text) **/
|
---|
118 |
|
---|
119 | function mkAllDir($destination_dir)
|
---|
120 | {
|
---|
121 | ///cho '<p> * Make all directories: ' . $destination_dir . '</p>' . "\n";
|
---|
122 | $dirs_to_create = array();
|
---|
123 | $dir = $destination_dir;
|
---|
124 | while (!empty($dir) && !file_exists($dir))
|
---|
125 | {
|
---|
126 | ///cho 'Does not exist - create...<br/>';
|
---|
127 | array_unshift($dirs_to_create, $dir);
|
---|
128 | $dir = substr($dir, 0, strrpos($dir, '/'));
|
---|
129 | ///cho 'Testing for the existance of: ' . $dir . '<br/>';
|
---|
130 | }
|
---|
131 | foreach ($dirs_to_create as $dir)
|
---|
132 | {
|
---|
133 | if (!file_exists($dir))
|
---|
134 | {
|
---|
135 | mkdir($dir, 0775);
|
---|
136 | }
|
---|
137 | }
|
---|
138 | if (!file_exists($destination_dir))
|
---|
139 | {
|
---|
140 | printError('Failed to create directory: ' . $destination_dir);
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | */
|
---|
146 | function parseCLIArguments()
|
---|
147 | {
|
---|
148 | if (isset($_SERVER['argc']) && $_SERVER['argc'] > 0)
|
---|
149 | {
|
---|
150 | for ($i = 1; $i < $_SERVER['argc'] && !$unknown_argument; $i++)
|
---|
151 | {
|
---|
152 | if (preg_match('/^\-+(l|m)/i', $_SERVER['argv'][$i], $matches))
|
---|
153 | {
|
---|
154 | $i++;
|
---|
155 | $_REQUEST[$matches[1]] = $_SERVER['argv'][$i];
|
---|
156 | }
|
---|
157 | else if (preg_match('/^\-+l/i', $_SERVER['argv'][$i]))
|
---|
158 | {
|
---|
159 | $i++;
|
---|
160 | $_REQUEST['l'] = $_SERVER['argv'][$i];
|
---|
161 | }
|
---|
162 | else
|
---|
163 | {
|
---|
164 | return false;
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|
168 | return true;
|
---|
169 | }
|
---|
170 | /** parseCLIArguments() **/
|
---|
171 |
|
---|
172 |
|
---|
173 | /**
|
---|
174 | */
|
---|
175 | function printError($message, $is_fatal=true)
|
---|
176 | {
|
---|
177 | if ($is_fatal)
|
---|
178 | {
|
---|
179 | exit('<p><b>Error!</b> ' . $message . "</p>\n");
|
---|
180 | }
|
---|
181 | else
|
---|
182 | {
|
---|
183 | echo '<p><b>Error!</b> ' . $message . "</p>\n";
|
---|
184 | }
|
---|
185 | }
|
---|
186 | /** printError($message) **/
|
---|
187 |
|
---|
188 |
|
---|
189 | /**
|
---|
190 | */
|
---|
191 | function recursiveRemove($dir, $base_dir, $remove_this_dir=false)
|
---|
192 | {
|
---|
193 | ///cho "<h2>recursiveRemove(\"$dir\", \"$base_dir\", $remove_this_dir)</h2>";
|
---|
194 | // Ensure that base_dir is within_dir - safety first!
|
---|
195 | if (strpos($dir, $base_dir) === false)
|
---|
196 | {
|
---|
197 | printError('Can\'t delete directory as is isn\'t within project');
|
---|
198 | }
|
---|
199 | if (is_dir($dir))
|
---|
200 | {
|
---|
201 | if ($dh = opendir($dir))
|
---|
202 | {
|
---|
203 | while (($file = readdir($dh)) !== false)
|
---|
204 | {
|
---|
205 | if (preg_match('/^\./', $file))
|
---|
206 | {
|
---|
207 | // Skip dot files
|
---|
208 | }
|
---|
209 | else
|
---|
210 | {
|
---|
211 | $path = fileCat(array($dir, $file));
|
---|
212 | if (is_dir($path))
|
---|
213 | {
|
---|
214 | recursiveRemove($path, $base_dir, true);
|
---|
215 | }
|
---|
216 | else
|
---|
217 | {
|
---|
218 | unlink($path);
|
---|
219 | if (file_exists($path))
|
---|
220 | {
|
---|
221 | printError('Failed to delete file: ' . $path);
|
---|
222 | }
|
---|
223 | }
|
---|
224 | }
|
---|
225 | }
|
---|
226 | closedir($dh);
|
---|
227 | }
|
---|
228 | else
|
---|
229 | {
|
---|
230 | printError('Failed to open directory for reading: ' . $dir);
|
---|
231 | }
|
---|
232 | // Now that the directory is (hopefully) empty, we can delete it if
|
---|
233 | // required to
|
---|
234 | if ($remove_this_dir)
|
---|
235 | {
|
---|
236 | rmdir($dir);
|
---|
237 | if (file_exists($dir))
|
---|
238 | {
|
---|
239 | printError('Failed to remove directory: ' . $dir);
|
---|
240 | }
|
---|
241 | }
|
---|
242 | }
|
---|
243 | else
|
---|
244 | {
|
---|
245 | printError('Can\'t recursive delete - not a directory: ' . $dir);
|
---|
246 | }
|
---|
247 | }
|
---|
248 | /** recursiveRemove() **/
|
---|
249 |
|
---|
250 | ?> |
---|