MediaWiki  master
ResourceLoaderWikiModule.php
Go to the documentation of this file.
1 <?php
30 
52 
53  // Origin defaults to users with sitewide authority
54  protected $origin = self::ORIGIN_USER_SITEWIDE;
55 
56  // In-process cache for title info, structured as an array
57  // [
58  // <batchKey> // Pipe-separated list of sorted keys from getPages
59  // => [
60  // <titleKey> => [ // Normalised title key
61  // 'page_len' => ..,
62  // 'page_latest' => ..,
63  // 'page_touched' => ..,
64  // ]
65  // ]
66  // ]
67  // @see self::fetchTitleInfo()
68  // @see self::makeTitleKey()
69  protected $titleInfo = [];
70 
71  // List of page names that contain CSS
72  protected $styles = [];
73 
74  // List of page names that contain JavaScript
75  protected $scripts = [];
76 
77  // Group of module
78  protected $group;
79 
84  public function __construct( array $options = null ) {
85  if ( $options === null ) {
86  return;
87  }
88 
89  foreach ( $options as $member => $option ) {
90  switch ( $member ) {
91  case 'styles':
92  case 'scripts':
93  case 'group':
94  case 'targets':
95  $this->{$member} = $option;
96  break;
97  }
98  }
99  }
100 
117  protected function getPages( ResourceLoaderContext $context ) {
118  $config = $this->getConfig();
119  $pages = [];
120 
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' ];
125  }
126  }
127 
128  if ( $config->get( 'UseSiteCss' ) ) {
129  foreach ( $this->styles as $style ) {
130  $pages[$style] = [ 'type' => 'style' ];
131  }
132  }
133 
134  return $pages;
135  }
136 
142  public function getGroup() {
143  return $this->group;
144  }
145 
160  protected function getDB() {
161  return wfGetDB( DB_REPLICA );
162  }
163 
170  protected function getContent( $titleText, ResourceLoaderContext $context = null ) {
171  $title = Title::newFromText( $titleText );
172  if ( !$title ) {
173  return null; // Bad title
174  }
175 
176  $content = $this->getContentObj( $title, $context );
177  if ( !$content ) {
178  return null; // No content found
179  }
180 
181  $handler = $content->getContentHandler();
182  if ( $handler->isSupportedFormat( CONTENT_FORMAT_CSS ) ) {
183  $format = CONTENT_FORMAT_CSS;
184  } elseif ( $handler->isSupportedFormat( CONTENT_FORMAT_JAVASCRIPT ) ) {
185  $format = CONTENT_FORMAT_JAVASCRIPT;
186  } else {
187  return null; // Bad content model
188  }
189 
190  return $content->serialize( $format );
191  }
192 
201  protected function getContentObj(
202  Title $title, ResourceLoaderContext $context = null, $maxRedirects = null
203  ) {
204  if ( $context === null ) {
205  wfDeprecated( __METHOD__ . ' without a ResourceLoader context', '1.32' );
206  }
207 
208  $overrideCallback = $context ? $context->getContentOverrideCallback() : null;
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( Revision::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' => wfTimestamp( TS_MW ),
406  ];
407  }
408  }
409  }
410 
411  return $titleInfo;
412  }
413 
414  protected static function fetchTitleInfo( IDatabase $db, array $pages, $fname = __METHOD__ ) {
415  $titleInfo = [];
416  $batch = new LinkBatch;
417  foreach ( $pages as $titleText ) {
418  $title = Title::newFromText( $titleText );
419  if ( $title ) {
420  // Page name may be invalid if user-provided (e.g. gadgets)
421  $batch->addObj( $title );
422  }
423  }
424  if ( !$batch->isEmpty() ) {
425  $res = $db->select( 'page',
426  // Include page_touched to allow purging if cache is poisoned (T117587, T113916)
427  [ 'page_namespace', 'page_title', 'page_touched', 'page_len', 'page_latest' ],
428  $batch->constructSet( 'page', $db ),
429  $fname
430  );
431  foreach ( $res as $row ) {
432  // Avoid including ids or timestamps of revision/page tables so
433  // that versions are not wasted
434  $title = new TitleValue( (int)$row->page_namespace, $row->page_title );
435  $titleInfo[self::makeTitleKey( $title )] = [
436  'page_len' => $row->page_len,
437  'page_latest' => $row->page_latest,
438  'page_touched' => $row->page_touched,
439  ];
440  }
441  }
442  return $titleInfo;
443  }
444 
451  public static function preloadTitleInfo(
452  ResourceLoaderContext $context, IDatabase $db, array $moduleNames
453  ) {
454  $rl = $context->getResourceLoader();
455  // getDB() can be overridden to point to a foreign database.
456  // For now, only preload local. In the future, we could preload by wikiID.
457  $allPages = [];
459  $wikiModules = [];
460  foreach ( $moduleNames as $name ) {
461  $module = $rl->getModule( $name );
462  if ( $module instanceof self ) {
463  $mDB = $module->getDB();
464  // Subclasses may implement getDB differently
465  if ( $mDB->getDomainID() === $db->getDomainID() ) {
466  $wikiModules[] = $module;
467  $allPages += $module->getPages( $context );
468  }
469  }
470  }
471 
472  if ( !$wikiModules ) {
473  // Nothing to preload
474  return;
475  }
476 
477  $pageNames = array_keys( $allPages );
478  sort( $pageNames );
479  $hash = sha1( implode( '|', $pageNames ) );
480 
481  // Avoid Zend bug where "static::" does not apply LSB in the closure
482  $func = [ static::class, 'fetchTitleInfo' ];
483  $fname = __METHOD__;
484 
485  $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
486  $allInfo = $cache->getWithSetCallback(
487  $cache->makeGlobalKey( 'resourceloader', 'titleinfo', $db->getDomainID(), $hash ),
488  $cache::TTL_HOUR,
489  function ( $curVal, &$ttl, array &$setOpts ) use ( $func, $pageNames, $db, $fname ) {
490  $setOpts += Database::getCacheSetOptions( $db );
491 
492  return call_user_func( $func, $db, $pageNames, $fname );
493  },
494  [
495  'checkKeys' => [
496  $cache->makeGlobalKey( 'resourceloader', 'titleinfo', $db->getDomainID() ) ]
497  ]
498  );
499 
500  foreach ( $wikiModules as $wikiModule ) {
501  $pages = $wikiModule->getPages( $context );
502  // Before we intersect, map the names to canonical form (T145673).
503  $intersect = [];
504  foreach ( $pages as $pageName => $unused ) {
505  $title = Title::newFromText( $pageName );
506  if ( $title ) {
507  $intersect[ self::makeTitleKey( $title ) ] = 1;
508  } else {
509  // Page name may be invalid if user-provided (e.g. gadgets)
510  $rl->getLogger()->info(
511  'Invalid wiki page title "{title}" in ' . __METHOD__,
512  [ 'title' => $pageName ]
513  );
514  }
515  }
516  $info = array_intersect_key( $allInfo, $intersect );
517  $pageNames = array_keys( $pages );
518  sort( $pageNames );
519  $batchKey = implode( '|', $pageNames );
520  $wikiModule->setTitleInfo( $batchKey, $info );
521  }
522  }
523 
534  public static function invalidateModuleCache(
535  Title $title, Revision $old = null, Revision $new = null, $domain
536  ) {
537  static $formats = [ CONTENT_FORMAT_CSS, CONTENT_FORMAT_JAVASCRIPT ];
538 
539  Assert::parameterType( 'string', $domain, '$domain' );
540 
541  // TODO: MCR: differentiate between page functionality and content model!
542  // Not all pages containing CSS or JS have to be modules! [PageType]
543  if ( $old && in_array( $old->getContentFormat(), $formats ) ) {
544  $purge = true;
545  } elseif ( $new && in_array( $new->getContentFormat(), $formats ) ) {
546  $purge = true;
547  } else {
548  $purge = ( $title->isSiteConfigPage() || $title->isUserConfigPage() );
549  }
550 
551  if ( $purge ) {
552  $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
553  $key = $cache->makeGlobalKey( 'resourceloader', 'titleinfo', $domain );
554  $cache->touchCheckKey( $key );
555  }
556  }
557 
562  public function getType() {
563  // Check both because subclasses don't always pass pages via the constructor,
564  // they may also override getPages() instead, in which case we should keep
565  // defaulting to LOAD_GENERAL and allow them to override getType() separately.
566  return ( $this->styles && !$this->scripts ) ? self::LOAD_STYLES : self::LOAD_GENERAL;
567  }
568 }
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
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:1691
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:1342
$batch
Definition: linkcache.txt:23
const CONTENT_FORMAT_CSS
Definition: Defines.php:254
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:780
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:1356
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:1327
$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:1982
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:780
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
const RAW
Definition: Revision.php:56
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:123
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)
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:2633
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:252
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:319