MediaWiki  master
ResourceLoaderWikiModule.php
Go to the documentation of this file.
1 <?php
27 use Wikimedia\Assert\Assert;
30 use Wikimedia\Timestamp\ConvertibleTimestamp;
31 
57  protected $origin = self::ORIGIN_USER_SITEWIDE;
58 
75  protected $titleInfo = [];
76 
78  protected $styles = [];
79 
81  protected $scripts = [];
82 
84  protected $group;
85 
90  public function __construct( array $options = null ) {
91  if ( $options === null ) {
92  return;
93  }
94 
95  foreach ( $options as $member => $option ) {
96  switch ( $member ) {
97  case 'styles':
98  case 'scripts':
99  case 'group':
100  case 'targets':
101  $this->{$member} = $option;
102  break;
103  }
104  }
105  }
106 
124  protected function getPages( ResourceLoaderContext $context ) {
125  $config = $this->getConfig();
126  $pages = [];
127 
128  // Filter out pages from origins not allowed by the current wiki configuration.
129  if ( $config->get( 'UseSiteJs' ) ) {
130  foreach ( $this->scripts as $script ) {
131  $pages[$script] = [ 'type' => 'script' ];
132  }
133  }
134 
135  if ( $config->get( 'UseSiteCss' ) ) {
136  foreach ( $this->styles as $style ) {
137  $pages[$style] = [ 'type' => 'style' ];
138  }
139  }
140 
141  return $pages;
142  }
143 
149  public function getGroup() {
150  return $this->group;
151  }
152 
167  protected function getDB() {
168  return wfGetDB( DB_REPLICA );
169  }
170 
177  protected function getContent( $titleText, ResourceLoaderContext $context ) {
178  $title = Title::newFromText( $titleText );
179  if ( !$title ) {
180  return null; // Bad title
181  }
182 
183  $content = $this->getContentObj( $title, $context );
184  if ( !$content ) {
185  return null; // No content found
186  }
187 
188  $handler = $content->getContentHandler();
189  if ( $handler->isSupportedFormat( CONTENT_FORMAT_CSS ) ) {
190  $format = CONTENT_FORMAT_CSS;
191  } elseif ( $handler->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT ) ) {
192  $format = CONTENT_FORMAT_JAVASCRIPT;
193  } else {
194  return null; // Bad content model
195  }
196 
197  return $content->serialize( $format );
198  }
199 
208  protected function getContentObj(
209  Title $title, ResourceLoaderContext $context, $maxRedirects = null
210  ) {
211  $overrideCallback = $context->getContentOverrideCallback();
212  $content = $overrideCallback ? call_user_func( $overrideCallback, $title ) : null;
213  if ( $content ) {
214  if ( !$content instanceof Content ) {
215  $this->getLogger()->error(
216  'Bad content override for "{title}" in ' . __METHOD__,
217  [ 'title' => $title->getPrefixedText() ]
218  );
219  return null;
220  }
221  } else {
222  $revision = MediaWikiServices::getInstance()
223  ->getRevisionLookup()
224  ->getKnownCurrentRevision( $title );
225  if ( !$revision ) {
226  return null;
227  }
228  $content = $revision->getContent( SlotRecord::MAIN, RevisionRecord::RAW );
229 
230  if ( !$content ) {
231  $this->getLogger()->error(
232  'Failed to load content of JS/CSS page "{title}" in ' . __METHOD__,
233  [ 'title' => $title->getPrefixedText() ]
234  );
235  return null;
236  }
237  }
238 
239  if ( $content->isRedirect() ) {
240  if ( $maxRedirects === null ) {
241  $maxRedirects = $this->getConfig()->get( 'MaxRedirects' ) ?: 0;
242  }
243  if ( $maxRedirects > 0 ) {
244  $newTitle = $content->getRedirectTarget();
245  return $newTitle ? $this->getContentObj( $newTitle, $context, $maxRedirects - 1 ) : null;
246  }
247  }
248 
249  return $content;
250  }
251 
256  public function shouldEmbedModule( ResourceLoaderContext $context ) {
257  $overrideCallback = $context->getContentOverrideCallback();
258  if ( $overrideCallback && $this->getSource() === 'local' ) {
259  foreach ( $this->getPages( $context ) as $page => $info ) {
260  $title = Title::newFromText( $page );
261  if ( $title && call_user_func( $overrideCallback, $title ) !== null ) {
262  return true;
263  }
264  }
265  }
266 
267  return parent::shouldEmbedModule( $context );
268  }
269 
274  public function getScript( ResourceLoaderContext $context ) {
275  $scripts = '';
276  foreach ( $this->getPages( $context ) as $titleText => $options ) {
277  if ( $options['type'] !== 'script' ) {
278  continue;
279  }
280  $script = $this->getContent( $titleText, $context );
281  if ( strval( $script ) !== '' ) {
282  $script = $this->validateScriptFile( $titleText, $script );
283  $scripts .= ResourceLoader::makeComment( $titleText ) . $script . "\n";
284  }
285  }
286  return $scripts;
287  }
288 
293  public function getStyles( ResourceLoaderContext $context ) {
294  $styles = [];
295  foreach ( $this->getPages( $context ) as $titleText => $options ) {
296  if ( $options['type'] !== 'style' ) {
297  continue;
298  }
299  $media = $options['media'] ?? 'all';
300  $style = $this->getContent( $titleText, $context );
301  if ( strval( $style ) === '' ) {
302  continue;
303  }
304  if ( $this->getFlip( $context ) ) {
305  $style = CSSJanus::transform( $style, true, false );
306  }
307  $style = MemoizedCallable::call( 'CSSMin::remap',
308  [ $style, false, $this->getConfig()->get( 'ScriptPath' ), true ] );
309  if ( !isset( $styles[$media] ) ) {
310  $styles[$media] = [];
311  }
312  $style = ResourceLoader::makeComment( $titleText ) . $style;
313  $styles[$media][] = $style;
314  }
315  return $styles;
316  }
317 
328  public function enableModuleContentVersion() {
329  return false;
330  }
331 
336  public function getDefinitionSummary( ResourceLoaderContext $context ) {
337  $summary = parent::getDefinitionSummary( $context );
338  $summary[] = [
339  'pages' => $this->getPages( $context ),
340  // Includes meta data of current revisions
341  'titleInfo' => $this->getTitleInfo( $context ),
342  ];
343  return $summary;
344  }
345 
350  public function isKnownEmpty( ResourceLoaderContext $context ) {
351  $revisions = $this->getTitleInfo( $context );
352 
353  // If a module has dependencies it cannot be empty. An empty array will be cast to false
354  if ( $this->getDependencies() ) {
355  return false;
356  }
357  // For user modules, don't needlessly load if there are no non-empty pages
358  if ( $this->getGroup() === 'user' ) {
359  foreach ( $revisions as $revision ) {
360  if ( $revision['page_len'] > 0 ) {
361  // At least one non-empty page, module should be loaded
362  return false;
363  }
364  }
365  return true;
366  }
367 
368  // T70488: For other modules (i.e. ones that are called in cached html output) only check
369  // page existance. This ensures that, if some pages in a module are temporarily blanked,
370  // we don't end omit the module's script or link tag on some pages.
371  return count( $revisions ) === 0;
372  }
373 
374  private function setTitleInfo( $batchKey, array $titleInfo ) {
375  $this->titleInfo[$batchKey] = $titleInfo;
376  }
377 
378  private static function makeTitleKey( LinkTarget $title ) {
379  // Used for keys in titleInfo.
380  return "{$title->getNamespace()}:{$title->getDBkey()}";
381  }
382 
388  protected function getTitleInfo( ResourceLoaderContext $context ) {
389  $dbr = $this->getDB();
390 
391  $pageNames = array_keys( $this->getPages( $context ) );
392  sort( $pageNames );
393  $batchKey = implode( '|', $pageNames );
394  if ( !isset( $this->titleInfo[$batchKey] ) ) {
395  $this->titleInfo[$batchKey] = static::fetchTitleInfo( $dbr, $pageNames, __METHOD__ );
396  }
397 
398  $titleInfo = $this->titleInfo[$batchKey];
399 
400  // Override the title info from the overrides, if any
401  $overrideCallback = $context->getContentOverrideCallback();
402  if ( $overrideCallback ) {
403  foreach ( $pageNames as $page ) {
404  $title = Title::newFromText( $page );
405  $content = $title ? call_user_func( $overrideCallback, $title ) : null;
406  if ( $content !== null ) {
407  $titleInfo[$title->getPrefixedText()] = [
408  'page_len' => $content->getSize(),
409  'page_latest' => 'TBD', // None available
410  'page_touched' => ConvertibleTimestamp::now( TS_MW ),
411  ];
412  }
413  }
414  }
415 
416  return $titleInfo;
417  }
418 
425  protected static function fetchTitleInfo( IDatabase $db, array $pages, $fname = __METHOD__ ) {
426  $titleInfo = [];
427  $linkBatchFactory = MediaWikiServices::getInstance()->getLinkBatchFactory();
428  $batch = $linkBatchFactory->newLinkBatch();
429  foreach ( $pages as $titleText ) {
430  $title = Title::newFromText( $titleText );
431  if ( $title ) {
432  // Page name may be invalid if user-provided (e.g. gadgets)
433  $batch->addObj( $title );
434  }
435  }
436  if ( !$batch->isEmpty() ) {
437  $res = $db->select( 'page',
438  // Include page_touched to allow purging if cache is poisoned (T117587, T113916)
439  [ 'page_namespace', 'page_title', 'page_touched', 'page_len', 'page_latest' ],
440  $batch->constructSet( 'page', $db ),
441  $fname
442  );
443  foreach ( $res as $row ) {
444  // Avoid including ids or timestamps of revision/page tables so
445  // that versions are not wasted
446  $title = new TitleValue( (int)$row->page_namespace, $row->page_title );
448  'page_len' => $row->page_len,
449  'page_latest' => $row->page_latest,
450  'page_touched' => $row->page_touched,
451  ];
452  }
453  }
454  return $titleInfo;
455  }
456 
463  public static function preloadTitleInfo(
464  ResourceLoaderContext $context, IDatabase $db, array $moduleNames
465  ) {
466  $rl = $context->getResourceLoader();
467  // getDB() can be overridden to point to a foreign database.
468  // For now, only preload local. In the future, we could preload by wikiID.
469  $allPages = [];
471  $wikiModules = [];
472  foreach ( $moduleNames as $name ) {
473  $module = $rl->getModule( $name );
474  if ( $module instanceof self ) {
475  $mDB = $module->getDB();
476  // Subclasses may implement getDB differently
477  if ( $mDB->getDomainID() === $db->getDomainID() ) {
478  $wikiModules[] = $module;
479  $allPages += $module->getPages( $context );
480  }
481  }
482  }
483 
484  if ( !$wikiModules ) {
485  // Nothing to preload
486  return;
487  }
488 
489  $pageNames = array_keys( $allPages );
490  sort( $pageNames );
491  $hash = sha1( implode( '|', $pageNames ) );
492 
493  // Avoid Zend bug where "static::" does not apply LSB in the closure
494  $func = [ static::class, 'fetchTitleInfo' ];
495  $fname = __METHOD__;
496 
497  $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
498  $allInfo = $cache->getWithSetCallback(
499  $cache->makeGlobalKey( 'resourceloader-titleinfo', $db->getDomainID(), $hash ),
500  $cache::TTL_HOUR,
501  function ( $curVal, &$ttl, array &$setOpts ) use ( $func, $pageNames, $db, $fname ) {
502  $setOpts += Database::getCacheSetOptions( $db );
503 
504  return call_user_func( $func, $db, $pageNames, $fname );
505  },
506  [
507  'checkKeys' => [
508  $cache->makeGlobalKey( 'resourceloader-titleinfo', $db->getDomainID() ) ]
509  ]
510  );
511 
512  foreach ( $wikiModules as $wikiModule ) {
513  $pages = $wikiModule->getPages( $context );
514  // Before we intersect, map the names to canonical form (T145673).
515  $intersect = [];
516  foreach ( $pages as $pageName => $unused ) {
517  $title = Title::newFromText( $pageName );
518  if ( $title ) {
519  $intersect[ self::makeTitleKey( $title ) ] = 1;
520  } else {
521  // Page name may be invalid if user-provided (e.g. gadgets)
522  $rl->getLogger()->info(
523  'Invalid wiki page title "{title}" in ' . __METHOD__,
524  [ 'title' => $pageName ]
525  );
526  }
527  }
528  $info = array_intersect_key( $allInfo, $intersect );
529  $pageNames = array_keys( $pages );
530  sort( $pageNames );
531  $batchKey = implode( '|', $pageNames );
532  $wikiModule->setTitleInfo( $batchKey, $info );
533  }
534  }
535 
546  public static function invalidateModuleCache(
547  Title $title,
548  ?RevisionRecord $old,
549  ?RevisionRecord $new,
550  $domain
551  ) {
552  static $models = [ CONTENT_MODEL_CSS, CONTENT_MODEL_JAVASCRIPT ];
553 
554  Assert::parameterType( 'string', $domain, '$domain' );
555 
556  $purge = false;
557  // TODO: MCR: differentiate between page functionality and content model!
558  // Not all pages containing CSS or JS have to be modules! [PageType]
559  if ( $old ) {
560  $oldModel = $old->getSlot( SlotRecord::MAIN, RevisionRecord::RAW )->getModel();
561  if ( in_array( $oldModel, $models ) ) {
562  $purge = true;
563  }
564  }
565 
566  if ( !$purge && $new ) {
567  $newModel = $new->getSlot( SlotRecord::MAIN, RevisionRecord::RAW )->getModel();
568  if ( in_array( $newModel, $models ) ) {
569  $purge = true;
570  }
571  }
572 
573  if ( !$purge ) {
574  $purge = ( $title->isSiteConfigPage() || $title->isUserConfigPage() );
575  }
576 
577  if ( $purge ) {
578  $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
579  $key = $cache->makeGlobalKey( 'resourceloader-titleinfo', $domain );
580  $cache->touchCheckKey( $key );
581  }
582  }
583 
588  public function getType() {
589  // Check both because subclasses don't always pass pages via the constructor,
590  // they may also override getPages() instead, in which case we should keep
591  // defaulting to LOAD_GENERAL and allow them to override getType() separately.
592  return ( $this->styles && !$this->scripts ) ? self::LOAD_STYLES : self::LOAD_GENERAL;
593  }
594 }
ResourceLoaderContext
Context object that contains information about the state of a specific ResourceLoader web request.
Definition: ResourceLoaderContext.php:33
Wikimedia\Rdbms\Database
Relational database abstraction object.
Definition: Database.php:50
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:361
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
ResourceLoaderModule\getFlip
getFlip(ResourceLoaderContext $context)
Definition: ResourceLoaderModule.php:156
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:166
ResourceLoaderContext\getResourceLoader
getResourceLoader()
Definition: ResourceLoaderContext.php:123
ResourceLoaderWikiModule\getDB
getDB()
Get the Database handle used for computing the module version.
Definition: ResourceLoaderWikiModule.php:167
ResourceLoaderWikiModule\invalidateModuleCache
static invalidateModuleCache(Title $title, ?RevisionRecord $old, ?RevisionRecord $new, $domain)
Clear the preloadTitleInfo() cache for all wiki modules on this wiki on page change if it was a JS or...
Definition: ResourceLoaderWikiModule.php:546
ResourceLoaderWikiModule\getStyles
getStyles(ResourceLoaderContext $context)
Definition: ResourceLoaderWikiModule.php:293
ResourceLoaderWikiModule
Abstraction for ResourceLoader modules which pull from wiki pages.
Definition: ResourceLoaderWikiModule.php:55
ResourceLoaderContext\getContentOverrideCallback
getContentOverrideCallback()
Return the replaced-content mapping callback.
Definition: ResourceLoaderContext.php:330
ResourceLoaderWikiModule\getGroup
getGroup()
Get group name.
Definition: ResourceLoaderWikiModule.php:149
ResourceLoaderWikiModule\getScript
getScript(ResourceLoaderContext $context)
Definition: ResourceLoaderWikiModule.php:274
Revision\RevisionRecord\getSlot
getSlot( $role, $audience=self::FOR_PUBLIC, User $user=null)
Returns meta-data for the given slot.
Definition: RevisionRecord.php:183
ResourceLoaderWikiModule\setTitleInfo
setTitleInfo( $batchKey, array $titleInfo)
Definition: ResourceLoaderWikiModule.php:374
CONTENT_FORMAT_CSS
const CONTENT_FORMAT_CSS
For CSS pages.
Definition: Defines.php:238
$res
$res
Definition: testCompression.php:57
ResourceLoaderWikiModule\isKnownEmpty
isKnownEmpty(ResourceLoaderContext $context)
Definition: ResourceLoaderWikiModule.php:350
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
ResourceLoaderWikiModule\$origin
string $origin
Origin defaults to users with sitewide authority.
Definition: ResourceLoaderWikiModule.php:57
$dbr
$dbr
Definition: testCompression.php:54
MemoizedCallable\call
static call( $callable, array $args=[], $ttl=3600)
Shortcut method for creating a MemoizedCallable and invoking it with the specified arguments.
Definition: MemoizedCallable.php:157
ResourceLoaderWikiModule\getType
getType()
Definition: ResourceLoaderWikiModule.php:588
ResourceLoaderModule\getLogger
getLogger()
Definition: ResourceLoaderModule.php:250
ResourceLoaderWikiModule\preloadTitleInfo
static preloadTitleInfo(ResourceLoaderContext $context, IDatabase $db, array $moduleNames)
Definition: ResourceLoaderWikiModule.php:463
Config\get
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2466
ResourceLoaderWikiModule\enableModuleContentVersion
enableModuleContentVersion()
Disable module content versioning.
Definition: ResourceLoaderWikiModule.php:328
ResourceLoaderWikiModule\getContentObj
getContentObj(Title $title, ResourceLoaderContext $context, $maxRedirects=null)
Definition: ResourceLoaderWikiModule.php:208
ResourceLoaderWikiModule\$titleInfo
array $titleInfo
In-process cache for title info, structured as an array [ <batchKey> // Pipe-separated list of sorted...
Definition: ResourceLoaderWikiModule.php:75
ResourceLoaderWikiModule\$styles
array $styles
List of page names that contain CSS.
Definition: ResourceLoaderWikiModule.php:78
$title
$title
Definition: testCompression.php:38
ResourceLoader\makeComment
static makeComment( $text)
Generate a CSS or JS comment block.
Definition: ResourceLoader.php:1113
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
ResourceLoaderWikiModule\$scripts
array $scripts
List of page names that contain JavaScript.
Definition: ResourceLoaderWikiModule.php:81
ResourceLoaderWikiModule\fetchTitleInfo
static fetchTitleInfo(IDatabase $db, array $pages, $fname=__METHOD__)
Definition: ResourceLoaderWikiModule.php:425
$content
$content
Definition: router.php:76
ResourceLoaderModule\$name
string null $name
Module name.
Definition: ResourceLoaderModule.php:55
ResourceLoaderModule\getDependencies
getDependencies(ResourceLoaderContext $context=null)
Get a list of modules this module depends on.
Definition: ResourceLoaderModule.php:405
Wikimedia\Rdbms\IDatabase\getDomainID
getDomainID()
Return the currently selected domain ID.
ResourceLoaderWikiModule\__construct
__construct(array $options=null)
Definition: ResourceLoaderWikiModule.php:90
ResourceLoaderModule\validateScriptFile
validateScriptFile( $fileName, $contents)
Validate a user-provided JavaScript blob.
Definition: ResourceLoaderModule.php:961
Content
Base interface for content objects.
Definition: Content.php:35
Title
Represents a title within MediaWiki.
Definition: Title.php:46
ResourceLoaderModule
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
Definition: ResourceLoaderModule.php:39
ResourceLoaderModule\$config
Config $config
Definition: ResourceLoaderModule.php:41
$cache
$cache
Definition: mcc.php:33
ResourceLoaderWikiModule\shouldEmbedModule
shouldEmbedModule(ResourceLoaderContext $context)
Definition: ResourceLoaderWikiModule.php:256
CONTENT_MODEL_JAVASCRIPT
const CONTENT_MODEL_JAVASCRIPT
Definition: Defines.php:219
ResourceLoaderWikiModule\getContent
getContent( $titleText, ResourceLoaderContext $context)
Definition: ResourceLoaderWikiModule.php:177
ResourceLoaderWikiModule\getDefinitionSummary
getDefinitionSummary(ResourceLoaderContext $context)
Definition: ResourceLoaderWikiModule.php:336
ResourceLoaderWikiModule\makeTitleKey
static makeTitleKey(LinkTarget $title)
Definition: ResourceLoaderWikiModule.php:378
Wikimedia\Rdbms\IDatabase\select
select( $table, $vars, $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Execute a SELECT query constructed using the various parameters provided.
ResourceLoaderModule\getSource
getSource()
Get the source of this module.
Definition: ResourceLoaderModule.php:387
ResourceLoaderWikiModule\getPages
getPages(ResourceLoaderContext $context)
Subclasses should return an associative array of resources in the module.
Definition: ResourceLoaderWikiModule.php:124
ResourceLoaderWikiModule\$group
string null $group
Group of module.
Definition: ResourceLoaderWikiModule.php:84
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
CONTENT_MODEL_CSS
const CONTENT_MODEL_CSS
Definition: Defines.php:220
CONTENT_FORMAT_JAVASCRIPT
const CONTENT_FORMAT_JAVASCRIPT
For JS pages.
Definition: Defines.php:236
ResourceLoaderModule\getConfig
getConfig()
Definition: ResourceLoaderModule.php:221
ResourceLoaderWikiModule\getTitleInfo
getTitleInfo(ResourceLoaderContext $context)
Get the information about the wiki pages for a given context.
Definition: ResourceLoaderWikiModule.php:388
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:40