ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
config-scanner.php
Go to the documentation of this file.
1 #!/usr/bin/php
2 <?php
3 
4 chdir(dirname(__FILE__));
5 require_once 'common.php';
6 require_once '../library/HTMLPurifier.auto.php';
7 assertCli();
8 
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);
12 }
13 
23 $FS = new FSTools();
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
31  $files[] = $file;
32 }
33 
37 function consumeWhitespace($tokens, &$i)
38 {
39  do {$i++;} while (is_array($tokens[$i]) && $tokens[$i][0] === T_WHITESPACE);
40 }
41 
49 function testToken($token, $value_or_token, $value = null)
50 {
51  if (is_null($value)) {
52  if (is_int($value_or_token)) return is_array($token) && $token[0] === $value_or_token;
53  else return $token === $value_or_token;
54  } else {
55  return is_array($token) && $token[0] === $value_or_token && $token[1] === $value;
56  }
57 }
58 
62 
63 foreach ($files as $file) {
64  $tokens = token_get_all(file_get_contents($file));
65  $file = str_replace('\\', '/', $file);
66  for ($i = 0, $c = count($tokens); $i < $c; $i++) {
67  $ok = false;
68  // Match $config
69  if (!$ok && testToken($tokens[$i], T_VARIABLE, '$config')) $ok = true;
70  // Match $this->config
71  while (!$ok && testToken($tokens[$i], T_VARIABLE, '$this')) {
72  consumeWhitespace($tokens, $i);
73  if (!testToken($tokens[$i], T_OBJECT_OPERATOR)) break;
74  consumeWhitespace($tokens, $i);
75  if (testToken($tokens[$i], T_STRING, 'config')) $ok = true;
76  break;
77  }
78  if (!$ok) continue;
79 
80  $ok = false;
81  for($i++; $i < $c; $i++) {
82  if ($tokens[$i] === ',' || $tokens[$i] === ')' || $tokens[$i] === ';') {
83  break;
84  }
85  if (is_string($tokens[$i])) continue;
86  if ($tokens[$i][0] === T_OBJECT_OPERATOR) {
87  $ok = true;
88  break;
89  }
90  }
91  if (!$ok) continue;
92 
93  $line = $tokens[$i][2];
94 
95  consumeWhitespace($tokens, $i);
96  if (!testToken($tokens[$i], T_STRING, 'get')) continue;
97 
98  consumeWhitespace($tokens, $i);
99  if (!testToken($tokens[$i], '(')) continue;
100 
101  $full_counter++;
102 
103  $matched = false;
104  do {
105 
106  // What we currently don't match are batch retrievals, and
107  // wildcard retrievals. This data might be useful in the future,
108  // which is why we have a do {} while loop that doesn't actually
109  // do anything.
110 
111  consumeWhitespace($tokens, $i);
112  if (!testToken($tokens[$i], T_CONSTANT_ENCAPSED_STRING)) continue;
113  $id = substr($tokens[$i][1], 1, -1);
114 
115  $counter++;
116  $matched = true;
117 
118  if (!isset($tracker[$id])) $tracker[$id] = array();
119  if (!isset($tracker[$id][$file])) $tracker[$id][$file] = array();
120  $tracker[$id][$file][] = $line;
121 
122  } while (0);
123 
124  //echo "$file:$line uses $namespace.$directive\n";
125  }
126 }
127 
128 echo "\n$counter/$full_counter instances of \$config or \$this->config found in source code.\n";
129 
130 echo "Generating XML... ";
131 
132 $xw = new XMLWriter();
133 $xw->openURI('../configdoc/usage.xml');
134 $xw->setIndent(true);
135 $xw->startDocument('1.0', 'UTF-8');
136 $xw->startElement('usage');
137 foreach ($tracker as $id => $files) {
138  $xw->startElement('directive');
139  $xw->writeAttribute('id', $id);
140  foreach ($files as $file => $lines) {
141  $xw->startElement('file');
142  $xw->writeAttribute('name', $file);
143  foreach ($lines as $line) {
144  $xw->writeElement('line', $line);
145  }
146  $xw->endElement();
147  }
148  $xw->endElement();
149 }
150 $xw->endElement();
151 $xw->flush();
152 
153 echo "done!\n";
154 
155 // vim: et sw=4 sts=4
$files
$raw_files
$full_counter
Filesystem tools not provided by default; can recursively create, copy and delete folders...
Definition: FSTools.php:10
$counter
Create styles array
The data for the language used.
assertCli()
Definition: common.php:3
foreach($raw_files as $file) consumeWhitespace($tokens, &$i)
Moves the $i cursor to the next non-whitespace token.
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
$tracker
testToken($token, $value_or_token, $value=null)
Tests whether or not a token is a particular type.