MediaWiki  master
ResourceLoaderWikiModule.php
Go to the documentation of this file.
1 <?php
31 
53 
54  // Origin defaults to users with sitewide authority
55  protected $origin = self::ORIGIN_USER_SITEWIDE;
56 
57  // In-process cache for title info, structured as an array
58  // [
59  // <batchKey> // Pipe-separated list of sorted keys from getPages
60  // => [
61  // <titleKey> => [ // Normalised title key
62  // 'page_len' => ..,
63  // 'page_latest' => ..,
64  // 'page_touched' => ..,
65  // ]
66  // ]
67  // ]
68  // @see self::fetchTitleInfo()
69  // @see self::makeTitleKey()
70  protected $titleInfo = [];
71 
72  // List of page names that contain CSS
73  protected $styles = [];
74 
75  // List of page names that contain JavaScript
76  protected $scripts = [];
77 
78  // Group of module
79  protected $group;
80 
85  public function __construct( array $options = null ) {
86  if ( $options === null ) {
87  return;
88  }
89 
90  foreach ( $options as $member => $option ) {
91  switch ( $member ) {
92  case 'styles':
93  case 'scripts':
94  case 'group':
95  case 'targets':
96  $this->{$member} = $option;
97  break;
98  }
99  }
100  }
101 
118  protected function getPages( ResourceLoaderContext $context ) {
119  $config = $this->getConfig();
120  $pages = [];
121 
122  // Filter out pages from origins not allowed by the current wiki configuration.
123  if ( $config->get( 'UseSiteJs' ) ) {
124  foreach ( $this->scripts as $script ) {
125  $pages[$script] = [ 'type' => 'script' ];
126  }
127  }
128 
129  if ( $config->get( 'UseSiteCss' ) ) {
130  foreach ( $this->styles as $style ) {
131  $pages[$style] = [ 'type' => 'style' ];
132  }
133  }
134 
135  return $pages;
136  }
137 
143  public function getGroup() {
144  return $this->group;
145  }
146 
161  protected function getDB() {
162  return wfGetDB( DB_REPLICA );
163  }
164 
171  protected function getContent( $titleText, ResourceLoaderContext $context = null ) {
172  $title = Title::newFromText( $titleText );
173  if ( !$title ) {
174  return null; // Bad title
175  }
176 
177  $content = $this->getContentObj( $title, $context );
178  if ( !$content ) {
179  return null; // No content found
180  }
181 
182  $handler = $content->getContentHandler();
183  if ( $handler->isSupportedFormat( CONTENT_FORMAT_CSS ) ) {
184  $format = CONTENT_FORMAT_CSS;
185  } elseif ( $handler->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT ) ) {
186  $format = CONTENT_FORMAT_JAVASCRIPT;
187  } else {
188  return null; // Bad content model
189  }
190 
191  return $content->serialize( $format );
192  }
193 
202  protected function getContentObj(
203  Title $title, ResourceLoaderContext $context = null, $maxRedirects = null
204  ) {
205  if ( $context === null ) {
206  wfDeprecated( __METHOD__ . ' without a ResourceLoader context', '1.32' );
207  }
208 
209  $overrideCallback = $context ? $context->getContentOverrideCallback() : null;
210  $content = $overrideCallback ? call_user_func( $overrideCallback, $title ) : null;
211  if ( $content ) {
212  if ( !$content instanceof Content ) {
213  $this->getLogger()->error(
214  'Bad content override for "{title}" in ' . __METHOD__,
215  [ 'title' => $title->getPrefixedText() ]
216  );
217  return null;
218  }
219  } else {
220  $revision = Revision::newKnownCurrent( wfGetDB( DB_REPLICA ), $title );
221  if ( !$revision ) {
222  return null;
223  }
224  $content = $revision->getContent( RevisionRecord::RAW );
225 
226  if ( !$content ) {
227  $this->getLogger()->error(
228  'Failed to load content of JS/CSS page "{title}" in ' . __METHOD__,
229  [ 'title' => $title->getPrefixedText() ]
230  );
231  return null;
232  }
233  }
234 
235  if ( $content && $content->isRedirect() ) {
236  if ( $maxRedirects === null ) {
237  $maxRedirects = $this->getConfig()->get( 'MaxRedirects' ) ?: 0;
238  }
239  if ( $maxRedirects > 0 ) {
240  $newTitle = $content->getRedirectTarget();
241  return $newTitle ? $this->getContentObj( $newTitle, $context, $maxRedirects - 1 ) : null;
242  }
243  }
244 
245  return $content;
246  }
247 
253  $overrideCallback = $context->getContentOverrideCallback();
254  if ( $overrideCallback && $this->getSource() === 'local' ) {
255  foreach ( $this->getPages( $context ) as $page => $info ) {
256  $title = Title::newFromText( $page );
257  if ( $title && call_user_func( $overrideCallback, $title ) !== null ) {
258  return true;
259  }
260  }
261  }
262 
263  return parent::shouldEmbedModule( $context );
264  }
265 
271  $scripts = '';
272  foreach ( $this->getPages( $context ) as $titleText => $options ) {
273  if ( $options['type'] !== 'script' ) {
274  continue;
275  }
276  $script = $this->getContent( $titleText, $context );
277  if ( strval( $script ) !== '' ) {
278  $script = $this->validateScriptFile( $titleText, $script );
279  $scripts .= ResourceLoader::makeComment( $titleText ) . $script . "\n";
280  }
281  }
282  return $scripts;
283  }
284 
290  $styles = [];
291  foreach ( $this->getPages( $context ) as $titleText => $options ) {
292  if ( $options['type'] !== 'style' ) {
293  continue;
294  }
295  $media = $options['media'] ?? 'all';
296  $style = $this->getContent( $titleText, $context );
297  if ( strval( $style ) === '' ) {
298  continue;
299  }
300  if ( $this->getFlip( $context ) ) {
301  $style = CSSJanus::transform( $style, true, false );
302  }
303  $style = MemoizedCallable::call( 'CSSMin::remap',
304  [ $style, false, $this->getConfig()->get( 'ScriptPath' ), true ] );
305  if ( !isset( $styles[$media] ) ) {
306  $styles[$media] = [];
307  }
308  $style = ResourceLoader::makeComment( $titleText ) . $style;
309  $styles[$media][] = $style;
310  }
311  return $styles;
312  }
313 
324  public function enableModuleContentVersion() {
325  return false;
326  }
327 
333  $summary = parent::getDefinitionSummary( $context );
334  $summary[] = [
335  'pages' => $this->getPages( $context ),
336  // Includes meta data of current revisions
337  'titleInfo' => $this->getTitleInfo( $context ),
338  ];
339  return $summary;
340  }
341 
347  $revisions = $this->getTitleInfo( $context );
348 
349  // If a module has dependencies it cannot be empty. An empty array will be cast to false
350  if ( $this->getDependencies() ) {
351  return false;
352  }
353  // For user modules, don't needlessly load if there are no non-empty pages
354  if ( $this->getGroup() === 'user' ) {
355  foreach ( $revisions as $revision ) {
356  if ( $revision['page_len'] > 0 ) {
357  // At least one non-empty page, module should be loaded
358  return false;
359  }
360  }
361  return true;
362  }
363 
364  // T70488: For other modules (i.e. ones that are called in cached html output) only check
365  // page existance. This ensures that, if some pages in a module are temporarily blanked,
366  // we don't end omit the module's script or link tag on some pages.
367  return count( $revisions ) === 0;
368  }
369 
370  private function setTitleInfo( $batchKey, array $titleInfo ) {
371  $this->titleInfo[$batchKey] = $titleInfo;
372  }
373 
374  private static function makeTitleKey( LinkTarget $title ) {
375  // Used for keys in titleInfo.
376  return "{$title->getNamespace()}:{$title->getDBkey()}";
377  }
378 
385  $dbr = $this->getDB();
386 
387  $pageNames = array_keys( $this->getPages( $context ) );
388  sort( $pageNames );
389  $batchKey = implode( '|', $pageNames );
390  if ( !isset( $this->titleInfo[$batchKey] ) ) {
391  $this->titleInfo[$batchKey] = static::fetchTitleInfo( $dbr, $pageNames, __METHOD__ );
392  }
393 
394  $titleInfo = $this->titleInfo[$batchKey];
395 
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 ),
407  ];
408  }
409  }
410  }
411 
412  return $titleInfo;
413  }
414 
416  protected static function fetchTitleInfo( IDatabase $db, array $pages, $fname = __METHOD__ ) {
417  $titleInfo = [];
418  $batch = new LinkBatch;
419  foreach ( $pages as $titleText ) {
420  $title = Title::newFromText( $titleText );
421  if ( $title ) {
422  // Page name may be invalid if user-provided (e.g. gadgets)
423  $batch->addObj( $title );
424  }
425  }
426  if ( !$batch->isEmpty() ) {
427  $res = $db->select( 'page',
428  // Include page_touched to allow purging if cache is poisoned (T117587, T113916)
429  [ 'page_namespace', 'page_title', 'page_touched', 'page_len', 'page_latest' ],
430  $batch->constructSet( 'page', $db ),
431  $fname
432  );
433  foreach ( $res as $row ) {
434  // Avoid including ids or timestamps of revision/page tables so
435  // that versions are not wasted
436  $title = new TitleValue( (int)$row->page_namespace, $row->page_title );
437  $titleInfo[self::makeTitleKey( $title )] = [
438  'page_len' => $row->page_len,
439  'page_latest' => $row->page_latest,
440  'page_touched' => $row->page_touched,
441  ];
442  }
443  }
444  return $titleInfo;
445  }
446 
453  public static function preloadTitleInfo(
454  ResourceLoaderContext $context, IDatabase $db, array $moduleNames
455  ) {
456  $rl = $context->getResourceLoader();
457  // getDB() can be overridden to point to a foreign database.
458  // For now, only preload local. In the future, we could preload by wikiID.
459  $allPages = [];
461  $wikiModules = [];
462  foreach ( $moduleNames as $name ) {
463  $module = $rl->getModule( $name );
464  if ( $module instanceof self ) {
465  $mDB = $module->getDB();
466  // Subclasses may implement getDB differently
467  if ( $mDB->getDomainID() === $db->getDomainID() ) {
468  $wikiModules[] = $module;
469  $allPages += $module->getPages( $context );
470  }
471  }
472  }
473 
474  if ( !$wikiModules ) {
475  // Nothing to preload
476  return;
477  }
478 
479  $pageNames = array_keys( $allPages );
480  sort( $pageNames );
481  $hash = sha1( implode( '|', $pageNames ) );
482 
483  // Avoid Zend bug where "static::" does not apply LSB in the closure
484  $func = [ static::class, 'fetchTitleInfo' ];
485  $fname = __METHOD__;
486 
487  $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
488  $allInfo = $cache->getWithSetCallback(
489  $cache->makeGlobalKey( 'resourceloader-titleinfo', $db->getDomainID(), $hash ),
490  $cache::TTL_HOUR,
491  function ( $curVal, &$ttl, array &$setOpts ) use ( $func, $pageNames, $db, $fname ) {
492  $setOpts += Database::getCacheSetOptions( $db );
493 
494  return call_user_func( $func, $db, $pageNames, $fname );
495  },
496  [
497  'checkKeys' => [
498  $cache->makeGlobalKey( 'resourceloader-titleinfo', $db->getDomainID() ) ]
499  ]
500  );
501 
502  foreach ( $wikiModules as $wikiModule ) {
503  $pages = $wikiModule->getPages( $context );
504  // Before we intersect, map the names to canonical form (T145673).
505  $intersect = [];
506  foreach ( $pages as $pageName => $unused ) {
507  $title = Title::newFromText( $pageName );
508  if ( $title ) {
509  $intersect[ self::makeTitleKey( $title ) ] = 1;
510  } else {
511  // Page name may be invalid if user-provided (e.g. gadgets)
512  $rl->getLogger()->info(
513  'Invalid wiki page title "{title}" in ' . __METHOD__,
514  [ 'title' => $pageName ]
515  );
516  }
517  }
518  $info = array_intersect_key( $allInfo, $intersect );
519  $pageNames = array_keys( $pages );
520  sort( $pageNames );
521  $batchKey = implode( '|', $pageNames );
522  $wikiModule->setTitleInfo( $batchKey, $info );
523  }
524  }
525 
536  public static function invalidateModuleCache(
537  Title $title, Revision $old = null, Revision $new = null, $domain
538  ) {
539  static $formats = [ CONTENT_FORMAT_CSS, CONTENT_FORMAT_JAVASCRIPT ];
540 
541  Assert::parameterType( 'string', $domain, '$domain' );
542 
543  // TODO: MCR: differentiate between page functionality and content model!
544  // Not all pages containing CSS or JS have to be modules! [PageType]
545  if ( $old && in_array( $old->getContentFormat(), $formats ) ) {
546  $purge = true;
547  } elseif ( $new && in_array( $new->getContentFormat(), $formats ) ) {
548  $purge = true;
549  } else {
550  $purge = ( $title->isSiteConfigPage() || $title->isUserConfigPage() );
551  }
552 
553  if ( $purge ) {
554  $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
555  $key = $cache->makeGlobalKey( 'resourceloader-titleinfo', $domain );
556  $cache->touchCheckKey( $key );
557  }
558  }
559 
564  public function getType() {
565  // Check both because subclasses don't always pass pages via the constructor,
566  // they may also override getPages() instead, in which case we should keep
567  // defaulting to LOAD_GENERAL and allow them to override getType() separately.
568  return ( $this->styles && !$this->scripts ) ? self::LOAD_STYLES : self::LOAD_GENERAL;
569  }
570 }
static fetchTitleInfo(IDatabase $db, array $pages, $fname=__METHOD__)
getContent( $titleText, ResourceLoaderContext $context=null)
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...
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...
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
shouldEmbedModule(ResourceLoaderContext $context)
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
getPrefixedText()
Get the prefixed title with spaces.
Definition: Title.php:1724
static makeTitleKey(LinkTarget $title)
getDomainID()
Return the currently selected domain ID.
The package scripts
Definition: README.txt:1
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:1380
$batch
Definition: linkcache.txt:23
const CONTENT_FORMAT_CSS
Definition: Defines.php:234
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves $handler
Definition: hooks.txt:767
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:34
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
enableModuleContentVersion()
Disable module content versioning.
isUserConfigPage()
Is this a "config" (.css, .json, or .js) sub-page of a user page?
Definition: Title.php:1394
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:1325
$res
Definition: database.txt:21
static preloadTitleInfo(ResourceLoaderContext $context, IDatabase $db, array $moduleNames)
$cache
Definition: mcc.php:33
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1972
getContentOverrideCallback()
Return the replaced-content mapping callback.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:912
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings, LocalSettings).
Definition: Setup.php:131
validateScriptFile( $fileName, $contents)
Validate a given script file; if valid returns the original source.
getStyles(ResourceLoaderContext $context)
setTitleInfo( $batchKey, array $titleInfo)
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
getScript(ResourceLoaderContext $context)
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
getContentObj(Title $title, ResourceLoaderContext $context=null, $maxRedirects=null)
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
isKnownEmpty(ResourceLoaderContext $context)
string null $name
Module name.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition: hooks.txt:2621
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
getDependencies(ResourceLoaderContext $context=null)
Get a list of modules this module depends on.
getDefinitionSummary(ResourceLoaderContext $context)
$content
Definition: pageupdater.txt:72
getPages(ResourceLoaderContext $context)
Subclasses should return an associative array of resources in the module.
static makeComment( $text)
Generate a CSS or JS comment block.
Object passed around to modules which contains information about the state of a specific loader reque...
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:322