Debian Codegraph @

dojox/data/tests/stores/filestore_funcs.php

raw symbols SCIP
1 <?php
2 /**
3 * Helper function to convert a simple pattern to a regular expression for matching.
4 *
5 * Returns a regular expression object that conforms to the defined conversion rules.
6 * For example:
7 * ca* -> /^ca.*$/
8 * *ca* -> /^.*ca.*$/
9 * *c\*a* -> /^.*c\*a.*$/
10 * *c\*a?* -> /^.*c\*a..*$/
11 * and so on.
12 *
13 * @param pattern: string
14 * A simple matching pattern to convert that follows basic rules:
15 * * Means match anything, so ca* means match anything starting with ca
16 * ? Means match single character. So, b?b will match to bob and bab, and so on.
17 * \ is an escape character. So for example, \* means do not treat * as a match, but literal character *.
18 * To use a \ as a character in the string, it must be escaped. So in the pattern it should be
19 * represented by \\ to be treated as an ordinary \ character instead of an escape.
20 */
21 function patternToRegExp(/*String*/$pattern){
22 $rxp = "^";
23 $c = "";
24 $len = strlen($pattern);
25 for ($i = 0; $i < $len; $i++) {
26 $c = $pattern[$i];
27 switch ($c) {
28 case '\\':
29 $rxp = $rxp.$c;
30 $i++;
31 $rxp = $rxp.$pattern[$i];
32 break;
33 case '*':
34 $rxp = $rxp.".*"; break;
35 case '?':
36 $rxp = $rxp."."; break;
37 case '$':
38 case '^':
39 case '/':
40 case '+':
41 case '.':
42 case '|':
43 case '(':
44 case ')':
45 case '{':
46 case '}':
47 case '[':
48 case ']':
49 $rxp = $rxp."\\"; //fallthrough
50 default:
51 $rxp = $rxp.$c;
52 }
53 }
54 return "/(".$rxp."$)/";
55 }
56
57 /**
58 * Function to load all file info from a particular directory.
59 *
60 * @param $dir The dir to seach from, relative to $rootDir.
61 * @param $rootDir The directory where the file service is rooted, used as separate var to allow easier checking and prevention of ../ing out of the tree.
62 * @param $recurse Whether or not to deep scan the dir and return all subfiles, or just return the toplevel files.
63 * @param $dirsOnly boolean to enote to only return directory names, not filenames.
64 * @param $expand boolean to indicate whether or not to inflate all children files along a path/file, or leave them as stubs.
65 * @param $showHiddenFiles boolean to indicate to return hidden files as part of the list.
66 */
67 function getAllfiles($dir, $rootDir, $recurse, $dirsOnly, $expand, $showHiddenFiles) {
68 // summary:
69 // A function to obtain all the files in a particular directory (file or dir)
70 $files = array();
71 $dirHandle = opendir($rootDir."/".$dir);
72 if ($dirHandle) {
73 while($file = readdir($dirHandle)) {
74 if ($file) {
75 if ($file != ".." && $file != ".") {
76 $path = $dir."/".$file;
77 $fileObj = generateFileObj($file, $dir, $rootDir,$expand,$showHiddenFiles);
78 if (is_dir($rootDir."/".$path)) {
79 if ($recurse) {
80 if ($showHiddenFiles || $fileObj["name"][0] != '.') {
81 $subfiles = getAllfiles($path,$rootDir,$recurse,$dirsOnly,$expand,$showHiddenFiles);
82 $length = count($subfiles);
83 for ($i = 0; $i < $length; $i++) {
84 $files[] = $subfiles[$i];
85 }
86 }
87 }
88 }
89 if (!$dirsOnly || $fileObj["directory"]) {
90 if ($showHiddenFiles || $fileObj["name"][0] !== '.') {
91 $files[] = $fileObj;
92 }
93 }
94 }
95 }
96 }
97 }
98 closedir($dirHandle);
99 return $files;
100 }
101
102 /**
103 * Function to generate an associative map of data about a specific file.
104 * @param $file The name of the file this object represents.
105 * @param $dir The sub-path that contains the file defined by $file
106 * @param $rootDir The directory from which to append dir and name to get the full path to the file.
107 * @param $expand boolean to denote that if the file is a directory, expand all children in the children attribute
108 * to a a full object
109 * @param $showHiddenFiles boolean to denote if hidden files should be shown in-view or not.
110 *
111 * @return Associative Map. The details about the file:
112 * $file["name"] - Returns the shortname of the file.
113 * $file["parentDir"] - Returns the relative path from the service root for the parent directory containing file $file["name"]
114 * $file["path"] - The relative path to the file.
115 * $file["directory"] - Boolean indicator if the file represents a directory.
116 * $file["size"] - The size of the file, in bytes.
117 * $file["modified] - The modified date of the file in milliseconds since Jan 1st, 1970.
118 * $file["children"] - Children files of a directory. Empty if a standard file.
119 */
120 function generateFileObj($file, $dir, $rootDir, $expand, $showHiddenFiles) {
121 // summary:
122 // Function to generate an object representation of a disk file.
123 $path = $file;
124 if ($dir != "." && $dir != "./") {
125 $path = $dir."/".$file;
126 }
127
128 $fullPath = $rootDir."/".$path;
129
130 $atts = stat($fullPath);
131
132 $rootPath = realPath($rootDir);
133 $resolvedDir = realPath($rootDir."/".$dir);
134 $resolvedFullPath = realPath($fullPath);
135
136 //Try to normalize down the paths so it does a consistent return.
137 if (strcmp($rootPath, $resolvedDir) === 0) {
138 $dir = ".";
139 } else {
140 $dir = substr($resolvedDir, (strlen($rootPath) + 1), strlen($resolvedDir));
141 $dir = "./".str_replace("\\","/",$dir);
142 }
143 if (strcmp($rootPath, $resolvedFullPath) === 0) {
144 $path = ".";
145 } else {
146 $path = substr($resolvedFullPath, (strlen($rootPath) + 1), strlen($resolvedFullPath));
147 $path = "./".str_replace("\\","/",$path);
148 }
149
150 $fObj = array();
151 $fObj["name"] = $file;
152 $fObj["parentDir"] = $dir;
153 $fObj["path"] = $path;
154 $fObj["directory"] = is_dir($fullPath);
155 $fObj["size"] = filesize($fullPath);
156 $fObj["modified"] = $atts[9];
157
158 if (is_dir($fullPath)) {
159 $children = array();
160 $dirHandle = opendir($fullPath);
161 while($cFile = readdir($dirHandle)) {
162 if ($cFile) {
163 if ($cFile != ".." && $cFile != ".") {
164 if ($showHiddenFiles || $cFile[0] != '.') {
165 if (!$expand) {
166 $children[] = $cFile;
167 }else{
168 $children[] = generateFileObj($cFile, $path, $rootDir, $expand, $showHiddenFiles);
169 }
170 }
171 }
172 }
173 }
174 closedir($dirHandle);
175 $fObj["children"] = $children;
176 }
177 return $fObj;
178 }
179
180 /**
181 * A field comparator class, whose role it is to define which fields on an associaive map to compare on
182 * and provide the comparison function to do so.
183 */
184 class FieldComparator {
185 var $field;
186 var $descending = false;
187
188 /**
189 * Constructor.
190 * @param $f The field of the item to compare.
191 * @param $d Parameter denoting whether it should be ascending or descending. Default is ascending.
192 */
193 function FieldComparator($f, $d) {
194 $this->field = $f;
195 $this->descending = $d;
196 }
197
198 /**
199 * Function to compare file objects A and B on the field defined by $this->field.
200 * @param $fileA The first file to compare.
201 * @param #fileB The second file to compare.
202 */
203 function compare($fileA,$fileB){
204 $f = $this->field;
205 $a = $fileA[$f];
206 $b = $fileB[$f];
207
208 $ret = 0;
209 if (is_string($a) && is_string($b)) {
210 $ret = strcmp($a,$b);
211 } else if($a > $b || $a === null){
212 $ret = 1;
213 }else if($a < $b || $b === null){
214 $ret = -1;
215 }
216
217 if (property_exists($this, "descending") && $this->descending == true) {
218 $ret = $ret * -1;
219 }
220
221 if ($ret > 0) {
222 $ret = 1;
223 } else if ($ret < 0) {
224 $ret = -1;
225 }
226 return $ret; //int, {-1,0,1}
227 }
228 }
229
230 /**
231 * A compound comparator class, whose role it is to sequentially call a set of comparators on two objects and
232 * return the combined result of the comparison.
233 */
234 class CompoundComparator {
235 //Comparator chain.
236 var $comparators = array();
237
238 /**
239 * Function to compare two objects $a and $b, using the chain of comparators.
240 * @param $a The first object to compare.
241 * @param $b The second object to compare.
242 * @returns -1, 0, 1. -1 if a < b, 1 if a > b, and 0 if a = b.
243 */
244 function compare($a, $b) {
245 $ret = 0;
246 $size = count($this->comparators);
247 for ($i = 0; $i < $size; $i++) {
248 $comp = $this->comparators[$i];
249 $ret = $comp->compare($a, $b);
250 if ($ret != 0) {
251 break;
252 }
253 }
254 return $ret;
255 }
256
257 /**
258 * Function to add a comparator to the chain.
259 * @param $comp The comparator to add.
260 */
261 function addComparator($comp){
262 $this->comparators[] = $comp;
263 }
264 }
265
266 /**
267 * A function to create a Comparator class with chained comparators based off the sort specification passed into the store.
268 * @param $sortSpec The Sort specification, which is an array of sort objects containing ( attribute: "someStr": descending: true|fase}
269 * @returns The constructed comparator.
270 */
271 function createComparator($sortSpec) {
272 //Function to construct the class that handles chained comparisons.
273 $comparator = new CompoundComparator();
274 $size = count($sortSpec);
275 for ($i = 0; $i < $size; $i++) {
276 $sort = $sortSpec[$i];
277 $desc = false;
278 if(property_exists($sort, "descending")){
279 $desc = $sort->descending;
280 }
281 $fileComp = new FieldComparator($sort->attribute,$desc);
282 $comparator->addComparator($fileComp);
283 }
284 return $comparator;
285 }
286
287 /**
288 * Function to match a set of queries against a directory and possibly all subfiles.
289 * @param query The Query send in to process and test against.
290 * @param patterns The set of regexp patterns generated off the query.
291 * @param dir the directory to search in.
292 * @param recurse Whether or not to recurse into subdirs and test files there too.
293 *
294 * @return Array. Returns an array of all matches of the query.
295 */
296 function matchFiles($query, $patterns, $ignoreCase, $dir, $rootDir, $recurse, $dirsOnly, $expand, $showHiddenFiles) {
297 $files = array();
298 $fullDir = $rootDir."/".$dir;
299
300 if ($fullDir != null && is_dir($fullDir)) {
301
302 $dirHandle = opendir($fullDir);
303 while ($file = readdir($dirHandle)) {
304 if ($file != "." && $file != "..") {
305 $item = generateFileObj($file, $dir, $rootDir, $expand,$showHiddenFiles);
306 $keys = array_keys($patterns);
307 $total = count($keys);
308 for ($i = 0; $i < $total; $i++) {
309 $key = $keys[$i];
310 $pattern = $query[$key];
311 $matched = containsValue($item,$key,$query[$key],$patterns[$key], $ignoreCase);
312 if (!$matched) {
313 break;
314 }
315 }
316 if ($matched) {
317 if (!$dirsOnly || $item["directory"]) {
318 if ($showHiddenFiles || $item["name"][0] != '.') {
319 $files[] = $item;
320 }
321 }
322 }
323
324 if (is_dir($rootDir."/".$item["path"]) && $recurse) {
325 if ($showHiddenFiles || $item["name"][0] != '.') {
326 $files = array_merge($files, matchFiles($query, $patterns, $ignoreCase, $item["path"], $rootDir, $recurse, $dirsOnly, $expand, $showHiddenFiles));
327 }
328 }
329 }
330 }
331 closedir($dirHandle);
332 }
333 return $files;
334 }
335
336 /**
337 * Function to handle comparing the value of an attribute on a file item.
338 * @param item The item to examine.
339 * @param attr The attribute of the tem to examine.
340 * @parma value The value to compare it to.
341 * @param rExp A regular Expression pattern object generated off 'value' if any.
342 *
343 * @returns boolean denoting if the value was matched or not.
344 */
345 function containsValue($item, $attr, $value, $rExp, $ignoreCase) {
346 $matched = false;
347 $possibleValue = $item[$attr];
348 if ($possibleValue === null && $value === null) {
349 $matched = true;
350 } else {
351 if ($rExp != null && is_string($possibleValue)) {
352 if ($ignoreCase) {
353 $matched = preg_match($rExp . 'i', $possibleValue);
354 } else {
355 $matched = preg_match($rExp, $possibleValue);
356 }
357
358 } else {
359 if ($value != null && $possibleValue != null) {
360 $matched = ($value == $possibleValue);
361 }
362 }
363 }
364 return $matched;
365 }
366 // No closing PHP tag on purpose. Do not want it to print whitepace and thus not allow setting headers later.