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 * 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');
25 $doc = $IP . '/docs/hooks.txt';
26 $pathinc = $IP . '/includes/';
27 $pathfile = $IP . '/includes/filerepo/LocalFile.php';
28 $pathlang = $IP . '/languages/Language.php';
29 $pathskin = $IP . '/skins/MonoBook.php';
35 * @return array of documented hooks
37 function getHooksFromDoc() {
39 $content = file_get_contents( $doc );
41 preg_match_all( "/\n'(.*?)'/", $content, $m);
46 * Get hooks from a PHP file
47 * @param $file Full filename to the PHP file.
48 * @return array of hooks found.
50 function getHooksFromFile( $file ) {
51 $content = file_get_contents( $file );
53 preg_match_all( '/wfRunHooks\(\s*([\'"])(.*?)\1/', $content, $m);
58 * Get hooks from the source code.
59 * @param $path Directory where the include files can be found
60 * @return array of hooks found.
62 function getHooksFromPath( $path ) {
64 if( $dh = opendir($path) ) {
65 while(($file = readdir($dh)) !== false) {
66 if( filetype($path.$file) == 'file' ) {
67 $hooks = array_merge( $hooks, getHooksFromFile($path.$file) );
76 * Get bad hooks (where the hook name could not be determined) from a PHP file
77 * @param $file Full filename to the PHP file.
78 * @return array of bad wfRunHooks() lines
80 function getBadHooksFromFile( $file ) {
81 $content = file_get_contents( $file );
83 # We want to skip the "function wfRunHooks()" one. :)
84 preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m);
89 * Get bad hooks from the source code.
90 * @param $path Directory where the include files can be found
91 * @return array of bad wfRunHooks() lines
93 function getBadHooksFromPath( $path ) {
95 if( $dh = opendir($path) ) {
96 while(($file = readdir($dh)) !== false) {
97 if( filetype($path.$file) == 'file' ) {
98 $hooks = array_merge( $hooks, getBadHooksFromFile($path.$file) );
107 * Nicely output the array
108 * @param $msg A message to show before the value
109 * @param $arr An array
110 * @param $sort Boolean : wheter to sort the array (Default: true)
112 function printArray( $msg, $arr, $sort = true ) {
113 if($sort) asort($arr);
114 foreach($arr as $v) print "$msg: $v\n";
120 $documented = getHooksFromDoc($doc);
122 $potenial_inc = getHooksFromPath($pathinc);
123 $potential_file = getHooksFromFile($pathfile);
124 $potential_lang = getHooksFromFile($pathlang);
125 $potential_skin = getHooksFromFile($pathskin);
127 $bad_inc = getBadHooksFromPath($pathinc);
128 $bad_file = getBadHooksFromFile($pathfile);
129 $bad_lang = getBadHooksFromFile($pathlang);
130 $bad_skin = getBadHooksFromFile($pathskin);
132 $potential = array_merge($potenial_inc, $potential_file, $potential_lang, $potential_skin);
133 $bad = array_merge($bad_inc, $bad_file, $bad_lang, $bad_skin);
135 $todo = array_diff($potential, $documented);
136 $deprecated = array_diff($documented, $potential);
138 // let's show the results:
139 printArray('undocumented', $todo );
140 printArray('not found', $deprecated );
141 printArray('unclear hook calls', $bad );