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