3 * Abstraction for ResourceLoader modules that pull from wiki pages.
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.
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.
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
21 * @author Trevor Parscal
22 * @author Roan Kattouw
25 use MediaWiki\Linker\LinkTarget
;
26 use MediaWiki\Storage\RevisionRecord
;
27 use Wikimedia\Assert\Assert
;
28 use Wikimedia\Rdbms\Database
;
29 use Wikimedia\Rdbms\IDatabase
;
30 use MediaWiki\MediaWikiServices
;
33 * Abstraction for ResourceLoader modules which pull from wiki pages
35 * This can only be used for wiki pages in the MediaWiki and User namespaces,
36 * because of its dependence on the functionality of Title::isUserConfigPage()
37 * and Title::isSiteConfigPage().
39 * This module supports being used as a placeholder for a module on a remote wiki.
40 * To do so, getDB() must be overloaded to return a foreign database object that
41 * allows local wikis to query page metadata.
43 * Safe for calls on local wikis are:
47 * - Basic methods that strictly involve the foreign database
52 class ResourceLoaderWikiModule
extends ResourceLoaderModule
{
54 // Origin defaults to users with sitewide authority
55 protected $origin = self
::ORIGIN_USER_SITEWIDE
;
57 // In-process cache for title info, structured as an array
59 // <batchKey> // Pipe-separated list of sorted keys from getPages
61 // <titleKey> => [ // Normalised title key
63 // 'page_latest' => ..,
64 // 'page_touched' => ..,
68 // @see self::fetchTitleInfo()
69 // @see self::makeTitleKey()
70 protected $titleInfo = [];
72 // List of page names that contain CSS
73 protected $styles = [];
75 // List of page names that contain JavaScript
76 protected $scripts = [];
82 * @param array|null $options For back-compat, this can be omitted in favour of overwriting
85 public function __construct( array $options = null ) {
86 if ( $options === null ) {
90 foreach ( $options as $member => $option ) {
96 $this->{$member} = $option;
103 * Subclasses should return an associative array of resources in the module.
104 * Keys should be the title of a page in the MediaWiki or User namespace.
106 * Values should be a nested array of options. The supported keys are 'type' and
107 * (CSS only) 'media'.
109 * For scripts, 'type' should be 'script'.
111 * For stylesheets, 'type' should be 'style'.
112 * There is an optional media key, the value of which can be the
113 * medium ('screen', 'print', etc.) of the stylesheet.
115 * @param ResourceLoaderContext $context
118 protected function getPages( ResourceLoaderContext
$context ) {
119 $config = $this->getConfig();
122 // Filter out pages from origins not allowed by the current wiki configuration.
123 if ( $config->get( 'UseSiteJs' ) ) {
124 foreach ( $this->scripts
as $script ) {
125 $pages[$script] = [ 'type' => 'script' ];
129 if ( $config->get( 'UseSiteCss' ) ) {
130 foreach ( $this->styles
as $style ) {
131 $pages[$style] = [ 'type' => 'style' ];
143 public function getGroup() {
148 * Get the Database handle used for computing the module version.
150 * Subclasses may override this to return a foreign database, which would
151 * allow them to register a module on wiki A that fetches wiki pages from
154 * The way this works is that the local module is a placeholder that can
155 * only computer a module version hash. The 'source' of the module must
156 * be set to the foreign wiki directly. Methods getScript() and getContent()
157 * will not use this handle and are not valid on the local wiki.
161 protected function getDB() {
162 return wfGetDB( DB_REPLICA
);
166 * @param string $titleText
167 * @param ResourceLoaderContext|null $context (but passing null is deprecated)
168 * @return null|string
169 * @since 1.32 added the $context parameter
171 protected function getContent( $titleText, ResourceLoaderContext
$context = null ) {
172 $title = Title
::newFromText( $titleText );
174 return null; // Bad title
177 $content = $this->getContentObj( $title, $context );
179 return null; // No content found
182 $handler = $content->getContentHandler();
183 if ( $handler->isSupportedFormat( CONTENT_FORMAT_CSS
) ) {
184 $format = CONTENT_FORMAT_CSS
;
185 } elseif ( $handler->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT
) ) {
186 $format = CONTENT_FORMAT_JAVASCRIPT
;
188 return null; // Bad content model
191 return $content->serialize( $format );
195 * @param Title $title
196 * @param ResourceLoaderContext|null $context (but passing null is deprecated)
197 * @param int|null $maxRedirects Maximum number of redirects to follow. If
198 * null, uses $wgMaxRedirects
199 * @return Content|null
200 * @since 1.32 added the $context and $maxRedirects parameters
202 protected function getContentObj(
203 Title
$title, ResourceLoaderContext
$context = null, $maxRedirects = null
205 if ( $context === null ) {
206 wfDeprecated( __METHOD__
. ' without a ResourceLoader context', '1.32' );
209 $overrideCallback = $context ?
$context->getContentOverrideCallback() : null;
210 $content = $overrideCallback ?
call_user_func( $overrideCallback, $title ) : null;
212 if ( !$content instanceof Content
) {
213 $this->getLogger()->error(
214 'Bad content override for "{title}" in ' . __METHOD__
,
215 [ 'title' => $title->getPrefixedText() ]
220 $revision = Revision
::newKnownCurrent( wfGetDB( DB_REPLICA
), $title );
224 $content = $revision->getContent( RevisionRecord
::RAW
);
227 $this->getLogger()->error(
228 'Failed to load content of JS/CSS page "{title}" in ' . __METHOD__
,
229 [ 'title' => $title->getPrefixedText() ]
235 if ( $content && $content->isRedirect() ) {
236 if ( $maxRedirects === null ) {
237 $maxRedirects = $this->getConfig()->get( 'MaxRedirects' ) ?
: 0;
239 if ( $maxRedirects > 0 ) {
240 $newTitle = $content->getRedirectTarget();
241 return $newTitle ?
$this->getContentObj( $newTitle, $context, $maxRedirects - 1 ) : null;
249 * @param ResourceLoaderContext $context
252 public function shouldEmbedModule( ResourceLoaderContext
$context ) {
253 $overrideCallback = $context->getContentOverrideCallback();
254 if ( $overrideCallback && $this->getSource() === 'local' ) {
255 foreach ( $this->getPages( $context ) as $page => $info ) {
256 $title = Title
::newFromText( $page );
257 if ( $title && call_user_func( $overrideCallback, $title ) !== null ) {
263 return parent
::shouldEmbedModule( $context );
267 * @param ResourceLoaderContext $context
268 * @return string JavaScript code
270 public function getScript( ResourceLoaderContext
$context ) {
272 foreach ( $this->getPages( $context ) as $titleText => $options ) {
273 if ( $options['type'] !== 'script' ) {
276 $script = $this->getContent( $titleText, $context );
277 if ( strval( $script ) !== '' ) {
278 $script = $this->validateScriptFile( $titleText, $script );
279 $scripts .= ResourceLoader
::makeComment( $titleText ) . $script . "\n";
286 * @param ResourceLoaderContext $context
289 public function getStyles( ResourceLoaderContext
$context ) {
291 foreach ( $this->getPages( $context ) as $titleText => $options ) {
292 if ( $options['type'] !== 'style' ) {
295 $media = $options['media'] ??
'all';
296 $style = $this->getContent( $titleText, $context );
297 if ( strval( $style ) === '' ) {
300 if ( $this->getFlip( $context ) ) {
301 $style = CSSJanus
::transform( $style, true, false );
303 $style = MemoizedCallable
::call( 'CSSMin::remap',
304 [ $style, false, $this->getConfig()->get( 'ScriptPath' ), true ] );
305 if ( !isset( $styles[$media] ) ) {
306 $styles[$media] = [];
308 $style = ResourceLoader
::makeComment( $titleText ) . $style;
309 $styles[$media][] = $style;
315 * Disable module content versioning.
317 * This class does not support generating content outside of a module
318 * request due to foreign database support.
320 * See getDefinitionSummary() for meta-data versioning.
324 public function enableModuleContentVersion() {
329 * @param ResourceLoaderContext $context
332 public function getDefinitionSummary( ResourceLoaderContext
$context ) {
333 $summary = parent
::getDefinitionSummary( $context );
335 'pages' => $this->getPages( $context ),
336 // Includes meta data of current revisions
337 'titleInfo' => $this->getTitleInfo( $context ),
343 * @param ResourceLoaderContext $context
346 public function isKnownEmpty( ResourceLoaderContext
$context ) {
347 $revisions = $this->getTitleInfo( $context );
349 // If a module has dependencies it cannot be empty. An empty array will be cast to false
350 if ( $this->getDependencies() ) {
353 // For user modules, don't needlessly load if there are no non-empty pages
354 if ( $this->getGroup() === 'user' ) {
355 foreach ( $revisions as $revision ) {
356 if ( $revision['page_len'] > 0 ) {
357 // At least one non-empty page, module should be loaded
364 // T70488: For other modules (i.e. ones that are called in cached html output) only check
365 // page existance. This ensures that, if some pages in a module are temporarily blanked,
366 // we don't end omit the module's script or link tag on some pages.
367 return count( $revisions ) === 0;
370 private function setTitleInfo( $batchKey, array $titleInfo ) {
371 $this->titleInfo
[$batchKey] = $titleInfo;
374 private static function makeTitleKey( LinkTarget
$title ) {
375 // Used for keys in titleInfo.
376 return "{$title->getNamespace()}:{$title->getDBkey()}";
380 * Get the information about the wiki pages for a given context.
381 * @param ResourceLoaderContext $context
382 * @return array Keyed by page name
384 protected function getTitleInfo( ResourceLoaderContext
$context ) {
385 $dbr = $this->getDB();
387 $pageNames = array_keys( $this->getPages( $context ) );
389 $batchKey = implode( '|', $pageNames );
390 if ( !isset( $this->titleInfo
[$batchKey] ) ) {
391 $this->titleInfo
[$batchKey] = static::fetchTitleInfo( $dbr, $pageNames, __METHOD__
);
394 $titleInfo = $this->titleInfo
[$batchKey];
396 // Override the title info from the overrides, if any
397 $overrideCallback = $context->getContentOverrideCallback();
398 if ( $overrideCallback ) {
399 foreach ( $pageNames as $page ) {
400 $title = Title
::newFromText( $page );
401 $content = $title ?
call_user_func( $overrideCallback, $title ) : null;
402 if ( $content !== null ) {
403 $titleInfo[$title->getPrefixedText()] = [
404 'page_len' => $content->getSize(),
405 'page_latest' => 'TBD', // None available
406 'page_touched' => wfTimestamp( TS_MW
),
416 protected static function fetchTitleInfo( IDatabase
$db, array $pages, $fname = __METHOD__
) {
418 $batch = new LinkBatch
;
419 foreach ( $pages as $titleText ) {
420 $title = Title
::newFromText( $titleText );
422 // Page name may be invalid if user-provided (e.g. gadgets)
423 $batch->addObj( $title );
426 if ( !$batch->isEmpty() ) {
427 $res = $db->select( 'page',
428 // Include page_touched to allow purging if cache is poisoned (T117587, T113916)
429 [ 'page_namespace', 'page_title', 'page_touched', 'page_len', 'page_latest' ],
430 $batch->constructSet( 'page', $db ),
433 foreach ( $res as $row ) {
434 // Avoid including ids or timestamps of revision/page tables so
435 // that versions are not wasted
436 $title = new TitleValue( (int)$row->page_namespace
, $row->page_title
);
437 $titleInfo[self
::makeTitleKey( $title )] = [
438 'page_len' => $row->page_len
,
439 'page_latest' => $row->page_latest
,
440 'page_touched' => $row->page_touched
,
449 * @param ResourceLoaderContext $context
450 * @param IDatabase $db
451 * @param string[] $moduleNames
453 public static function preloadTitleInfo(
454 ResourceLoaderContext
$context, IDatabase
$db, array $moduleNames
456 $rl = $context->getResourceLoader();
457 // getDB() can be overridden to point to a foreign database.
458 // For now, only preload local. In the future, we could preload by wikiID.
460 /** @var ResourceLoaderWikiModule[] $wikiModules */
462 foreach ( $moduleNames as $name ) {
463 $module = $rl->getModule( $name );
464 if ( $module instanceof self
) {
465 $mDB = $module->getDB();
466 // Subclasses may implement getDB differently
467 if ( $mDB->getDomainID() === $db->getDomainID() ) {
468 $wikiModules[] = $module;
469 $allPages +
= $module->getPages( $context );
474 if ( !$wikiModules ) {
475 // Nothing to preload
479 $pageNames = array_keys( $allPages );
481 $hash = sha1( implode( '|', $pageNames ) );
483 // Avoid Zend bug where "static::" does not apply LSB in the closure
484 $func = [ static::class, 'fetchTitleInfo' ];
487 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
488 $allInfo = $cache->getWithSetCallback(
489 $cache->makeGlobalKey( 'resourceloader-titleinfo', $db->getDomainID(), $hash ),
491 function ( $curVal, &$ttl, array &$setOpts ) use ( $func, $pageNames, $db, $fname ) {
492 $setOpts +
= Database
::getCacheSetOptions( $db );
494 return call_user_func( $func, $db, $pageNames, $fname );
498 $cache->makeGlobalKey( 'resourceloader-titleinfo', $db->getDomainID() ) ]
502 foreach ( $wikiModules as $wikiModule ) {
503 $pages = $wikiModule->getPages( $context );
504 // Before we intersect, map the names to canonical form (T145673).
506 foreach ( $pages as $pageName => $unused ) {
507 $title = Title
::newFromText( $pageName );
509 $intersect[ self
::makeTitleKey( $title ) ] = 1;
511 // Page name may be invalid if user-provided (e.g. gadgets)
512 $rl->getLogger()->info(
513 'Invalid wiki page title "{title}" in ' . __METHOD__
,
514 [ 'title' => $pageName ]
518 $info = array_intersect_key( $allInfo, $intersect );
519 $pageNames = array_keys( $pages );
521 $batchKey = implode( '|', $pageNames );
522 $wikiModule->setTitleInfo( $batchKey, $info );
527 * Clear the preloadTitleInfo() cache for all wiki modules on this wiki on
528 * page change if it was a JS or CSS page
530 * @param Title $title
531 * @param Revision|null $old Prior page revision
532 * @param Revision|null $new New page revision
533 * @param string $domain Database domain ID
536 public static function invalidateModuleCache(
537 Title
$title, Revision
$old = null, Revision
$new = null, $domain
539 static $formats = [ CONTENT_FORMAT_CSS
, CONTENT_FORMAT_JAVASCRIPT
];
541 Assert
::parameterType( 'string', $domain, '$domain' );
543 // TODO: MCR: differentiate between page functionality and content model!
544 // Not all pages containing CSS or JS have to be modules! [PageType]
545 if ( $old && in_array( $old->getContentFormat(), $formats ) ) {
547 } elseif ( $new && in_array( $new->getContentFormat(), $formats ) ) {
550 $purge = ( $title->isSiteConfigPage() ||
$title->isUserConfigPage() );
554 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
555 $key = $cache->makeGlobalKey( 'resourceloader-titleinfo', $domain );
556 $cache->touchCheckKey( $key );
564 public function getType() {
565 // Check both because subclasses don't always pass pages via the constructor,
566 // they may also override getPages() instead, in which case we should keep
567 // defaulting to LOAD_GENERAL and allow them to override getType() separately.
568 return ( $this->styles
&& !$this->scripts
) ? self
::LOAD_STYLES
: self
::LOAD_GENERAL
;