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';
34 $IP.'/includes/filerepo/',
35 $IP.'/includes/parser/',
36 $IP.'/includes/specials/',
45 * @return array of documented hooks
47 function getHooksFromDoc() {
48 global $doc, $options;
50 if( isset( $options['online'] ) ){
51 $content = Http
::get( 'http://www.mediawiki.org/w/index.php?title=Manual:Hooks&action=raw' );
52 preg_match_all( '/\[\[\/([a-zA-Z0-9-_:]+)\|/', $content, $m );
54 $content = file_get_contents( $doc );
55 preg_match_all( "/\n'(.*?)'/", $content, $m );
57 return array_unique( $m[1] );
61 * Get hooks from a PHP file
62 * @param $file Full filename to the PHP file.
63 * @return array of hooks found.
65 function getHooksFromFile( $file ) {
66 $content = file_get_contents( $file );
68 preg_match_all( '/wfRunHooks\(\s*([\'"])(.*?)\1/', $content, $m);
73 * Get hooks from the source code.
74 * @param $path Directory where the include files can be found
75 * @return array of hooks found.
77 function getHooksFromPath( $path ) {
79 if( $dh = opendir($path) ) {
80 while(($file = readdir($dh)) !== false) {
81 if( filetype($path.$file) == 'file' ) {
82 $hooks = array_merge( $hooks, getHooksFromFile($path.$file) );
91 * Get bad hooks (where the hook name could not be determined) from a PHP file
92 * @param $file Full filename to the PHP file.
93 * @return array of bad wfRunHooks() lines
95 function getBadHooksFromFile( $file ) {
96 $content = file_get_contents( $file );
98 # We want to skip the "function wfRunHooks()" one. :)
99 preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m);
101 foreach( $m[0] as $match ){
102 $list[] = $match . "(" . $file . ")";
108 * Get bad hooks from the source code.
109 * @param $path Directory where the include files can be found
110 * @return array of bad wfRunHooks() lines
112 function getBadHooksFromPath( $path ) {
114 if( $dh = opendir($path) ) {
115 while(($file = readdir($dh)) !== false) {
116 # We don't want to read this file as it contains bad calls to wfRunHooks()
117 if( filetype( $path.$file ) == 'file' && !$path.$file == __FILE__
) {
118 $hooks = array_merge( $hooks, getBadHooksFromFile($path.$file) );
127 * Nicely output the array
128 * @param $msg A message to show before the value
129 * @param $arr An array
130 * @param $sort Boolean : wheter to sort the array (Default: true)
132 function printArray( $msg, $arr, $sort = true ) {
133 if($sort) asort($arr);
134 foreach($arr as $v) echo "$msg: $v\n";
139 $documented = getHooksFromDoc($doc);
140 $potential = array();
142 foreach( $pathinc as $dir ) {
143 $potential = array_merge( $potential, getHooksFromPath( $dir ) );
144 $bad = array_merge( $bad, getBadHooksFromPath( $dir ) );
147 $potential = array_unique( $potential );
148 $bad = array_unique( $bad );
149 $todo = array_diff( $potential, $documented );
150 $deprecated = array_diff( $documented, $potential );
152 // let's show the results:
153 printArray('undocumented', $todo );
154 printArray('not found', $deprecated );
155 printArray('unclear hook calls', $bad );
157 if ( count( $todo ) == 0 && count( $deprecated ) == 0 && count( $bad ) == 0 )
158 echo "Looks good!\n";