MediaWiki REL1_34
ResourceLoaderWikiModule.php
Go to the documentation of this file.
1<?php
25use Wikimedia\Assert\Assert;
29
54
55 // Origin defaults to users with sitewide authority
56 protected $origin = self::ORIGIN_USER_SITEWIDE;
57
58 // In-process cache for title info, structured as an array
59 // [
60 // <batchKey> // Pipe-separated list of sorted keys from getPages
61 // => [
62 // <titleKey> => [ // Normalised title key
63 // 'page_len' => ..,
64 // 'page_latest' => ..,
65 // 'page_touched' => ..,
66 // ]
67 // ]
68 // ]
69 // @see self::fetchTitleInfo()
70 // @see self::makeTitleKey()
71 protected $titleInfo = [];
72
73 // List of page names that contain CSS
74 protected $styles = [];
75
76 // List of page names that contain JavaScript
77 protected $scripts = [];
78
79 // Group of module
80 protected $group;
81
86 public function __construct( array $options = null ) {
87 if ( $options === null ) {
88 return;
89 }
90
91 foreach ( $options as $member => $option ) {
92 switch ( $member ) {
93 case 'styles':
94 case 'scripts':
95 case 'group':
96 case 'targets':
97 $this->{$member} = $option;
98 break;
99 }
100 }
101 }
102
120 $config = $this->getConfig();
121 $pages = [];
122
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' ];
127 }
128 }
129
130 if ( $config->get( 'UseSiteCss' ) ) {
131 foreach ( $this->styles as $style ) {
132 $pages[$style] = [ 'type' => 'style' ];
133 }
134 }
135
136 return $pages;
137 }
138
144 public function getGroup() {
145 return $this->group;
146 }
147
162 protected function getDB() {
163 return wfGetDB( DB_REPLICA );
164 }
165
172 protected function getContent( $titleText, ResourceLoaderContext $context ) {
173 $title = Title::newFromText( $titleText );
174 if ( !$title ) {
175 return null; // Bad title
176 }
177
179 if ( !$content ) {
180 return null; // No content found
181 }
182
183 $handler = $content->getContentHandler();
184 if ( $handler->isSupportedFormat( CONTENT_FORMAT_CSS ) ) {
185 $format = CONTENT_FORMAT_CSS;
186 } elseif ( $handler->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT ) ) {
188 } else {
189 return null; // Bad content model
190 }
191
192 return $content->serialize( $format );
193 }
194
203 protected function getContentObj(
204 Title $title, ResourceLoaderContext $context, $maxRedirects = null
205 ) {
206 $overrideCallback = $context->getContentOverrideCallback();
207 $content = $overrideCallback ? call_user_func( $overrideCallback, $title ) : null;
208 if ( $content ) {
209 if ( !$content instanceof Content ) {
210 $this->getLogger()->error(
211 'Bad content override for "{title}" in ' . __METHOD__,
212 [ 'title' => $title->getPrefixedText() ]
213 );
214 return null;
215 }
216 } else {
217 $revision = Revision::newKnownCurrent( wfGetDB( DB_REPLICA ), $title );
218 if ( !$revision ) {
219 return null;
220 }
221 $content = $revision->getContent( RevisionRecord::RAW );
222
223 if ( !$content ) {
224 $this->getLogger()->error(
225 'Failed to load content of JS/CSS page "{title}" in ' . __METHOD__,
226 [ 'title' => $title->getPrefixedText() ]
227 );
228 return null;
229 }
230 }
231
232 if ( $content && $content->isRedirect() ) {
233 if ( $maxRedirects === null ) {
234 $maxRedirects = $this->getConfig()->get( 'MaxRedirects' ) ?: 0;
235 }
236 if ( $maxRedirects > 0 ) {
237 $newTitle = $content->getRedirectTarget();
238 return $newTitle ? $this->getContentObj( $newTitle, $context, $maxRedirects - 1 ) : null;
239 }
240 }
241
242 return $content;
243 }
244
250 $overrideCallback = $context->getContentOverrideCallback();
251 if ( $overrideCallback && $this->getSource() === 'local' ) {
252 foreach ( $this->getPages( $context ) as $page => $info ) {
253 $title = Title::newFromText( $page );
254 if ( $title && call_user_func( $overrideCallback, $title ) !== null ) {
255 return true;
256 }
257 }
258 }
259
260 return parent::shouldEmbedModule( $context );
261 }
262
268 $scripts = '';
269 foreach ( $this->getPages( $context ) as $titleText => $options ) {
270 if ( $options['type'] !== 'script' ) {
271 continue;
272 }
273 $script = $this->getContent( $titleText, $context );
274 if ( strval( $script ) !== '' ) {
275 $script = $this->validateScriptFile( $titleText, $script );
276 $scripts .= ResourceLoader::makeComment( $titleText ) . $script . "\n";
277 }
278 }
279 return $scripts;
280 }
281
287 $styles = [];
288 foreach ( $this->getPages( $context ) as $titleText => $options ) {
289 if ( $options['type'] !== 'style' ) {
290 continue;
291 }
292 $media = $options['media'] ?? 'all';
293 $style = $this->getContent( $titleText, $context );
294 if ( strval( $style ) === '' ) {
295 continue;
296 }
297 if ( $this->getFlip( $context ) ) {
298 $style = CSSJanus::transform( $style, true, false );
299 }
300 $style = MemoizedCallable::call( 'CSSMin::remap',
301 [ $style, false, $this->getConfig()->get( 'ScriptPath' ), true ] );
302 if ( !isset( $styles[$media] ) ) {
303 $styles[$media] = [];
304 }
305 $style = ResourceLoader::makeComment( $titleText ) . $style;
306 $styles[$media][] = $style;
307 }
308 return $styles;
309 }
310
321 public function enableModuleContentVersion() {
322 return false;
323 }
324
330 $summary = parent::getDefinitionSummary( $context );
331 $summary[] = [
332 'pages' => $this->getPages( $context ),
333 // Includes meta data of current revisions
334 'titleInfo' => $this->getTitleInfo( $context ),
335 ];
336 return $summary;
337 }
338
344 $revisions = $this->getTitleInfo( $context );
345
346 // If a module has dependencies it cannot be empty. An empty array will be cast to false
347 if ( $this->getDependencies() ) {
348 return false;
349 }
350 // For user modules, don't needlessly load if there are no non-empty pages
351 if ( $this->getGroup() === 'user' ) {
352 foreach ( $revisions as $revision ) {
353 if ( $revision['page_len'] > 0 ) {
354 // At least one non-empty page, module should be loaded
355 return false;
356 }
357 }
358 return true;
359 }
360
361 // T70488: For other modules (i.e. ones that are called in cached html output) only check
362 // page existance. This ensures that, if some pages in a module are temporarily blanked,
363 // we don't end omit the module's script or link tag on some pages.
364 return count( $revisions ) === 0;
365 }
366
367 private function setTitleInfo( $batchKey, array $titleInfo ) {
368 $this->titleInfo[$batchKey] = $titleInfo;
369 }
370
371 private static function makeTitleKey( LinkTarget $title ) {
372 // Used for keys in titleInfo.
373 return "{$title->getNamespace()}:{$title->getDBkey()}";
374 }
375
382 $dbr = $this->getDB();
383
384 $pageNames = array_keys( $this->getPages( $context ) );
385 sort( $pageNames );
386 $batchKey = implode( '|', $pageNames );
387 if ( !isset( $this->titleInfo[$batchKey] ) ) {
388 $this->titleInfo[$batchKey] = static::fetchTitleInfo( $dbr, $pageNames, __METHOD__ );
389 }
390
391 $titleInfo = $this->titleInfo[$batchKey];
392
393 // Override the title info from the overrides, if any
394 $overrideCallback = $context->getContentOverrideCallback();
395 if ( $overrideCallback ) {
396 foreach ( $pageNames as $page ) {
397 $title = Title::newFromText( $page );
398 $content = $title ? call_user_func( $overrideCallback, $title ) : null;
399 if ( $content !== null ) {
400 $titleInfo[$title->getPrefixedText()] = [
401 'page_len' => $content->getSize(),
402 'page_latest' => 'TBD', // None available
403 'page_touched' => wfTimestamp( TS_MW ),
404 ];
405 }
406 }
407 }
408
409 return $titleInfo;
410 }
411
413 protected static function fetchTitleInfo( IDatabase $db, array $pages, $fname = __METHOD__ ) {
414 $titleInfo = [];
415 $batch = new LinkBatch;
416 foreach ( $pages as $titleText ) {
417 $title = Title::newFromText( $titleText );
418 if ( $title ) {
419 // Page name may be invalid if user-provided (e.g. gadgets)
420 $batch->addObj( $title );
421 }
422 }
423 if ( !$batch->isEmpty() ) {
424 $res = $db->select( 'page',
425 // Include page_touched to allow purging if cache is poisoned (T117587, T113916)
426 [ 'page_namespace', 'page_title', 'page_touched', 'page_len', 'page_latest' ],
427 $batch->constructSet( 'page', $db ),
428 $fname
429 );
430 foreach ( $res as $row ) {
431 // Avoid including ids or timestamps of revision/page tables so
432 // that versions are not wasted
433 $title = new TitleValue( (int)$row->page_namespace, $row->page_title );
434 $titleInfo[self::makeTitleKey( $title )] = [
435 'page_len' => $row->page_len,
436 'page_latest' => $row->page_latest,
437 'page_touched' => $row->page_touched,
438 ];
439 }
440 }
441 return $titleInfo;
442 }
443
450 public static function preloadTitleInfo(
451 ResourceLoaderContext $context, IDatabase $db, array $moduleNames
452 ) {
453 $rl = $context->getResourceLoader();
454 // getDB() can be overridden to point to a foreign database.
455 // For now, only preload local. In the future, we could preload by wikiID.
456 $allPages = [];
458 $wikiModules = [];
459 foreach ( $moduleNames as $name ) {
460 $module = $rl->getModule( $name );
461 if ( $module instanceof self ) {
462 $mDB = $module->getDB();
463 // Subclasses may implement getDB differently
464 if ( $mDB->getDomainID() === $db->getDomainID() ) {
465 $wikiModules[] = $module;
466 $allPages += $module->getPages( $context );
467 }
468 }
469 }
470
471 if ( !$wikiModules ) {
472 // Nothing to preload
473 return;
474 }
475
476 $pageNames = array_keys( $allPages );
477 sort( $pageNames );
478 $hash = sha1( implode( '|', $pageNames ) );
479
480 // Avoid Zend bug where "static::" does not apply LSB in the closure
481 $func = [ static::class, 'fetchTitleInfo' ];
482 $fname = __METHOD__;
483
484 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
485 $allInfo = $cache->getWithSetCallback(
486 $cache->makeGlobalKey( 'resourceloader-titleinfo', $db->getDomainID(), $hash ),
487 $cache::TTL_HOUR,
488 function ( $curVal, &$ttl, array &$setOpts ) use ( $func, $pageNames, $db, $fname ) {
489 $setOpts += Database::getCacheSetOptions( $db );
490
491 return call_user_func( $func, $db, $pageNames, $fname );
492 },
493 [
494 'checkKeys' => [
495 $cache->makeGlobalKey( 'resourceloader-titleinfo', $db->getDomainID() ) ]
496 ]
497 );
498
499 foreach ( $wikiModules as $wikiModule ) {
500 $pages = $wikiModule->getPages( $context );
501 // Before we intersect, map the names to canonical form (T145673).
502 $intersect = [];
503 foreach ( $pages as $pageName => $unused ) {
504 $title = Title::newFromText( $pageName );
505 if ( $title ) {
506 $intersect[ self::makeTitleKey( $title ) ] = 1;
507 } else {
508 // Page name may be invalid if user-provided (e.g. gadgets)
509 $rl->getLogger()->info(
510 'Invalid wiki page title "{title}" in ' . __METHOD__,
511 [ 'title' => $pageName ]
512 );
513 }
514 }
515 $info = array_intersect_key( $allInfo, $intersect );
516 $pageNames = array_keys( $pages );
517 sort( $pageNames );
518 $batchKey = implode( '|', $pageNames );
519 $wikiModule->setTitleInfo( $batchKey, $info );
520 }
521 }
522
533 public static function invalidateModuleCache(
534 Title $title, Revision $old = null, Revision $new = null, $domain
535 ) {
536 static $formats = [ CONTENT_FORMAT_CSS, CONTENT_FORMAT_JAVASCRIPT ];
537
538 Assert::parameterType( 'string', $domain, '$domain' );
539
540 // TODO: MCR: differentiate between page functionality and content model!
541 // Not all pages containing CSS or JS have to be modules! [PageType]
542 if ( $old && in_array( $old->getContentFormat(), $formats ) ) {
543 $purge = true;
544 } elseif ( $new && in_array( $new->getContentFormat(), $formats ) ) {
545 $purge = true;
546 } else {
547 $purge = ( $title->isSiteConfigPage() || $title->isUserConfigPage() );
548 }
549
550 if ( $purge ) {
551 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
552 $key = $cache->makeGlobalKey( 'resourceloader-titleinfo', $domain );
553 $cache->touchCheckKey( $key );
554 }
555 }
556
561 public function getType() {
562 // Check both because subclasses don't always pass pages via the constructor,
563 // they may also override getPages() instead, in which case we should keep
564 // defaulting to LOAD_GENERAL and allow them to override getType() separately.
565 return ( $this->styles && !$this->scripts ) ? self::LOAD_STYLES : self::LOAD_GENERAL;
566 }
567}
getDB()
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition LinkBatch.php:34
MediaWikiServices is the service locator for the application scope of MediaWiki.
Page revision base class.
static call( $callable, array $args=[], $ttl=3600)
Shortcut method for creating a MemoizedCallable and invoking it with the specified arguments.
Context object that contains information about the state of a specific ResourceLoader web request.
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
getDependencies(ResourceLoaderContext $context=null)
Get a list of modules this module depends on.
validateScriptFile( $fileName, $contents)
Validate a given script file; if valid returns the original source.
getFlip(ResourceLoaderContext $context)
string null $name
Module name.
getSource()
Get the source of this module.
Abstraction for ResourceLoader modules which pull from wiki pages.
getTitleInfo(ResourceLoaderContext $context)
Get the information about the wiki pages for a given context.
getDB()
Get the Database handle used for computing the module version.
static preloadTitleInfo(ResourceLoaderContext $context, IDatabase $db, array $moduleNames)
enableModuleContentVersion()
Disable module content versioning.
getPages(ResourceLoaderContext $context)
Subclasses should return an associative array of resources in the module.
shouldEmbedModule(ResourceLoaderContext $context)
static fetchTitleInfo(IDatabase $db, array $pages, $fname=__METHOD__)
static makeTitleKey(LinkTarget $title)
getDefinitionSummary(ResourceLoaderContext $context)
setTitleInfo( $batchKey, array $titleInfo)
getStyles(ResourceLoaderContext $context)
isKnownEmpty(ResourceLoaderContext $context)
getContent( $titleText, ResourceLoaderContext $context)
getContentObj(Title $title, ResourceLoaderContext $context, $maxRedirects=null)
getScript(ResourceLoaderContext $context)
static invalidateModuleCache(Title $title, Revision $old=null, Revision $new=null, $domain)
Clear the preloadTitleInfo() cache for all wiki modules on this wiki on page change if it was a JS or...
Represents a page (or page fragment) title within MediaWiki.
Represents a title within MediaWiki.
Definition Title.php:42
Relational database abstraction object.
Definition Database.php:49
const CONTENT_FORMAT_JAVASCRIPT
Definition Defines.php:241
const CONTENT_FORMAT_CSS
Definition Defines.php:243
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
Base interface for content objects.
Definition Content.php:34
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
getDomainID()
Return the currently selected domain ID.
select( $table, $vars, $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Execute a SELECT query constructed using the various parameters provided.
$context
Definition load.php:45
$cache
Definition mcc.php:33
const DB_REPLICA
Definition defines.php:25
$content
Definition router.php:78