source: documentation/trunk/php/common.php@ 25026

Last change on this file since 25026 was 25026, checked in by jmt12, 12 years ago

PHP scripts for importing and exporting the XML manuals into and out of Dokuwiki

File size: 4.5 KB
Line 
1<?php
2
3$base_path = getcwd();
4
5// The dokuwiki directory is always in the same place, relative to the PHP
6// directory.
7$dokuwiki_path = fileCat(array($base_path,'..','packages','dokuwiki'));
8$dokuwiki_url = "../packages/dokuwiki";
9
10// Similarly, the xml-source directory is also relative to the PHP directory.
11$xml_source_path = fileCat(array($base_path, '..', 'manuals', 'xml-source'));
12
13// Keep track of the id's we've seen to ensure when generating new ones they
14// are unique.
15$seen_ids = array();
16
17/**
18 */
19function fileCat($raw_parts)
20{
21 // 1. Expand out all of the parts first (as some may contain nested
22 // directories)
23 $long_path = array();
24 foreach ($raw_parts as $raw_part)
25 {
26 $long_path = array_merge($long_path, preg_split('/[\\\\\/]/', $raw_part));
27 }
28 // 2. Resolve any '..' in the path
29 $path = array();
30 foreach ($long_path as $part)
31 {
32 if ($part == '..')
33 {
34 array_pop($path);
35 }
36 else
37 {
38 $path[] = $part;
39 }
40 }
41 return implode('/', $path);
42}
43/** fileCat() **/
44
45/** Given a dokuwiki namespace use the wikis metadata to determine the file
46 * path to the most recently approved version of the page (which may not be
47 * the active page in the 'pages' directory, but an older version in the
48 * attic somewhere).
49 * @param $namespace A namespace in the form part:part:part
50 */
51function getApprovedVersionPath($namespace)
52{
53 global $dokuwiki_path;
54 $path = '#ERROR#';
55 $dokuwiki_data_path = $dokuwiki_path . '/data';
56 $namespace_path = str_replace(':','/',$namespace);
57 // 1. Build the path to the pages metadata file
58 $metadata_path = $dokuwiki_data_path . '/meta/' . $namespace_path . '.meta';
59 if (file_exists($metadata_path))
60 {
61 // 2. Deserialize the file
62 $page_metadata = unserialize(file_get_contents($metadata_path));
63
64 // 3. Look up the 'current' versions timestamp
65 $current_timestamp = $page_metadata['current']['date']['modified'];
66
67 // 4. Now look up the timestamp of the most recently approved version
68 $approved_timestamp = $current_timestamp; // default to the 'live' page
69 // - note that if a page have never been approved the 'approval' array
70 // will not exist
71 if (isset($page_metadata['current']['approval']) && !empty($page_metadata['current']['approval']))
72 {
73 $approved_timestamp = max(array_keys($page_metadata['current']['approval']));
74 }
75
76 // 5. If they are the same, we can simply return the path to the 'live'
77 // page in the 'pages' directory. Otherwise we construct a path to
78 // the 'approved' version, which lurks in the mirrored location in
79 // the 'attic' directory
80 if ($current_timestamp == $approved_timestamp)
81 {
82 $path = $dokuwiki_data_path . '/pages/' . $namespace_path . '.txt';
83 }
84 else
85 {
86 $path = $dokuwiki_data_path . '/attic/' . $namespace_path . '.' . $approved_timestamp . '.txt';
87 }
88 }
89 ///cho "[debug] Approved path: $path\n";
90 return $path;
91}
92/** getApprovedVersionPath() **/
93
94/** Given a text string generate a unique identifier.
95 */
96function generateID($text)
97{
98 global $seen_ids;
99 $text = preg_replace('/(<!--.*?-->|\(.*?\))/', '', $text);
100 $text = preg_replace('/\'s/', '', $text);
101 $text = strtolower(trim($text));
102 $poss_id = preg_replace('/\s+/', '_', $text);
103 $id = $poss_id;
104 $counter = 1;
105 while (isset($seen_ids[$id]))
106 {
107 $id = $poss_id . '_' . $counter;
108 $counter++;
109 }
110 $seen_ids[$id] = true;
111 return $id;
112}
113/** generateID($text) **/
114
115function mkAllDir($destination_dir, $mode)
116{
117 ///cho '<p> * Make all directories: ' . $destination_dir . '</p>' . "\n";
118 $dirs_to_create = array();
119 $dir = $destination_dir;
120 while (!empty($dir) && !file_exists($dir))
121 {
122 array_unshift($dirs_to_create, $dir);
123 $dir = substr($dir, 0, strrpos($dir, '/'));
124 }
125 foreach ($dirs_to_create as $dir)
126 {
127 mkdir($dir, 0755);
128 }
129 if (!file_exists($destination_dir))
130 {
131 printError('Failed to create directory: ' . $destination_dir);
132 }
133}
134
135/**
136 */
137function parseCLIArguments()
138{
139 if ($_SERVER['argc'])
140 {
141 for ($i = 1; $i < $_SERVER['argc'] && !$unknown_argument; $i++)
142 {
143 if (preg_match('/^\-+m/i', $_SERVER['argv'][$i]))
144 {
145 $i++;
146 $_REQUEST['m'] = $_SERVER['argv'][$i];
147 }
148 else
149 {
150 return false;
151 }
152 }
153 }
154 return true;
155}
156/** parseCLIArguments() **/
157
158
159/**
160 */
161function printError($message, $is_fatal=true)
162{
163 if ($is_fatal)
164 {
165 exit('<p><b>Error!</b> ' . $message . "</p>\n");
166 }
167 else
168 {
169 echo '<p><b>Error!</b> ' . $message . "</p>\n";
170 }
171}
172/** printError($message) **/
173
174?>
Note: See TracBrowser for help on using the repository browser.