97e6c57d04f8e3384cbdf832ff65ee664385152c
[lhc/web/wiklou.git] / maintenance / findhooks.php
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.
9 *
10 * @package MediaWiki
11 * @subpackage Maintenance
12 *
13 * @author Ashar Voultoiz <hashar@altern.org>
14 * @copyright Copyright © Ashar voultoiz
15 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public Licence 2.0 or later
16 */
17
18 /** This is a command line script*/
19 include('commandLine.inc');
20
21
22 # GLOBALS
23
24 $doc = $IP . '/docs/hooks.txt';
25 $pathinc = $IP . '/includes/';
26
27
28 # FUNCTIONS
29
30 /**
31 * @return array of documented hooks
32 */
33 function getHooksFromDoc() {
34 global $doc;
35 $content = file_get_contents( $doc );
36 $m = array();
37 preg_match_all( "/\n'(.*?)'/", $content, $m);
38 return $m[1];
39 }
40
41 /**
42 * Get hooks from a php file
43 * @param $file Full filename to the PHP file.
44 * @return array of hooks found.
45 */
46 function getHooksFromFile( $file ) {
47 $content = file_get_contents( $file );
48 $m = array();
49 preg_match_all( "/wfRunHooks\(\s*\'(.*?)\'/", $content, $m);
50 return $m[1];
51 }
52
53 /**
54 * Get hooks from the source code.
55 * @param $path Directory where the include files can be found
56 * @return array of hooks found.
57 */
58 function getHooksFromPath( $path ) {
59 $hooks = array();
60 if( $dh = opendir($path) ) {
61 while(($file = readdir($dh)) !== false) {
62 if( filetype($path.$file) == 'file' ) {
63 $hooks = array_merge( $hooks, getHooksFromFile($path.$file) );
64 }
65 }
66 closedir($dh);
67 }
68 return $hooks;
69 }
70
71 /**
72 * Nicely output the array
73 * @param $msg A message to show before the value
74 * @param $arr An array
75 * @param $sort Boolean : wheter to sort the array (Default: true)
76 */
77 function printArray( $msg, $arr, $sort = true ) {
78 if($sort) asort($arr);
79 foreach($arr as $v) print "$msg: $v\n";
80 }
81
82
83 # MAIN
84
85 $documented = getHooksFromDoc($doc);
86 $potential = getHooksFromPath($pathinc);
87
88 $todo = array_diff($potential, $documented);
89 $deprecated = array_diff($documented, $potential);
90
91 // let's show the results:
92 printArray('undocumented', $todo );
93 printArray('not found', $deprecated );
94
95 ?>