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

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

Adding comments and more tests to mkalldir function. Allowing language argument to be passed to CGI version.

File size: 4.7 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 echo '<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 echo 'Does not exist - create...<br/>';
123 array_unshift($dirs_to_create, $dir);
124 $dir = substr($dir, 0, strrpos($dir, '/'));
125 echo 'Testing for the existance of: ' . $dir . '<br/>';
126 }
127 foreach ($dirs_to_create as $dir)
128 {
129 if (!file_exists($dir))
130 {
131 mkdir($dir, 0755);
132 }
133 }
134 if (!file_exists($destination_dir))
135 {
136 printError('Failed to create directory: ' . $destination_dir);
137 }
138}
139
140/**
141 */
142function parseCLIArguments()
143{
144 if (isset($_SERVER['argc']) && $_SERVER['argc'] > 0)
145 {
146 for ($i = 1; $i < $_SERVER['argc'] && !$unknown_argument; $i++)
147 {
148 if (preg_match('/^\-+(l|m)/i', $_SERVER['argv'][$i], $matches))
149 {
150 $i++;
151 $_REQUEST[$matches[1]] = $_SERVER['argv'][$i];
152 }
153 else
154 {
155 return false;
156 }
157 }
158 }
159 return true;
160}
161/** parseCLIArguments() **/
162
163
164/**
165 */
166function printError($message, $is_fatal=true)
167{
168 if ($is_fatal)
169 {
170 exit('<p><b>Error!</b> ' . $message . "</p>\n");
171 }
172 else
173 {
174 echo '<p><b>Error!</b> ' . $message . "</p>\n";
175 }
176}
177/** printError($message) **/
178
179?>
Note: See TracBrowser for help on using the repository browser.