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 Wikimedia\Assert\Assert
;
27 use Wikimedia\Rdbms\Database
;
28 use Wikimedia\Rdbms\IDatabase
;
29 use MediaWiki\MediaWikiServices
;
32 * Abstraction for ResourceLoader modules which pull from wiki pages
34 * This can only be used for wiki pages in the MediaWiki and User namespaces,
35 * because of its dependence on the functionality of Title::isUserConfigPage()
36 * and Title::isSiteConfigPage().
38 * This module supports being used as a placeholder for a module on a remote wiki.
39 * To do so, getDB() must be overloaded to return a foreign database object that
40 * allows local wikis to query page metadata.
42 * Safe for calls on local wikis are:
46 * - Basic methods that strictly involve the foreign database
51 class ResourceLoaderWikiModule
extends ResourceLoaderModule
{
53 // Origin defaults to users with sitewide authority
54 protected $origin = self
::ORIGIN_USER_SITEWIDE
;
56 // In-process cache for title info, structured as an array
58 // <batchKey> // Pipe-separated list of sorted keys from getPages
60 // <titleKey> => [ // Normalised title key
62 // 'page_latest' => ..,
63 // 'page_touched' => ..,
67 // @see self::fetchTitleInfo()
68 // @see self::makeTitleKey()
69 protected $titleInfo = [];
71 // List of page names that contain CSS
72 protected $styles = [];
74 // List of page names that contain JavaScript
75 protected $scripts = [];
81 * @param array|null $options For back-compat, this can be omitted in favour of overwriting
84 public function __construct( array $options = null ) {
85 if ( $options === null ) {
89 foreach ( $options as $member => $option ) {
95 $this->{$member} = $option;
102 * Subclasses should return an associative array of resources in the module.
103 * Keys should be the title of a page in the MediaWiki or User namespace.
105 * Values should be a nested array of options. The supported keys are 'type' and
106 * (CSS only) 'media'.
108 * For scripts, 'type' should be 'script'.
110 * For stylesheets, 'type' should be 'style'.
111 * There is an optional media key, the value of which can be the
112 * medium ('screen', 'print', etc.) of the stylesheet.
114 * @param ResourceLoaderContext $context
117 protected function getPages( ResourceLoaderContext
$context ) {
118 $config = $this->getConfig();
121 // Filter out pages from origins not allowed by the current wiki configuration.
122 if ( $config->get( 'UseSiteJs' ) ) {
123 foreach ( $this->scripts
as $script ) {
124 $pages[$script] = [ 'type' => 'script' ];
128 if ( $config->get( 'UseSiteCss' ) ) {
129 foreach ( $this->styles
as $style ) {
130 $pages[$style] = [ 'type' => 'style' ];
142 public function getGroup() {
147 * Get the Database handle used for computing the module version.
149 * Subclasses may override this to return a foreign database, which would
150 * allow them to register a module on wiki A that fetches wiki pages from
153 * The way this works is that the local module is a placeholder that can
154 * only computer a module version hash. The 'source' of the module must
155 * be set to the foreign wiki directly. Methods getScript() and getContent()
156 * will not use this handle and are not valid on the local wiki.
160 protected function getDB() {
161 return wfGetDB( DB_REPLICA
);
165 * @param string $titleText
166 * @param ResourceLoaderContext|null $context (but passing null is deprecated)
167 * @return null|string
168 * @since 1.32 added the $context parameter
170 protected function getContent( $titleText, ResourceLoaderContext
$context = null ) {
171 $title = Title
::newFromText( $titleText );
173 return null; // Bad title
176 $content = $this->getContentObj( $title, $context );
178 return null; // No content found
181 $handler = $content->getContentHandler();
182 if ( $handler->isSupportedFormat( CONTENT_FORMAT_CSS
) ) {
183 $format = CONTENT_FORMAT_CSS
;
184 } elseif ( $handler->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT
) ) {
185 $format = CONTENT_FORMAT_JAVASCRIPT
;
187 return null; // Bad content model
190 return $content->serialize( $format );
194 * @param Title $title
195 * @param ResourceLoaderContext|null $context (but passing null is deprecated)
196 * @param int|null $maxRedirects Maximum number of redirects to follow. If
197 * null, uses $wgMaxRedirects
198 * @return Content|null
199 * @since 1.32 added the $context and $maxRedirects parameters
201 protected function getContentObj(
202 Title
$title, ResourceLoaderContext
$context = null, $maxRedirects = null
204 if ( $context === null ) {
205 wfDeprecated( __METHOD__
. ' without a ResourceLoader context', '1.32' );
208 $overrideCallback = $context ?
$context->getContentOverrideCallback() : null;
209 $content = $overrideCallback ?
call_user_func( $overrideCallback, $title ) : null;
211 if ( !$content instanceof Content
) {
212 $this->getLogger()->error(
213 'Bad content override for "{title}" in ' . __METHOD__
,
214 [ 'title' => $title->getPrefixedText() ]
219 $revision = Revision
::newKnownCurrent( wfGetDB( DB_REPLICA
), $title );
223 $content = $revision->getContent( Revision
::RAW
);
226 $this->getLogger()->error(
227 'Failed to load content of JS/CSS page "{title}" in ' . __METHOD__
,
228 [ 'title' => $title->getPrefixedText() ]
234 if ( $content && $content->isRedirect() ) {
235 if ( $maxRedirects === null ) {
236 $maxRedirects = $this->getConfig()->get( 'MaxRedirects' ) ?
: 0;
238 if ( $maxRedirects > 0 ) {
239 $newTitle = $content->getRedirectTarget();
240 return $newTitle ?
$this->getContentObj( $newTitle, $context, $maxRedirects - 1 ) : null;
248 * @param ResourceLoaderContext $context
251 public function shouldEmbedModule( ResourceLoaderContext
$context ) {
252 $overrideCallback = $context->getContentOverrideCallback();
253 if ( $overrideCallback && $this->getSource() === 'local' ) {
254 foreach ( $this->getPages( $context ) as $page => $info ) {
255 $title = Title
::newFromText( $page );
256 if ( $title && call_user_func( $overrideCallback, $title ) !== null ) {
262 return parent
::shouldEmbedModule( $context );
266 * @param ResourceLoaderContext $context
267 * @return string JavaScript code
269 public function getScript( ResourceLoaderContext
$context ) {
271 foreach ( $this->getPages( $context ) as $titleText => $options ) {
272 if ( $options['type'] !== 'script' ) {
275 $script = $this->getContent( $titleText, $context );
276 if ( strval( $script ) !== '' ) {
277 $script = $this->validateScriptFile( $titleText, $script );
278 $scripts .= ResourceLoader
::makeComment( $titleText ) . $script . "\n";
285 * @param ResourceLoaderContext $context
288 public function getStyles( ResourceLoaderContext
$context ) {
290 foreach ( $this->getPages( $context ) as $titleText => $options ) {
291 if ( $options['type'] !== 'style' ) {
294 $media = $options['media'] ??
'all';
295 $style = $this->getContent( $titleText, $context );
296 if ( strval( $style ) === '' ) {
299 if ( $this->getFlip( $context ) ) {
300 $style = CSSJanus
::transform( $style, true, false );
302 $style = MemoizedCallable
::call( 'CSSMin::remap',
303 [ $style, false, $this->getConfig()->get( 'ScriptPath' ), true ] );
304 if ( !isset( $styles[$media] ) ) {
305 $styles[$media] = [];
307 $style = ResourceLoader
::makeComment( $titleText ) . $style;
308 $styles[$media][] = $style;
314 * Disable module content versioning.
316 * This class does not support generating content outside of a module
317 * request due to foreign database support.
319 * See getDefinitionSummary() for meta-data versioning.
323 public function enableModuleContentVersion() {
328 * @param ResourceLoaderContext $context
331 public function getDefinitionSummary( ResourceLoaderContext
$context ) {
332 $summary = parent
::getDefinitionSummary( $context );
334 'pages' => $this->getPages( $context ),
335 // Includes meta data of current revisions
336 'titleInfo' => $this->getTitleInfo( $context ),
342 * @param ResourceLoaderContext $context
345 public function isKnownEmpty( ResourceLoaderContext
$context ) {
346 $revisions = $this->getTitleInfo( $context );
348 // If a module has dependencies it cannot be empty. An empty array will be cast to false
349 if ( $this->getDependencies() ) {
352 // For user modules, don't needlessly load if there are no non-empty pages
353 if ( $this->getGroup() === 'user' ) {
354 foreach ( $revisions as $revision ) {
355 if ( $revision['page_len'] > 0 ) {
356 // At least one non-empty page, module should be loaded
363 // T70488: For other modules (i.e. ones that are called in cached html output) only check
364 // page existance. This ensures that, if some pages in a module are temporarily blanked,
365 // we don't end omit the module's script or link tag on some pages.
366 return count( $revisions ) === 0;
369 private function setTitleInfo( $batchKey, array $titleInfo ) {
370 $this->titleInfo
[$batchKey] = $titleInfo;
373 private static function makeTitleKey( LinkTarget
$title ) {
374 // Used for keys in titleInfo.
375 return "{$title->getNamespace()}:{$title->getDBkey()}";
379 * Get the information about the wiki pages for a given context.
380 * @param ResourceLoaderContext $context
381 * @return array Keyed by page name
383 protected function getTitleInfo( ResourceLoaderContext
$context ) {
384 $dbr = $this->getDB();
386 $pageNames = array_keys( $this->getPages( $context ) );
388 $batchKey = implode( '|', $pageNames );
389 if ( !isset( $this->titleInfo
[$batchKey] ) ) {
390 $this->titleInfo
[$batchKey] = static::fetchTitleInfo( $dbr, $pageNames, __METHOD__
);
393 $titleInfo = $this->titleInfo
[$batchKey];
395 // Override the title info from the overrides, if any
396 $overrideCallback = $context->getContentOverrideCallback();
397 if ( $overrideCallback ) {
398 foreach ( $pageNames as $page ) {
399 $title = Title
::newFromText( $page );
400 $content = $title ?
call_user_func( $overrideCallback, $title ) : null;
401 if ( $content !== null ) {
402 $titleInfo[$title->getPrefixedText()] = [
403 'page_len' => $content->getSize(),
404 'page_latest' => 'TBD', // None available
405 'page_touched' => wfTimestamp( TS_MW
),
414 protected static function fetchTitleInfo( IDatabase
$db, array $pages, $fname = __METHOD__
) {
416 $batch = new LinkBatch
;
417 foreach ( $pages as $titleText ) {
418 $title = Title
::newFromText( $titleText );
420 // Page name may be invalid if user-provided (e.g. gadgets)
421 $batch->addObj( $title );
424 if ( !$batch->isEmpty() ) {
425 $res = $db->select( 'page',
426 // Include page_touched to allow purging if cache is poisoned (T117587, T113916)
427 [ 'page_namespace', 'page_title', 'page_touched', 'page_len', 'page_latest' ],
428 $batch->constructSet( 'page', $db ),
431 foreach ( $res as $row ) {
432 // Avoid including ids or timestamps of revision/page tables so
433 // that versions are not wasted
434 $title = new TitleValue( (int)$row->page_namespace
, $row->page_title
);
435 $titleInfo[self
::makeTitleKey( $title )] = [
436 'page_len' => $row->page_len
,
437 'page_latest' => $row->page_latest
,
438 'page_touched' => $row->page_touched
,
447 * @param ResourceLoaderContext $context
448 * @param IDatabase $db
449 * @param string[] $moduleNames
451 public static function preloadTitleInfo(
452 ResourceLoaderContext
$context, IDatabase
$db, array $moduleNames
454 $rl = $context->getResourceLoader();
455 // getDB() can be overridden to point to a foreign database.
456 // For now, only preload local. In the future, we could preload by wikiID.
458 /** @var ResourceLoaderWikiModule[] $wikiModules */
460 foreach ( $moduleNames as $name ) {
461 $module = $rl->getModule( $name );
462 if ( $module instanceof self
) {
463 $mDB = $module->getDB();
464 // Subclasses may implement getDB differently
465 if ( $mDB->getDomainID() === $db->getDomainID() ) {
466 $wikiModules[] = $module;
467 $allPages +
= $module->getPages( $context );
472 if ( !$wikiModules ) {
473 // Nothing to preload
477 $pageNames = array_keys( $allPages );
479 $hash = sha1( implode( '|', $pageNames ) );
481 // Avoid Zend bug where "static::" does not apply LSB in the closure
482 $func = [ static::class, 'fetchTitleInfo' ];
485 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
486 $allInfo = $cache->getWithSetCallback(
487 $cache->makeGlobalKey( 'resourceloader-titleinfo', $db->getDomainID(), $hash ),
489 function ( $curVal, &$ttl, array &$setOpts ) use ( $func, $pageNames, $db, $fname ) {
490 $setOpts +
= Database
::getCacheSetOptions( $db );
492 return call_user_func( $func, $db, $pageNames, $fname );
496 $cache->makeGlobalKey( 'resourceloader-titleinfo', $db->getDomainID() ) ]
500 foreach ( $wikiModules as $wikiModule ) {
501 $pages = $wikiModule->getPages( $context );
502 // Before we intersect, map the names to canonical form (T145673).
504 foreach ( $pages as $pageName => $unused ) {
505 $title = Title
::newFromText( $pageName );
507 $intersect[ self
::makeTitleKey( $title ) ] = 1;
509 // Page name may be invalid if user-provided (e.g. gadgets)
510 $rl->getLogger()->info(
511 'Invalid wiki page title "{title}" in ' . __METHOD__
,
512 [ 'title' => $pageName ]
516 $info = array_intersect_key( $allInfo, $intersect );
517 $pageNames = array_keys( $pages );
519 $batchKey = implode( '|', $pageNames );
520 $wikiModule->setTitleInfo( $batchKey, $info );
525 * Clear the preloadTitleInfo() cache for all wiki modules on this wiki on
526 * page change if it was a JS or CSS page
528 * @param Title $title
529 * @param Revision|null $old Prior page revision
530 * @param Revision|null $new New page revision
531 * @param string $domain Database domain ID
534 public static function invalidateModuleCache(
535 Title
$title, Revision
$old = null, Revision
$new = null, $domain
537 static $formats = [ CONTENT_FORMAT_CSS
, CONTENT_FORMAT_JAVASCRIPT
];
539 Assert
::parameterType( 'string', $domain, '$domain' );
541 // TODO: MCR: differentiate between page functionality and content model!
542 // Not all pages containing CSS or JS have to be modules! [PageType]
543 if ( $old && in_array( $old->getContentFormat(), $formats ) ) {
545 } elseif ( $new && in_array( $new->getContentFormat(), $formats ) ) {
548 $purge = ( $title->isSiteConfigPage() ||
$title->isUserConfigPage() );
552 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
553 $key = $cache->makeGlobalKey( 'resourceloader-titleinfo', $domain );
554 $cache->touchCheckKey( $key );
562 public function getType() {
563 // Check both because subclasses don't always pass pages via the constructor,
564 // they may also override getPages() instead, in which case we should keep
565 // defaulting to LOAD_GENERAL and allow them to override getType() separately.
566 return ( $this->styles
&& !$this->scripts
) ? self
::LOAD_STYLES
: self
::LOAD_GENERAL
;