3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
19 * @author Trevor Parscal
20 * @author Roan Kattouw
23 use MediaWiki\Linker\LinkTarget
;
24 use MediaWiki\Revision\RevisionRecord
;
25 use Wikimedia\Assert\Assert
;
26 use Wikimedia\Rdbms\Database
;
27 use Wikimedia\Rdbms\IDatabase
;
28 use MediaWiki\MediaWikiServices
;
31 * Abstraction for ResourceLoader modules which pull from wiki pages
33 * This can only be used for wiki pages in the MediaWiki and User namespaces,
34 * because of its dependence on the functionality of Title::isUserConfigPage()
35 * and Title::isSiteConfigPage().
37 * This module supports being used as a placeholder for a module on a remote wiki.
38 * To do so, getDB() must be overloaded to return a foreign database object that
39 * allows local wikis to query page metadata.
41 * Safe for calls on local wikis are:
45 * - Basic methods that strictly involve the foreign database
50 * @ingroup ResourceLoader
53 class ResourceLoaderWikiModule
extends ResourceLoaderModule
{
55 // Origin defaults to users with sitewide authority
56 protected $origin = self
::ORIGIN_USER_SITEWIDE
;
58 // In-process cache for title info, structured as an array
60 // <batchKey> // Pipe-separated list of sorted keys from getPages
62 // <titleKey> => [ // Normalised title key
64 // 'page_latest' => ..,
65 // 'page_touched' => ..,
69 // @see self::fetchTitleInfo()
70 // @see self::makeTitleKey()
71 protected $titleInfo = [];
73 // List of page names that contain CSS
74 protected $styles = [];
76 // List of page names that contain JavaScript
77 protected $scripts = [];
83 * @param array|null $options For back-compat, this can be omitted in favour of overwriting
86 public function __construct( array $options = null ) {
87 if ( $options === null ) {
91 foreach ( $options as $member => $option ) {
97 $this->{$member} = $option;
104 * Subclasses should return an associative array of resources in the module.
105 * Keys should be the title of a page in the MediaWiki or User namespace.
107 * Values should be a nested array of options. The supported keys are 'type' and
108 * (CSS only) 'media'.
110 * For scripts, 'type' should be 'script'.
112 * For stylesheets, 'type' should be 'style'.
113 * There is an optional media key, the value of which can be the
114 * medium ('screen', 'print', etc.) of the stylesheet.
116 * @param ResourceLoaderContext $context
119 protected function getPages( ResourceLoaderContext
$context ) {
120 $config = $this->getConfig();
123 // Filter out pages from origins not allowed by the current wiki configuration.
124 if ( $config->get( 'UseSiteJs' ) ) {
125 foreach ( $this->scripts
as $script ) {
126 $pages[$script] = [ 'type' => 'script' ];
130 if ( $config->get( 'UseSiteCss' ) ) {
131 foreach ( $this->styles
as $style ) {
132 $pages[$style] = [ 'type' => 'style' ];
144 public function getGroup() {
149 * Get the Database handle used for computing the module version.
151 * Subclasses may override this to return a foreign database, which would
152 * allow them to register a module on wiki A that fetches wiki pages from
155 * The way this works is that the local module is a placeholder that can
156 * only computer a module version hash. The 'source' of the module must
157 * be set to the foreign wiki directly. Methods getScript() and getContent()
158 * will not use this handle and are not valid on the local wiki.
162 protected function getDB() {
163 return wfGetDB( DB_REPLICA
);
167 * @param string $titleText
168 * @param ResourceLoaderContext|null $context (but passing null is deprecated)
169 * @return null|string
170 * @since 1.32 added the $context parameter
172 protected function getContent( $titleText, ResourceLoaderContext
$context = null ) {
173 $title = Title
::newFromText( $titleText );
175 return null; // Bad title
178 $content = $this->getContentObj( $title, $context );
180 return null; // No content found
183 $handler = $content->getContentHandler();
184 if ( $handler->isSupportedFormat( CONTENT_FORMAT_CSS
) ) {
185 $format = CONTENT_FORMAT_CSS
;
186 } elseif ( $handler->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT
) ) {
187 $format = CONTENT_FORMAT_JAVASCRIPT
;
189 return null; // Bad content model
192 return $content->serialize( $format );
196 * @param Title $title
197 * @param ResourceLoaderContext|null $context (but passing null is deprecated)
198 * @param int|null $maxRedirects Maximum number of redirects to follow. If
199 * null, uses $wgMaxRedirects
200 * @return Content|null
201 * @since 1.32 added the $context and $maxRedirects parameters
203 protected function getContentObj(
204 Title
$title, ResourceLoaderContext
$context = null, $maxRedirects = null
206 if ( $context === null ) {
207 wfDeprecated( __METHOD__
. ' without a ResourceLoader context', '1.32' );
210 $overrideCallback = $context ?
$context->getContentOverrideCallback() : null;
211 $content = $overrideCallback ?
call_user_func( $overrideCallback, $title ) : null;
213 if ( !$content instanceof Content
) {
214 $this->getLogger()->error(
215 'Bad content override for "{title}" in ' . __METHOD__
,
216 [ 'title' => $title->getPrefixedText() ]
221 $revision = Revision
::newKnownCurrent( wfGetDB( DB_REPLICA
), $title );
225 $content = $revision->getContent( RevisionRecord
::RAW
);
228 $this->getLogger()->error(
229 'Failed to load content of JS/CSS page "{title}" in ' . __METHOD__
,
230 [ 'title' => $title->getPrefixedText() ]
236 if ( $content && $content->isRedirect() ) {
237 if ( $maxRedirects === null ) {
238 $maxRedirects = $this->getConfig()->get( 'MaxRedirects' ) ?
: 0;
240 if ( $maxRedirects > 0 ) {
241 $newTitle = $content->getRedirectTarget();
242 return $newTitle ?
$this->getContentObj( $newTitle, $context, $maxRedirects - 1 ) : null;
250 * @param ResourceLoaderContext $context
253 public function shouldEmbedModule( ResourceLoaderContext
$context ) {
254 $overrideCallback = $context->getContentOverrideCallback();
255 if ( $overrideCallback && $this->getSource() === 'local' ) {
256 foreach ( $this->getPages( $context ) as $page => $info ) {
257 $title = Title
::newFromText( $page );
258 if ( $title && call_user_func( $overrideCallback, $title ) !== null ) {
264 return parent
::shouldEmbedModule( $context );
268 * @param ResourceLoaderContext $context
269 * @return string JavaScript code
271 public function getScript( ResourceLoaderContext
$context ) {
273 foreach ( $this->getPages( $context ) as $titleText => $options ) {
274 if ( $options['type'] !== 'script' ) {
277 $script = $this->getContent( $titleText, $context );
278 if ( strval( $script ) !== '' ) {
279 $script = $this->validateScriptFile( $titleText, $script );
280 $scripts .= ResourceLoader
::makeComment( $titleText ) . $script . "\n";
287 * @param ResourceLoaderContext $context
290 public function getStyles( ResourceLoaderContext
$context ) {
292 foreach ( $this->getPages( $context ) as $titleText => $options ) {
293 if ( $options['type'] !== 'style' ) {
296 $media = $options['media'] ??
'all';
297 $style = $this->getContent( $titleText, $context );
298 if ( strval( $style ) === '' ) {
301 if ( $this->getFlip( $context ) ) {
302 $style = CSSJanus
::transform( $style, true, false );
304 $style = MemoizedCallable
::call( 'CSSMin::remap',
305 [ $style, false, $this->getConfig()->get( 'ScriptPath' ), true ] );
306 if ( !isset( $styles[$media] ) ) {
307 $styles[$media] = [];
309 $style = ResourceLoader
::makeComment( $titleText ) . $style;
310 $styles[$media][] = $style;
316 * Disable module content versioning.
318 * This class does not support generating content outside of a module
319 * request due to foreign database support.
321 * See getDefinitionSummary() for meta-data versioning.
325 public function enableModuleContentVersion() {
330 * @param ResourceLoaderContext $context
333 public function getDefinitionSummary( ResourceLoaderContext
$context ) {
334 $summary = parent
::getDefinitionSummary( $context );
336 'pages' => $this->getPages( $context ),
337 // Includes meta data of current revisions
338 'titleInfo' => $this->getTitleInfo( $context ),
344 * @param ResourceLoaderContext $context
347 public function isKnownEmpty( ResourceLoaderContext
$context ) {
348 $revisions = $this->getTitleInfo( $context );
350 // If a module has dependencies it cannot be empty. An empty array will be cast to false
351 if ( $this->getDependencies() ) {
354 // For user modules, don't needlessly load if there are no non-empty pages
355 if ( $this->getGroup() === 'user' ) {
356 foreach ( $revisions as $revision ) {
357 if ( $revision['page_len'] > 0 ) {
358 // At least one non-empty page, module should be loaded
365 // T70488: For other modules (i.e. ones that are called in cached html output) only check
366 // page existance. This ensures that, if some pages in a module are temporarily blanked,
367 // we don't end omit the module's script or link tag on some pages.
368 return count( $revisions ) === 0;
371 private function setTitleInfo( $batchKey, array $titleInfo ) {
372 $this->titleInfo
[$batchKey] = $titleInfo;
375 private static function makeTitleKey( LinkTarget
$title ) {
376 // Used for keys in titleInfo.
377 return "{$title->getNamespace()}:{$title->getDBkey()}";
381 * Get the information about the wiki pages for a given context.
382 * @param ResourceLoaderContext $context
383 * @return array Keyed by page name
385 protected function getTitleInfo( ResourceLoaderContext
$context ) {
386 $dbr = $this->getDB();
388 $pageNames = array_keys( $this->getPages( $context ) );
390 $batchKey = implode( '|', $pageNames );
391 if ( !isset( $this->titleInfo
[$batchKey] ) ) {
392 $this->titleInfo
[$batchKey] = static::fetchTitleInfo( $dbr, $pageNames, __METHOD__
);
395 $titleInfo = $this->titleInfo
[$batchKey];
397 // Override the title info from the overrides, if any
398 $overrideCallback = $context->getContentOverrideCallback();
399 if ( $overrideCallback ) {
400 foreach ( $pageNames as $page ) {
401 $title = Title
::newFromText( $page );
402 $content = $title ?
call_user_func( $overrideCallback, $title ) : null;
403 if ( $content !== null ) {
404 $titleInfo[$title->getPrefixedText()] = [
405 'page_len' => $content->getSize(),
406 'page_latest' => 'TBD', // None available
407 'page_touched' => wfTimestamp( TS_MW
),
417 protected static function fetchTitleInfo( IDatabase
$db, array $pages, $fname = __METHOD__
) {
419 $batch = new LinkBatch
;
420 foreach ( $pages as $titleText ) {
421 $title = Title
::newFromText( $titleText );
423 // Page name may be invalid if user-provided (e.g. gadgets)
424 $batch->addObj( $title );
427 if ( !$batch->isEmpty() ) {
428 $res = $db->select( 'page',
429 // Include page_touched to allow purging if cache is poisoned (T117587, T113916)
430 [ 'page_namespace', 'page_title', 'page_touched', 'page_len', 'page_latest' ],
431 $batch->constructSet( 'page', $db ),
434 foreach ( $res as $row ) {
435 // Avoid including ids or timestamps of revision/page tables so
436 // that versions are not wasted
437 $title = new TitleValue( (int)$row->page_namespace
, $row->page_title
);
438 $titleInfo[self
::makeTitleKey( $title )] = [
439 'page_len' => $row->page_len
,
440 'page_latest' => $row->page_latest
,
441 'page_touched' => $row->page_touched
,
450 * @param ResourceLoaderContext $context
451 * @param IDatabase $db
452 * @param string[] $moduleNames
454 public static function preloadTitleInfo(
455 ResourceLoaderContext
$context, IDatabase
$db, array $moduleNames
457 $rl = $context->getResourceLoader();
458 // getDB() can be overridden to point to a foreign database.
459 // For now, only preload local. In the future, we could preload by wikiID.
461 /** @var ResourceLoaderWikiModule[] $wikiModules */
463 foreach ( $moduleNames as $name ) {
464 $module = $rl->getModule( $name );
465 if ( $module instanceof self
) {
466 $mDB = $module->getDB();
467 // Subclasses may implement getDB differently
468 if ( $mDB->getDomainID() === $db->getDomainID() ) {
469 $wikiModules[] = $module;
470 $allPages +
= $module->getPages( $context );
475 if ( !$wikiModules ) {
476 // Nothing to preload
480 $pageNames = array_keys( $allPages );
482 $hash = sha1( implode( '|', $pageNames ) );
484 // Avoid Zend bug where "static::" does not apply LSB in the closure
485 $func = [ static::class, 'fetchTitleInfo' ];
488 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
489 $allInfo = $cache->getWithSetCallback(
490 $cache->makeGlobalKey( 'resourceloader-titleinfo', $db->getDomainID(), $hash ),
492 function ( $curVal, &$ttl, array &$setOpts ) use ( $func, $pageNames, $db, $fname ) {
493 $setOpts +
= Database
::getCacheSetOptions( $db );
495 return call_user_func( $func, $db, $pageNames, $fname );
499 $cache->makeGlobalKey( 'resourceloader-titleinfo', $db->getDomainID() ) ]
503 foreach ( $wikiModules as $wikiModule ) {
504 $pages = $wikiModule->getPages( $context );
505 // Before we intersect, map the names to canonical form (T145673).
507 foreach ( $pages as $pageName => $unused ) {
508 $title = Title
::newFromText( $pageName );
510 $intersect[ self
::makeTitleKey( $title ) ] = 1;
512 // Page name may be invalid if user-provided (e.g. gadgets)
513 $rl->getLogger()->info(
514 'Invalid wiki page title "{title}" in ' . __METHOD__
,
515 [ 'title' => $pageName ]
519 $info = array_intersect_key( $allInfo, $intersect );
520 $pageNames = array_keys( $pages );
522 $batchKey = implode( '|', $pageNames );
523 $wikiModule->setTitleInfo( $batchKey, $info );
528 * Clear the preloadTitleInfo() cache for all wiki modules on this wiki on
529 * page change if it was a JS or CSS page
531 * @param Title $title
532 * @param Revision|null $old Prior page revision
533 * @param Revision|null $new New page revision
534 * @param string $domain Database domain ID
537 public static function invalidateModuleCache(
538 Title
$title, Revision
$old = null, Revision
$new = null, $domain
540 static $formats = [ CONTENT_FORMAT_CSS
, CONTENT_FORMAT_JAVASCRIPT
];
542 Assert
::parameterType( 'string', $domain, '$domain' );
544 // TODO: MCR: differentiate between page functionality and content model!
545 // Not all pages containing CSS or JS have to be modules! [PageType]
546 if ( $old && in_array( $old->getContentFormat(), $formats ) ) {
548 } elseif ( $new && in_array( $new->getContentFormat(), $formats ) ) {
551 $purge = ( $title->isSiteConfigPage() ||
$title->isUserConfigPage() );
555 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
556 $key = $cache->makeGlobalKey( 'resourceloader-titleinfo', $domain );
557 $cache->touchCheckKey( $key );
565 public function getType() {
566 // Check both because subclasses don't always pass pages via the constructor,
567 // they may also override getPages() instead, in which case we should keep
568 // defaulting to LOAD_GENERAL and allow them to override getType() separately.
569 return ( $this->styles
&& !$this->scripts
) ? self
::LOAD_STYLES
: self
::LOAD_GENERAL
;