ResourceLoader.php 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  1. <?php
  2. /**
  3. * Base class for resource loading system.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @author Roan Kattouw
  22. * @author Trevor Parscal
  23. */
  24. use MediaWiki\MediaWikiServices;
  25. use Psr\Log\LoggerAwareInterface;
  26. use Psr\Log\LoggerInterface;
  27. use Psr\Log\NullLogger;
  28. use Wikimedia\Rdbms\DBConnectionError;
  29. use Wikimedia\WrappedString;
  30. /**
  31. * Dynamic JavaScript and CSS resource loading system.
  32. *
  33. * Most of the documentation is on the MediaWiki documentation wiki starting at:
  34. * https://www.mediawiki.org/wiki/ResourceLoader
  35. */
  36. class ResourceLoader implements LoggerAwareInterface {
  37. /** @var int */
  38. protected static $filterCacheVersion = 8;
  39. /** @var bool */
  40. protected static $debugMode = null;
  41. /**
  42. * Module name/ResourceLoaderModule object pairs
  43. * @var array
  44. */
  45. protected $modules = [];
  46. /**
  47. * Associative array mapping module name to info associative array
  48. * @var array
  49. */
  50. protected $moduleInfos = [];
  51. /** @var Config $config */
  52. protected $config;
  53. /**
  54. * Associative array mapping framework ids to a list of names of test suite modules
  55. * like [ 'qunit' => [ 'mediawiki.tests.qunit.suites', 'ext.foo.tests', ... ], ... ]
  56. * @var array
  57. */
  58. protected $testModuleNames = [];
  59. /**
  60. * E.g. [ 'source-id' => 'http://.../load.php' ]
  61. * @var array
  62. */
  63. protected $sources = [];
  64. /**
  65. * Errors accumulated during current respond() call.
  66. * @var array
  67. */
  68. protected $errors = [];
  69. /**
  70. * List of extra HTTP response headers provided by loaded modules.
  71. *
  72. * Populated by makeModuleResponse().
  73. *
  74. * @var array
  75. */
  76. protected $extraHeaders = [];
  77. /**
  78. * @var MessageBlobStore
  79. */
  80. protected $blobStore;
  81. /**
  82. * @var LoggerInterface
  83. */
  84. private $logger;
  85. /** @var string JavaScript / CSS pragma to disable minification. **/
  86. const FILTER_NOMIN = '/*@nomin*/';
  87. /**
  88. * Load information stored in the database about modules.
  89. *
  90. * This method grabs modules dependencies from the database and updates modules
  91. * objects.
  92. *
  93. * This is not inside the module code because it is much faster to
  94. * request all of the information at once than it is to have each module
  95. * requests its own information. This sacrifice of modularity yields a substantial
  96. * performance improvement.
  97. *
  98. * @param array $moduleNames List of module names to preload information for
  99. * @param ResourceLoaderContext $context Context to load the information within
  100. */
  101. public function preloadModuleInfo( array $moduleNames, ResourceLoaderContext $context ) {
  102. if ( !$moduleNames ) {
  103. // Or else Database*::select() will explode, plus it's cheaper!
  104. return;
  105. }
  106. $dbr = wfGetDB( DB_REPLICA );
  107. $skin = $context->getSkin();
  108. $lang = $context->getLanguage();
  109. // Batched version of ResourceLoaderModule::getFileDependencies
  110. $vary = "$skin|$lang";
  111. $res = $dbr->select( 'module_deps', [ 'md_module', 'md_deps' ], [
  112. 'md_module' => $moduleNames,
  113. 'md_skin' => $vary,
  114. ], __METHOD__
  115. );
  116. // Prime in-object cache for file dependencies
  117. $modulesWithDeps = [];
  118. foreach ( $res as $row ) {
  119. $module = $this->getModule( $row->md_module );
  120. if ( $module ) {
  121. $module->setFileDependencies( $context, ResourceLoaderModule::expandRelativePaths(
  122. json_decode( $row->md_deps, true )
  123. ) );
  124. $modulesWithDeps[] = $row->md_module;
  125. }
  126. }
  127. // Register the absence of a dependency row too
  128. foreach ( array_diff( $moduleNames, $modulesWithDeps ) as $name ) {
  129. $module = $this->getModule( $name );
  130. if ( $module ) {
  131. $this->getModule( $name )->setFileDependencies( $context, [] );
  132. }
  133. }
  134. // Batched version of ResourceLoaderWikiModule::getTitleInfo
  135. ResourceLoaderWikiModule::preloadTitleInfo( $context, $dbr, $moduleNames );
  136. // Prime in-object cache for message blobs for modules with messages
  137. $modules = [];
  138. foreach ( $moduleNames as $name ) {
  139. $module = $this->getModule( $name );
  140. if ( $module && $module->getMessages() ) {
  141. $modules[$name] = $module;
  142. }
  143. }
  144. $store = $this->getMessageBlobStore();
  145. $blobs = $store->getBlobs( $modules, $lang );
  146. foreach ( $blobs as $name => $blob ) {
  147. $modules[$name]->setMessageBlob( $blob, $lang );
  148. }
  149. }
  150. /**
  151. * Run JavaScript or CSS data through a filter, caching the filtered result for future calls.
  152. *
  153. * Available filters are:
  154. *
  155. * - minify-js \see JavaScriptMinifier::minify
  156. * - minify-css \see CSSMin::minify
  157. *
  158. * If $data is empty, only contains whitespace or the filter was unknown,
  159. * $data is returned unmodified.
  160. *
  161. * @param string $filter Name of filter to run
  162. * @param string $data Text to filter, such as JavaScript or CSS text
  163. * @param array $options Keys:
  164. * - (bool) cache: Whether to allow caching this data. Default: true.
  165. * @return string Filtered data, or a comment containing an error message
  166. */
  167. public static function filter( $filter, $data, array $options = [] ) {
  168. if ( strpos( $data, self::FILTER_NOMIN ) !== false ) {
  169. return $data;
  170. }
  171. if ( isset( $options['cache'] ) && $options['cache'] === false ) {
  172. return self::applyFilter( $filter, $data );
  173. }
  174. $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
  175. $cache = ObjectCache::getLocalServerInstance( CACHE_ANYTHING );
  176. $key = $cache->makeGlobalKey(
  177. 'resourceloader',
  178. 'filter',
  179. $filter,
  180. self::$filterCacheVersion, md5( $data )
  181. );
  182. $result = $cache->get( $key );
  183. if ( $result === false ) {
  184. $stats->increment( "resourceloader_cache.$filter.miss" );
  185. $result = self::applyFilter( $filter, $data );
  186. $cache->set( $key, $result, 24 * 3600 );
  187. } else {
  188. $stats->increment( "resourceloader_cache.$filter.hit" );
  189. }
  190. if ( $result === null ) {
  191. // Cached failure
  192. $result = $data;
  193. }
  194. return $result;
  195. }
  196. private static function applyFilter( $filter, $data ) {
  197. $data = trim( $data );
  198. if ( $data ) {
  199. try {
  200. $data = ( $filter === 'minify-css' )
  201. ? CSSMin::minify( $data )
  202. : JavaScriptMinifier::minify( $data );
  203. } catch ( Exception $e ) {
  204. MWExceptionHandler::logException( $e );
  205. return null;
  206. }
  207. }
  208. return $data;
  209. }
  210. /**
  211. * Register core modules and runs registration hooks.
  212. * @param Config|null $config [optional]
  213. * @param LoggerInterface|null $logger [optional]
  214. */
  215. public function __construct( Config $config = null, LoggerInterface $logger = null ) {
  216. global $IP;
  217. $this->logger = $logger ?: new NullLogger();
  218. if ( !$config ) {
  219. $this->logger->debug( __METHOD__ . ' was called without providing a Config instance' );
  220. $config = MediaWikiServices::getInstance()->getMainConfig();
  221. }
  222. $this->config = $config;
  223. // Add 'local' source first
  224. $this->addSource( 'local', $config->get( 'LoadScript' ) );
  225. // Add other sources
  226. $this->addSource( $config->get( 'ResourceLoaderSources' ) );
  227. // Register core modules
  228. $this->register( include "$IP/resources/Resources.php" );
  229. // Register extension modules
  230. $this->register( $config->get( 'ResourceModules' ) );
  231. // Avoid PHP 7.1 warning from passing $this by reference
  232. $rl = $this;
  233. Hooks::run( 'ResourceLoaderRegisterModules', [ &$rl ] );
  234. if ( $config->get( 'EnableJavaScriptTest' ) === true ) {
  235. $this->registerTestModules();
  236. }
  237. $this->setMessageBlobStore( new MessageBlobStore( $this, $this->logger ) );
  238. }
  239. /**
  240. * @return Config
  241. */
  242. public function getConfig() {
  243. return $this->config;
  244. }
  245. /**
  246. * @since 1.26
  247. * @param LoggerInterface $logger
  248. */
  249. public function setLogger( LoggerInterface $logger ) {
  250. $this->logger = $logger;
  251. }
  252. /**
  253. * @since 1.27
  254. * @return LoggerInterface
  255. */
  256. public function getLogger() {
  257. return $this->logger;
  258. }
  259. /**
  260. * @since 1.26
  261. * @return MessageBlobStore
  262. */
  263. public function getMessageBlobStore() {
  264. return $this->blobStore;
  265. }
  266. /**
  267. * @since 1.25
  268. * @param MessageBlobStore $blobStore
  269. */
  270. public function setMessageBlobStore( MessageBlobStore $blobStore ) {
  271. $this->blobStore = $blobStore;
  272. }
  273. /**
  274. * Register a module with the ResourceLoader system.
  275. *
  276. * @param mixed $name Name of module as a string or List of name/object pairs as an array
  277. * @param array|null $info Module info array. For backwards compatibility with 1.17alpha,
  278. * this may also be a ResourceLoaderModule object. Optional when using
  279. * multiple-registration calling style.
  280. * @throws MWException If a duplicate module registration is attempted
  281. * @throws MWException If a module name contains illegal characters (pipes or commas)
  282. * @throws MWException If something other than a ResourceLoaderModule is being registered
  283. * @return bool False if there were any errors, in which case one or more modules were
  284. * not registered
  285. */
  286. public function register( $name, $info = null ) {
  287. $moduleSkinStyles = $this->config->get( 'ResourceModuleSkinStyles' );
  288. // Allow multiple modules to be registered in one call
  289. $registrations = is_array( $name ) ? $name : [ $name => $info ];
  290. foreach ( $registrations as $name => $info ) {
  291. // Warn on duplicate registrations
  292. if ( isset( $this->moduleInfos[$name] ) ) {
  293. // A module has already been registered by this name
  294. $this->logger->warning(
  295. 'ResourceLoader duplicate registration warning. ' .
  296. 'Another module has already been registered as ' . $name
  297. );
  298. }
  299. // Check $name for validity
  300. if ( !self::isValidModuleName( $name ) ) {
  301. throw new MWException( "ResourceLoader module name '$name' is invalid, "
  302. . "see ResourceLoader::isValidModuleName()" );
  303. }
  304. // Attach module
  305. if ( $info instanceof ResourceLoaderModule ) {
  306. $this->moduleInfos[$name] = [ 'object' => $info ];
  307. $info->setName( $name );
  308. $this->modules[$name] = $info;
  309. } elseif ( is_array( $info ) ) {
  310. // New calling convention
  311. $this->moduleInfos[$name] = $info;
  312. } else {
  313. throw new MWException(
  314. 'ResourceLoader module info type error for module \'' . $name .
  315. '\': expected ResourceLoaderModule or array (got: ' . gettype( $info ) . ')'
  316. );
  317. }
  318. // Last-minute changes
  319. // Apply custom skin-defined styles to existing modules.
  320. if ( $this->isFileModule( $name ) ) {
  321. foreach ( $moduleSkinStyles as $skinName => $skinStyles ) {
  322. // If this module already defines skinStyles for this skin, ignore $wgResourceModuleSkinStyles.
  323. if ( isset( $this->moduleInfos[$name]['skinStyles'][$skinName] ) ) {
  324. continue;
  325. }
  326. // If $name is preceded with a '+', the defined style files will be added to 'default'
  327. // skinStyles, otherwise 'default' will be ignored as it normally would be.
  328. if ( isset( $skinStyles[$name] ) ) {
  329. $paths = (array)$skinStyles[$name];
  330. $styleFiles = [];
  331. } elseif ( isset( $skinStyles['+' . $name] ) ) {
  332. $paths = (array)$skinStyles['+' . $name];
  333. $styleFiles = isset( $this->moduleInfos[$name]['skinStyles']['default'] ) ?
  334. (array)$this->moduleInfos[$name]['skinStyles']['default'] :
  335. [];
  336. } else {
  337. continue;
  338. }
  339. // Add new file paths, remapping them to refer to our directories and not use settings
  340. // from the module we're modifying, which come from the base definition.
  341. list( $localBasePath, $remoteBasePath ) =
  342. ResourceLoaderFileModule::extractBasePaths( $skinStyles );
  343. foreach ( $paths as $path ) {
  344. $styleFiles[] = new ResourceLoaderFilePath( $path, $localBasePath, $remoteBasePath );
  345. }
  346. $this->moduleInfos[$name]['skinStyles'][$skinName] = $styleFiles;
  347. }
  348. }
  349. }
  350. }
  351. public function registerTestModules() {
  352. global $IP;
  353. if ( $this->config->get( 'EnableJavaScriptTest' ) !== true ) {
  354. throw new MWException( 'Attempt to register JavaScript test modules '
  355. . 'but <code>$wgEnableJavaScriptTest</code> is false. '
  356. . 'Edit your <code>LocalSettings.php</code> to enable it.' );
  357. }
  358. // Get core test suites
  359. $testModules = [];
  360. $testModules['qunit'] = [];
  361. // Get other test suites (e.g. from extensions)
  362. // Avoid PHP 7.1 warning from passing $this by reference
  363. $rl = $this;
  364. Hooks::run( 'ResourceLoaderTestModules', [ &$testModules, &$rl ] );
  365. // Add the testrunner (which configures QUnit) to the dependencies.
  366. // Since it must be ready before any of the test suites are executed.
  367. foreach ( $testModules['qunit'] as &$module ) {
  368. // Make sure all test modules are top-loading so that when QUnit starts
  369. // on document-ready, it will run once and finish. If some tests arrive
  370. // later (possibly after QUnit has already finished) they will be ignored.
  371. $module['position'] = 'top';
  372. $module['dependencies'][] = 'test.mediawiki.qunit.testrunner';
  373. }
  374. $testModules['qunit'] =
  375. ( include "$IP/tests/qunit/QUnitTestResources.php" ) + $testModules['qunit'];
  376. foreach ( $testModules as $id => $names ) {
  377. // Register test modules
  378. $this->register( $testModules[$id] );
  379. // Keep track of their names so that they can be loaded together
  380. $this->testModuleNames[$id] = array_keys( $testModules[$id] );
  381. }
  382. }
  383. /**
  384. * Add a foreign source of modules.
  385. *
  386. * Source IDs are typically the same as the Wiki ID or database name (e.g. lowercase a-z).
  387. *
  388. * @param array|string $id Source ID (string), or [ id1 => loadUrl, id2 => loadUrl, ... ]
  389. * @param string|array|null $loadUrl load.php url (string), or array with loadUrl key for
  390. * backwards-compatibility.
  391. * @throws MWException
  392. */
  393. public function addSource( $id, $loadUrl = null ) {
  394. // Allow multiple sources to be registered in one call
  395. if ( is_array( $id ) ) {
  396. foreach ( $id as $key => $value ) {
  397. $this->addSource( $key, $value );
  398. }
  399. return;
  400. }
  401. // Disallow duplicates
  402. if ( isset( $this->sources[$id] ) ) {
  403. throw new MWException(
  404. 'ResourceLoader duplicate source addition error. ' .
  405. 'Another source has already been registered as ' . $id
  406. );
  407. }
  408. // Pre 1.24 backwards-compatibility
  409. if ( is_array( $loadUrl ) ) {
  410. if ( !isset( $loadUrl['loadScript'] ) ) {
  411. throw new MWException(
  412. __METHOD__ . ' was passed an array with no "loadScript" key.'
  413. );
  414. }
  415. $loadUrl = $loadUrl['loadScript'];
  416. }
  417. $this->sources[$id] = $loadUrl;
  418. }
  419. /**
  420. * Get a list of module names.
  421. *
  422. * @return array List of module names
  423. */
  424. public function getModuleNames() {
  425. return array_keys( $this->moduleInfos );
  426. }
  427. /**
  428. * Get a list of test module names for one (or all) frameworks.
  429. *
  430. * If the given framework id is unknkown, or if the in-object variable is not an array,
  431. * then it will return an empty array.
  432. *
  433. * @param string $framework Get only the test module names for one
  434. * particular framework (optional)
  435. * @return array
  436. */
  437. public function getTestModuleNames( $framework = 'all' ) {
  438. /** @todo api siteinfo prop testmodulenames modulenames */
  439. if ( $framework == 'all' ) {
  440. return $this->testModuleNames;
  441. } elseif ( isset( $this->testModuleNames[$framework] )
  442. && is_array( $this->testModuleNames[$framework] )
  443. ) {
  444. return $this->testModuleNames[$framework];
  445. } else {
  446. return [];
  447. }
  448. }
  449. /**
  450. * Check whether a ResourceLoader module is registered
  451. *
  452. * @since 1.25
  453. * @param string $name
  454. * @return bool
  455. */
  456. public function isModuleRegistered( $name ) {
  457. return isset( $this->moduleInfos[$name] );
  458. }
  459. /**
  460. * Get the ResourceLoaderModule object for a given module name.
  461. *
  462. * If an array of module parameters exists but a ResourceLoaderModule object has not
  463. * yet been instantiated, this method will instantiate and cache that object such that
  464. * subsequent calls simply return the same object.
  465. *
  466. * @param string $name Module name
  467. * @return ResourceLoaderModule|null If module has been registered, return a
  468. * ResourceLoaderModule instance. Otherwise, return null.
  469. */
  470. public function getModule( $name ) {
  471. if ( !isset( $this->modules[$name] ) ) {
  472. if ( !isset( $this->moduleInfos[$name] ) ) {
  473. // No such module
  474. return null;
  475. }
  476. // Construct the requested object
  477. $info = $this->moduleInfos[$name];
  478. /** @var ResourceLoaderModule $object */
  479. if ( isset( $info['object'] ) ) {
  480. // Object given in info array
  481. $object = $info['object'];
  482. } elseif ( isset( $info['factory'] ) ) {
  483. $object = call_user_func( $info['factory'], $info );
  484. $object->setConfig( $this->getConfig() );
  485. $object->setLogger( $this->logger );
  486. } else {
  487. if ( !isset( $info['class'] ) ) {
  488. $class = ResourceLoaderFileModule::class;
  489. } else {
  490. $class = $info['class'];
  491. }
  492. /** @var ResourceLoaderModule $object */
  493. $object = new $class( $info );
  494. $object->setConfig( $this->getConfig() );
  495. $object->setLogger( $this->logger );
  496. }
  497. $object->setName( $name );
  498. $this->modules[$name] = $object;
  499. }
  500. return $this->modules[$name];
  501. }
  502. /**
  503. * Whether the module is a ResourceLoaderFileModule (including subclasses).
  504. *
  505. * @param string $name Module name
  506. * @return bool
  507. */
  508. protected function isFileModule( $name ) {
  509. if ( !isset( $this->moduleInfos[$name] ) ) {
  510. return false;
  511. }
  512. $info = $this->moduleInfos[$name];
  513. if ( isset( $info['object'] ) ) {
  514. return false;
  515. }
  516. return (
  517. // The implied default for 'class' is ResourceLoaderFileModule
  518. !isset( $info['class'] ) ||
  519. // Explicit default
  520. $info['class'] === ResourceLoaderFileModule::class ||
  521. is_subclass_of( $info['class'], ResourceLoaderFileModule::class )
  522. );
  523. }
  524. /**
  525. * Get the list of sources.
  526. *
  527. * @return array Like [ id => load.php url, ... ]
  528. */
  529. public function getSources() {
  530. return $this->sources;
  531. }
  532. /**
  533. * Get the URL to the load.php endpoint for the given
  534. * ResourceLoader source
  535. *
  536. * @since 1.24
  537. * @param string $source
  538. * @throws MWException On an invalid $source name
  539. * @return string
  540. */
  541. public function getLoadScript( $source ) {
  542. if ( !isset( $this->sources[$source] ) ) {
  543. throw new MWException( "The $source source was never registered in ResourceLoader." );
  544. }
  545. return $this->sources[$source];
  546. }
  547. /**
  548. * @since 1.26
  549. * @param string $value
  550. * @return string Hash
  551. */
  552. public static function makeHash( $value ) {
  553. $hash = hash( 'fnv132', $value );
  554. return Wikimedia\base_convert( $hash, 16, 36, 7 );
  555. }
  556. /**
  557. * Add an error to the 'errors' array and log it.
  558. *
  559. * @private For internal use by ResourceLoader and ResourceLoaderStartUpModule.
  560. * @since 1.29
  561. * @param Exception $e
  562. * @param string $msg
  563. * @param array $context
  564. */
  565. public function outputErrorAndLog( Exception $e, $msg, array $context = [] ) {
  566. MWExceptionHandler::logException( $e );
  567. $this->logger->warning(
  568. $msg,
  569. $context + [ 'exception' => $e ]
  570. );
  571. $this->errors[] = self::formatExceptionNoComment( $e );
  572. }
  573. /**
  574. * Helper method to get and combine versions of multiple modules.
  575. *
  576. * @since 1.26
  577. * @param ResourceLoaderContext $context
  578. * @param string[] $moduleNames List of known module names
  579. * @return string Hash
  580. */
  581. public function getCombinedVersion( ResourceLoaderContext $context, array $moduleNames ) {
  582. if ( !$moduleNames ) {
  583. return '';
  584. }
  585. $hashes = array_map( function ( $module ) use ( $context ) {
  586. try {
  587. return $this->getModule( $module )->getVersionHash( $context );
  588. } catch ( Exception $e ) {
  589. // If modules fail to compute a version, don't fail the request (T152266)
  590. // and still compute versions of other modules.
  591. $this->outputErrorAndLog( $e,
  592. 'Calculating version for "{module}" failed: {exception}',
  593. [
  594. 'module' => $module,
  595. ]
  596. );
  597. return '';
  598. }
  599. }, $moduleNames );
  600. return self::makeHash( implode( '', $hashes ) );
  601. }
  602. /**
  603. * Get the expected value of the 'version' query parameter.
  604. *
  605. * This is used by respond() to set a short Cache-Control header for requests with
  606. * information newer than the current server has. This avoids pollution of edge caches.
  607. * Typically during deployment. (T117587)
  608. *
  609. * This MUST match return value of `mw.loader#getCombinedVersion()` client-side.
  610. *
  611. * @since 1.28
  612. * @param ResourceLoaderContext $context
  613. * @return string Hash
  614. */
  615. public function makeVersionQuery( ResourceLoaderContext $context ) {
  616. // As of MediaWiki 1.28, the server and client use the same algorithm for combining
  617. // version hashes. There is no technical reason for this to be same, and for years the
  618. // implementations differed. If getCombinedVersion in PHP (used for StartupModule and
  619. // E-Tag headers) differs in the future from getCombinedVersion in JS (used for 'version'
  620. // query parameter), then this method must continue to match the JS one.
  621. $moduleNames = [];
  622. foreach ( $context->getModules() as $name ) {
  623. if ( !$this->getModule( $name ) ) {
  624. // If a versioned request contains a missing module, the version is a mismatch
  625. // as the client considered a module (and version) we don't have.
  626. return '';
  627. }
  628. $moduleNames[] = $name;
  629. }
  630. return $this->getCombinedVersion( $context, $moduleNames );
  631. }
  632. /**
  633. * Output a response to a load request, including the content-type header.
  634. *
  635. * @param ResourceLoaderContext $context Context in which a response should be formed
  636. */
  637. public function respond( ResourceLoaderContext $context ) {
  638. // Buffer output to catch warnings. Normally we'd use ob_clean() on the
  639. // top-level output buffer to clear warnings, but that breaks when ob_gzhandler
  640. // is used: ob_clean() will clear the GZIP header in that case and it won't come
  641. // back for subsequent output, resulting in invalid GZIP. So we have to wrap
  642. // the whole thing in our own output buffer to be sure the active buffer
  643. // doesn't use ob_gzhandler.
  644. // See https://bugs.php.net/bug.php?id=36514
  645. ob_start();
  646. $this->measureResponseTime( RequestContext::getMain()->getTiming() );
  647. // Find out which modules are missing and instantiate the others
  648. $modules = [];
  649. $missing = [];
  650. foreach ( $context->getModules() as $name ) {
  651. $module = $this->getModule( $name );
  652. if ( $module ) {
  653. // Do not allow private modules to be loaded from the web.
  654. // This is a security issue, see T36907.
  655. if ( $module->getGroup() === 'private' ) {
  656. $this->logger->debug( "Request for private module '$name' denied" );
  657. $this->errors[] = "Cannot show private module \"$name\"";
  658. continue;
  659. }
  660. $modules[$name] = $module;
  661. } else {
  662. $missing[] = $name;
  663. }
  664. }
  665. try {
  666. // Preload for getCombinedVersion() and for batch makeModuleResponse()
  667. $this->preloadModuleInfo( array_keys( $modules ), $context );
  668. } catch ( Exception $e ) {
  669. $this->outputErrorAndLog( $e, 'Preloading module info failed: {exception}' );
  670. }
  671. // Combine versions to propagate cache invalidation
  672. $versionHash = '';
  673. try {
  674. $versionHash = $this->getCombinedVersion( $context, array_keys( $modules ) );
  675. } catch ( Exception $e ) {
  676. $this->outputErrorAndLog( $e, 'Calculating version hash failed: {exception}' );
  677. }
  678. // See RFC 2616 § 3.11 Entity Tags
  679. // https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.11
  680. $etag = 'W/"' . $versionHash . '"';
  681. // Try the client-side cache first
  682. if ( $this->tryRespondNotModified( $context, $etag ) ) {
  683. return; // output handled (buffers cleared)
  684. }
  685. // Use file cache if enabled and available...
  686. if ( $this->config->get( 'UseFileCache' ) ) {
  687. $fileCache = ResourceFileCache::newFromContext( $context );
  688. if ( $this->tryRespondFromFileCache( $fileCache, $context, $etag ) ) {
  689. return; // output handled
  690. }
  691. }
  692. // Generate a response
  693. $response = $this->makeModuleResponse( $context, $modules, $missing );
  694. // Capture any PHP warnings from the output buffer and append them to the
  695. // error list if we're in debug mode.
  696. if ( $context->getDebug() ) {
  697. $warnings = ob_get_contents();
  698. if ( strlen( $warnings ) ) {
  699. $this->errors[] = $warnings;
  700. }
  701. }
  702. // Save response to file cache unless there are errors
  703. if ( isset( $fileCache ) && !$this->errors && !count( $missing ) ) {
  704. // Cache single modules and images...and other requests if there are enough hits
  705. if ( ResourceFileCache::useFileCache( $context ) ) {
  706. if ( $fileCache->isCacheWorthy() ) {
  707. $fileCache->saveText( $response );
  708. } else {
  709. $fileCache->incrMissesRecent( $context->getRequest() );
  710. }
  711. }
  712. }
  713. $this->sendResponseHeaders( $context, $etag, (bool)$this->errors, $this->extraHeaders );
  714. // Remove the output buffer and output the response
  715. ob_end_clean();
  716. if ( $context->getImageObj() && $this->errors ) {
  717. // We can't show both the error messages and the response when it's an image.
  718. $response = implode( "\n\n", $this->errors );
  719. } elseif ( $this->errors ) {
  720. $errorText = implode( "\n\n", $this->errors );
  721. $errorResponse = self::makeComment( $errorText );
  722. if ( $context->shouldIncludeScripts() ) {
  723. $errorResponse .= 'if (window.console && console.error) {'
  724. . Xml::encodeJsCall( 'console.error', [ $errorText ] )
  725. . "}\n";
  726. }
  727. // Prepend error info to the response
  728. $response = $errorResponse . $response;
  729. }
  730. $this->errors = [];
  731. echo $response;
  732. }
  733. protected function measureResponseTime( Timing $timing ) {
  734. DeferredUpdates::addCallableUpdate( function () use ( $timing ) {
  735. $measure = $timing->measure( 'responseTime', 'requestStart', 'requestShutdown' );
  736. if ( $measure !== false ) {
  737. $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
  738. $stats->timing( 'resourceloader.responseTime', $measure['duration'] * 1000 );
  739. }
  740. } );
  741. }
  742. /**
  743. * Send main response headers to the client.
  744. *
  745. * Deals with Content-Type, CORS (for stylesheets), and caching.
  746. *
  747. * @param ResourceLoaderContext $context
  748. * @param string $etag ETag header value
  749. * @param bool $errors Whether there are errors in the response
  750. * @param string[] $extra Array of extra HTTP response headers
  751. * @return void
  752. */
  753. protected function sendResponseHeaders(
  754. ResourceLoaderContext $context, $etag, $errors, array $extra = []
  755. ) {
  756. \MediaWiki\HeaderCallback::warnIfHeadersSent();
  757. $rlMaxage = $this->config->get( 'ResourceLoaderMaxage' );
  758. // Use a short cache expiry so that updates propagate to clients quickly, if:
  759. // - No version specified (shared resources, e.g. stylesheets)
  760. // - There were errors (recover quickly)
  761. // - Version mismatch (T117587, T47877)
  762. if ( is_null( $context->getVersion() )
  763. || $errors
  764. || $context->getVersion() !== $this->makeVersionQuery( $context )
  765. ) {
  766. $maxage = $rlMaxage['unversioned']['client'];
  767. $smaxage = $rlMaxage['unversioned']['server'];
  768. // If a version was specified we can use a longer expiry time since changing
  769. // version numbers causes cache misses
  770. } else {
  771. $maxage = $rlMaxage['versioned']['client'];
  772. $smaxage = $rlMaxage['versioned']['server'];
  773. }
  774. if ( $context->getImageObj() ) {
  775. // Output different headers if we're outputting textual errors.
  776. if ( $errors ) {
  777. header( 'Content-Type: text/plain; charset=utf-8' );
  778. } else {
  779. $context->getImageObj()->sendResponseHeaders( $context );
  780. }
  781. } elseif ( $context->getOnly() === 'styles' ) {
  782. header( 'Content-Type: text/css; charset=utf-8' );
  783. header( 'Access-Control-Allow-Origin: *' );
  784. } else {
  785. header( 'Content-Type: text/javascript; charset=utf-8' );
  786. }
  787. // See RFC 2616 § 14.19 ETag
  788. // https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.19
  789. header( 'ETag: ' . $etag );
  790. if ( $context->getDebug() ) {
  791. // Do not cache debug responses
  792. header( 'Cache-Control: private, no-cache, must-revalidate' );
  793. header( 'Pragma: no-cache' );
  794. } else {
  795. header( "Cache-Control: public, max-age=$maxage, s-maxage=$smaxage" );
  796. $exp = min( $maxage, $smaxage );
  797. header( 'Expires: ' . wfTimestamp( TS_RFC2822, $exp + time() ) );
  798. }
  799. foreach ( $extra as $header ) {
  800. header( $header );
  801. }
  802. }
  803. /**
  804. * Respond with HTTP 304 Not Modified if appropiate.
  805. *
  806. * If there's an If-None-Match header, respond with a 304 appropriately
  807. * and clear out the output buffer. If the client cache is too old then do nothing.
  808. *
  809. * @param ResourceLoaderContext $context
  810. * @param string $etag ETag header value
  811. * @return bool True if HTTP 304 was sent and output handled
  812. */
  813. protected function tryRespondNotModified( ResourceLoaderContext $context, $etag ) {
  814. // See RFC 2616 § 14.26 If-None-Match
  815. // https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.26
  816. $clientKeys = $context->getRequest()->getHeader( 'If-None-Match', WebRequest::GETHEADER_LIST );
  817. // Never send 304s in debug mode
  818. if ( $clientKeys !== false && !$context->getDebug() && in_array( $etag, $clientKeys ) ) {
  819. // There's another bug in ob_gzhandler (see also the comment at
  820. // the top of this function) that causes it to gzip even empty
  821. // responses, meaning it's impossible to produce a truly empty
  822. // response (because the gzip header is always there). This is
  823. // a problem because 304 responses have to be completely empty
  824. // per the HTTP spec, and Firefox behaves buggily when they're not.
  825. // See also https://bugs.php.net/bug.php?id=51579
  826. // To work around this, we tear down all output buffering before
  827. // sending the 304.
  828. wfResetOutputBuffers( /* $resetGzipEncoding = */ true );
  829. HttpStatus::header( 304 );
  830. $this->sendResponseHeaders( $context, $etag, false );
  831. return true;
  832. }
  833. return false;
  834. }
  835. /**
  836. * Send out code for a response from file cache if possible.
  837. *
  838. * @param ResourceFileCache $fileCache Cache object for this request URL
  839. * @param ResourceLoaderContext $context Context in which to generate a response
  840. * @param string $etag ETag header value
  841. * @return bool If this found a cache file and handled the response
  842. */
  843. protected function tryRespondFromFileCache(
  844. ResourceFileCache $fileCache,
  845. ResourceLoaderContext $context,
  846. $etag
  847. ) {
  848. $rlMaxage = $this->config->get( 'ResourceLoaderMaxage' );
  849. // Buffer output to catch warnings.
  850. ob_start();
  851. // Get the maximum age the cache can be
  852. $maxage = is_null( $context->getVersion() )
  853. ? $rlMaxage['unversioned']['server']
  854. : $rlMaxage['versioned']['server'];
  855. // Minimum timestamp the cache file must have
  856. $good = $fileCache->isCacheGood( wfTimestamp( TS_MW, time() - $maxage ) );
  857. if ( !$good ) {
  858. try { // RL always hits the DB on file cache miss...
  859. wfGetDB( DB_REPLICA );
  860. } catch ( DBConnectionError $e ) { // ...check if we need to fallback to cache
  861. $good = $fileCache->isCacheGood(); // cache existence check
  862. }
  863. }
  864. if ( $good ) {
  865. $ts = $fileCache->cacheTimestamp();
  866. // Send content type and cache headers
  867. $this->sendResponseHeaders( $context, $etag, false );
  868. $response = $fileCache->fetchText();
  869. // Capture any PHP warnings from the output buffer and append them to the
  870. // response in a comment if we're in debug mode.
  871. if ( $context->getDebug() ) {
  872. $warnings = ob_get_contents();
  873. if ( strlen( $warnings ) ) {
  874. $response = self::makeComment( $warnings ) . $response;
  875. }
  876. }
  877. // Remove the output buffer and output the response
  878. ob_end_clean();
  879. echo $response . "\n/* Cached {$ts} */";
  880. return true; // cache hit
  881. }
  882. // Clear buffer
  883. ob_end_clean();
  884. return false; // cache miss
  885. }
  886. /**
  887. * Generate a CSS or JS comment block.
  888. *
  889. * Only use this for public data, not error message details.
  890. *
  891. * @param string $text
  892. * @return string
  893. */
  894. public static function makeComment( $text ) {
  895. $encText = str_replace( '*/', '* /', $text );
  896. return "/*\n$encText\n*/\n";
  897. }
  898. /**
  899. * Handle exception display.
  900. *
  901. * @param Exception $e Exception to be shown to the user
  902. * @return string Sanitized text in a CSS/JS comment that can be returned to the user
  903. */
  904. public static function formatException( $e ) {
  905. return self::makeComment( self::formatExceptionNoComment( $e ) );
  906. }
  907. /**
  908. * Handle exception display.
  909. *
  910. * @since 1.25
  911. * @param Exception $e Exception to be shown to the user
  912. * @return string Sanitized text that can be returned to the user
  913. */
  914. protected static function formatExceptionNoComment( $e ) {
  915. global $wgShowExceptionDetails;
  916. if ( !$wgShowExceptionDetails ) {
  917. return MWExceptionHandler::getPublicLogMessage( $e );
  918. }
  919. return MWExceptionHandler::getLogMessage( $e ) .
  920. "\nBacktrace:\n" .
  921. MWExceptionHandler::getRedactedTraceAsString( $e );
  922. }
  923. /**
  924. * Generate code for a response.
  925. *
  926. * Calling this method also populates the `errors` and `headers` members,
  927. * later used by respond().
  928. *
  929. * @param ResourceLoaderContext $context Context in which to generate a response
  930. * @param ResourceLoaderModule[] $modules List of module objects keyed by module name
  931. * @param string[] $missing List of requested module names that are unregistered (optional)
  932. * @return string Response data
  933. */
  934. public function makeModuleResponse( ResourceLoaderContext $context,
  935. array $modules, array $missing = []
  936. ) {
  937. $out = '';
  938. $states = [];
  939. if ( !count( $modules ) && !count( $missing ) ) {
  940. return <<<MESSAGE
  941. /* This file is the Web entry point for MediaWiki's ResourceLoader:
  942. <https://www.mediawiki.org/wiki/ResourceLoader>. In this request,
  943. no modules were requested. Max made me put this here. */
  944. MESSAGE;
  945. }
  946. $image = $context->getImageObj();
  947. if ( $image ) {
  948. $data = $image->getImageData( $context );
  949. if ( $data === false ) {
  950. $data = '';
  951. $this->errors[] = 'Image generation failed';
  952. }
  953. return $data;
  954. }
  955. foreach ( $missing as $name ) {
  956. $states[$name] = 'missing';
  957. }
  958. // Generate output
  959. $isRaw = false;
  960. $filter = $context->getOnly() === 'styles' ? 'minify-css' : 'minify-js';
  961. foreach ( $modules as $name => $module ) {
  962. try {
  963. $content = $module->getModuleContent( $context );
  964. $implementKey = $name . '@' . $module->getVersionHash( $context );
  965. $strContent = '';
  966. if ( isset( $content['headers'] ) ) {
  967. $this->extraHeaders = array_merge( $this->extraHeaders, $content['headers'] );
  968. }
  969. // Append output
  970. switch ( $context->getOnly() ) {
  971. case 'scripts':
  972. $scripts = $content['scripts'];
  973. if ( is_string( $scripts ) ) {
  974. // Load scripts raw...
  975. $strContent = $scripts;
  976. } elseif ( is_array( $scripts ) ) {
  977. // ...except when $scripts is an array of URLs
  978. $strContent = self::makeLoaderImplementScript( $implementKey, $scripts, [], [], [] );
  979. }
  980. break;
  981. case 'styles':
  982. $styles = $content['styles'];
  983. // We no longer separate into media, they are all combined now with
  984. // custom media type groups into @media .. {} sections as part of the css string.
  985. // Module returns either an empty array or a numerical array with css strings.
  986. $strContent = isset( $styles['css'] ) ? implode( '', $styles['css'] ) : '';
  987. break;
  988. default:
  989. $scripts = $content['scripts'] ?? '';
  990. if ( is_string( $scripts ) ) {
  991. if ( $name === 'site' || $name === 'user' ) {
  992. // Legacy scripts that run in the global scope without a closure.
  993. // mw.loader.implement will use globalEval if scripts is a string.
  994. // Minify manually here, because general response minification is
  995. // not effective due it being a string literal, not a function.
  996. if ( !self::inDebugMode() ) {
  997. $scripts = self::filter( 'minify-js', $scripts ); // T107377
  998. }
  999. } else {
  1000. $scripts = new XmlJsCode( $scripts );
  1001. }
  1002. }
  1003. $strContent = self::makeLoaderImplementScript(
  1004. $implementKey,
  1005. $scripts,
  1006. $content['styles'] ?? [],
  1007. isset( $content['messagesBlob'] ) ? new XmlJsCode( $content['messagesBlob'] ) : [],
  1008. $content['templates'] ?? []
  1009. );
  1010. break;
  1011. }
  1012. if ( !$context->getDebug() ) {
  1013. $strContent = self::filter( $filter, $strContent );
  1014. }
  1015. if ( $context->getOnly() === 'scripts' ) {
  1016. // Use a linebreak between module scripts (T162719)
  1017. $out .= $this->ensureNewline( $strContent );
  1018. } else {
  1019. $out .= $strContent;
  1020. }
  1021. } catch ( Exception $e ) {
  1022. $this->outputErrorAndLog( $e, 'Generating module package failed: {exception}' );
  1023. // Respond to client with error-state instead of module implementation
  1024. $states[$name] = 'error';
  1025. unset( $modules[$name] );
  1026. }
  1027. $isRaw |= $module->isRaw();
  1028. }
  1029. // Update module states
  1030. if ( $context->shouldIncludeScripts() && !$context->getRaw() && !$isRaw ) {
  1031. if ( count( $modules ) && $context->getOnly() === 'scripts' ) {
  1032. // Set the state of modules loaded as only scripts to ready as
  1033. // they don't have an mw.loader.implement wrapper that sets the state
  1034. foreach ( $modules as $name => $module ) {
  1035. $states[$name] = 'ready';
  1036. }
  1037. }
  1038. // Set the state of modules we didn't respond to with mw.loader.implement
  1039. if ( count( $states ) ) {
  1040. $stateScript = self::makeLoaderStateScript( $states );
  1041. if ( !$context->getDebug() ) {
  1042. $stateScript = self::filter( 'minify-js', $stateScript );
  1043. }
  1044. // Use a linebreak between module script and state script (T162719)
  1045. $out = $this->ensureNewline( $out ) . $stateScript;
  1046. }
  1047. } else {
  1048. if ( $states ) {
  1049. $this->errors[] = 'Problematic modules: '
  1050. . self::encodeJsonForScript( $states );
  1051. }
  1052. }
  1053. return $out;
  1054. }
  1055. /**
  1056. * Ensure the string is either empty or ends in a line break
  1057. * @param string $str
  1058. * @return string
  1059. */
  1060. private function ensureNewline( $str ) {
  1061. $end = substr( $str, -1 );
  1062. if ( $end === false || $end === "\n" ) {
  1063. return $str;
  1064. }
  1065. return $str . "\n";
  1066. }
  1067. /**
  1068. * Get names of modules that use a certain message.
  1069. *
  1070. * @param string $messageKey
  1071. * @return array List of module names
  1072. */
  1073. public function getModulesByMessage( $messageKey ) {
  1074. $moduleNames = [];
  1075. foreach ( $this->getModuleNames() as $moduleName ) {
  1076. $module = $this->getModule( $moduleName );
  1077. if ( in_array( $messageKey, $module->getMessages() ) ) {
  1078. $moduleNames[] = $moduleName;
  1079. }
  1080. }
  1081. return $moduleNames;
  1082. }
  1083. /**
  1084. * Return JS code that calls mw.loader.implement with given module properties.
  1085. *
  1086. * @param string $name Module name or implement key (format "`[name]@[version]`")
  1087. * @param XmlJsCode|array|string $scripts Code as XmlJsCode (to be wrapped in a closure),
  1088. * list of URLs to JavaScript files, or a string of JavaScript for `$.globalEval`.
  1089. * @param mixed $styles Array of CSS strings keyed by media type, or an array of lists of URLs
  1090. * to CSS files keyed by media type
  1091. * @param mixed $messages List of messages associated with this module. May either be an
  1092. * associative array mapping message key to value, or a JSON-encoded message blob containing
  1093. * the same data, wrapped in an XmlJsCode object.
  1094. * @param array $templates Keys are name of templates and values are the source of
  1095. * the template.
  1096. * @throws MWException
  1097. * @return string JavaScript code
  1098. */
  1099. protected static function makeLoaderImplementScript(
  1100. $name, $scripts, $styles, $messages, $templates
  1101. ) {
  1102. if ( $scripts instanceof XmlJsCode ) {
  1103. if ( self::inDebugMode() ) {
  1104. $scripts = new XmlJsCode( "function ( $, jQuery, require, module ) {\n{$scripts->value}\n}" );
  1105. } else {
  1106. $scripts = new XmlJsCode( 'function($,jQuery,require,module){' . $scripts->value . '}' );
  1107. }
  1108. } elseif ( !is_string( $scripts ) && !is_array( $scripts ) ) {
  1109. throw new MWException( 'Invalid scripts error. Array of URLs or string of code expected.' );
  1110. }
  1111. // mw.loader.implement requires 'styles', 'messages' and 'templates' to be objects (not
  1112. // arrays). json_encode considers empty arrays to be numerical and outputs "[]" instead
  1113. // of "{}". Force them to objects.
  1114. $module = [
  1115. $name,
  1116. $scripts,
  1117. (object)$styles,
  1118. (object)$messages,
  1119. (object)$templates,
  1120. ];
  1121. self::trimArray( $module );
  1122. return Xml::encodeJsCall( 'mw.loader.implement', $module, self::inDebugMode() );
  1123. }
  1124. /**
  1125. * Returns JS code which, when called, will register a given list of messages.
  1126. *
  1127. * @param mixed $messages Either an associative array mapping message key to value, or a
  1128. * JSON-encoded message blob containing the same data, wrapped in an XmlJsCode object.
  1129. * @return string JavaScript code
  1130. */
  1131. public static function makeMessageSetScript( $messages ) {
  1132. return Xml::encodeJsCall(
  1133. 'mw.messages.set',
  1134. [ (object)$messages ],
  1135. self::inDebugMode()
  1136. );
  1137. }
  1138. /**
  1139. * Combines an associative array mapping media type to CSS into a
  1140. * single stylesheet with "@media" blocks.
  1141. *
  1142. * @param array $stylePairs Array keyed by media type containing (arrays of) CSS strings
  1143. * @return array
  1144. */
  1145. public static function makeCombinedStyles( array $stylePairs ) {
  1146. $out = [];
  1147. foreach ( $stylePairs as $media => $styles ) {
  1148. // ResourceLoaderFileModule::getStyle can return the styles
  1149. // as a string or an array of strings. This is to allow separation in
  1150. // the front-end.
  1151. $styles = (array)$styles;
  1152. foreach ( $styles as $style ) {
  1153. $style = trim( $style );
  1154. // Don't output an empty "@media print { }" block (T42498)
  1155. if ( $style !== '' ) {
  1156. // Transform the media type based on request params and config
  1157. // The way that this relies on $wgRequest to propagate request params is slightly evil
  1158. $media = OutputPage::transformCssMedia( $media );
  1159. if ( $media === '' || $media == 'all' ) {
  1160. $out[] = $style;
  1161. } elseif ( is_string( $media ) ) {
  1162. $out[] = "@media $media {\n" . str_replace( "\n", "\n\t", "\t" . $style ) . "}";
  1163. }
  1164. // else: skip
  1165. }
  1166. }
  1167. }
  1168. return $out;
  1169. }
  1170. /**
  1171. * Wrapper around json_encode that avoids needless escapes,
  1172. * and pretty-prints in debug mode.
  1173. *
  1174. * @internal
  1175. * @since 1.32
  1176. * @param bool|string|array $data
  1177. * @return string JSON
  1178. */
  1179. public static function encodeJsonForScript( $data ) {
  1180. // Keep output as small as possible by disabling needless escape modes
  1181. // that PHP uses by default.
  1182. // However, while most module scripts are only served on HTTP responses
  1183. // for JavaScript, some modules can also be embedded in the HTML as inline
  1184. // scripts. This, and the fact that we sometimes need to export strings
  1185. // containing user-generated content and labels that may genuinely contain
  1186. // a sequences like "</script>", we need to encode either '/' or '<'.
  1187. // By default PHP escapes '/'. Let's escape '<' instead which is less common
  1188. // and allows URLs to mostly remain readable.
  1189. $jsonFlags = JSON_UNESCAPED_SLASHES |
  1190. JSON_UNESCAPED_UNICODE |
  1191. JSON_HEX_TAG |
  1192. JSON_HEX_AMP;
  1193. if ( self::inDebugMode() ) {
  1194. $jsonFlags |= JSON_PRETTY_PRINT;
  1195. }
  1196. return json_encode( $data, $jsonFlags );
  1197. }
  1198. /**
  1199. * Returns a JS call to mw.loader.state, which sets the state of one
  1200. * ore more modules to a given value. Has two calling conventions:
  1201. *
  1202. * - ResourceLoader::makeLoaderStateScript( $name, $state ):
  1203. * Set the state of a single module called $name to $state
  1204. *
  1205. * - ResourceLoader::makeLoaderStateScript( [ $name => $state, ... ] ):
  1206. * Set the state of modules with the given names to the given states
  1207. *
  1208. * @param array|string $states
  1209. * @param string|null $state
  1210. * @return string JavaScript code
  1211. */
  1212. public static function makeLoaderStateScript( $states, $state = null ) {
  1213. if ( !is_array( $states ) ) {
  1214. $states = [ $states => $state ];
  1215. }
  1216. return Xml::encodeJsCall(
  1217. 'mw.loader.state',
  1218. [ $states ],
  1219. self::inDebugMode()
  1220. );
  1221. }
  1222. private static function isEmptyObject( stdClass $obj ) {
  1223. foreach ( $obj as $key => $value ) {
  1224. return false;
  1225. }
  1226. return true;
  1227. }
  1228. /**
  1229. * Remove empty values from the end of an array.
  1230. *
  1231. * Values considered empty:
  1232. *
  1233. * - null
  1234. * - []
  1235. * - new XmlJsCode( '{}' )
  1236. * - new stdClass() // (object) []
  1237. *
  1238. * @param Array $array
  1239. */
  1240. private static function trimArray( array &$array ) {
  1241. $i = count( $array );
  1242. while ( $i-- ) {
  1243. if ( $array[$i] === null
  1244. || $array[$i] === []
  1245. || ( $array[$i] instanceof XmlJsCode && $array[$i]->value === '{}' )
  1246. || ( $array[$i] instanceof stdClass && self::isEmptyObject( $array[$i] ) )
  1247. ) {
  1248. unset( $array[$i] );
  1249. } else {
  1250. break;
  1251. }
  1252. }
  1253. }
  1254. /**
  1255. * Returns JS code which calls mw.loader.register with the given
  1256. * parameter.
  1257. *
  1258. * @par Example
  1259. * @code
  1260. *
  1261. * ResourceLoader::makeLoaderRegisterScript( [
  1262. * [ $name1, $version1, $dependencies1, $group1, $source1, $skip1 ],
  1263. * [ $name2, $version2, $dependencies1, $group2, $source2, $skip2 ],
  1264. * ...
  1265. * ] ):
  1266. * @endcode
  1267. *
  1268. * @internal
  1269. * @since 1.32
  1270. * @param array $modules Array of module registration arrays, each containing
  1271. * - string: module name
  1272. * - string: module version
  1273. * - array|null: List of dependencies (optional)
  1274. * - string|null: Module group (optional)
  1275. * - string|null: Name of foreign module source, or 'local' (optional)
  1276. * - string|null: Script body of a skip function (optional)
  1277. * @return string JavaScript code
  1278. */
  1279. public static function makeLoaderRegisterScript( array $modules ) {
  1280. // Optimisation: Transform dependency names into indexes when possible
  1281. // to produce smaller output. They are expanded by mw.loader.register on
  1282. // the other end using resolveIndexedDependencies().
  1283. $index = [];
  1284. foreach ( $modules as $i => &$module ) {
  1285. // Build module name index
  1286. $index[$module[0]] = $i;
  1287. }
  1288. foreach ( $modules as &$module ) {
  1289. if ( isset( $module[2] ) ) {
  1290. foreach ( $module[2] as &$dependency ) {
  1291. if ( isset( $index[$dependency] ) ) {
  1292. // Replace module name in dependency list with index
  1293. $dependency = $index[$dependency];
  1294. }
  1295. }
  1296. }
  1297. }
  1298. array_walk( $modules, [ 'self', 'trimArray' ] );
  1299. return Xml::encodeJsCall(
  1300. 'mw.loader.register',
  1301. [ $modules ],
  1302. self::inDebugMode()
  1303. );
  1304. }
  1305. /**
  1306. * Returns JS code which calls mw.loader.addSource() with the given
  1307. * parameters. Has two calling conventions:
  1308. *
  1309. * - ResourceLoader::makeLoaderSourcesScript( $id, $properties ):
  1310. * Register a single source
  1311. *
  1312. * - ResourceLoader::makeLoaderSourcesScript( [ $id1 => $loadUrl, $id2 => $loadUrl, ... ] );
  1313. * Register sources with the given IDs and properties.
  1314. *
  1315. * @param string|array $sources Source ID
  1316. * @param string|null $loadUrl load.php url
  1317. * @return string JavaScript code
  1318. */
  1319. public static function makeLoaderSourcesScript( $sources, $loadUrl = null ) {
  1320. if ( !is_array( $sources ) ) {
  1321. $sources = [ $sources => $loadUrl ];
  1322. }
  1323. return Xml::encodeJsCall(
  1324. 'mw.loader.addSource',
  1325. [ $sources ],
  1326. self::inDebugMode()
  1327. );
  1328. }
  1329. /**
  1330. * Wraps JavaScript code to run after the startup module.
  1331. *
  1332. * @param string $script JavaScript code
  1333. * @return string JavaScript code
  1334. */
  1335. public static function makeLoaderConditionalScript( $script ) {
  1336. // Adds a function to lazy-created RLQ
  1337. return '(window.RLQ=window.RLQ||[]).push(function(){' .
  1338. trim( $script ) . '});';
  1339. }
  1340. /**
  1341. * Wraps JavaScript code to run after a required module.
  1342. *
  1343. * @since 1.32
  1344. * @param string|string[] $modules Module name(s)
  1345. * @param string $script JavaScript code
  1346. * @return string JavaScript code
  1347. */
  1348. public static function makeInlineCodeWithModule( $modules, $script ) {
  1349. // Adds an array to lazy-created RLQ
  1350. return '(window.RLQ=window.RLQ||[]).push(['
  1351. . self::encodeJsonForScript( $modules ) . ','
  1352. . 'function(){' . trim( $script ) . '}'
  1353. . ']);';
  1354. }
  1355. /**
  1356. * Returns an HTML script tag that runs given JS code after startup and base modules.
  1357. *
  1358. * The code will be wrapped in a closure, and it will be executed by ResourceLoader's
  1359. * startup module if the client has adequate support for MediaWiki JavaScript code.
  1360. *
  1361. * @param string $script JavaScript code
  1362. * @param string|null $nonce [optional] Content-Security-Policy nonce
  1363. * (from OutputPage::getCSPNonce)
  1364. * @return string|WrappedString HTML
  1365. */
  1366. public static function makeInlineScript( $script, $nonce = null ) {
  1367. $js = self::makeLoaderConditionalScript( $script );
  1368. $escNonce = '';
  1369. if ( $nonce === null ) {
  1370. wfWarn( __METHOD__ . " did not get nonce. Will break CSP" );
  1371. } elseif ( $nonce !== false ) {
  1372. // If it was false, CSP is disabled, so no nonce attribute.
  1373. // Nonce should be only base64 characters, so should be safe,
  1374. // but better to be safely escaped than sorry.
  1375. $escNonce = ' nonce="' . htmlspecialchars( $nonce ) . '"';
  1376. }
  1377. return new WrappedString(
  1378. Html::inlineScript( $js, $nonce ),
  1379. "<script$escNonce>(window.RLQ=window.RLQ||[]).push(function(){",
  1380. '});</script>'
  1381. );
  1382. }
  1383. /**
  1384. * Returns JS code which will set the MediaWiki configuration array to
  1385. * the given value.
  1386. *
  1387. * @param array $configuration List of configuration values keyed by variable name
  1388. * @return string JavaScript code
  1389. */
  1390. public static function makeConfigSetScript( array $configuration ) {
  1391. return Xml::encodeJsCall(
  1392. 'mw.config.set',
  1393. [ $configuration ],
  1394. self::inDebugMode()
  1395. );
  1396. }
  1397. /**
  1398. * Convert an array of module names to a packed query string.
  1399. *
  1400. * For example, `[ 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ]`
  1401. * becomes `'foo.bar,baz|bar.baz,quux'`.
  1402. *
  1403. * This process is reversed by ResourceLoaderContext::expandModuleNames().
  1404. * See also mw.loader#buildModulesString() which is a port of this, used
  1405. * on the client-side.
  1406. *
  1407. * @param array $modules List of module names (strings)
  1408. * @return string Packed query string
  1409. */
  1410. public static function makePackedModulesString( $modules ) {
  1411. $moduleMap = []; // [ prefix => [ suffixes ] ]
  1412. foreach ( $modules as $module ) {
  1413. $pos = strrpos( $module, '.' );
  1414. $prefix = $pos === false ? '' : substr( $module, 0, $pos );
  1415. $suffix = $pos === false ? $module : substr( $module, $pos + 1 );
  1416. $moduleMap[$prefix][] = $suffix;
  1417. }
  1418. $arr = [];
  1419. foreach ( $moduleMap as $prefix => $suffixes ) {
  1420. $p = $prefix === '' ? '' : $prefix . '.';
  1421. $arr[] = $p . implode( ',', $suffixes );
  1422. }
  1423. return implode( '|', $arr );
  1424. }
  1425. /**
  1426. * Determine whether debug mode was requested
  1427. * Order of priority is 1) request param, 2) cookie, 3) $wg setting
  1428. * @return bool
  1429. */
  1430. public static function inDebugMode() {
  1431. if ( self::$debugMode === null ) {
  1432. global $wgRequest, $wgResourceLoaderDebug;
  1433. self::$debugMode = $wgRequest->getFuzzyBool( 'debug',
  1434. $wgRequest->getCookie( 'resourceLoaderDebug', '', $wgResourceLoaderDebug )
  1435. );
  1436. }
  1437. return self::$debugMode;
  1438. }
  1439. /**
  1440. * Reset static members used for caching.
  1441. *
  1442. * Global state and $wgRequest are evil, but we're using it right
  1443. * now and sometimes we need to be able to force ResourceLoader to
  1444. * re-evaluate the context because it has changed (e.g. in the test suite).
  1445. */
  1446. public static function clearCache() {
  1447. self::$debugMode = null;
  1448. }
  1449. /**
  1450. * Build a load.php URL
  1451. *
  1452. * @since 1.24
  1453. * @param string $source Name of the ResourceLoader source
  1454. * @param ResourceLoaderContext $context
  1455. * @param array $extraQuery
  1456. * @return string URL to load.php. May be protocol-relative if $wgLoadScript is, too.
  1457. */
  1458. public function createLoaderURL( $source, ResourceLoaderContext $context,
  1459. $extraQuery = []
  1460. ) {
  1461. $query = self::createLoaderQuery( $context, $extraQuery );
  1462. $script = $this->getLoadScript( $source );
  1463. return wfAppendQuery( $script, $query );
  1464. }
  1465. /**
  1466. * Helper for createLoaderURL()
  1467. *
  1468. * @since 1.24
  1469. * @see makeLoaderQuery
  1470. * @param ResourceLoaderContext $context
  1471. * @param array $extraQuery
  1472. * @return array
  1473. */
  1474. protected static function createLoaderQuery( ResourceLoaderContext $context, $extraQuery = [] ) {
  1475. return self::makeLoaderQuery(
  1476. $context->getModules(),
  1477. $context->getLanguage(),
  1478. $context->getSkin(),
  1479. $context->getUser(),
  1480. $context->getVersion(),
  1481. $context->getDebug(),
  1482. $context->getOnly(),
  1483. $context->getRequest()->getBool( 'printable' ),
  1484. $context->getRequest()->getBool( 'handheld' ),
  1485. $extraQuery
  1486. );
  1487. }
  1488. /**
  1489. * Build a query array (array representation of query string) for load.php. Helper
  1490. * function for createLoaderURL().
  1491. *
  1492. * @param array $modules
  1493. * @param string $lang
  1494. * @param string $skin
  1495. * @param string|null $user
  1496. * @param string|null $version
  1497. * @param bool $debug
  1498. * @param string|null $only
  1499. * @param bool $printable
  1500. * @param bool $handheld
  1501. * @param array $extraQuery
  1502. *
  1503. * @return array
  1504. */
  1505. public static function makeLoaderQuery( $modules, $lang, $skin, $user = null,
  1506. $version = null, $debug = false, $only = null, $printable = false,
  1507. $handheld = false, $extraQuery = []
  1508. ) {
  1509. $query = [
  1510. 'modules' => self::makePackedModulesString( $modules ),
  1511. 'lang' => $lang,
  1512. 'skin' => $skin,
  1513. 'debug' => $debug ? 'true' : 'false',
  1514. ];
  1515. if ( $user !== null ) {
  1516. $query['user'] = $user;
  1517. }
  1518. if ( $version !== null ) {
  1519. $query['version'] = $version;
  1520. }
  1521. if ( $only !== null ) {
  1522. $query['only'] = $only;
  1523. }
  1524. if ( $printable ) {
  1525. $query['printable'] = 1;
  1526. }
  1527. if ( $handheld ) {
  1528. $query['handheld'] = 1;
  1529. }
  1530. $query += $extraQuery;
  1531. // Make queries uniform in order
  1532. ksort( $query );
  1533. return $query;
  1534. }
  1535. /**
  1536. * Check a module name for validity.
  1537. *
  1538. * Module names may not contain pipes (|), commas (,) or exclamation marks (!) and can be
  1539. * at most 255 bytes.
  1540. *
  1541. * @param string $moduleName Module name to check
  1542. * @return bool Whether $moduleName is a valid module name
  1543. */
  1544. public static function isValidModuleName( $moduleName ) {
  1545. return strcspn( $moduleName, '!,|', 0, 255 ) === strlen( $moduleName );
  1546. }
  1547. /**
  1548. * Returns LESS compiler set up for use with MediaWiki
  1549. *
  1550. * @since 1.27
  1551. * @param array $vars Associative array of variables that should be used
  1552. * for compilation. Since 1.32, this method no longer automatically includes
  1553. * global LESS vars from ResourceLoader::getLessVars (T191937).
  1554. * @throws MWException
  1555. * @return Less_Parser
  1556. */
  1557. public function getLessCompiler( $vars = [] ) {
  1558. global $IP;
  1559. // When called from the installer, it is possible that a required PHP extension
  1560. // is missing (at least for now; see T49564). If this is the case, throw an
  1561. // exception (caught by the installer) to prevent a fatal error later on.
  1562. if ( !class_exists( 'Less_Parser' ) ) {
  1563. throw new MWException( 'MediaWiki requires the less.php parser' );
  1564. }
  1565. $parser = new Less_Parser;
  1566. $parser->ModifyVars( $vars );
  1567. $parser->SetImportDirs( [
  1568. "$IP/resources/src/mediawiki.less/" => '',
  1569. ] );
  1570. $parser->SetOption( 'relativeUrls', false );
  1571. return $parser;
  1572. }
  1573. /**
  1574. * Get global LESS variables.
  1575. *
  1576. * @since 1.27
  1577. * @deprecated since 1.32 Use ResourceLoderModule::getLessVars() instead.
  1578. * @return array Map of variable names to string CSS values.
  1579. */
  1580. public function getLessVars() {
  1581. return [];
  1582. }
  1583. }