API: Various docu and clean-up.
[mediawiki.git] / maintenance / findhooks.php
blob0240b63155865939dd19f70d1ae92601bc60bf27
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 * Any instance of wfRunHooks that doesn't meet these parameters will be noted.
12 * @addtogroup Maintenance
14 * @author Ashar Voultoiz <hashar@altern.org>
15 * @copyright Copyright © Ashar voultoiz
16 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public Licence 2.0 or later
19 /** This is a command line script*/
20 include('commandLine.inc');
23 # GLOBALS
25 $doc = $IP . '/docs/hooks.txt';
26 $pathinc = $IP . '/includes/';
29 # FUNCTIONS
31 /**
32 * @return array of documented hooks
34 function getHooksFromDoc() {
35 global $doc;
36 $content = file_get_contents( $doc );
37 $m = array();
38 preg_match_all( "/\n'(.*?)'/", $content, $m);
39 return $m[1];
42 /**
43 * Get hooks from a PHP file
44 * @param $file Full filename to the PHP file.
45 * @return array of hooks found.
47 function getHooksFromFile( $file ) {
48 $content = file_get_contents( $file );
49 $m = array();
50 preg_match_all( '/wfRunHooks\(\s*([\'"])(.*?)\1/', $content, $m);
51 return $m[2];
54 /**
55 * Get hooks from the source code.
56 * @param $path Directory where the include files can be found
57 * @return array of hooks found.
59 function getHooksFromPath( $path ) {
60 $hooks = array();
61 if( $dh = opendir($path) ) {
62 while(($file = readdir($dh)) !== false) {
63 if( filetype($path.$file) == 'file' ) {
64 $hooks = array_merge( $hooks, getHooksFromFile($path.$file) );
67 closedir($dh);
69 return $hooks;
72 /**
73 * Get bad hooks (where the hook name could not be determined) from a PHP file
74 * @param $file Full filename to the PHP file.
75 * @return array of bad wfRunHooks() lines
77 function getBadHooksFromFile( $file ) {
78 $content = file_get_contents( $file );
79 $m = array();
80 # We want to skip the "function wfRunHooks()" one. :)
81 preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m);
82 return $m[0];
85 /**
86 * Get bad hooks from the source code.
87 * @param $path Directory where the include files can be found
88 * @return array of bad wfRunHooks() lines
90 function getBadHooksFromPath( $path ) {
91 $hooks = array();
92 if( $dh = opendir($path) ) {
93 while(($file = readdir($dh)) !== false) {
94 if( filetype($path.$file) == 'file' ) {
95 $hooks = array_merge( $hooks, getBadHooksFromFile($path.$file) );
98 closedir($dh);
100 return $hooks;
104 * Nicely output the array
105 * @param $msg A message to show before the value
106 * @param $arr An array
107 * @param $sort Boolean : wheter to sort the array (Default: true)
109 function printArray( $msg, $arr, $sort = true ) {
110 if($sort) asort($arr);
111 foreach($arr as $v) print "$msg: $v\n";
115 # MAIN
117 $documented = getHooksFromDoc($doc);
118 $potential = getHooksFromPath($pathinc);
119 $bad = getBadHooksFromPath($pathinc);
121 $todo = array_diff($potential, $documented);
122 $deprecated = array_diff($documented, $potential);
124 // let's show the results:
125 printArray('undocumented', $todo );
126 printArray('not found', $deprecated );
127 printArray('unclear hook calls', $bad );