MediaWiki  master
MediaWiki\ResourceLoader\WikiModule Class Reference

Abstraction for ResourceLoader modules which pull from wiki pages. More...

Inheritance diagram for MediaWiki\ResourceLoader\WikiModule:
Collaboration diagram for MediaWiki\ResourceLoader\WikiModule:

Public Member Functions

 __construct (array $options=null)
 
 enableModuleContentVersion ()
 Disable module content versioning. More...
 
 getDefinitionSummary (Context $context)
 
 getGroup ()
 Get group name. More...
 
 getScript (Context $context)
 
 getStyles (Context $context)
 
 getType ()
 
 isKnownEmpty (Context $context)
 
 shouldEmbedModule (Context $context)
 
 supportsURLLoading ()
 
- Public Member Functions inherited from MediaWiki\ResourceLoader\Module
 getConfig ()
 
 getDependencies (Context $context=null)
 Get a list of modules this module depends on. More...
 
 getDeprecationInformation (Context $context)
 Get JS representing deprecation information for the current module if available. More...
 
 getFlip (Context $context)
 
 getHeaders (Context $context)
 Get headers to send as part of a module web response. More...
 
 getMessages ()
 Get the messages needed for this module. More...
 
 getModuleContent (Context $context)
 Get an array of this module's resources. More...
 
 getName ()
 Get this module's name. More...
 
 getOrigin ()
 Get this module's origin. More...
 
 getScriptURLsForDebug (Context $context)
 Get alternative script URLs for legacy debug mode. More...
 
 getSkins ()
 Get list of skins for which this module must be available to load. More...
 
 getSkipFunction ()
 Get the skip function. More...
 
 getSource ()
 Get the source of this module. More...
 
 getStyleURLsForDebug (Context $context)
 Get the URL or URLs to load for this module's CSS in debug mode. More...
 
 getTargets ()
 Get target(s) for the module, eg ['desktop'] or ['desktop', 'mobile']. More...
 
 getTemplates ()
 Takes named templates by the module and returns an array mapping. More...
 
 getVersionHash (Context $context)
 Get a string identifying the current version of this module in a given context. More...
 
 requiresES6 ()
 Whether the module requires ES6 support in the client. More...
 
 setConfig (Config $config)
 
 setDependencyAccessCallbacks (callable $loadCallback, callable $saveCallback)
 Inject the functions that load/save the indirect file path dependency list from storage. More...
 
 setFileDependencies (Context $context, array $paths)
 Set the indirect dependencies for this module pursuant to the skin/language context. More...
 
 setHookContainer (HookContainer $hookContainer)
 
 setLogger (LoggerInterface $logger)
 
 setMessageBlob ( $blob, $lang)
 Set in-object cache for message blobs. More...
 
 setName ( $name)
 Set this module's name. More...
 
 setSkinStylesOverride (array $moduleSkinStyles)
 Provide overrides for skinStyles to modules that support that. More...
 

Static Public Member Functions

static invalidateModuleCache (PageIdentity $page, ?RevisionRecord $old, ?RevisionRecord $new, string $domain)
 Clear the preloadTitleInfo() cache for all wiki modules on this wiki on page change if it was a JS or CSS page. More...
 
static preloadTitleInfo (Context $context, IDatabase $db, array $moduleNames)
 
- Static Public Member Functions inherited from MediaWiki\ResourceLoader\Module
static expandRelativePaths (array $filePaths)
 Expand directories relative to $IP. More...
 
static getRelativePaths (array $filePaths)
 Make file paths relative to MediaWiki directory. More...
 
static getVary (Context $context)
 Get vary string. More...
 

Protected Member Functions

 getContent ( $titleText, Context $context)
 
 getContentObj (PageIdentity $page, Context $context, $maxRedirects=1)
 
 getDB ()
 Get the Database handle used for computing the module version. More...
 
 getPages (Context $context)
 Subclasses should return an associative array of resources in the module. More...
 
 getRequireKey (string $titleText)
 Convert a namespace-formatted page title to a virtual package file name. More...
 
 getTitleInfo (Context $context)
 Get the information about the wiki pages for a given context. More...
 
 isPackaged ()
 Get whether this module is a packaged module. More...
 
