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