Language object cache, for faster wfMsg() performance with unusual languages, and...
[mediawiki.git] / maintenance / findhooks.php
blobd7cad253c642705af042f5e444188e157ffe1cab
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.
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
13 * Any instance of wfRunHooks that doesn't meet these parameters will be noted.
15 * @file
16 * @ingroup Maintenance
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
23 /** This is a command line script*/
24 include('commandLine.inc');
26 # GLOBALS
28 $doc = $IP . '/docs/hooks.txt';
29 $pathinc = array(
30 $IP.'/',
31 $IP.'/includes/',
32 $IP.'/includes/api/',
33 $IP.'/includes/db/',
34 $IP.'/includes/diff/',
35 $IP.'/includes/filerepo/',
36 $IP.'/includes/parser/',
37 $IP.'/includes/specials/',
38 $IP.'/languages/',
39 $IP.'/maintenance/',
40 $IP.'/skins/',
43 # FUNCTIONS
45 /**
46 * @return array of documented hooks
48 function getHooksFromDoc() {
49 global $doc, $options;
50 $m = array();
51 if( isset( $options['online'] ) ){
52 $content = Http::get( 'http://www.mediawiki.org/w/index.php?title=Manual:Hooks&action=raw' );
53 preg_match_all( '/\[\[\/([a-zA-Z0-9-_:]+)\|/', $content, $m );
54 } else {
55 $content = file_get_contents( $doc );
56 preg_match_all( "/\n'(.*?)'/", $content, $m );
58 return array_unique( $m[1] );
61 /**
62 * Get hooks from a PHP file
63 * @param $file Full filename to the PHP file.
64 * @return array of hooks found.
66 function getHooksFromFile( $file ) {
67 $content = file_get_contents( $file );
68 $m = array();
69 preg_match_all( '/wfRunHooks\(\s*([\'"])(.*?)\1/', $content, $m);
70 return $m[2];
73 /**
74 * Get hooks from the source code.
75 * @param $path Directory where the include files can be found
76 * @return array of hooks found.
78 function getHooksFromPath( $path ) {
79 $hooks = array();
80 if( $dh = opendir($path) ) {
81 while(($file = readdir($dh)) !== false) {
82 if( filetype($path.$file) == 'file' ) {
83 $hooks = array_merge( $hooks, getHooksFromFile($path.$file) );
86 closedir($dh);
88 return $hooks;
91 /**
92 * Get bad hooks (where the hook name could not be determined) from a PHP file
93 * @param $file Full filename to the PHP file.
94 * @return array of bad wfRunHooks() lines
96 function getBadHooksFromFile( $file ) {
97 $content = file_get_contents( $file );
98 $m = array();
99 # We want to skip the "function wfRunHooks()" one. :)
100 preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m);
101 $list = array();
102 foreach( $m[0] as $match ){
103 $list[] = $match . "(" . $file . ")";
105 return $list;
109 * Get bad hooks from the source code.
110 * @param $path Directory where the include files can be found
111 * @return array of bad wfRunHooks() lines
113 function getBadHooksFromPath( $path ) {
114 $hooks = array();
115 if( $dh = opendir($path) ) {
116 while(($file = readdir($dh)) !== false) {
117 # We don't want to read this file as it contains bad calls to wfRunHooks()
118 if( filetype( $path.$file ) == 'file' && !$path.$file == __FILE__ ) {
119 $hooks = array_merge( $hooks, getBadHooksFromFile($path.$file) );
122 closedir($dh);
124 return $hooks;
128 * Nicely output the array
129 * @param $msg A message to show before the value
130 * @param $arr An array
131 * @param $sort Boolean : wheter to sort the array (Default: true)
133 function printArray( $msg, $arr, $sort = true ) {
134 if($sort) asort($arr);
135 foreach($arr as $v) echo "$msg: $v\n";
138 # MAIN
140 $documented = getHooksFromDoc($doc);
141 $potential = array();
142 $bad = array();
143 foreach( $pathinc as $dir ) {
144 $potential = array_merge( $potential, getHooksFromPath( $dir ) );
145 $bad = array_merge( $bad, getBadHooksFromPath( $dir ) );
148 $potential = array_unique( $potential );
149 $bad = array_unique( $bad );
150 $todo = array_diff( $potential, $documented );
151 $deprecated = array_diff( $documented, $potential );
153 // let's show the results:
154 printArray('undocumented', $todo );
155 printArray('not found', $deprecated );
156 printArray('unclear hook calls', $bad );
158 if ( count( $todo ) == 0 && count( $deprecated ) == 0 && count( $bad ) == 0 )
159 echo "Looks good!\n";