- Protected Member Functions inherited from MediaWiki\ResourceLoader\Module
 buildContent (Context $context)
 Bundle all resources attached to this module into an array. More...
 
 getFileDependencies (Context $context)
 Get the indirect dependencies for this module pursuant to the skin/language context. More...
 
 getHookRunner ()
 Get a HookRunner for running core hooks. More...
 
 getLessVars (Context $context)
 Get module-specific LESS variables, if any. More...
 
 getLogger ()
 
 getMessageBlob (Context $context)
 Get the hash of the message blob. More...
 
 getPreloadLinks (Context $context)
 Get a list of resources that web browsers may preload. More...
 
 saveFileDependencies (Context $context, array $curFileRefs)
 Save the indirect dependencies for this module pursuant to the skin/language context. More...
 
 validateScriptFile ( $fileName, $contents)
 Validate a user-provided JavaScript blob. More...
 

Static Protected Member Functions

static fetchTitleInfo (IDatabase $db, array $pages, $fname=__METHOD__)
 
- Static Protected Member Functions inherited from MediaWiki\ResourceLoader\Module
static safeFileHash ( $filePath)
 Compute a non-cryptographic string hash of a file's contents. More...
 

Protected Attributes

array $datas = []
 List of page names that contain JSON. More...
 
string null $group
 Group of module. More...
 
string $origin = self::ORIGIN_USER_SITEWIDE
 Origin defaults to users with sitewide authority. More...
 
array $scripts = []
 List of page names that contain JavaScript. More...
 
array $styles = []
 List of page names that contain CSS. More...
 
