Remove annoying `s from the definition of the protected_titles table.
[lhc/web/wiklou.git] / maintenance / findhooks.php
1 <?php
2 /**
3 * Simple script that try to find documented hook and hooks actually
4 * in the code and show what's missing.
5 *
6 * This script assumes that:
7 * - hooks names in hooks.txt are at the beginning of a line and single quoted.
8 * - hooks names in code are the first parameter of wfRunHooks.
9 *
10 * Any instance of wfRunHooks that doesn't meet these parameters will be noted.
11 *
12 * @addtogroup Maintenance
13 *
14 * @author Ashar Voultoiz <hashar@altern.org>
15 * @copyright Copyright © Ashar voultoiz
16 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public Licence 2.0 or later
17 */
18
19 /** This is a command line script*/
20 include('commandLine.inc');
21
22
23 # GLOBALS
24
25 $doc = $IP . '/docs/hooks.txt';
26 $pathinc = $IP . '/includes/';
27
28
29 # FUNCTIONS
30
31 /**
32 * @return array of documented hooks
33 */
34 function getHooksFromDoc() {
35 global $doc;
36 $content = file_get_contents( $doc );
37 $m = array();
38 preg_match_all( "/\n'(.*?)'/", $content, $m);
39 return $m[1];
40 }
41
42 /**
43 * Get hooks from a PHP file
44 * @param $file Full filename to the PHP file.
45 * @return array of hooks found.
46 */
47 function getHooksFromFile( $file ) {
48 $content = file_get_contents( $file );
49 $m = array();
50 preg_match_all( '/wfRunHooks\(\s*([\'"])(.*?)\1/', $content, $m);
51 return $m[2];
52 }
53
54 /**
55 * Get hooks from the source code.
56 * @param $path Directory where the include files can be found
57 * @return array of hooks found.
58 */
59 function getHooksFromPath( $path ) {
60 $hooks = array();
61 if( $dh = opendir($path) ) {
62 while(($file = readdir($dh)) !== false) {
63 if( filetype($path.$file) == 'file' ) {
64 $hooks = array_merge( $hooks, getHooksFromFile($path.$file) );
65 }
66 }
67 closedir($dh);
68 }
69 return $hooks;
70 }
71
72 /**
73 * Get bad hooks (where the hook name could not be determined) from a PHP file
74 * @param $file Full filename to the PHP file.
75 * @return array of bad wfRunHooks() lines
76 */
77 function getBadHooksFromFile( $file ) {
78 $content = file_get_contents( $file );
79 $m = array();
80 # We want to skip the "function wfRunHooks()" one. :)
81 preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m);
82 return $m[0];
83 }
84
85 /**
86 * Get bad hooks from the source code.
87 * @param $path Directory where the include files can be found
88 * @return array of bad wfRunHooks() lines
89 */
90 function getBadHooksFromPath( $path ) {
91 $hooks = array();
92 if( $dh = opendir($path) ) {
93 while(($file = readdir($dh)) !== false) {
94 if( filetype($path.$file) == 'file' ) {
95 $hooks = array_merge( $hooks, getBadHooksFromFile($path.$file) );
96 }
97 }
98 closedir($dh);
99 }
100 return $hooks;
101 }
102
103 /**
104 * Nicely output the array
105 * @param $msg A message to show before the value
106 * @param $arr An array
107 * @param $sort Boolean : wheter to sort the array (Default: true)
108 */
109 function printArray( $msg, $arr, $sort = true ) {
110 if($sort) asort($arr);
111 foreach($arr as $v) print "$msg: $v\n";
112 }
113
114
115 # MAIN
116
117 $documented = getHooksFromDoc($doc);
118 $potential = getHooksFromPath($pathinc);
119 $bad = getBadHooksFromPath($pathinc);
120
121 $todo = array_diff($potential, $documented);
122 $deprecated = array_diff($documented, $potential);
123
124 // let's show the results:
125 printArray('undocumented', $todo );
126 printArray('not found', $deprecated );
127 printArray('unclear hook calls', $bad );
128
129