PageRenderTime 59ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/maintenance/findHooks.php

https://bitbucket.org/brunodefraine/mediawiki
PHP | 243 lines | 147 code | 18 blank | 78 comment | 21 complexity | 27aad83baddd1beddd895ec2f12f06f4 MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, LGPL-3.0
  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. * 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
  12. *
  13. * Any instance of wfRunHooks that doesn't meet these parameters will be noted.
  14. *
  15. * Copyright Š Antoine Musso
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License along
  28. * with this program; if not, write to the Free Software Foundation, Inc.,
  29. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  30. * http://www.gnu.org/copyleft/gpl.html
  31. *
  32. * @file
  33. * @ingroup Maintenance
  34. * @author Antoine Musso <hashar at free dot fr>
  35. */
  36. require_once( dirname( __FILE__ ) . '/Maintenance.php' );
  37. class FindHooks extends Maintenance {
  38. public function __construct() {
  39. parent::__construct();
  40. $this->mDescription = 'Find hooks that are undocumented, missing, or just plain wrong';
  41. $this->addOption( 'online', 'Check against MediaWiki.org hook documentation' );
  42. }
  43. public function getDbType() {
  44. return Maintenance::DB_NONE;
  45. }
  46. public function execute() {
  47. global $IP;
  48. $documented = $this->getHooksFromDoc( $IP . '/docs/hooks.txt' );
  49. $potential = array();
  50. $bad = array();
  51. $pathinc = array(
  52. $IP . '/',
  53. $IP . '/includes/',
  54. $IP . '/includes/actions/',
  55. $IP . '/includes/api/',
  56. $IP . '/includes/cache/',
  57. $IP . '/includes/context/',
  58. $IP . '/includes/db/',
  59. $IP . '/includes/diff/',
  60. $IP . '/includes/filerepo/',
  61. $IP . '/includes/installer/',
  62. $IP . '/includes/interwiki/',
  63. $IP . '/includes/media/',
  64. $IP . '/includes/parser/',
  65. $IP . '/includes/resourceloader/',
  66. $IP . '/includes/revisiondelete/',
  67. $IP . '/includes/search/',
  68. $IP . '/includes/specials/',
  69. $IP . '/includes/upload/',
  70. $IP . '/languages/',
  71. $IP . '/maintenance/',
  72. $IP . '/tests/',
  73. $IP . '/tests/parser/',
  74. $IP . '/tests/phpunit/suites/',
  75. $IP . '/skins/',
  76. );
  77. foreach ( $pathinc as $dir ) {
  78. $potential = array_merge( $potential, $this->getHooksFromPath( $dir ) );
  79. $bad = array_merge( $bad, $this->getBadHooksFromPath( $dir ) );
  80. }
  81. $potential = array_unique( $potential );
  82. $bad = array_unique( $bad );
  83. $todo = array_diff( $potential, $documented );
  84. $deprecated = array_diff( $documented, $potential );
  85. // let's show the results:
  86. $this->printArray( 'Undocumented', $todo );
  87. $this->printArray( 'Documented and not found', $deprecated );
  88. $this->printArray( 'Unclear hook calls', $bad );
  89. if ( count( $todo ) == 0 && count( $deprecated ) == 0 && count( $bad ) == 0 )
  90. {
  91. $this->output( "Looks good!\n" );
  92. }
  93. }
  94. /**
  95. * Get the hook documentation, either locally or from MediaWiki.org
  96. * @return array of documented hooks
  97. */
  98. private function getHooksFromDoc( $doc ) {
  99. if ( $this->hasOption( 'online' ) ) {
  100. return $this->getHooksFromOnlineDoc( );
  101. } else {
  102. return $this->getHooksFromLocalDoc( $doc );
  103. }
  104. }
  105. /**
  106. * Get hooks from a local file (for example docs/hooks.txt)
  107. * @param $doc string: filename to look in
  108. * @return array of documented hooks
  109. */
  110. private function getHooksFromLocalDoc( $doc ) {
  111. $m = array();
  112. $content = file_get_contents( $doc );
  113. preg_match_all( "/\n'(.*?)'/", $content, $m );
  114. return array_unique( $m[1] );
  115. }
  116. /**
  117. * Get hooks from www.mediawiki.org using the API
  118. * @return array of documented hooks
  119. */
  120. private function getHooksFromOnlineDoc( ) {
  121. // All hooks
  122. $allhookdata = Http::get( 'http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:MediaWiki_hooks&cmlimit=500&format=php' );
  123. $allhookdata = unserialize( $allhookdata );
  124. $allhooks = array();
  125. foreach ( $allhookdata['query']['categorymembers'] as $page ) {
  126. $found = preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $matches );
  127. if ( $found ) {
  128. $hook = str_replace( ' ', '_', $matches[1] );
  129. $allhooks[] = $hook;
  130. }
  131. }
  132. // Removed hooks
  133. $oldhookdata = Http::get( 'http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Removed_hooks&cmlimit=500&format=php' );
  134. $oldhookdata = unserialize( $oldhookdata );
  135. $removed = array();
  136. foreach ( $oldhookdata['query']['categorymembers'] as $page ) {
  137. $found = preg_match( '/Manual\:Hooks\/([a-zA-Z0-9- :]+)/', $page['title'], $matches );
  138. if ( $found ) {
  139. $hook = str_replace( ' ', '_', $matches[1] );
  140. $removed[] = $hook;
  141. }
  142. }
  143. return array_diff( $allhooks, $removed );
  144. }
  145. /**
  146. * Get hooks from a PHP file
  147. * @param $file Full filename to the PHP file.
  148. * @return array of hooks found.
  149. */
  150. private function getHooksFromFile( $file ) {
  151. $content = file_get_contents( $file );
  152. $m = array();
  153. preg_match_all( '/(?:wfRunHooks|Hooks\:\:run)\(\s*([\'"])(.*?)\1/', $content, $m );
  154. return $m[2];
  155. }
  156. /**
  157. * Get hooks from the source code.
  158. * @param $path Directory where the include files can be found
  159. * @return array of hooks found.
  160. */
  161. private function getHooksFromPath( $path ) {
  162. $hooks = array();
  163. $dh = opendir( $path );
  164. if ( $dh ) {
  165. while ( ( $file = readdir( $dh ) ) !== false ) {
  166. if ( filetype( $path . $file ) == 'file' ) {
  167. $hooks = array_merge( $hooks, $this->getHooksFromFile( $path . $file ) );
  168. }
  169. }
  170. closedir( $dh );
  171. }
  172. return $hooks;
  173. }
  174. /**
  175. * Get bad hooks (where the hook name could not be determined) from a PHP file
  176. * @param $file Full filename to the PHP file.
  177. * @return array of bad wfRunHooks() lines
  178. */
  179. private function getBadHooksFromFile( $file ) {
  180. $content = file_get_contents( $file );
  181. $m = array();
  182. # We want to skip the "function wfRunHooks()" one. :)
  183. preg_match_all( '/(?<!function )wfRunHooks\(\s*[^\s\'"].*/', $content, $m );
  184. $list = array();
  185. foreach ( $m[0] as $match ) {
  186. $list[] = $match . "(" . $file . ")";
  187. }
  188. return $list;
  189. }
  190. /**
  191. * Get bad hooks from the source code.
  192. * @param $path Directory where the include files can be found
  193. * @return array of bad wfRunHooks() lines
  194. */
  195. private function getBadHooksFromPath( $path ) {
  196. $hooks = array();
  197. $dh = opendir( $path );
  198. if ( $dh ) {
  199. while ( ( $file = readdir( $dh ) ) !== false ) {
  200. # We don't want to read this file as it contains bad calls to wfRunHooks()
  201. if ( filetype( $path . $file ) == 'file' && !$path . $file == __FILE__ ) {
  202. $hooks = array_merge( $hooks, $this->getBadHooksFromFile( $path . $file ) );
  203. }
  204. }
  205. closedir( $dh );
  206. }
  207. return $hooks;
  208. }
  209. /**
  210. * Nicely output the array
  211. * @param $msg String: a message to show before the value
  212. * @param $arr Array: an array
  213. * @param $sort Boolean: whether to sort the array (Default: true)
  214. */
  215. private function printArray( $msg, $arr, $sort = true ) {
  216. if ( $sort ) {
  217. asort( $arr );
  218. }
  219. foreach ( $arr as $v ) {
  220. $this->output( "$msg: $v\n" );
  221. }
  222. }
  223. }
  224. $maintClass = 'FindHooks';
  225. require_once( RUN_MAINTENANCE_IF_MAIN );