PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/htmlpurifier/maintenance/config-scanner.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 192 lines | 149 code | 19 blank | 24 comment | 15 complexity | 1123153117d8b593c9e44f118f3a5b91 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. #!/usr/bin/php
  2. <?php
  3. chdir(dirname(__FILE__));
  4. require_once 'common.php';
  5. require_once '../library/HTMLPurifier.auto.php';
  6. assertCli();
  7. if (version_compare(PHP_VERSION, '5.2.2', '<'))
  8. {
  9. echo "This script requires PHP 5.2.2 or later, for tokenizer line numbers.";
  10. exit(1);
  11. }
  12. /**
  13. * @file
  14. * Scans HTML Purifier source code for $config tokens and records the
  15. * directive being used; configdoc can use this info later.
  16. *
  17. * Currently, this just dumps all the info onto the console. Eventually, it
  18. * will create an XML file that our XSLT transform can use.
  19. */
  20. $FS = new FSTools();
  21. chdir(dirname(__FILE__) . '/../library/');
  22. $raw_files = $FS->globr('.', '*.php');
  23. $files = array();
  24. foreach ($raw_files as $file)
  25. {
  26. $file = substr($file, 2); // rm leading './'
  27. if (strncmp('standalone/', $file, 11) === 0)
  28. continue; // rm generated files
  29. if (substr_count($file, '.') > 1)
  30. continue; // rm meta files
  31. $files[] = $file;
  32. }
  33. /**
  34. * Moves the $i cursor to the next non-whitespace token
  35. */
  36. function consumeWhitespace($tokens, &$i)
  37. {
  38. do
  39. {
  40. $i ++;
  41. }
  42. while (is_array($tokens[$i]) && $tokens[$i][0] === T_WHITESPACE);
  43. }
  44. /**
  45. * Tests whether or not a token is a particular type. There are three run-cases:
  46. * - ($token, $expect_token): tests if the token is $expect_token type;
  47. * - ($token, $expect_value): tests if the token is the string $expect_value;
  48. * - ($token, $expect_token, $expect_value): tests if token is $expect_token type, and
  49. * its string representation is $expect_value
  50. */
  51. function testToken($token, $value_or_token, $value = null)
  52. {
  53. if (is_null($value))
  54. {
  55. if (is_int($value_or_token))
  56. return is_array($token) && $token[0] === $value_or_token;
  57. else
  58. return $token === $value_or_token;
  59. }
  60. else
  61. {
  62. return is_array($token) && $token[0] === $value_or_token && $token[1] === $value;
  63. }
  64. }
  65. $counter = 0;
  66. $full_counter = 0;
  67. $tracker = array();
  68. foreach ($files as $file)
  69. {
  70. $tokens = token_get_all(file_get_contents($file));
  71. $file = str_replace('\\', '/', $file);
  72. for($i = 0, $c = count($tokens); $i < $c; $i ++)
  73. {
  74. $ok = false;
  75. // Match $config
  76. if (! $ok && testToken($tokens[$i], T_VARIABLE, '$config'))
  77. $ok = true;
  78. // Match $this->config
  79. while (! $ok && testToken($tokens[$i], T_VARIABLE, '$this'))
  80. {
  81. consumeWhitespace($tokens, $i);
  82. if (! testToken($tokens[$i], T_OBJECT_OPERATOR))
  83. break;
  84. consumeWhitespace($tokens, $i);
  85. if (testToken($tokens[$i], T_STRING, 'config'))
  86. $ok = true;
  87. break;
  88. }
  89. if (! $ok)
  90. continue;
  91. $ok = false;
  92. for($i ++; $i < $c; $i ++)
  93. {
  94. if ($tokens[$i] === ',' || $tokens[$i] === ')' || $tokens[$i] === ';')
  95. {
  96. break;
  97. }
  98. if (is_string($tokens[$i]))
  99. continue;
  100. if ($tokens[$i][0] === T_OBJECT_OPERATOR)
  101. {
  102. $ok = true;
  103. break;
  104. }
  105. }
  106. if (! $ok)
  107. continue;
  108. $line = $tokens[$i][2];
  109. consumeWhitespace($tokens, $i);
  110. if (! testToken($tokens[$i], T_STRING, 'get'))
  111. continue;
  112. consumeWhitespace($tokens, $i);
  113. if (! testToken($tokens[$i], '('))
  114. continue;
  115. $full_counter ++;
  116. $matched = false;
  117. do
  118. {
  119. // What we currently don't match are batch retrievals, and
  120. // wildcard retrievals. This data might be useful in the future,
  121. // which is why we have a do {} while loop that doesn't actually
  122. // do anything.
  123. consumeWhitespace($tokens, $i);
  124. if (! testToken($tokens[$i], T_CONSTANT_ENCAPSED_STRING))
  125. continue;
  126. $id = substr($tokens[$i][1], 1, - 1);
  127. $counter ++;
  128. $matched = true;
  129. if (! isset($tracker[$id]))
  130. $tracker[$id] = array();
  131. if (! isset($tracker[$id][$file]))
  132. $tracker[$id][$file] = array();
  133. $tracker[$id][$file][] = $line;
  134. }
  135. while (0);
  136. //echo "$file:$line uses $namespace.$directive\n";
  137. }
  138. }
  139. echo "\n$counter/$full_counter instances of \$config or \$this->config found in source code.\n";
  140. echo "Generating XML... ";
  141. $xw = new XMLWriter();
  142. $xw->openURI('../configdoc/usage.xml');
  143. $xw->setIndent(true);
  144. $xw->startDocument('1.0', 'UTF-8');
  145. $xw->startElement('usage');
  146. foreach ($tracker as $id => $files)
  147. {
  148. $xw->startElement('directive');
  149. $xw->writeAttribute('id', $id);
  150. foreach ($files as $file => $lines)
  151. {
  152. $xw->startElement('file');
  153. $xw->writeAttribute('name', $file);
  154. foreach ($lines as $line)
  155. {
  156. $xw->writeElement('line', $line);
  157. }
  158. $xw->endElement();
  159. }
  160. $xw->endElement();
  161. }
  162. $xw->endElement();
  163. $xw->flush();
  164. echo "done!\n";
  165. // vim: et sw=4 sts=4