c2b1d75b256ba8b77dfdfe727a4e6160af4ec3a3
3 * Simple script that try to find documented hook and hooks actually
4 * in the code and show what's missing.
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.
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');
28 $doc = $IP . '/docs/hooks.txt';
33 $IP.'/includes/filerepo/',
34 $IP.'/includes/parser/',
35 $IP.'/includes/specials/',
44 * @return array of documented hooks
46 function getHooksFromDoc() {
47 global $doc, $options;
49 if( isset( $options['online'] ) ){
50 $content = Http
::get( 'http://www.mediawiki.org/w/index.php?title=Manual:Hooks&action=raw' );
51 preg_match_all( '/\[\[\/([a-zA-Z0-9-_:]+)\|/', $content, $m );
53 $content = file_get_contents( $doc );
54 preg_match_all( "/\n'(.*?)'/", $content, $m );
60 * Get hooks from a PHP file
61 * @param $file Full filename to the PHP file.
62 * @return array of hooks found.
64 function getHooksFromFile( $file ) {
65 $content = file_get_contents( $file );
67 preg_match_all( '/wfRunHooks\(\s*([\'"])(.*?)\1/', $content, $m);
72 * Get hooks from the source code.
73 * @param $path Directory where the include files can be found
74 * @return array of hooks found.
76 function getHooksFromPath( $path ) {
78 if( $dh = opendir($path) ) {
79 while(($file = readdir($dh)) !== false) {
80 if( filetype($path.$file) == 'file' ) {
81 $hooks = array_merge( $hooks, getHooksFromFile($path.$file) );
90 * Get bad hooks (where the hook name could not be determined) from a PHP file
91 * @param $file Full filename to the PHP file.
92 * @return array of bad wfRunHooks() lines
94 function getBadHooksFromFile( $file ) {
95 $content = file_get_contents( $file );
97 # We want to skip the "function wfRunHooks()" one. :)
98 preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m);
100 foreach( $m[0] as $match ){
101 $list[] = $match . "(" . $file . ")";
107 * Get bad hooks from the source code.
108 * @param $path Directory where the include files can be found
109 * @return array of bad wfRunHooks() lines
111 function getBadHooksFromPath( $path ) {
113 if( $dh = opendir($path) ) {
114 while(($file = readdir($dh)) !== false) {
115 # We don't want to read this file as it contains bad calls to wfRunHooks()
116 if( filetype( $path.$file ) == 'file' && !$path.$file == __FILE__
) {
117 $hooks = array_merge( $hooks, getBadHooksFromFile($path.$file) );
126 * Nicely output the array
127 * @param $msg A message to show before the value
128 * @param $arr An array
129 * @param $sort Boolean : wheter to sort the array (Default: true)
131 function printArray( $msg, $arr, $sort = true ) {
132 if($sort) asort($arr);
133 foreach($arr as $v) echo "$msg: $v\n";
138 $documented = getHooksFromDoc($doc);
139 $potential = array();
141 foreach( $pathinc as $dir ) {
142 $potential = array_merge( $potential, getHooksFromPath( $dir ) );
143 $bad = array_merge( $bad, getBadHooksFromPath( $dir ) );
146 $potential = array_unique( $potential );
147 $bad = array_unique( $bad );
148 $todo = array_diff( $potential, $documented );
149 $deprecated = array_diff( $documented, $potential );
151 // let's show the results:
152 printArray('undocumented', $todo );
153 printArray('not found', $deprecated );
154 printArray('unclear hook calls', $bad );
156 if ( count( $todo ) == 0 && count( $deprecated ) == 0 && count( $bad ) == 0 )
157 echo "Looks good!\n";