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 object used in getTitleInfo().
149 * Defaults to the local replica DB. Subclasses may want to override this to return a foreign
150 * database object, or null if getTitleInfo() shouldn't access the database.
152 * NOTE: This ONLY works for getTitleInfo() and isKnownEmpty(), NOT FOR ANYTHING ELSE.
153 * In particular, it doesn't work for getContent() or getScript() etc.
155 * @return IDatabase|null
157 protected function getDB() {
158 return wfGetDB( DB_REPLICA
);
162 * @param string $titleText
163 * @param ResourceLoaderContext|null $context (but passing null is deprecated)
164 * @return null|string
165 * @since 1.32 added the $context parameter
167 protected function getContent( $titleText, ResourceLoaderContext
$context = null ) {
168 $title = Title
::newFromText( $titleText );
170 return null; // Bad title
173 $content = $this->getContentObj( $title, $context );
175 return null; // No content found
178 $handler = $content->getContentHandler();
179 if ( $handler->isSupportedFormat( CONTENT_FORMAT_CSS
) ) {
180 $format = CONTENT_FORMAT_CSS
;
181 } elseif ( $handler->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT
) ) {
182 $format = CONTENT_FORMAT_JAVASCRIPT
;
184 return null; // Bad content model
187 return $content->serialize( $format );
191 * @param Title $title
192 * @param ResourceLoaderContext|null $context (but passing null is deprecated)
193 * @param int|null $maxRedirects Maximum number of redirects to follow. If
194 * null, uses $wgMaxRedirects
195 * @return Content|null
196 * @since 1.32 added the $context and $maxRedirects parameters
198 protected function getContentObj(
199 Title
$title, ResourceLoaderContext
$context = null, $maxRedirects = null
201 if ( $context === null ) {
202 wfDeprecated( __METHOD__
. ' without a ResourceLoader context', '1.32' );
205 $overrideCallback = $context ?
$context->getContentOverrideCallback() : null;
206 $content = $overrideCallback ?
call_user_func( $overrideCallback, $title ) : null;
208 if ( !$content instanceof Content
) {
209 $this->getLogger()->error(
210 'Bad content override for "{title}" in ' . __METHOD__
,
211 [ 'title' => $title->getPrefixedText() ]
216 $revision = Revision
::newKnownCurrent( wfGetDB( DB_REPLICA
), $title );
220 $content = $revision->getContent( Revision
::RAW
);
223 $this->getLogger()->error(
224 'Failed to load content of JS/CSS page "{title}" in ' . __METHOD__
,
225 [ 'title' => $title->getPrefixedText() ]
231 if ( $content && $content->isRedirect() ) {
232 if ( $maxRedirects === null ) {
233 $maxRedirects = $this->getConfig()->get( 'MaxRedirects' ) ?
: 0;
235 if ( $maxRedirects > 0 ) {
236 $newTitle = $content->getRedirectTarget();
237 return $newTitle ?
$this->getContentObj( $newTitle, $context, $maxRedirects - 1 ) : null;
245 * @param ResourceLoaderContext $context
248 public function shouldEmbedModule( ResourceLoaderContext
$context ) {
249 $overrideCallback = $context->getContentOverrideCallback();
250 if ( $overrideCallback && $this->getSource() === 'local' ) {
251 foreach ( $this->getPages( $context ) as $page => $info ) {
252 $title = Title
::newFromText( $page );
253 if ( $title && call_user_func( $overrideCallback, $title ) !== null ) {
259 return parent
::shouldEmbedModule( $context );
263 * @param ResourceLoaderContext $context
264 * @return string JavaScript code
266 public function getScript( ResourceLoaderContext
$context ) {
268 foreach ( $this->getPages( $context ) as $titleText => $options ) {
269 if ( $options['type'] !== 'script' ) {
272 $script = $this->getContent( $titleText, $context );
273 if ( strval( $script ) !== '' ) {
274 $script = $this->validateScriptFile( $titleText, $script );
275 $scripts .= ResourceLoader
::makeComment( $titleText ) . $script . "\n";
282 * @param ResourceLoaderContext $context
285 public function getStyles( ResourceLoaderContext
$context ) {
287 foreach ( $this->getPages( $context ) as $titleText => $options ) {
288 if ( $options['type'] !== 'style' ) {
291 $media = $options['media'] ??
'all';
292 $style = $this->getContent( $titleText, $context );
293 if ( strval( $style ) === '' ) {
296 if ( $this->getFlip( $context ) ) {
297 $style = CSSJanus
::transform( $style, true, false );
299 $style = MemoizedCallable
::call( 'CSSMin::remap',
300 [ $style, false, $this->getConfig()->get( 'ScriptPath' ), true ] );
301 if ( !isset( $styles[$media] ) ) {
302 $styles[$media] = [];
304 $style = ResourceLoader
::makeComment( $titleText ) . $style;
305 $styles[$media][] = $style;
311 * Disable module content versioning.
313 * This class does not support generating content outside of a module
314 * request due to foreign database support.
316 * See getDefinitionSummary() for meta-data versioning.
320 public function enableModuleContentVersion() {
325 * @param ResourceLoaderContext $context
328 public function getDefinitionSummary( ResourceLoaderContext
$context ) {
329 $summary = parent
::getDefinitionSummary( $context );
331 'pages' => $this->getPages( $context ),
332 // Includes meta data of current revisions
333 'titleInfo' => $this->getTitleInfo( $context ),
339 * @param ResourceLoaderContext $context
342 public function isKnownEmpty( ResourceLoaderContext
$context ) {
343 $revisions = $this->getTitleInfo( $context );
345 // If a module has dependencies it cannot be empty. An empty array will be cast to false
346 if ( $this->getDependencies() ) {
349 // For user modules, don't needlessly load if there are no non-empty pages
350 if ( $this->getGroup() === 'user' ) {
351 foreach ( $revisions as $revision ) {
352 if ( $revision['page_len'] > 0 ) {
353 // At least one non-empty page, module should be loaded
360 // T70488: For other modules (i.e. ones that are called in cached html output) only check
361 // page existance. This ensures that, if some pages in a module are temporarily blanked,
362 // we don't end omit the module's script or link tag on some pages.
363 return count( $revisions ) === 0;
366 private function setTitleInfo( $batchKey, array $titleInfo ) {
367 $this->titleInfo
[$batchKey] = $titleInfo;
370 private static function makeTitleKey( LinkTarget
$title ) {
371 // Used for keys in titleInfo.
372 return "{$title->getNamespace()}:{$title->getDBkey()}";
376 * Get the information about the wiki pages for a given context.
377 * @param ResourceLoaderContext $context
378 * @return array Keyed by page name
380 protected function getTitleInfo( ResourceLoaderContext
$context ) {
381 $dbr = $this->getDB();
383 // We're dealing with a subclass that doesn't have a DB
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
),
415 protected static function fetchTitleInfo( IDatabase
$db, array $pages, $fname = __METHOD__
) {
417 $batch = new LinkBatch
;
418 foreach ( $pages as $titleText ) {
419 $title = Title
::newFromText( $titleText );
421 // Page name may be invalid if user-provided (e.g. gadgets)
422 $batch->addObj( $title );
425 if ( !$batch->isEmpty() ) {
426 $res = $db->select( 'page',
427 // Include page_touched to allow purging if cache is poisoned (T117587, T113916)
428 [ 'page_namespace', 'page_title', 'page_touched', 'page_len', 'page_latest' ],
429 $batch->constructSet( 'page', $db ),
432 foreach ( $res as $row ) {
433 // Avoid including ids or timestamps of revision/page tables so
434 // that versions are not wasted
435 $title = new TitleValue( (int)$row->page_namespace
, $row->page_title
);
436 $titleInfo[self
::makeTitleKey( $title )] = [
437 'page_len' => $row->page_len
,
438 'page_latest' => $row->page_latest
,
439 'page_touched' => $row->page_touched
,
448 * @param ResourceLoaderContext $context
449 * @param IDatabase $db
450 * @param string[] $moduleNames
452 public static function preloadTitleInfo(
453 ResourceLoaderContext
$context, IDatabase
$db, array $moduleNames
455 $rl = $context->getResourceLoader();
456 // getDB() can be overridden to point to a foreign database.
457 // For now, only preload local. In the future, we could preload by wikiID.
459 /** @var ResourceLoaderWikiModule[] $wikiModules */
461 foreach ( $moduleNames as $name ) {
462 $module = $rl->getModule( $name );
463 if ( $module instanceof self
) {
464 $mDB = $module->getDB();
465 // Subclasses may disable getDB and implement getTitleInfo differently
466 if ( $mDB && $mDB->getDomainID() === $db->getDomainID() ) {
467 $wikiModules[] = $module;
468 $allPages +
= $module->getPages( $context );
473 if ( !$wikiModules ) {
474 // Nothing to preload
478 $pageNames = array_keys( $allPages );
480 $hash = sha1( implode( '|', $pageNames ) );
482 // Avoid Zend bug where "static::" does not apply LSB in the closure
483 $func = [ static::class, 'fetchTitleInfo' ];
486 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
487 $allInfo = $cache->getWithSetCallback(
488 $cache->makeGlobalKey( 'resourceloader', 'titleinfo', $db->getDomainID(), $hash ),
490 function ( $curVal, &$ttl, array &$setOpts ) use ( $func, $pageNames, $db, $fname ) {
491 $setOpts +
= Database
::getCacheSetOptions( $db );
493 return call_user_func( $func, $db, $pageNames, $fname );
497 $cache->makeGlobalKey( 'resourceloader', 'titleinfo', $db->getDomainID() ) ]
501 foreach ( $wikiModules as $wikiModule ) {
502 $pages = $wikiModule->getPages( $context );
503 // Before we intersect, map the names to canonical form (T145673).
505 foreach ( $pages as $pageName => $unused ) {
506 $title = Title
::newFromText( $pageName );
508 $intersect[ self
::makeTitleKey( $title ) ] = 1;
510 // Page name may be invalid if user-provided (e.g. gadgets)
511 $rl->getLogger()->info(
512 'Invalid wiki page title "{title}" in ' . __METHOD__
,
513 [ 'title' => $pageName ]
517 $info = array_intersect_key( $allInfo, $intersect );
518 $pageNames = array_keys( $pages );
520 $batchKey = implode( '|', $pageNames );
521 $wikiModule->setTitleInfo( $batchKey, $info );
526 * Clear the preloadTitleInfo() cache for all wiki modules on this wiki on
527 * page change if it was a JS or CSS page
529 * @param Title $title
530 * @param Revision|null $old Prior page revision
531 * @param Revision|null $new New page revision
532 * @param string $domain Database domain ID
535 public static function invalidateModuleCache(
536 Title
$title, Revision
$old = null, Revision
$new = null, $domain
538 static $formats = [ CONTENT_FORMAT_CSS
, CONTENT_FORMAT_JAVASCRIPT
];
540 Assert
::parameterType( 'string', $domain, '$domain' );
542 // TODO: MCR: differentiate between page functionality and content model!
543 // Not all pages containing CSS or JS have to be modules! [PageType]
544 if ( $old && in_array( $old->getContentFormat(), $formats ) ) {
546 } elseif ( $new && in_array( $new->getContentFormat(), $formats ) ) {
549 $purge = ( $title->isSiteConfigPage() ||
$title->isUserConfigPage() );
553 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
554 $key = $cache->makeGlobalKey( 'resourceloader', 'titleinfo', $domain );
555 $cache->touchCheckKey( $key );
563 public function getType() {
564 // Check both because subclasses don't always pass pages via the constructor,
565 // they may also override getPages() instead, in which case we should keep
566 // defaulting to LOAD_GENERAL and allow them to override getType() separately.
567 return ( $this->styles
&& !$this->scripts
) ? self
::LOAD_STYLES
: self
::LOAD_GENERAL
;