* Added --online option to maintenance/findhooks.php to compare hooks with http:...
[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 * if --online option is passed, the script will compare the hooks in the code
11 * with the ones at http://www.mediawiki.org/wiki/Manual:Hooks
12 *
13 * Any instance of wfRunHooks that doesn't meet these parameters will be noted.
14 *
15 * @addtogroup Maintenance
16 *
17 * @author Ashar Voultoiz <hashar@altern.org>
18 * @copyright Copyright © Ashar voultoiz
19 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public Licence 2.0 or later
20 */
21
22 /** This is a command line script*/
23 include('commandLine.inc');
24
25 # GLOBALS
26
27 $doc = $IP . '/docs/hooks.txt';
28 $pathinc = array( $IP.'/includes/', $IP.'/includes/api/', $IP.'/includes/filerepo/', $IP.'/languages/', $IP.'/maintenance/', $IP.'/skins/' );
29
30 # FUNCTIONS
31
32 /**
33 * @return array of documented hooks
34 */
35 function getHooksFromDoc() {
36 global $doc, $options;
37 $m = array();
38 if( isset( $options['online'] ) ){
39 $content = Http::get( 'http://www.mediawiki.org/w/index.php?title=Manual:Hooks&action=raw' );
40 preg_match_all( '/\[\[\/([a-zA-Z0-9-_:]+)\|/', $content, $m );
41 } else {
42 $content = file_get_contents( $doc );
43 preg_match_all( "/\n'(.*?)'/", $content, $m );
44 }
45 return $m[1];
46 }
47
48 /**
49 * Get hooks from a PHP file
50 * @param $file Full filename to the PHP file.
51 * @return array of hooks found.
52 */
53 function getHooksFromFile( $file ) {
54 $content = file_get_contents( $file );
55 $m = array();
56 preg_match_all( '/wfRunHooks\(\s*([\'"])(.*?)\1/', $content, $m);
57 return $m[2];
58 }
59
60 /**
61 * Get hooks from the source code.
62 * @param $path Directory where the include files can be found
63 * @return array of hooks found.
64 */
65 function getHooksFromPath( $path ) {
66 $hooks = array();
67 if( $dh = opendir($path) ) {
68 while(($file = readdir($dh)) !== false) {
69 if( filetype($path.$file) == 'file' ) {
70 $hooks = array_merge( $hooks, getHooksFromFile($path.$file) );
71 }
72 }
73 closedir($dh);
74 }
75 return $hooks;
76 }
77
78 /**
79 * Get bad hooks (where the hook name could not be determined) from a PHP file
80 * @param $file Full filename to the PHP file.
81 * @return array of bad wfRunHooks() lines
82 */
83 function getBadHooksFromFile( $file ) {
84 $content = file_get_contents( $file );
85 $m = array();
86 # We want to skip the "function wfRunHooks()" one. :)
87 preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m);
88 $list = array();
89 foreach( $m[0] as $match ){
90 $list[] = $match . "(" . $file . ")";
91 }
92 return $list;
93 }
94
95 /**
96 * Get bad hooks from the source code.
97 * @param $path Directory where the include files can be found
98 * @return array of bad wfRunHooks() lines
99 */
100 function getBadHooksFromPath( $path ) {
101 $hooks = array();
102 if( $dh = opendir($path) ) {
103 while(($file = readdir($dh)) !== false) {
104 # We don't want to read this file as it contains bad calls to wfRunHooks()
105 if( filetype( $path.$file ) == 'file' && !$path.$file == __FILE__ ) {
106 $hooks = array_merge( $hooks, getBadHooksFromFile($path.$file) );
107 }
108 }
109 closedir($dh);
110 }
111 return $hooks;
112 }
113
114 /**
115 * Nicely output the array
116 * @param $msg A message to show before the value
117 * @param $arr An array
118 * @param $sort Boolean : wheter to sort the array (Default: true)
119 */
120 function printArray( $msg, $arr, $sort = true ) {
121 if($sort) asort($arr);
122 foreach($arr as $v) echo "$msg: $v\n";
123 }
124
125 # MAIN
126
127 $documented = getHooksFromDoc($doc);
128 $potential = array();
129 $bad = array();
130 foreach( $pathinc as $dir ) {
131 $potential = array_merge( $potential, getHooksFromPath( $dir ) );
132 $bad = array_merge( $bad, getBadHooksFromPath( $dir ) );
133 }
134
135 $potential = array_unique( $potential );
136 $bad = array_unique( $bad );
137 $todo = array_diff( $potential, $documented );
138 $deprecated = array_diff( $documented, $potential );
139
140 // let's show the results:
141 printArray('undocumented', $todo );
142 printArray('not found', $deprecated );
143 printArray('unclear hook calls', $bad );
144
145 if ( count( $todo ) == 0 && count( $deprecated ) == 0 && count( $bad ) == 0 )
146 echo "Looks good!\n";