convertExtensionToRegistration.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. require_once __DIR__ . '/Maintenance.php';
  3. class ConvertExtensionToRegistration extends Maintenance {
  4. protected $custom = [
  5. 'MessagesDirs' => 'handleMessagesDirs',
  6. 'ExtensionMessagesFiles' => 'handleExtensionMessagesFiles',
  7. 'AutoloadClasses' => 'removeAbsolutePath',
  8. 'ExtensionCredits' => 'handleCredits',
  9. 'ResourceModules' => 'handleResourceModules',
  10. 'ResourceModuleSkinStyles' => 'handleResourceModules',
  11. 'Hooks' => 'handleHooks',
  12. 'ExtensionFunctions' => 'handleExtensionFunctions',
  13. 'ParserTestFiles' => 'removeAbsolutePath',
  14. ];
  15. /**
  16. * Things that were formerly globals and should still be converted
  17. *
  18. * @var array
  19. */
  20. protected $formerGlobals = [
  21. 'TrackingCategories',
  22. ];
  23. /**
  24. * No longer supported globals (with reason) should not be converted and emit a warning
  25. *
  26. * @var array
  27. */
  28. protected $noLongerSupportedGlobals = [
  29. 'SpecialPageGroups' => 'deprecated', // Deprecated 1.21, removed in 1.26
  30. ];
  31. /**
  32. * Keys that should be put at the top of the generated JSON file (T86608)
  33. *
  34. * @var array
  35. */
  36. protected $promote = [
  37. 'name',
  38. 'namemsg',
  39. 'version',
  40. 'author',
  41. 'url',
  42. 'description',
  43. 'descriptionmsg',
  44. 'license-name',
  45. 'type',
  46. ];
  47. private $json, $dir, $hasWarning = false;
  48. public function __construct() {
  49. parent::__construct();
  50. $this->addDescription( 'Converts extension entry points to the new JSON registration format' );
  51. $this->addArg( 'path', 'Location to the PHP entry point you wish to convert',
  52. /* $required = */ true );
  53. $this->addOption( 'skin', 'Whether to write to skin.json', false, false );
  54. }
  55. protected function getAllGlobals() {
  56. $processor = new ReflectionClass( 'ExtensionProcessor' );
  57. $settings = $processor->getProperty( 'globalSettings' );
  58. $settings->setAccessible( true );
  59. return $settings->getValue() + $this->formerGlobals;
  60. }
  61. public function execute() {
  62. // Extensions will do stuff like $wgResourceModules += array(...) which is a
  63. // fatal unless an array is already set. So set an empty value.
  64. // And use the weird $__settings name to avoid any conflicts
  65. // with real poorly named settings.
  66. $__settings = array_merge( $this->getAllGlobals(), array_keys( $this->custom ) );
  67. foreach ( $__settings as $var ) {
  68. $var = 'wg' . $var;
  69. $$var = [];
  70. }
  71. unset( $var );
  72. $arg = $this->getArg( 0 );
  73. if ( !is_file( $arg ) ) {
  74. $this->error( "$arg is not a file.", true );
  75. }
  76. require $arg;
  77. unset( $arg );
  78. // Try not to create any local variables before this line
  79. $vars = get_defined_vars();
  80. unset( $vars['this'] );
  81. unset( $vars['__settings'] );
  82. $this->dir = dirname( realpath( $this->getArg( 0 ) ) );
  83. $this->json = [];
  84. $globalSettings = $this->getAllGlobals();
  85. foreach ( $vars as $name => $value ) {
  86. $realName = substr( $name, 2 ); // Strip 'wg'
  87. // If it's an empty array that we likely set, skip it
  88. if ( is_array( $value ) && count( $value ) === 0 && in_array( $realName, $__settings ) ) {
  89. continue;
  90. }
  91. if ( isset( $this->custom[$realName] ) ) {
  92. call_user_func_array( [ $this, $this->custom[$realName] ],
  93. [ $realName, $value, $vars ] );
  94. } elseif ( in_array( $realName, $globalSettings ) ) {
  95. $this->json[$realName] = $value;
  96. } elseif ( array_key_exists( $realName, $this->noLongerSupportedGlobals ) ) {
  97. $this->output( 'Warning: Skipped global "' . $name . '" (' .
  98. $this->noLongerSupportedGlobals[$realName] . '). ' .
  99. "Please update the entry point before convert to registration.\n" );
  100. $this->hasWarning = true;
  101. } elseif ( strpos( $name, 'wg' ) === 0 ) {
  102. // Most likely a config setting
  103. $this->json['config'][$realName] = $value;
  104. }
  105. }
  106. // check, if the extension requires composer libraries
  107. if ( $this->needsComposerAutoloader( dirname( $this->getArg( 0 ) ) ) ) {
  108. // set the load composer autoloader automatically property
  109. $this->output( "Detected composer dependencies, setting 'load_composer_autoloader' to true.\n" );
  110. $this->json['load_composer_autoloader'] = true;
  111. }
  112. // Move some keys to the top
  113. $out = [];
  114. foreach ( $this->promote as $key ) {
  115. if ( isset( $this->json[$key] ) ) {
  116. $out[$key] = $this->json[$key];
  117. unset( $this->json[$key] );
  118. }
  119. }
  120. $out += $this->json;
  121. // Put this at the bottom
  122. $out['manifest_version'] = ExtensionRegistry::MANIFEST_VERSION;
  123. $type = $this->hasOption( 'skin' ) ? 'skin' : 'extension';
  124. $fname = "{$this->dir}/$type.json";
  125. $prettyJSON = FormatJson::encode( $out, "\t", FormatJson::ALL_OK );
  126. file_put_contents( $fname, $prettyJSON . "\n" );
  127. $this->output( "Wrote output to $fname.\n" );
  128. if ( $this->hasWarning ) {
  129. $this->output( "Found warnings! Please resolve the warnings and rerun this script.\n" );
  130. }
  131. }
  132. protected function handleExtensionFunctions( $realName, $value ) {
  133. foreach ( $value as $func ) {
  134. if ( $func instanceof Closure ) {
  135. $this->error( "Error: Closures cannot be converted to JSON. " .
  136. "Please move your extension function somewhere else.", 1
  137. );
  138. }
  139. // check if $func exists in the global scope
  140. if ( function_exists( $func ) ) {
  141. $this->error( "Error: Global functions cannot be converted to JSON. " .
  142. "Please move your extension function ($func) into a class.", 1
  143. );
  144. }
  145. }
  146. $this->json[$realName] = $value;
  147. }
  148. protected function handleMessagesDirs( $realName, $value ) {
  149. foreach ( $value as $key => $dirs ) {
  150. foreach ( (array)$dirs as $dir ) {
  151. $this->json[$realName][$key][] = $this->stripPath( $dir, $this->dir );
  152. }
  153. }
  154. }
  155. protected function handleExtensionMessagesFiles( $realName, $value, $vars ) {
  156. foreach ( $value as $key => $file ) {
  157. $strippedFile = $this->stripPath( $file, $this->dir );
  158. if ( isset( $vars['wgMessagesDirs'][$key] ) ) {
  159. $this->output(
  160. "Note: Ignoring PHP shim $strippedFile. " .
  161. "If your extension no longer supports versions of MediaWiki " .
  162. "older than 1.23.0, you can safely delete it.\n"
  163. );
  164. } else {
  165. $this->json[$realName][$key] = $strippedFile;
  166. }
  167. }
  168. }
  169. private function stripPath( $val, $dir ) {
  170. if ( $val === $dir ) {
  171. $val = '';
  172. } elseif ( strpos( $val, $dir ) === 0 ) {
  173. // +1 is for the trailing / that won't be in $this->dir
  174. $val = substr( $val, strlen( $dir ) + 1 );
  175. }
  176. return $val;
  177. }
  178. protected function removeAbsolutePath( $realName, $value ) {
  179. $out = [];
  180. foreach ( $value as $key => $val ) {
  181. $out[$key] = $this->stripPath( $val, $this->dir );
  182. }
  183. $this->json[$realName] = $out;
  184. }
  185. protected function handleCredits( $realName, $value ) {
  186. $keys = array_keys( $value );
  187. $this->json['type'] = $keys[0];
  188. $values = array_values( $value );
  189. foreach ( $values[0][0] as $name => $val ) {
  190. if ( $name !== 'path' ) {
  191. $this->json[$name] = $val;
  192. }
  193. }
  194. }
  195. public function handleHooks( $realName, $value ) {
  196. foreach ( $value as $hookName => &$handlers ) {
  197. foreach ( $handlers as $func ) {
  198. if ( $func instanceof Closure ) {
  199. $this->error( "Error: Closures cannot be converted to JSON. " .
  200. "Please move the handler for $hookName somewhere else.", 1
  201. );
  202. }
  203. // Check if $func exists in the global scope
  204. if ( function_exists( $func ) ) {
  205. $this->error( "Error: Global functions cannot be converted to JSON. " .
  206. "Please move the handler for $hookName inside a class.", 1
  207. );
  208. }
  209. }
  210. if ( count( $handlers ) === 1 ) {
  211. $handlers = $handlers[0];
  212. }
  213. }
  214. $this->json[$realName] = $value;
  215. }
  216. protected function handleResourceModules( $realName, $value ) {
  217. $defaults = [];
  218. $remote = $this->hasOption( 'skin' ) ? 'remoteSkinPath' : 'remoteExtPath';
  219. foreach ( $value as $name => $data ) {
  220. if ( isset( $data['localBasePath'] ) ) {
  221. $data['localBasePath'] = $this->stripPath( $data['localBasePath'], $this->dir );
  222. if ( !$defaults ) {
  223. $defaults['localBasePath'] = $data['localBasePath'];
  224. unset( $data['localBasePath'] );
  225. if ( isset( $data[$remote] ) ) {
  226. $defaults[$remote] = $data[$remote];
  227. unset( $data[$remote] );
  228. }
  229. } else {
  230. if ( $data['localBasePath'] === $defaults['localBasePath'] ) {
  231. unset( $data['localBasePath'] );
  232. }
  233. if ( isset( $data[$remote] ) && isset( $defaults[$remote] )
  234. && $data[$remote] === $defaults[$remote]
  235. ) {
  236. unset( $data[$remote] );
  237. }
  238. }
  239. }
  240. $this->json[$realName][$name] = $data;
  241. }
  242. if ( $defaults ) {
  243. $this->json['ResourceFileModulePaths'] = $defaults;
  244. }
  245. }
  246. protected function needsComposerAutoloader( $path ) {
  247. $path .= '/composer.json';
  248. if ( file_exists( $path ) ) {
  249. // assume, that the composer.json file is in the root of the extension path
  250. $composerJson = new ComposerJson( $path );
  251. // check, if there are some dependencies in the require section
  252. if ( $composerJson->getRequiredDependencies() ) {
  253. return true;
  254. }
  255. }
  256. return false;
  257. }
  258. }
  259. $maintClass = 'ConvertExtensionToRegistration';
  260. require_once RUN_MAINTENANCE_IF_MAIN;