array $titleInfo = []
 In-process cache for title info, structured as an array [ <batchKey> // Pipe-separated list of sorted keys from getPages => [ <titleKey> => [ // Normalised title key 'page_len' => .., 'page_latest' => .., 'page_touched' => .., ] ] ]. More...
 
- Protected Attributes inherited from MediaWiki\ResourceLoader\Module
Config $config
 
array $contents = []
 Map of (context hash => cached module content) More...
 
string bool $deprecated = false
 Deprecation string or true if deprecated; false otherwise. More...
 
array $fileDeps = []
 Map of (variant => indirect file dependencies) More...
 
LoggerInterface $logger
 
array $msgBlobs = []
 Map of (language => in-object cache for message blob) More...
 
string null $name = null
 Module name. More...
 
int $origin = self::ORIGIN_CORE_SITEWIDE
 Script and style modules form a hierarchy of trustworthiness, with core modules like skins and jQuery as most trustworthy, and user scripts as least trustworthy. More...
 
string[] null $skins = null
 Skin names. More...
 
string[] $targets = [ 'desktop' ]
 What client platforms the module targets (e.g. More...
 
array $versionHash = []
 Map of (context hash => cached module version hash) More...
 

Detailed Description

Abstraction for ResourceLoader modules which pull from wiki pages.

This can only be used for wiki pages in the MediaWiki and User namespaces, because of its dependence on the functionality of Title::isUserConfigPage() and Title::isSiteConfigPage().

This module supports being used as a placeholder for a module on a remote wiki. To do so, getDB() must be overloaded to return a foreign database object that allows local wikis to query page metadata.

Safe for calls on local wikis are:

Since
1.17

Definition at line 65 of file WikiModule.php.

Constructor & Destructor Documentation

◆ __construct()

MediaWiki\ResourceLoader\WikiModule::__construct ( array  $options = null)
Parameters
array | null$optionsFor back-compat, this can be omitted in favour of overwriting getPages.

Definition at line 103 of file WikiModule.php.

Member Function Documentation

◆ enableModuleContentVersion()

MediaWiki\ResourceLoader\WikiModule::enableModuleContentVersion ( )

Disable module content versioning.

This class does not support generating content outside of a module request due to foreign database support.

See getDefinitionSummary() for meta-data versioning.

Returns
bool

Reimplemented from MediaWiki\ResourceLoader\Module.

Definition at line 454 of file WikiModule.php.

◆ fetchTitleInfo()

static MediaWiki\ResourceLoader\WikiModule::fetchTitleInfo ( IDatabase  $db,
array  $pages,
  $fname = __METHOD__ 
)
staticprotected
Parameters
IDatabase$db
array$pages
string$fname
Returns
array

Definition at line 553 of file WikiModule.php.

References $res, $title, Title\newFromText(), and Wikimedia\Rdbms\IDatabase\select().

◆ getContent()

MediaWiki\ResourceLoader\WikiModule::getContent (   $titleText,
Context  $context 
)
protected
Parameters
string$titleText
Context$context
Returns
null|string
Since
1.32 added the $context parameter

Definition at line 195 of file WikiModule.php.

References $content, $title, CONTENT_FORMAT_CSS, CONTENT_FORMAT_JAVASCRIPT, CONTENT_FORMAT_JSON, MediaWiki\ResourceLoader\WikiModule\getContentObj(), and MediaWiki\MediaWikiServices\getInstance().

Referenced by MediaWiki\ResourceLoader\WikiModule\getScript().

◆ getContentObj()

MediaWiki\ResourceLoader\WikiModule::getContentObj ( PageIdentity  $page,
Context  $context,
  $maxRedirects = 1 
)
protected
Parameters
PageIdentity$page
Context$context
int$maxRedirectsMaximum number of redirects to follow. Either 0 or 1.
Returns
Content|null
Since
1.32 added the $context and $maxRedirects parameters
Access: internal
for testing

Definition at line 230 of file WikiModule.php.

References $content, MediaWiki\ResourceLoader\Context\getContentOverrideCallback(), MediaWiki\MediaWikiServices\getInstance(), MediaWiki\ResourceLoader\Module\getLogger(), MediaWiki\Revision\SlotRecord\MAIN, and MediaWiki\Revision\RevisionRecord\RAW.

Referenced by MediaWiki\ResourceLoader\WikiModule\getContent().

◆ getDB()

MediaWiki\ResourceLoader\WikiModule::getDB ( )
protected

Get the Database handle used for computing the module version.

Subclasses may override this to return a foreign database, which would allow them to register a module on wiki A that fetches wiki pages from wiki B.

The way this works is that the local module is a placeholder that can only computer a module version hash. The 'source' of the module must be set to the foreign wiki directly. Methods getScript() and getContent() will not use this handle and are not valid on the local wiki.

Returns
IDatabase

Definition at line 185 of file WikiModule.php.

References DB_REPLICA, and wfGetDB().

◆ getDefinitionSummary()

MediaWiki\ResourceLoader\WikiModule::getDefinitionSummary ( Context  $context)
Parameters
Context$context
Returns
array

Reimplemented from MediaWiki\ResourceLoader\Module.

Definition at line 462 of file WikiModule.php.

◆ getGroup()

MediaWiki\ResourceLoader\WikiModule::getGroup ( )

◆ getPages()

MediaWiki\ResourceLoader\WikiModule::getPages ( Context  $context)
protected

Subclasses should return an associative array of resources in the module.

Keys should be the title of a page in the MediaWiki or User namespace.

Values should be a nested array of options. The supported keys are 'type' and (CSS only) 'media'.

For scripts, 'type' should be 'script'. For JSON files, 'type' should be 'data'. For stylesheets, 'type' should be 'style'.

There is an optional 'media' key, the value of which can be the medium ('screen', 'print', etc.) of the stylesheet.

Parameters
Context$context
Returns
array[]

Reimplemented in MediaWiki\ResourceLoader\UserStylesModule, MediaWiki\ResourceLoader\UserModule, MediaWiki\ResourceLoader\SiteStylesModule, and MediaWiki\ResourceLoader\SiteModule.

Definition at line 139 of file WikiModule.php.

References MediaWiki\ResourceLoader\Module\$config, Config\get(), MediaWiki\ResourceLoader\Module\getConfig(), MediaWiki\MainConfigNames\UseSiteCss, and MediaWiki\MainConfigNames\UseSiteJs.

Referenced by MediaWiki\ResourceLoader\WikiModule\getScript(), and MediaWiki\ResourceLoader\WikiModule\shouldEmbedModule().

◆ getRequireKey()

MediaWiki\ResourceLoader\WikiModule::getRequireKey ( string  $titleText)
protected

Convert a namespace-formatted page title to a virtual package file name.

This determines how the page may be imported in client-side code via require().

Stability: stable
to override
Since
1.38
Parameters
string$titleText
Returns
string

Definition at line 350 of file WikiModule.php.

◆ getScript()

◆ getStyles()

MediaWiki\ResourceLoader\WikiModule::getStyles ( Context  $context)
Parameters
Context$context
Returns
array

Reimplemented from MediaWiki\ResourceLoader\Module.

Definition at line 406 of file WikiModule.php.

References MemoizedCallable\call().

◆ getTitleInfo()

MediaWiki\ResourceLoader\WikiModule::getTitleInfo ( Context  $context)
protected

Get the information about the wiki pages for a given context.

Parameters
Context$context
Returns
array[] Keyed by page name

Definition at line 516 of file WikiModule.php.

References $content, $dbr, $title, MediaWiki\ResourceLoader\Context\getContentOverrideCallback(), getDB(), and Title\newFromText().

◆ getType()

MediaWiki\ResourceLoader\WikiModule::getType ( )
Since
1.28
Returns
string

Reimplemented from MediaWiki\ResourceLoader\Module.

Reimplemented in MediaWiki\ResourceLoader\UserStylesModule, and MediaWiki\ResourceLoader\SiteStylesModule.

Definition at line 715 of file WikiModule.php.

◆ invalidateModuleCache()

static MediaWiki\ResourceLoader\WikiModule::invalidateModuleCache ( PageIdentity  $page,
?RevisionRecord  $old,
?RevisionRecord  $new,
string  $domain 
)
static

Clear the preloadTitleInfo() cache for all wiki modules on this wiki on page change if it was a JS or CSS page.

Access: internal
Parameters
PageIdentity$page
RevisionRecord | null$oldPrior page revision
RevisionRecord | null$newNew page revision
string$domainDatabase domain ID

Definition at line 674 of file WikiModule.php.

References $cache, $title, Title\castFromPageIdentity(), CONTENT_MODEL_CSS, CONTENT_MODEL_JAVASCRIPT, and MediaWiki\Revision\RevisionRecord\getSlot().

Referenced by MediaWiki\Storage\DerivedPageDataUpdater\doUpdates().

◆ isKnownEmpty()

MediaWiki\ResourceLoader\WikiModule::isKnownEmpty ( Context  $context)
Parameters
Context$context
Returns
bool

Reimplemented from MediaWiki\ResourceLoader\Module.

Definition at line 476 of file WikiModule.php.

◆ isPackaged()

MediaWiki\ResourceLoader\WikiModule::isPackaged ( )
protected

Get whether this module is a packaged module.

If false (the default), JavaScript pages are concatenated and executed as a single script. JSON pages are not supported.

If true, the pages are bundled such that each page gets a virtual file name, where only the "main" script will be executed at first, and other JS or JSON pages may be be imported in client-side code through the require() function.

Stability: stable
to override
Since
1.38
Returns
bool

Definition at line 326 of file WikiModule.php.

Referenced by MediaWiki\ResourceLoader\WikiModule\getScript().

◆ preloadTitleInfo()

static MediaWiki\ResourceLoader\WikiModule::preloadTitleInfo ( Context  $context,
IDatabase  $db,
array  $moduleNames 
)
static

◆ shouldEmbedModule()

MediaWiki\ResourceLoader\WikiModule::shouldEmbedModule ( Context  $context)

◆ supportsURLLoading()

MediaWiki\ResourceLoader\WikiModule::supportsURLLoading ( )
Returns
bool

Reimplemented from MediaWiki\ResourceLoader\Module.

Definition at line 335 of file WikiModule.php.

Member Data Documentation

◆ $datas

array MediaWiki\ResourceLoader\WikiModule::$datas = []
protected

List of page names that contain JSON.

Definition at line 94 of file WikiModule.php.

◆ $group

string null MediaWiki\ResourceLoader\WikiModule::$group
protected

◆ $origin

string MediaWiki\ResourceLoader\WikiModule::$origin = self::ORIGIN_USER_SITEWIDE
protected

Origin defaults to users with sitewide authority.

Definition at line 67 of file WikiModule.php.

◆ $scripts

array MediaWiki\ResourceLoader\WikiModule::$scripts = []
protected

List of page names that contain JavaScript.

Definition at line 91 of file WikiModule.php.

Referenced by MediaWiki\ResourceLoader\WikiModule\getScript().

◆ $styles

array MediaWiki\ResourceLoader\WikiModule::$styles = []
protected

List of page names that contain CSS.

Definition at line 88 of file WikiModule.php.

◆ $titleInfo

array MediaWiki\ResourceLoader\WikiModule::$titleInfo = []
protected

In-process cache for title info, structured as an array [ <batchKey> // Pipe-separated list of sorted keys from getPages => [ <titleKey> => [ // Normalised title key 'page_len' => .., 'page_latest' => .., 'page_touched' => .., ] ] ].

See also
self::fetchTitleInfo()
self::makeTitleKey()

Definition at line 85 of file WikiModule.php.


The documentation for this class was generated from the following file: