MediaWiki  master
ResourceLoaderWikiModule.php
Go to the documentation of this file.
1 <?php
30 
55 
56  // Origin defaults to users with sitewide authority
57  protected $origin = self::ORIGIN_USER_SITEWIDE;
58 
59  // In-process cache for title info, structured as an array
60  // [
61  // <batchKey> // Pipe-separated list of sorted keys from getPages
62  // => [
63  // <titleKey> => [ // Normalised title key
64  // 'page_len' => ..,
65  // 'page_latest' => ..,
66  // 'page_touched' => ..,
67  // ]
68  // ]
69  // ]
70  // @see self::fetchTitleInfo()
71  // @see self::makeTitleKey()
72  protected $titleInfo = [];
73 
74  // List of page names that contain CSS
75  protected $styles = [];
76 
77  // List of page names that contain JavaScript
78  protected $scripts = [];
79 
80  // Group of module
81  protected $group;
82 
87  public function __construct( array $options = null ) {
88  if ( $options === null ) {
89  return;
90  }
91 
92  foreach ( $options as $member => $option ) {
93  switch ( $member ) {
94  case 'styles':
95  case 'scripts':
96  case 'group':
97  case 'targets':
98  $this->{$member} = $option;
99  break;
100  }
101  }
102  }
103 
121  protected function getPages( ResourceLoaderContext $context ) {
122  $config = $this->getConfig();
123  $pages = [];
124 
125  // Filter out pages from origins not allowed by the current wiki configuration.
126  if ( $config->get( 'UseSiteJs' ) ) {
127  foreach ( $this->scripts as $script ) {
128  $pages[$script] = [ 'type' => 'script' ];
129  }
130  }
131 
132  if ( $config->get( 'UseSiteCss' ) ) {
133  foreach ( $this->styles as $style ) {
134  $pages[$style] = [ 'type' => 'style' ];
135  }
136  }
137 
138  return $pages;
139  }
140 
146  public function getGroup() {
147  return $this->group;
148  }
149 
164  protected function getDB() {
165  return wfGetDB( DB_REPLICA );
166  }
167 
174  protected function getContent( $titleText, ResourceLoaderContext $context ) {
175  $title = Title::newFromText( $titleText );
176  if ( !$title ) {
177  return null; // Bad title
178  }
179 
180  $content = $this->getContentObj( $title, $context );
181  if ( !$content ) {
182  return null; // No content found
183  }
184 
185  $handler = $content->getContentHandler();
186  if ( $handler->isSupportedFormat( CONTENT_FORMAT_CSS ) ) {
187  $format = CONTENT_FORMAT_CSS;
188  } elseif ( $handler->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT ) ) {
189  $format = CONTENT_FORMAT_JAVASCRIPT;
190  } else {
191  return null; // Bad content model
192  }
193 
194  return $content->serialize( $format );
195  }
196 
205  protected function getContentObj(
206  Title $title, ResourceLoaderContext $context, $maxRedirects = null
207  ) {
208  $overrideCallback = $context->getContentOverrideCallback();
209  $content = $overrideCallback ? call_user_func( $overrideCallback, $title ) : null;
210  if ( $content ) {
211  if ( !$content instanceof Content ) {
212  $this->getLogger()->error(
213  'Bad content override for "{title}" in ' . __METHOD__,
214  [ 'title' => $title->getPrefixedText() ]
215  );
216  return null;
217  }
218  } else {
219  $revision = Revision::newKnownCurrent( wfGetDB( DB_REPLICA ), $title );
220  if ( !$revision ) {
221  return null;
222  }
223  $content = $revision->getContent( RevisionRecord::RAW );
224 
225  if ( !$content ) {
226  $this->getLogger()->error(
227  'Failed to load content of JS/CSS page "{title}" in ' . __METHOD__,
228  [ 'title' => $title->getPrefixedText() ]
229  );
230  return null;
231  }
232  }
233 
234  if ( $content && $content->isRedirect() ) {
235  if ( $maxRedirects === null ) {
236  $maxRedirects = $this->getConfig()->get( 'MaxRedirects' ) ?: 0;
237  }
238  if ( $maxRedirects > 0 ) {
239  $newTitle = $content->getRedirectTarget();
240  return $newTitle ? $this->getContentObj( $newTitle, $context, $maxRedirects - 1 ) : null;
241  }
242  }
243 
244  return $content;
245  }
246 
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 ) {
257  return true;
258  }
259  }
260  }
261 
262  return parent::shouldEmbedModule( $context );
263  }
264 
270  $scripts = '';
271  foreach ( $this->getPages( $context ) as $titleText => $options ) {
272  if ( $options['type'] !== 'script' ) {
273  continue;
274  }
275  $script = $this->getContent( $titleText, $context );
276  if ( strval( $script ) !== '' ) {
277  $script = $this->validateScriptFile( $titleText, $script );
278  $scripts .= ResourceLoader::makeComment( $titleText ) . $script . "\n";
279  }
280  }
281  return $scripts;
282  }
283 
289  $styles = [];
290  foreach ( $this->getPages( $context ) as $titleText => $options ) {
291  if ( $options['type'] !== 'style' ) {
292  continue;
293  }
294  $media = $options['media'] ?? 'all';
295  $style = $this->getContent( $titleText, $context );
296  if ( strval( $style ) === '' ) {
297  continue;
298  }
299  if ( $this->getFlip( $context ) ) {
300  $style = CSSJanus::transform( $style, true, false );
301  }
302  $style = MemoizedCallable::call( 'CSSMin::remap',
303  [ $style, false, $this->getConfig()->get( 'ScriptPath' ), true ] );
304  if ( !isset( $styles[$media] ) ) {
305  $styles[$media] = [];
306  }
307  $style = ResourceLoader::makeComment( $titleText ) . $style;
308  $styles[$media][] = $style;
309  }
310  return $styles;
311  }
312 
323  public function enableModuleContentVersion() {
324  return false;
325  }
326 
332  $summary = parent::getDefinitionSummary( $context );
333  $summary[] = [
334  'pages' => $this->getPages( $context ),
335  // Includes meta data of current revisions
336  'titleInfo' => $this->getTitleInfo( $context ),
337  ];
338  return $summary;
339  }
340 
346  $revisions = $this->getTitleInfo( $context );
347 
348  // If a module has dependencies it cannot be empty. An empty array will be cast to false
349  if ( $this->getDependencies() ) {
350  return false;
351  }
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
357  return false;
358  }
359  }
360  return true;
361  }
362 
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;
367  }
368 
369  private function setTitleInfo( $batchKey, array $titleInfo ) {
370  $this->titleInfo[$batchKey] = $titleInfo;
371  }
372 
373  private static function makeTitleKey( LinkTarget $title ) {
374  // Used for keys in titleInfo.
375  return "{$title->getNamespace()}:{$title->getDBkey()}";
376  }
377 
384  $dbr = $this->getDB();
385 
386  $pageNames = array_keys( $this->getPages( $context ) );
387  sort( $pageNames );
388  $batchKey = implode( '|', $pageNames );
389  if ( !isset( $this->titleInfo[$batchKey] ) ) {
390  $this->titleInfo[$batchKey] = static::fetchTitleInfo( $dbr, $pageNames, __METHOD__ );
391  }
392 
393  $titleInfo = $this->titleInfo[$batchKey];
394 
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' => ConvertibleTimestamp::now( TS_MW ),
406  ];
407  }
408  }
409  }
410 
411  return $titleInfo;
412  }
413 
420  protected static function fetchTitleInfo( IDatabase $db, array $pages, $fname = __METHOD__ ) {
421  $titleInfo = [];
422  $batch = new LinkBatch;
423  foreach ( $pages as $titleText ) {
424  $title = Title::newFromText( $titleText );
425  if ( $title ) {
426  // Page name may be invalid if user-provided (e.g. gadgets)
427  $batch->addObj( $title );
428  }
429  }
430  if ( !$batch->isEmpty() ) {
431  $res = $db->select( 'page',
432  // Include page_touched to allow purging if cache is poisoned (T117587, T113916)
433  [ 'page_namespace', 'page_title', 'page_touched', 'page_len', 'page_latest' ],
434  $batch->constructSet( 'page', $db ),
435  $fname
436  );
437  foreach ( $res as $row ) {
438  // Avoid including ids or timestamps of revision/page tables so
439  // that versions are not wasted
440  $title = new TitleValue( (int)$row->page_namespace, $row->page_title );
441  $titleInfo[self::makeTitleKey( $title )] = [
442  'page_len' => $row->page_len,
443  'page_latest' => $row->page_latest,
444  'page_touched' => $row->page_touched,
445  ];
446  }
447  }
448  return $titleInfo;
449  }
450 
457  public static function preloadTitleInfo(
458  ResourceLoaderContext $context, IDatabase $db, array $moduleNames
459  ) {
460  $rl = $context->getResourceLoader();
461  // getDB() can be overridden to point to a foreign database.
462  // For now, only preload local. In the future, we could preload by wikiID.
463  $allPages = [];
465  $wikiModules = [];
466  foreach ( $moduleNames as $name ) {
467  $module = $rl->getModule( $name );
468  if ( $module instanceof self ) {
469  $mDB = $module->getDB();
470  // Subclasses may implement getDB differently
471  if ( $mDB->getDomainID() === $db->getDomainID() ) {
472  $wikiModules[] = $module;
473  $allPages += $module->getPages( $context );
474  }
475  }
476  }
477 
478  if ( !$wikiModules ) {
479  // Nothing to preload
480  return;
481  }
482 
483  $pageNames = array_keys( $allPages );
484  sort( $pageNames );
485  $hash = sha1( implode( '|', $pageNames ) );
486 
487  // Avoid Zend bug where "static::" does not apply LSB in the closure
488  $func = [ static::class, 'fetchTitleInfo' ];
489  $fname = __METHOD__;
490 
491  $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
492  $allInfo = $cache->getWithSetCallback(
493  $cache->makeGlobalKey( 'resourceloader-titleinfo', $db->getDomainID(), $hash ),
494  $cache::TTL_HOUR,
495  function ( $curVal, &$ttl, array &$setOpts ) use ( $func, $pageNames, $db, $fname ) {
496  $setOpts += Database::getCacheSetOptions( $db );
497 
498  return call_user_func( $func, $db, $pageNames, $fname );
499  },
500  [
501  'checkKeys' => [
502  $cache->makeGlobalKey( 'resourceloader-titleinfo', $db->getDomainID() ) ]
503  ]
504  );
505 
506  foreach ( $wikiModules as $wikiModule ) {
507  $pages = $wikiModule->getPages( $context );
508  // Before we intersect, map the names to canonical form (T145673).
509  $intersect = [];
510  foreach ( $pages as $pageName => $unused ) {
511  $title = Title::newFromText( $pageName );
512  if ( $title ) {
513  $intersect[ self::makeTitleKey( $title ) ] = 1;
514  } else {
515  // Page name may be invalid if user-provided (e.g. gadgets)
516  $rl->getLogger()->info(
517  'Invalid wiki page title "{title}" in ' . __METHOD__,
518  [ 'title' => $pageName ]
519  );
520  }
521  }
522  $info = array_intersect_key( $allInfo, $intersect );
523  $pageNames = array_keys( $pages );
524  sort( $pageNames );
525  $batchKey = implode( '|', $pageNames );
526  $wikiModule->setTitleInfo( $batchKey, $info );
527  }
528  }
529 
540  public static function invalidateModuleCache(
541  Title $title, ?Revision $old, ?Revision $new, $domain
542  ) {
543  static $formats = [ CONTENT_FORMAT_CSS, CONTENT_FORMAT_JAVASCRIPT ];
544 
545  Assert::parameterType( 'string', $domain, '$domain' );
546 
547  // TODO: MCR: differentiate between page functionality and content model!
548  // Not all pages containing CSS or JS have to be modules! [PageType]
549  if ( $old && in_array( $old->getContentFormat(), $formats ) ) {
550  $purge = true;
551  } elseif ( $new && in_array( $new->getContentFormat(), $formats ) ) {
552  $purge = true;
553  } else {
554  $purge = ( $title->isSiteConfigPage() || $title->isUserConfigPage() );
555  }
556 
557  if ( $purge ) {
558  $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
559  $key = $cache->makeGlobalKey( 'resourceloader-titleinfo', $domain );
560  $cache->touchCheckKey( $key );
561  }
562  }
563 
568  public function getType() {
569  // Check both because subclasses don't always pass pages via the constructor,
570  // they may also override getPages() instead, in which case we should keep
571  // defaulting to LOAD_GENERAL and allow them to override getType() separately.
572  return ( $this->styles && !$this->scripts ) ? self::LOAD_STYLES : self::LOAD_GENERAL;
573  }
574 }
static fetchTitleInfo(IDatabase $db, array $pages, $fname=__METHOD__)
getDB()
Get the Database handle used for computing the module version.
static call( $callable, array $args=[], $ttl=3600)
Shortcut method for creating a MemoizedCallable and invoking it with the specified arguments...
$context
Definition: load.php:40
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:37
shouldEmbedModule(ResourceLoaderContext $context)
getPrefixedText()
Get the prefixed title with spaces.
Definition: Title.php:1853
static makeTitleKey(LinkTarget $title)
getDomainID()
Return the currently selected domain ID.
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
isSiteConfigPage()
Could this MediaWiki namespace page contain custom CSS, JSON, or JavaScript for the global UI...
Definition: Title.php:1433
const CONTENT_FORMAT_CSS
Definition: Defines.php:234
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:34
enableModuleContentVersion()
Disable module content versioning.
isUserConfigPage()
Is this a "config" (.css, .json, or .js) sub-page of a user page?
Definition: Title.php:1447
getSource()
Get the source of this module.
getTitleInfo(ResourceLoaderContext $context)
Get the information about the wiki pages for a given context.
static newKnownCurrent(IDatabase $db, $pageIdOrTitle, $revId=0)
Load a revision based on a known page ID and current revision ID from the DB.
Definition: Revision.php:1124
static preloadTitleInfo(ResourceLoaderContext $context, IDatabase $db, array $moduleNames)
$cache
Definition: mcc.php:33
getContentOverrideCallback()
Return the replaced-content mapping callback.
getContentObj(Title $title, ResourceLoaderContext $context, $maxRedirects=null)
static invalidateModuleCache(Title $title, ?Revision $old, ?Revision $new, $domain)
Clear the preloadTitleInfo() cache for all wiki modules on this wiki on page change if it was a JS or...
validateScriptFile( $fileName, $contents)
Validate a given script file; if valid returns the original source.
getStyles(ResourceLoaderContext $context)
setTitleInfo( $batchKey, array $titleInfo)
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
getScript(ResourceLoaderContext $context)
isKnownEmpty(ResourceLoaderContext $context)
string null $name
Module name.
select( $table, $vars, $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Execute a SELECT query constructed using the various parameters provided.
const CONTENT_FORMAT_JAVASCRIPT
Definition: Defines.php:232
const DB_REPLICA
Definition: defines.php:25
$content
Definition: router.php:78
getDependencies(ResourceLoaderContext $context=null)
Get a list of modules this module depends on.
getDefinitionSummary(ResourceLoaderContext $context)
getContentFormat()
Returns the content format for the main slot of this revision.
Definition: Revision.php:775
getPages(ResourceLoaderContext $context)
Subclasses should return an associative array of resources in the module.
getContent( $titleText, ResourceLoaderContext $context)
getFlip(ResourceLoaderContext $context)
static makeComment( $text)
Generate a CSS or JS comment block.
Context object that contains information about the state of a specific ResourceLoader web request...
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:317