PageRenderTime 66ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/webUI/util/docscripts/generate.php

http://n2hell.googlecode.com/
PHP | 454 lines | 360 code | 67 blank | 27 comment | 104 complexity | b80a896a0ab246390d26a372ddc89529 MD5 | raw file
Possible License(s): MIT, Apache-2.0, BSD-3-Clause, LGPL-2.0
  1. <?php
  2. # php generate.php
  3. # -- Runs everything in the modules directory
  4. # php generate.php dojo
  5. # php generate.php dijit
  6. # php generate.php dojox
  7. # -- Runs only the module starting with custom, custom2, etc.
  8. # php generate.php --store=file
  9. # php generate.php --store=mysql --db_host=localhost --db_user=api --db_password=password --db_name=api
  10. # -- Specifies storage type. "hash", "file" and "resource" currently supported
  11. # php generate.php --serialize=xml,json
  12. # -- Comma-separated list of serializations. "xml" and "json" supported
  13. # php generate.php --outfile=custom-api custom
  14. # -- Runs the custom module, serializes to custom-api.xml
  15. if (isset($_SERVER['HTTP_HOST'])) {
  16. die('Run from command line');
  17. }
  18. ini_set('memory_limit', '256M');
  19. ini_set('display_errors', 1);
  20. error_reporting(E_ALL ^ E_NOTICE);
  21. $debug = true;
  22. require_once('lib/parser2/dojo2.inc');
  23. $keys = array();
  24. $namespaces = array();
  25. // Do this in 3 parts
  26. // 1. Turn all variables into single objects
  27. // 2. Internalize class members
  28. // 3. Serialize objects
  29. $args = array();
  30. $kwargs = array();
  31. $clean = false;
  32. $db_host = 'localhost';
  33. $db_user = 'root';
  34. $db_password = '';
  35. $db_name = 'generate';
  36. foreach (array_slice($argv, 1) as $arg) {
  37. if ($arg{0} == '-') {
  38. if (preg_match('%^--(outfile|store|serialize|db_host|db_user|db_password|db_name)=([^ ]+)$%', $arg, $match)) {
  39. if ($match[1] == 'db_host') {
  40. $db_host = $match[2];
  41. }
  42. elseif ($match[1] == 'db_user') {
  43. $db_user = $match[2];
  44. }
  45. elseif ($match[1] == 'db_password') {
  46. $db_password = $match[2];
  47. }
  48. elseif ($match[1] == 'db_name') {
  49. $db_name == $match[2];
  50. }
  51. elseif ($match[1] == 'serialize') {
  52. foreach (explode(',', $match[2]) as $serialize_type) {
  53. $kwargs[$match[1]][$serialize_type] = true;
  54. }
  55. }
  56. else {
  57. $kwargs[$match[1]] = $match[2];
  58. }
  59. }
  60. elseif ($arg == '--clean') {
  61. $clean = true;
  62. }
  63. else {
  64. die("ERROR: Unrecognized argument: $arg\n");
  65. }
  66. }
  67. else {
  68. $args[] = $arg;
  69. }
  70. }
  71. if (!isset($kwargs['serialize'])) {
  72. $kwargs['serialize'] = array(
  73. 'json' => true,
  74. 'xml' => true
  75. );
  76. }
  77. // Use hash storage by default
  78. if (!isset($kwargs['store'])) {
  79. $kwargs['store'] = 'hash';
  80. }
  81. require_once('lib/generator/' . $kwargs['store'] . '/Freezer.php');
  82. require_once('lib/generator/' . $kwargs['store'] . '/Serializer.php');
  83. require_once('lib/generator/JsonSerializer.php');
  84. require_once('lib/generator/XmlSerializer.php');
  85. if ($clean) {
  86. Freezer::clean('cache', 'nodes');
  87. Freezer::clean('cache', 'resources');
  88. if ($kwargs['outfile']) {
  89. if (isset($kwargs['serialize']['json'])) {
  90. JsonSerializer::clean('cache', 'json', $kwargs['outfile']);
  91. }
  92. if (isset($kwargs['serialize']['xml'])) {
  93. XmlSerializer::clean('cache', 'xml', $kwargs['outfile']);
  94. }
  95. }
  96. else {
  97. if (isset($kwargs['serialize']['json'])) {
  98. JsonSerializer::clean('cache', 'json');
  99. }
  100. if (isset($kwargs['serialize']['xml'])) {
  101. XmlSerializer::clean('cache', 'xml');
  102. }
  103. }
  104. }
  105. $files = dojo_get_files($args);
  106. $nodes = new Freezer('cache', 'nodes');
  107. $resources = new Freezer('cache', 'resources');
  108. print "=== PARSING FILES ===\n";
  109. flush();
  110. foreach ($files as $set){
  111. list($namespace, $file) = $set;
  112. if (!$namespaces[$namespace]) {
  113. $namespaces[$namespace] = true;
  114. }
  115. $ctime = dojo_get_file_time($namespace, $file);
  116. if ($ctime == $resources->open($namespace . '%' . $file, null)) {
  117. continue;
  118. }
  119. printf("%-100s %6s KB\n", $namespace . '/' . $file, number_format(memory_get_usage() / 1024));
  120. flush();
  121. $contents = dojo_get_contents($namespace, $file);
  122. $provides = $contents['#provides'];
  123. unset($contents['#provides']);
  124. $resource = $contents['#resource'];
  125. unset($contents['#resource']);
  126. $requires = $contents['#requires'];
  127. unset($contents['#requires']);
  128. // set by debugging in parsing
  129. unset($contents['#debug']);
  130. unset($contents['#unwrapped_source']);
  131. unset($contents['#raw_source']);
  132. foreach ($contents as $var => $content) {
  133. foreach ($content as $key_key => $key_value) {
  134. $key_type = 'undefined';
  135. if (is_numeric($key_value)) {
  136. $key_type = 'numeric';
  137. }elseif(is_array($key_value)) {
  138. $key_type = 'array';
  139. }elseif(is_bool($key_value)) {
  140. $key_type = 'bool';
  141. }elseif(is_string($key_value)) {
  142. $key_type = 'string';
  143. }
  144. $keys[$key_key][] = $key_type;
  145. $keys[$key_key] = array_unique($keys[$key_key]);
  146. }
  147. $node = $nodes->open($var, array());
  148. $new = !empty($node);
  149. // Handle file-level information
  150. if ($provides && (!is_array($node['#provides']) || !in_array($provides, $node['#provides']))) {
  151. $node['#provides'][] = $provides;
  152. }
  153. if (!is_array($node['#namespaces']) || !in_array($namespace, $node['#namespaces'])) {
  154. $node['#namespaces'][] = $namespace;
  155. }
  156. $node['#resource'][] = "$namespace/$resource";
  157. if (trim($content['type']) && (empty($node['type']) || $content['type'] != 'Object')) {
  158. $node['type'] = $content['type'];
  159. }
  160. if (!empty($content['tags'])) {
  161. $node['tags'] = $content['tags'];
  162. }
  163. if (!empty($content['private'])) {
  164. $node['private'] = $content['private'];
  165. }
  166. if (!empty($content['private_parent'])) {
  167. $node['private_parent'] = $content['private_parent'];
  168. }
  169. if (trim($content['summary'])) {
  170. $node['summary'] = $content['summary'];
  171. }
  172. if (trim($content['description'])) {
  173. $node['description'] = $content['description'];
  174. }
  175. if (trim($content['exceptions'])) {
  176. $node['exceptions'] = $content['exceptions'];
  177. }
  178. if ($content['private']) {
  179. $node['private'] = $content['private'];
  180. }
  181. if ($content['private_parent']) {
  182. $node['private_parent'] = $content['private_parent'];
  183. }
  184. if (is_array($content['alias'])) {
  185. foreach ($content['alias'] as $alias) {
  186. $node['alias'] = $alias;
  187. }
  188. }
  189. if (is_array($content['examples'])) {
  190. foreach ($content['examples'] as $example) {
  191. if (!is_array($node['examples']) || !in_array($example, $node['examples'])) {
  192. $node['examples'][] = $example;
  193. }
  194. }
  195. }
  196. if ($content['instance']) {
  197. $node['instance'] = $content['instance'];
  198. }
  199. if ($content['prototype']) {
  200. $node['prototype'] = $content['prototype'];
  201. }
  202. if (!is_array($node['returns'])) {
  203. $node['returns'] = array();
  204. }
  205. if (trim($content['returns'])) {
  206. $node['returns'] = array_unique(array_merge($node['returns'], explode('|', $content['returns'])));
  207. }
  208. if (trim($content['return_summary'])) {
  209. $node['return_summary'] = $content['return_summary'];
  210. }
  211. foreach (array('prototype', 'instance', 'normal') as $scope) {
  212. if (!empty($content['mixins'][$scope])) {
  213. if (empty($node['mixins'][$scope])) {
  214. $node['mixins'][$scope] = array();
  215. }
  216. if (!is_array($content['mixins'][$scope])) {
  217. print $content['mixins'][$scope];
  218. }
  219. $node['mixins'][$scope] = array_unique(array_merge($node['mixins'][$scope], $content['mixins'][$scope]));
  220. }
  221. }
  222. if ($content['type'] == 'Function') {
  223. if ($content['classlike']) {
  224. $node['classlike'] = true;
  225. }
  226. if ($node['chains']) {
  227. if (!$content['chains']['prototype']) {
  228. $content['chains']['prototype'] = array();
  229. }
  230. $node['chains']['prototype'] = array_unique(array_merge($node['chains']['prototype'], $content['chains']['prototype']));
  231. if (!$content['chains']['call']) {
  232. $content['chains']['call'] = array();
  233. }
  234. $node['chains']['call'] = array_unique(array_merge($node['chains']['call'], $content['chains']['call']));
  235. }
  236. else {
  237. $node['chains']['prototype'] = ($content['chains']['prototype']) ? $content['chains']['prototype'] : array();
  238. $node['chains']['call'] = ($content['chains']['call']) ? $content['chains']['call'] : array();
  239. }
  240. if ($content['chains']) {
  241. unset($content['chains']['prototype']);
  242. unset($content['chains']['call']);
  243. $types = array_keys($content['chains']);
  244. if (!empty($types)) {
  245. print_r($types);
  246. die('Unexpected chain type');
  247. }
  248. }
  249. if (!empty($content['parameters'])) {
  250. if (!empty($node['parameters'])) {
  251. $node_parameters = array_keys($node['parameters']);
  252. $content_parameters = array_keys($content['parameters']);
  253. $long_parameters = (count($node_parameters) > count($content_parameters)) ? $node_parameters : $content_parameters;
  254. $short_parameters = (count($node_parameters) > count($content_parameters)) ? $content_parameters : $node_parameters;
  255. $match = true;
  256. $total_short = count($short_parameters);
  257. foreach ($long_parameters as $i => $parameter) {
  258. if ($i < $total_short && $parameter != $short_parameters[$i]) {
  259. $match = false;
  260. }
  261. }
  262. if ($match) {
  263. // Only process these if they match the first occurence
  264. foreach ($content['parameters'] as $parameter_name => $parameter) {
  265. if (empty($node['parameters'][$parameter_name]['type'])) {
  266. $node['parameters'][$parameter_name]['type'] = $parameter['type'];
  267. }
  268. if (trim($parameter['summary'])) {
  269. $node['parameters'][$parameter_name]['summary'] = $parameter['summary'];
  270. }
  271. }
  272. }
  273. }
  274. else {
  275. $node['parameters'] = $content['parameters'];
  276. }
  277. }
  278. }
  279. $nodes->save($var, $node);
  280. }
  281. $resources->save($namespace . '%' . $file, $ctime);
  282. // print_r($keys);
  283. }
  284. unset($resources);
  285. print "=== BUILDING OBJECT STRUCTURE ===\n";
  286. flush();
  287. $roots = array();
  288. $ids = $nodes->ids();
  289. $percent = 0;
  290. $total = count($ids);
  291. $count_args = count($args);
  292. foreach ($ids as $pos => $id) {
  293. $new_percent = floor($pos / $total * 50);
  294. if ($new_percent % 5 == 0 && $percent % 5 != 0) {
  295. print $new_percent . "%\n";
  296. }
  297. $percent = $new_percent;
  298. $parts = explode('.', $id);
  299. if (count($parts) > 1) {
  300. $name = array_pop($parts);
  301. $parent = implode('.', $parts);
  302. $node = $nodes->open($id, array());
  303. if (!is_array($node['#namespaces']) || ($count_args && !count(array_intersect($args, $node['#namespaces'])))) {
  304. continue;
  305. }
  306. if (!array_key_exists($parent, $roots)) {
  307. $roots[$parent] = array();
  308. }
  309. if ($node['type'] == 'Function') {
  310. $roots[$id]['function'] = true;
  311. }
  312. if ($node['classlike']) {
  313. $roots[$id]['classlike'] = true;
  314. }
  315. }
  316. }
  317. // Figure out whether a root item has children or not
  318. $pos = 0;
  319. $root_count = count($roots);
  320. $has_children_map = array();
  321. $rootids = array_keys($roots);
  322. //descending sort rootids, so children are processed before parents
  323. rsort($rootids);
  324. foreach ($rootids as $id) {
  325. $root = $roots[$id];
  326. $new_percent = floor(50 + ($pos++ / $root_count * 50));
  327. if ($new_percent % 5 == 0 && $percent % 5 != 0) {
  328. print floor($new_percent) . "%\n";
  329. }
  330. $percent = $new_percent;
  331. $parts = explode('.', $id);
  332. $parts_count = count($parts);
  333. if ($parts_count > 1) {
  334. if ($root['function'] && !$root['classlike']) {
  335. if(!array_key_exists($id, $has_children_map)){
  336. unset($roots[$id]);
  337. }
  338. }
  339. $name = array_pop($parts);
  340. $parent_id = implode('.', $parts);
  341. $obj = array("name"=>$name, "id"=>$id);
  342. if(array_key_exists($parent_id, $has_children_map)) {
  343. array_push($has_children_map[$parent_id], $obj);
  344. }
  345. else{
  346. $has_children_map[$parent_id] = array($obj);
  347. }
  348. }
  349. }
  350. print "=== SERIALIZING OBJECTS ===\n";
  351. // Aggregate and save
  352. $serializers = array();
  353. if ($kwargs['outfile']) {
  354. if (isset($kwargs['serialize']['json'])) {
  355. $serializers['json'] = new JsonSerializer('cache', 'json', $kwargs['outfile']);
  356. }
  357. if (isset($kwargs['serialize']['xml'])) {
  358. $serializers['xml'] = new XmlSerializer('cache', 'xml', $kwargs['outfile']);
  359. }
  360. }
  361. else {
  362. if (isset($kwargs['serialize']['json'])) {
  363. $serializers['json'] = new JsonSerializer('cache', 'json');
  364. }
  365. if (isset($kwargs['serialize']['xml'])) {
  366. $serializers['xml'] = new XmlSerializer('cache', 'xml');
  367. }
  368. }
  369. foreach ($roots as $id => $root) {
  370. if(!$id){
  371. // Minor bug
  372. continue;
  373. }
  374. $node = $nodes->open($id, null);
  375. if(array_key_exists($id, $has_children_map)){
  376. foreach ($has_children_map[$id] as $child) {
  377. $node['#children'][$child['name']] = $nodes->open($child['id'], null);
  378. }
  379. }
  380. printf("%-100s %6s KB\n", $id, number_format(memory_get_usage() / 1024));
  381. flush();
  382. foreach ($serializers as $serializer) {
  383. $serializer->setObject($id, $node);
  384. }
  385. }
  386. // * Assemble parent/child relationships
  387. ?>