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