4 chdir(dirname(__FILE__
));
5 require_once 'common.php';
6 require_once '../library/HTMLPurifier.auto.php';
9 if (version_compare(PHP_VERSION
, '5.2.2', '<')) {
10 echo "This script requires PHP 5.2.2 or later, for tokenizer line numbers.";
16 * Scans HTML Purifier source code for $config tokens and records the
17 * directive being used; configdoc can use this info later.
19 * Currently, this just dumps all the info onto the console. Eventually, it
20 * will create an XML file that our XSLT transform can use.
24 chdir(dirname(__FILE__
) . '/../library/');
25 $raw_files = $FS->globr('.', '*.php');
27 foreach ($raw_files as $file) {
28 $file = substr($file, 2); // rm leading './'
29 if (strncmp('standalone/', $file, 11) === 0) continue; // rm generated files
30 if (substr_count($file, '.') > 1) continue; // rm meta files
35 * Moves the $i cursor to the next non-whitespace token
37 function consumeWhitespace($tokens, &$i) {
38 do {$i++
;} while (is_array($tokens[$i]) && $tokens[$i][0] === T_WHITESPACE
);
42 * Tests whether or not a token is a particular type. There are three run-cases:
43 * - ($token, $expect_token): tests if the token is $expect_token type;
44 * - ($token, $expect_value): tests if the token is the string $expect_value;
45 * - ($token, $expect_token, $expect_value): tests if token is $expect_token type, and
46 * its string representation is $expect_value
48 function testToken($token, $value_or_token, $value = null) {
49 if (is_null($value)) {
50 if (is_int($value_or_token)) return is_array($token) && $token[0] === $value_or_token;
51 else return $token === $value_or_token;
53 return is_array($token) && $token[0] === $value_or_token && $token[1] === $value;
61 foreach ($files as $file) {
62 $tokens = token_get_all(file_get_contents($file));
63 $file = str_replace('\\', '/', $file);
64 for ($i = 0, $c = count($tokens); $i < $c; $i++
) {
67 if (!$ok && testToken($tokens[$i], T_VARIABLE
, '$config')) $ok = true;
68 // Match $this->config
69 while (!$ok && testToken($tokens[$i], T_VARIABLE
, '$this')) {
70 consumeWhitespace($tokens, $i);
71 if (!testToken($tokens[$i], T_OBJECT_OPERATOR
)) break;
72 consumeWhitespace($tokens, $i);
73 if (testToken($tokens[$i], T_STRING
, 'config')) $ok = true;
79 for($i++
; $i < $c; $i++
) {
80 if ($tokens[$i] === ',' ||
$tokens[$i] === ')' ||
$tokens[$i] === ';') {
83 if (is_string($tokens[$i])) continue;
84 if ($tokens[$i][0] === T_OBJECT_OPERATOR
) {
91 $line = $tokens[$i][2];
93 consumeWhitespace($tokens, $i);
94 if (!testToken($tokens[$i], T_STRING
, 'get')) continue;
96 consumeWhitespace($tokens, $i);
97 if (!testToken($tokens[$i], '(')) continue;
104 // What we currently don't match are batch retrievals, and
105 // wildcard retrievals. This data might be useful in the future,
106 // which is why we have a do {} while loop that doesn't actually
109 consumeWhitespace($tokens, $i);
110 if (!testToken($tokens[$i], T_CONSTANT_ENCAPSED_STRING
)) continue;
111 $namespace = substr($tokens[$i][1], 1, -1);
113 consumeWhitespace($tokens, $i);
114 if (!testToken($tokens[$i], ',')) continue;
116 consumeWhitespace($tokens, $i);
117 if (!testToken($tokens[$i], T_CONSTANT_ENCAPSED_STRING
)) continue;
118 $directive = substr($tokens[$i][1], 1, -1);
123 $id = "$namespace.$directive";
124 if (!isset($tracker[$id])) $tracker[$id] = array();
125 if (!isset($tracker[$id][$file])) $tracker[$id][$file] = array();
126 $tracker[$id][$file][] = $line;
130 //echo "$file:$line uses $namespace.$directive\n";
134 echo "\n$counter/$full_counter instances of \$config or \$this->config found in source code.\n";
136 echo "Generating XML... ";
138 $xw = new XMLWriter();
139 $xw->openURI('../configdoc/usage.xml');
140 $xw->setIndent(true);
141 $xw->startDocument('1.0', 'UTF-8');
142 $xw->startElement('usage');
143 foreach ($tracker as $id => $files) {
144 $xw->startElement('directive');
145 $xw->writeAttribute('id', $id);
146 foreach ($files as $file => $lines) {
147 $xw->startElement('file');
148 $xw->writeAttribute('name', $file);
149 foreach ($lines as $line) {
150 $xw->writeElement('line', $line);
161 // vim: et sw=4 sts=4