Make test script less chatty when log_errors is on.
[htmlpurifier.git] / maintenance / config-scanner.php
blob2b4efa3f85ff6a47d748621c4c864de92983730e
1 #!/usr/bin/php
2 <?php
4 chdir(dirname(__FILE__));
5 require_once 'common.php';
6 require_once '../library/HTMLPurifier.auto.php';
7 assertCli();
9 if (version_compare(PHP_VERSION, '5.2.2', '<')) {
10 echo "This script requires PHP 5.2.2 or later, for tokenizer line numbers.";
11 exit(1);
14 /**
15 * @file
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.
23 $FS = new FSTools();
24 chdir(dirname(__FILE__) . '/../library/');
25 $raw_files = $FS->globr('.', '*.php');
26 $files = array();
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
31 $files[] = $file;
34 /**
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);
41 /**
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;
52 } else {
53 return is_array($token) && $token[0] === $value_or_token && $token[1] === $value;
57 $counter = 0;
58 $full_counter = 0;
59 $tracker = array();
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++) {
65 $ok = false;
66 // Match $config
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;
74 break;
76 if (!$ok) continue;
78 $ok = false;
79 for($i++; $i < $c; $i++) {
80 if ($tokens[$i] === ',' || $tokens[$i] === ')' || $tokens[$i] === ';') {
81 break;
83 if (is_string($tokens[$i])) continue;
84 if ($tokens[$i][0] === T_OBJECT_OPERATOR) {
85 $ok = true;
86 break;
89 if (!$ok) continue;
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;
99 $full_counter++;
101 $matched = false;
102 do {
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
107 // do anything.
109 consumeWhitespace($tokens, $i);
110 if (!testToken($tokens[$i], T_CONSTANT_ENCAPSED_STRING)) continue;
111 $id = substr($tokens[$i][1], 1, -1);
113 $counter++;
114 $matched = true;
116 if (!isset($tracker[$id])) $tracker[$id] = array();
117 if (!isset($tracker[$id][$file])) $tracker[$id][$file] = array();
118 $tracker[$id][$file][] = $line;
120 } while (0);
122 //echo "$file:$line uses $namespace.$directive\n";
126 echo "\n$counter/$full_counter instances of \$config or \$this->config found in source code.\n";
128 echo "Generating XML... ";
130 $xw = new XMLWriter();
131 $xw->openURI('../configdoc/usage.xml');
132 $xw->setIndent(true);
133 $xw->startDocument('1.0', 'UTF-8');
134 $xw->startElement('usage');
135 foreach ($tracker as $id => $files) {
136 $xw->startElement('directive');
137 $xw->writeAttribute('id', $id);
138 foreach ($files as $file => $lines) {
139 $xw->startElement('file');
140 $xw->writeAttribute('name', $file);
141 foreach ($lines as $line) {
142 $xw->writeElement('line', $line);
144 $xw->endElement();
146 $xw->endElement();
148 $xw->endElement();
149 $xw->flush();
151 echo "done!\n";
153 // vim: et sw=4 sts=4