Merge maintenance-work branch (now with less errors!):
[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 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 * http://www.gnu.org/copyleft/gpl.html
29 *
30 * @ingroup Maintenance
31 *
32 * @author Ashar Voultoiz <hashar@altern.org>
33 * @copyright Copyright © Ashar voultoiz
34 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public Licence 2.0 or later
35 */
36
37 require_once( "Maintenance.php" );
38
39 class FindHooks extends Maintenance {
40 public function __construct() {
41 parent::__construct();
42 $this->mDescription = "Find hooks that are undocumented, missing, or just plain wrong";
43 $this->addOption( 'online', 'Check against mediawiki.org hook documentation' );
44 }
45
46 public function execute() {
47 global $IP;
48
49 $documented = $this->getHooksFromDoc( $IP . '/docs/hooks.txt' );
50 $potential = array();
51 $bad = array();
52 $pathinc = array(
53 $IP.'/',
54 $IP.'/includes/',
55 $IP.'/includes/api/',
56 $IP.'/includes/db/',
57 $IP.'/includes/diff/',
58 $IP.'/includes/filerepo/',
59 $IP.'/includes/parser/',
60 $IP.'/includes/search/',
61 $IP.'/includes/specials/',
62 $IP.'/includes/upload/',
63 $IP.'/languages/',
64 $IP.'/maintenance/',
65 $IP.'/skins/',
66 );
67
68 foreach( $pathinc as $dir ) {
69 $potential = array_merge( $potential, $this->getHooksFromPath( $dir ) );
70 $bad = array_merge( $bad, $this->getBadHooksFromPath( $dir ) );
71 }
72
73 $potential = array_unique( $potential );
74 $bad = array_unique( $bad );
75 $todo = array_diff( $potential, $documented );
76 $deprecated = array_diff( $documented, $potential );
77
78 // let's show the results:
79 $this->printArray('Undocumented', $todo );
80 $this->printArray('Documented and not found', $deprecated );
81 $this->printArray('Unclear hook calls', $bad );
82
83 if ( count( $todo ) == 0 && count( $deprecated ) == 0 && count( $bad ) == 0 )
84 $this->output( "Looks good!\n" );
85 }
86
87 /**
88 * Get the hook documentation, either locally or from mediawiki.org
89 * @return array of documented hooks
90 */
91 private function getHooksFromDoc( $doc ) {
92 if( $this->hasOption( 'online' ) ){
93 // All hooks
94 $allhookdata = Http::get( 'http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:MediaWiki_hooks&cmlimit=500&format=php' );
95 $allhookdata = unserialize( $allhookdata );
96 $allhooks = array();
97 foreach( $allhookdata['query']['categorymembers'] as $page ) {
98 $found = preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $matches );
99 if( $found ) {
100 $hook = str_replace( ' ', '_', $matches[1] );
101 $allhooks[] = $hook;
102 }
103 }
104 // Removed hooks
105 $oldhookdata = Http::get( 'http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Removed_hooks&cmlimit=500&format=php' );
106 $oldhookdata = unserialize( $oldhookdata );
107 $removed = array();
108 foreach( $oldhookdata['query']['categorymembers'] as $page ) {
109 $found = preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $matches );
110 if( $found ) {
111 $hook = str_replace( ' ', '_', $matches[1] );
112 $removed[] = $hook;
113 }
114 }
115 return array_diff( $allhooks, $removed );
116 } else {
117 $m = array();
118 $content = file_get_contents( $doc );
119 preg_match_all( "/\n'(.*?)'/", $content, $m );
120 return array_unique( $m[1] );
121 }
122 }
123
124 /**
125 * Get hooks from a PHP file
126 * @param $file Full filename to the PHP file.
127 * @return array of hooks found.
128 */
129 private function getHooksFromFile( $file ) {
130 $content = file_get_contents( $file );
131 $m = array();
132 preg_match_all( '/wfRunHooks\(\s*([\'"])(.*?)\1/', $content, $m);
133 return $m[2];
134 }
135
136 /**
137 * Get hooks from the source code.
138 * @param $path Directory where the include files can be found
139 * @return array of hooks found.
140 */
141 private function getHooksFromPath( $path ) {
142 $hooks = array();
143 if( $dh = opendir($path) ) {
144 while(($file = readdir($dh)) !== false) {
145 if( filetype($path.$file) == 'file' ) {
146 $hooks = array_merge( $hooks, $this->getHooksFromFile($path.$file) );
147 }
148 }
149 closedir($dh);
150 }
151 return $hooks;
152 }
153
154 /**
155 * Get bad hooks (where the hook name could not be determined) from a PHP file
156 * @param $file Full filename to the PHP file.
157 * @return array of bad wfRunHooks() lines
158 */
159 private function getBadHooksFromFile( $file ) {
160 $content = file_get_contents( $file );
161 $m = array();
162 # We want to skip the "function wfRunHooks()" one. :)
163 preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m);
164 $list = array();
165 foreach( $m[0] as $match ){
166 $list[] = $match . "(" . $file . ")";
167 }
168 return $list;
169 }
170
171 /**
172 * Get bad hooks from the source code.
173 * @param $path Directory where the include files can be found
174 * @return array of bad wfRunHooks() lines
175 */
176 private function getBadHooksFromPath( $path ) {
177 $hooks = array();
178 if( $dh = opendir($path) ) {
179 while(($file = readdir($dh)) !== false) {
180 # We don't want to read this file as it contains bad calls to wfRunHooks()
181 if( filetype( $path.$file ) == 'file' && !$path.$file == __FILE__ ) {
182 $hooks = array_merge( $hooks, $this->getBadHooksFromFile($path.$file) );
183 }
184 }
185 closedir($dh);
186 }
187 return $hooks;
188 }
189
190 /**
191 * Nicely output the array
192 * @param $msg A message to show before the value
193 * @param $arr An array
194 * @param $sort Boolean : wheter to sort the array (Default: true)
195 */
196 private function printArray( $msg, $arr, $sort = true ) {
197 if($sort) asort($arr);
198 foreach($arr as $v) $this->output( "$msg: $v\n" );
199 }
200 }
201
202 $maintClass = "FindHooks";
203 require_once( DO_MAINTENANCE );