Followup r87225
[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/api/',
60 $IP . '/includes/db/',
61 $IP . '/includes/diff/',
62 $IP . '/includes/filerepo/',
63 $IP . '/includes/installer/',
64 $IP . '/includes/parser/',
65 $IP . '/includes/resourceloader/',
66 $IP . '/includes/revisiondelete/',
67 $IP . '/includes/search/',
68 $IP . '/includes/specials/',
69 $IP . '/includes/upload/',
70 $IP . '/languages/',
71 $IP . '/maintenance/',
72 $IP . '/tests/',
73 $IP . '/tests/parser/',
74 $IP . '/tests/phpunit/suites/',
75 $IP . '/skins/',
76 );
77
78 foreach ( $pathinc as $dir ) {
79 $potential = array_merge( $potential, $this->getHooksFromPath( $dir ) );
80 $bad = array_merge( $bad, $this->getBadHooksFromPath( $dir ) );
81 }
82
83 $potential = array_unique( $potential );
84 $bad = array_unique( $bad );
85 $todo = array_diff( $potential, $documented );
86 $deprecated = array_diff( $documented, $potential );
87
88 // let's show the results:
89 $this->printArray( 'Undocumented', $todo );
90 $this->printArray( 'Documented and not found', $deprecated );
91 $this->printArray( 'Unclear hook calls', $bad );
92
93 if ( count( $todo ) == 0 && count( $deprecated ) == 0 && count( $bad ) == 0 )
94 {
95 $this->output( "Looks good!\n" );
96 }
97 }
98
99 /**
100 * Get the hook documentation, either locally or from MediaWiki.org
101 * @return array of documented hooks
102 */
103 private function getHooksFromDoc( $doc ) {
104 if ( $this->hasOption( 'online' ) ) {
105 // All hooks
106 $allhookdata = Http::get( 'http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:MediaWiki_hooks&cmlimit=500&format=php' );
107 $allhookdata = unserialize( $allhookdata );
108 $allhooks = array();
109 foreach ( $allhookdata['query']['categorymembers'] as $page ) {
110 $found = preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $matches );
111 if ( $found ) {
112 $hook = str_replace( ' ', '_', $matches[1] );
113 $allhooks[] = $hook;
114 }
115 }
116 // Removed hooks
117 $oldhookdata = Http::get( 'http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Removed_hooks&cmlimit=500&format=php' );
118 $oldhookdata = unserialize( $oldhookdata );
119 $removed = array();
120 foreach ( $oldhookdata['query']['categorymembers'] as $page ) {
121 $found = preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $matches );
122 if ( $found ) {
123 $hook = str_replace( ' ', '_', $matches[1] );
124 $removed[] = $hook;
125 }
126 }
127 return array_diff( $allhooks, $removed );
128 } else {
129 $m = array();
130 $content = file_get_contents( $doc );
131 preg_match_all( "/\n'(.*?)'/", $content, $m );
132 return array_unique( $m[1] );
133 }
134 }
135
136 /**
137 * Get hooks from a PHP file
138 * @param $file Full filename to the PHP file.
139 * @return array of hooks found.
140 */
141 private function getHooksFromFile( $file ) {
142 $content = file_get_contents( $file );
143 $m = array();
144 preg_match_all( '/(wfRunHooks|Hooks\:\:run)\(\s*([\'"])(.*?)\1/', $content, $m );
145 return $m[2];
146 }
147
148 /**
149 * Get hooks from the source code.
150 * @param $path Directory where the include files can be found
151 * @return array of hooks found.
152 */
153 private function getHooksFromPath( $path ) {
154 $hooks = array();
155 $dh = opendir( $path );
156 if ( $dh ) {
157 while ( ( $file = readdir( $dh ) ) !== false ) {
158 if ( filetype( $path . $file ) == 'file' ) {
159 $hooks = array_merge( $hooks, $this->getHooksFromFile( $path . $file ) );
160 }
161 }
162 closedir( $dh );
163 }
164 return $hooks;
165 }
166
167 /**
168 * Get bad hooks (where the hook name could not be determined) from a PHP file
169 * @param $file Full filename to the PHP file.
170 * @return array of bad wfRunHooks() lines
171 */
172 private function getBadHooksFromFile( $file ) {
173 $content = file_get_contents( $file );
174 $m = array();
175 # We want to skip the "function wfRunHooks()" one. :)
176 preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m );
177 $list = array();
178 foreach ( $m[0] as $match ) {
179 $list[] = $match . "(" . $file . ")";
180 }
181 return $list;
182 }
183
184 /**
185 * Get bad hooks from the source code.
186 * @param $path Directory where the include files can be found
187 * @return array of bad wfRunHooks() lines
188 */
189 private function getBadHooksFromPath( $path ) {
190 $hooks = array();
191 $dh = opendir( $path );
192 if ( $dh ) {
193 while ( ( $file = readdir( $dh ) ) !== false ) {
194 # We don't want to read this file as it contains bad calls to wfRunHooks()
195 if ( filetype( $path . $file ) == 'file' && !$path . $file == __FILE__ ) {
196 $hooks = array_merge( $hooks, $this->getBadHooksFromFile( $path . $file ) );
197 }
198 }
199 closedir( $dh );
200 }
201 return $hooks;
202 }
203
204 /**
205 * Nicely output the array
206 * @param $msg String: a message to show before the value
207 * @param $arr Array: an array
208 * @param $sort Boolean: whether to sort the array (Default: true)
209 */
210 private function printArray( $msg, $arr, $sort = true ) {
211 if ( $sort ) {
212 asort( $arr );
213 }
214 foreach ( $arr as $v ) {
215 $this->output( "$msg: $v\n" );
216 }
217 }
218 }
219
220 $maintClass = 'FindHooks';
221 require_once( RUN_MAINTENANCE_IF_MAIN );