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