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