PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/public/javascripts/dojo/dojox/data/tests/stores/filestore_funcs.php

https://github.com/suhaspnehete/EngineY
PHP | 362 lines | 343 code | 0 blank | 19 comment | 2 complexity | b49c63d2afd684f8cf86ab6084202cba MD5 | raw file
  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. * Function to load all file info from a particular directory.
  58. *
  59. * @param $dir The dir to seach from, relative to $rootDir.
  60. * @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.
  61. * @param $recurse Whether or not to deep scan the dir and return all subfiles, or just return the toplevel files.
  62. * @param $dirsOnly boolean to enote to only return directory names, not filenames.
  63. * @param $expand boolean to indicate whether or not to inflate all children files along a path/file, or leave them as stubs.
  64. * @param $showHiddenFiles boolean to indicate to return hidden files as part of the list.
  65. */
  66. function getAllfiles($dir, $rootDir, $recurse, $dirsOnly, $expand, $showHiddenFiles) {
  67. // summary:
  68. // A function to obtain all the files in a particular directory (file or dir)
  69. $files = array();
  70. $dirHandle = opendir($rootDir."/".$dir);
  71. if ($dirHandle) {
  72. while($file = readdir($dirHandle)) {
  73. if ($file) {
  74. if ($file != ".." && $file != ".") {
  75. $path = $dir."/".$file;
  76. $fileObj = generateFileObj($file, $dir, $rootDir,$expand,$showHiddenFiles);
  77. if (is_dir($rootDir."/".$path)) {
  78. if ($recurse) {
  79. if (!showHiddenFiles || $fileObj["name"][0] != '.') {
  80. $subfiles = getAllfiles($path,$rootDir,$recurse,$dirsOnly,$expand,$showHiddenFiles);
  81. $length = count($subfiles);
  82. for ($i = 0; $i < $length; $i++) {
  83. $files[] = $subfiles[$i];
  84. }
  85. }
  86. }
  87. }
  88. if (!$dirsOnly || $fileObj["directory"]) {
  89. if (!showHiddenFiles || $fileObj["name"][0] !== '.') {
  90. $files[] = $fileObj;
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. closedir($dirHandle);
  98. return $files;
  99. }
  100. /**
  101. * Function to generate an associative map of data about a specific file.
  102. * @param $file The name of the file this object represents.
  103. * @param $dir The sub-path that contains the file defined by $file
  104. * @param $rootDir The directory from which to append dir and name to get the full path to the file.
  105. * @param $expand boolean to denote that if the file is a directory, expand all children in the children attribute
  106. * to a a full object
  107. * @param $showHiddenFiles boolean to denote if hidden files should be shown in-view or not.
  108. *
  109. * @return Associative Map. The details about the file:
  110. * $file["name"] - Returns the shortname of the file.
  111. * $file["parentDir"] - Returns the relative path from the service root for the parent directory containing file $file["name"]
  112. * $file["path"] - The relative path to the file.
  113. * $file["directory"] - Boolean indicator if the file represents a directory.
  114. * $file["size"] - The size of the file, in bytes.
  115. * $file["modified] - The modified date of the file in milliseconds since Jan 1st, 1970.
  116. * $file["children"] - Children files of a directory. Empty if a standard file.
  117. */
  118. function generateFileObj($file, $dir, $rootDir, $expand, $showHiddenFiles) {
  119. // summary:
  120. // Function to generate an object representation of a disk file.
  121. $path = $file;
  122. if ($dir != "." && $dir != "./") {
  123. $path = $dir."/".$file;
  124. }
  125. $fullPath = $rootDir."/".$path;
  126. $atts = stat($fullPath);
  127. $rootPath = realPath($rootDir);
  128. $resolvedDir = realPath($rootDir."/".$dir);
  129. $resolvedFullPath = realPath($fullPath);
  130. //Try to normalize down the paths so it does a consistent return.
  131. if (strcmp($rootPath, $resolvedDir) === 0) {
  132. $dir = ".";
  133. } else {
  134. $dir = substr($resolvedDir, (strlen($rootPath) + 1), strlen($resolvedDir));
  135. $dir = "./".str_replace("\\","/",$dir);
  136. }
  137. if (strcmp($rootPath, $resolvedFullPath) === 0) {
  138. $path = ".";
  139. } else {
  140. $path = substr($resolvedFullPath, (strlen($rootPath) + 1), strlen($resolvedFullPath));
  141. $path = "./".str_replace("\\","/",$path);
  142. }
  143. $fObj = array();
  144. $fObj["name"] = $file;
  145. $fObj["parentDir"] = $dir;
  146. $fObj["path"] = $path;
  147. $fObj["directory"] = is_dir($fullPath);
  148. $fObj["size"] = filesize($fullPath);
  149. $fObj["modified"] = $atts[9];
  150. if (is_dir($fullPath)) {
  151. $children = array();
  152. $dirHandle = opendir($fullPath);
  153. while($cFile = readdir($dirHandle)) {
  154. if ($cFile) {
  155. if ($cFile != ".." && $cFile != ".") {
  156. if (!showHiddenFiles || $cFile[0] != '.') {
  157. if (!$expand) {
  158. $children[] = $cFile;
  159. }else{
  160. $children[] = generateFileObj($cFile, $path, $rootDir, $expand, $showHiddenFiles);
  161. }
  162. }
  163. }
  164. }
  165. }
  166. closedir($dirHandle);
  167. $fObj["children"] = $children;
  168. }
  169. return $fObj;
  170. }
  171. /**
  172. * A field comparator class, whose role it is to define which fields on an associaive map to compare on
  173. * and provide the comparison function to do so.
  174. */
  175. class FieldComparator {
  176. var $field;
  177. var $descending = false;
  178. /**
  179. * Constructor.
  180. * @param $f The field of the item to compare.
  181. * @param $d Parameter denoting whether it should be ascending or descending. Default is ascending.
  182. */
  183. function FieldComparator($f, $d) {
  184. $this->field = $f;
  185. $this->descending = $d;
  186. }
  187. /**
  188. * Function to compare file objects A and B on the field defined by $this->field.
  189. * @param $fileA The first file to compare.
  190. * @param #fileB The second file to compare.
  191. */
  192. function compare($fileA,$fileB){
  193. $f = $this->field;
  194. $a = $fileA[$f];
  195. $b = $fileB[$f];
  196. $ret = 0;
  197. if (is_string($a) && is_string($b)) {
  198. $ret = strcmp($a,$b);
  199. } else if($a > $b || $a === null){
  200. $ret = 1;
  201. }else if($a < $b || $b === null){
  202. $ret = -1;
  203. }
  204. if ($this->descending) {
  205. $ret = $ret * -1;
  206. }
  207. if ($ret > 0) {
  208. $ret = 1;
  209. } else if ($ret < 0) {
  210. $ret = -1;
  211. }
  212. return $ret; //int, {-1,0,1}
  213. }
  214. }
  215. /**
  216. * A compound comparator class, whose role it is to sequentially call a set of comparators on two objects and
  217. * return the combined result of the comparison.
  218. */
  219. class CompoundComparator {
  220. //Comparator chain.
  221. var $comparators = array();
  222. /**
  223. * Function to compare two objects $a and $b, using the chain of comparators.
  224. * @param $a The first object to compare.
  225. * @param $b The second object to compare.
  226. * @returns -1, 0, 1. -1 if a < b, 1 if a > b, and 0 if a = b.
  227. */
  228. function compare($a, $b) {
  229. $ret = 0;
  230. $size = count($this->comparators);
  231. for ($i = 0; $i < $size; $i++) {
  232. $comp = $this->comparators[$i];
  233. $ret = $comp->compare($a, $b);
  234. if ($ret != 0) {
  235. break;
  236. }
  237. }
  238. return $ret;
  239. }
  240. /**
  241. * Function to add a comparator to the chain.
  242. * @param $comp The comparator to add.
  243. */
  244. function addComparator($comp){
  245. $this->comparators[] = $comp;
  246. }
  247. }
  248. /**
  249. * A function to create a Comparator class with chained comparators based off the sort specification passed into the store.
  250. * @param $sortSpec The Sort specification, which is an array of sort objects containing ( attribute: "someStr": descending: true|fase}
  251. * @returns The constructed comparator.
  252. */
  253. function createComparator($sortSpec) {
  254. //Function to construct the class that handles chained comparisons.
  255. $comparator = new CompoundComparator();
  256. $size = count($sortSpec);
  257. for ($i = 0; $i < $size; $i++) {
  258. $sort = $sortSpec[$i];
  259. $fileComp = new FieldComparator($sort->attribute,$sort->descending);
  260. $comparator->addComparator($fileComp);
  261. }
  262. return $comparator;
  263. }
  264. /**
  265. * Function to match a set of queries against a directory and possibly all subfiles.
  266. * @param query The Query send in to process and test against.
  267. * @param patterns The set of regexp patterns generated off the query.
  268. * @param dir the directory to search in.
  269. * @param recurse Whether or not to recurse into subdirs and test files there too.
  270. *
  271. * @return Array. Returns an array of all matches of the query.
  272. */
  273. function matchFiles($query, $patterns, $ignoreCase, $dir, $rootDir, $recurse, $dirsOnly, $expand, $showHiddenFiles) {
  274. $files = array();
  275. $fullDir = $rootDir."/".$dir;
  276. if ($fullDir != null && is_dir($fullDir)) {
  277. $dirHandle = opendir($fullDir);
  278. while ($file = readdir($dirHandle)) {
  279. if ($file != "." && $file != "..") {
  280. $item = generateFileObj($file, $dir, $rootDir, $expand,$showHiddenFiles);
  281. $keys = array_keys($patterns);
  282. $total = count($keys);
  283. for ($i = 0; $i < $total; $i++) {
  284. $key = $keys[$i];
  285. $pattern = $query[$key];
  286. $matched = containsValue($item,$key,$query[$key],$patterns[$key], $ignoreCase);
  287. if (!$matched) {
  288. break;
  289. }
  290. }
  291. if ($matched) {
  292. if (!$dirsOnly || $item["directory"]) {
  293. if (!showHiddenFiles || $item["name"][0] != '.') {
  294. $files[] = $item;
  295. }
  296. }
  297. }
  298. if (is_dir($rootDir."/".$item["path"]) && $recurse) {
  299. if (!showHiddenFiles || $item["name"][0] != '.') {
  300. $files = array_merge($files, matchFiles($query, $patterns, $ignoreCase, $item["path"], $rootDir, $recurse, $dirsOnly, $expand, $showHiddenFiles));
  301. }
  302. }
  303. }
  304. }
  305. closedir($dirHandle);
  306. }
  307. return $files;
  308. }
  309. /**
  310. * Function to handle comparing the value of an attribute on a file item.
  311. * @param item The item to examine.
  312. * @param attr The attribute of the tem to examine.
  313. * @parma value The value to compare it to.
  314. * @param rExp A regular Expression pattern object generated off 'value' if any.
  315. *
  316. * @returns boolean denoting if the value was matched or not.
  317. */
  318. function containsValue($item, $attr, $value, $rExp, $ignoreCase) {
  319. $matched = false;
  320. $possibleValue = $item[$attr];
  321. if ($possibleValue === null && $value === null) {
  322. $matched = true;
  323. } else {
  324. if ($rExp != null && is_string(possibleValue)) {
  325. if ($ignoreCase) {
  326. $matched = eregi($rExp, $possibleValue);
  327. } else {
  328. $matched = ereg($rExp, $possibleValue);
  329. }
  330. } else {
  331. if ($value != null && $possibleValue != null) {
  332. $matched = ($value == $possibleValue);
  333. }
  334. }
  335. }
  336. return $matched;
  337. }
  338. // No closing PHP tag on purpose. Do not want it to print whitepace and thus not allow setting headers later.