PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/javascript/dojo-release-1.6.1-src/util/docscripts/generate.php

https://code.google.com/p/realitybuilder/
PHP | 440 lines | 350 code | 65 blank | 25 comment | 109 complexity | a831a5525de952ae1be1c85b545a7b7e MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, LGPL-2.0, MIT
  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. "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. // Use file serializers by default
  24. $keys = array();
  25. $namespaces = array();
  26. // Do this in 3 parts
  27. // 1. Turn all variables into single objects
  28. // 2. Internalize class members
  29. // 3. Serialize objects
  30. $args = array();
  31. $kwargs = array();
  32. $clean = false;
  33. $db_host = 'localhost';
  34. $db_user = 'root';
  35. $db_password = '';
  36. $db_name = 'generate';
  37. foreach (array_slice($argv, 1) as $arg) {
  38. if ($arg{0} == '-') {
  39. if (preg_match('%^--(outfile|store|serialize|db_host|db_user|db_password|db_name)=([^ ]+)$%', $arg, $match)) {
  40. if ($match[1] == 'db_host') {
  41. $db_host = $match[2];
  42. }
  43. elseif ($match[1] == 'db_user') {
  44. $db_user = $match[2];
  45. }
  46. elseif ($match[1] == 'db_password') {
  47. $db_password = $match[2];
  48. }
  49. elseif ($match[1] == 'db_name') {
  50. $db_name == $match[2];
  51. }
  52. elseif ($match[1] == 'serialize') {
  53. foreach (explode(',', $match[2]) as $serialize_type) {
  54. $kwargs[$match[1]][$serialize_type] = true;
  55. }
  56. }
  57. else {
  58. $kwargs[$match[1]] = $match[2];
  59. }
  60. }
  61. elseif ($arg == '--clean') {
  62. $clean = true;
  63. }
  64. else {
  65. die("ERROR: Unrecognized argument: $arg\n");
  66. }
  67. }
  68. else {
  69. $args[] = $arg;
  70. }
  71. }
  72. if (!isset($kwargs['serialize'])) {
  73. $kwargs['serialize'] = array(
  74. 'json' => true,
  75. 'xml' => true
  76. );
  77. }
  78. if (!isset($kwargs['store'])) {
  79. $kwargs['store'] = 'file';
  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. foreach ($contents as $var => $content) {
  129. foreach ($content as $key_key => $key_value) {
  130. $key_type = 'undefined';
  131. if (is_numeric($key_value)) {
  132. $key_type = 'numeric';
  133. }elseif(is_array($key_value)) {
  134. $key_type = 'array';
  135. }elseif(is_bool($key_value)) {
  136. $key_type = 'bool';
  137. }elseif(is_string($key_value)) {
  138. $key_type = 'string';
  139. }
  140. $keys[$key_key][] = $key_type;
  141. $keys[$key_key] = array_unique($keys[$key_key]);
  142. }
  143. $node = $nodes->open($var, array());
  144. $new = !empty($node);
  145. // Handle file-level information
  146. if ($provides && (!is_array($node['#provides']) || !in_array($provides, $node['#provides']))) {
  147. $node['#provides'][] = $provides;
  148. }
  149. if (!is_array($node['#namespaces']) || !in_array($namespace, $node['#namespaces'])) {
  150. $node['#namespaces'][] = $namespace;
  151. }
  152. $node['#resource'][] = "$namespace/$resource";
  153. if (trim($content['type']) && (empty($node['type']) || $content['type'] != 'Object')) {
  154. $node['type'] = $content['type'];
  155. }
  156. if (!empty($content['tags'])) {
  157. $node['tags'] = $content['tags'];
  158. }
  159. if (!empty($content['private'])) {
  160. $node['private'] = $content['private'];
  161. }
  162. if (!empty($content['private_parent'])) {
  163. $node['private_parent'] = $content['private_parent'];
  164. }
  165. if (trim($content['summary'])) {
  166. $node['summary'] = $content['summary'];
  167. }
  168. if (trim($content['description'])) {
  169. $node['description'] = $content['description'];
  170. }
  171. if (trim($content['exceptions'])) {
  172. $node['exceptions'] = $content['exceptions'];
  173. }
  174. if ($content['private']) {
  175. $node['private'] = $content['private'];
  176. }
  177. if ($content['private_parent']) {
  178. $node['private_parent'] = $content['private_parent'];
  179. }
  180. if (is_array($content['alias'])) {
  181. foreach ($content['alias'] as $alias) {
  182. $node['alias'] = $alias;
  183. }
  184. }
  185. if (is_array($content['examples'])) {
  186. foreach ($content['examples'] as $example) {
  187. if (!is_array($node['examples']) || !in_array($example, $node['examples'])) {
  188. $node['examples'][] = $example;
  189. }
  190. }
  191. }
  192. if ($content['instance']) {
  193. $node['instance'] = $content['instance'];
  194. }
  195. if ($content['prototype']) {
  196. $node['prototype'] = $content['prototype'];
  197. }
  198. if (!is_array($node['returns'])) {
  199. $node['returns'] = array();
  200. }
  201. if (trim($content['returns'])) {
  202. $node['returns'] = array_unique(array_merge($node['returns'], explode('|', $content['returns'])));
  203. }
  204. if (trim($content['return_summary'])) {
  205. $node['return_summary'] = $content['return_summary'];
  206. }
  207. foreach (array('prototype', 'instance', 'normal') as $scope) {
  208. if (!empty($content['mixins'][$scope])) {
  209. if (empty($node['mixins'][$scope])) {
  210. $node['mixins'][$scope] = array();
  211. }
  212. if (!is_array($content['mixins'][$scope])) {
  213. print $content['mixins'][$scope];
  214. }
  215. $node['mixins'][$scope] = array_unique(array_merge($node['mixins'][$scope], $content['mixins'][$scope]));
  216. }
  217. }
  218. if ($content['type'] == 'Function') {
  219. if ($content['classlike']) {
  220. $node['classlike'] = true;
  221. }
  222. if ($node['chains']) {
  223. if (!$content['chains']['prototype']) {
  224. $content['chains']['prototype'] = array();
  225. }
  226. $node['chains']['prototype'] = array_unique(array_merge($node['chains']['prototype'], $content['chains']['prototype']));
  227. if (!$content['chains']['call']) {
  228. $content['chains']['call'] = array();
  229. }
  230. $node['chains']['call'] = array_unique(array_merge($node['chains']['call'], $content['chains']['call']));
  231. }
  232. else {
  233. $node['chains']['prototype'] = ($content['chains']['prototype']) ? $content['chains']['prototype'] : array();
  234. $node['chains']['call'] = ($content['chains']['call']) ? $content['chains']['call'] : array();
  235. }
  236. if ($content['chains']) {
  237. unset($content['chains']['prototype']);
  238. unset($content['chains']['call']);
  239. $types = array_keys($content['chains']);
  240. if (!empty($types)) {
  241. print_r($types);
  242. die('Unexpected chain type');
  243. }
  244. }
  245. if (!empty($content['parameters'])) {
  246. if (!empty($node['parameters'])) {
  247. $node_parameters = array_keys($node['parameters']);
  248. $content_parameters = array_keys($content['parameters']);
  249. $long_parameters = (count($node_parameters) > count($content_parameters)) ? $node_parameters : $content_parameters;
  250. $short_parameters = (count($node_parameters) > count($content_parameters)) ? $content_parameters : $node_parameters;
  251. $match = true;
  252. foreach ($long_parameters as $i => $parameter) {
  253. if ($i < count($short_parameters) && $parameter != $short_parameters[$i]) {
  254. $match = false;
  255. }
  256. }
  257. if ($match) {
  258. // Only process these if they match the first occurence
  259. foreach ($content['parameters'] as $parameter_name => $parameter) {
  260. if (empty($node['parameters'][$parameter_name]['type'])) {
  261. $node['parameters'][$parameter_name]['type'] = $parameter['type'];
  262. }
  263. if (trim($parameter['summary'])) {
  264. $node['parameters'][$parameter_name]['summary'] = $parameter['summary'];
  265. }
  266. }
  267. }
  268. }
  269. else {
  270. $node['parameters'] = $content['parameters'];
  271. }
  272. }
  273. }
  274. $nodes->save($var, $node);
  275. }
  276. $resources->save($namespace . '%' . $file, $ctime);
  277. // print_r($keys);
  278. }
  279. unset($resources);
  280. print "=== BUILDING OBJECT STRUCTURE ===\n";
  281. flush();
  282. $roots = array();
  283. $ids = $nodes->ids();
  284. $percent = 0;
  285. foreach ($ids as $pos => $id) {
  286. $new_percent = floor($pos / count($ids) * 50);
  287. if ($new_percent % 5 == 0 && $percent % 5 != 0) {
  288. print floor($new_percent) . "%\n";
  289. }
  290. $percent = $new_percent;
  291. $parts = explode('.', $id);
  292. if (count($parts) > 1) {
  293. $name = array_pop($parts);
  294. $parent = implode('.', $parts);
  295. $node = $nodes->open($id, array());
  296. if (!is_array($node['#namespaces']) || (count($args) && !count(array_intersect($args, $node['#namespaces'])))) {
  297. continue;
  298. }
  299. if (!array_key_exists($parent, $roots)) {
  300. $roots[$parent] = array();
  301. }
  302. if ($node['type'] == 'Function') {
  303. $roots[$id]['function'] = true;
  304. }
  305. if ($node['classlike']) {
  306. $roots[$id]['classlike'] = true;
  307. }
  308. }
  309. }
  310. // Figure out whether a root item has children or not
  311. $pos = 0;
  312. $root_count = count($roots);
  313. foreach ($roots as $id => $root) {
  314. $new_percent = floor(50 + ($pos++ / $root_count * 50));
  315. if ($new_percent % 5 == 0 && $percent % 5 != 0) {
  316. print floor($new_percent) . "%\n";
  317. }
  318. $percent = $new_percent;
  319. if ($root['function'] && !$root['classlike']) {
  320. $has_children = false;
  321. $parts = explode('.', $id);
  322. if (count($parts) > 1) {
  323. foreach ($roots as $possible_child_id => $possible_child) {
  324. $child_parts = explode('.', $possible_child_id);
  325. if (count($child_parts) == count($parts)+1 && strpos($possible_child_id, "$id.") === 0) {
  326. $has_children = true;
  327. break;
  328. }
  329. }
  330. if (!$has_children) {
  331. unset($roots[$id]);
  332. }
  333. }
  334. }
  335. }
  336. print "=== SERIALIZING OBJECTS ===\n";
  337. // Aggregate and save
  338. $serializers = array();
  339. if ($kwargs['outfile']) {
  340. if (isset($kwargs['serialize']['json'])) {
  341. $serializers['json'] = new JsonSerializer('cache', 'json', $kwargs['outfile']);
  342. }
  343. if (isset($kwargs['serialize']['xml'])) {
  344. $serializers['xml'] = new XmlSerializer('cache', 'xml', $kwargs['outfile']);
  345. }
  346. }
  347. else {
  348. if (isset($kwargs['serialize']['json'])) {
  349. $serializers['json'] = new JsonSerializer('cache', 'json');
  350. }
  351. if (isset($kwargs['serialize']['xml'])) {
  352. $serializers['xml'] = new XmlSerializer('cache', 'xml');
  353. }
  354. }
  355. foreach ($roots as $id => $root) {
  356. if(!$id){
  357. // Minor bug
  358. continue;
  359. }
  360. $node = $nodes->open($id, null);
  361. $parts = explode('.', $id);
  362. foreach ($ids as $child_id) {
  363. $child_parts = explode('.', $child_id);
  364. if (count($child_parts) == count($parts)+1 && strpos($child_id, "$id.") === 0 && !array_key_exists($child_id, $roots)) {
  365. $node['#children'][array_pop($child_parts)] = $nodes->open($child_id, null);
  366. }
  367. }
  368. printf("%-100s %6s KB\n", $id, number_format(memory_get_usage() / 1024));
  369. flush();
  370. foreach ($serializers as $serializer) {
  371. $serializer->setObject($id, $node);
  372. }
  373. }
  374. // * Assemble parent/child relationships
  375. ?>