MediaWiki  1.23.12
ResourceLoaderWikiModule.php
Go to the documentation of this file.
1 <?php
33 
34  /* Protected Members */
35 
36  # Origin is user-supplied code
38 
39  // In-object cache for title mtimes
40  protected $titleMtimes = array();
41 
42  /* Abstract Protected Methods */
43 
60  abstract protected function getPages( ResourceLoaderContext $context );
61 
62  /* Protected Methods */
63 
75  protected function getDB() {
76  return wfGetDB( DB_SLAVE );
77  }
78 
83  protected function getContent( $title ) {
84  if ( !$title->isCssJsSubpage() && !$title->isCssOrJsPage() ) {
85  return null;
86  }
88  if ( !$revision ) {
89  return null;
90  }
91 
92  $content = $revision->getContent( Revision::RAW );
93 
94  if ( !$content ) {
95  wfDebugLog( 'resourceloader', __METHOD__ . ': failed to load content of JS/CSS page!' );
96  return null;
97  }
98 
99  $model = $content->getModel();
100 
101  if ( $model !== CONTENT_MODEL_CSS && $model !== CONTENT_MODEL_JAVASCRIPT ) {
102  wfDebugLog( 'resourceloader', __METHOD__ . ': bad content model $model for JS/CSS page!' );
103  return null;
104  }
105 
106  return $content->getNativeData(); //NOTE: this is safe, we know it's JS or CSS
107  }
108 
109  /* Methods */
110 
115  public function getScript( ResourceLoaderContext $context ) {
116  $scripts = '';
117  foreach ( $this->getPages( $context ) as $titleText => $options ) {
118  if ( $options['type'] !== 'script' ) {
119  continue;
120  }
121  $title = Title::newFromText( $titleText );
122  if ( !$title || $title->isRedirect() ) {
123  continue;
124  }
125  $script = $this->getContent( $title );
126  if ( strval( $script ) !== '' ) {
127  $script = $this->validateScriptFile( $titleText, $script );
128  if ( strpos( $titleText, '*/' ) === false ) {
129  $scripts .= "/* $titleText */\n";
130  }
131  $scripts .= $script . "\n";
132  }
133  }
134  return $scripts;
135  }
136 
141  public function getStyles( ResourceLoaderContext $context ) {
142  global $wgScriptPath;
143 
144  $styles = array();
145  foreach ( $this->getPages( $context ) as $titleText => $options ) {
146  if ( $options['type'] !== 'style' ) {
147  continue;
148  }
149  $title = Title::newFromText( $titleText );
150  if ( !$title || $title->isRedirect() ) {
151  continue;
152  }
153  $media = isset( $options['media'] ) ? $options['media'] : 'all';
154  $style = $this->getContent( $title );
155  if ( strval( $style ) === '' ) {
156  continue;
157  }
158  if ( $this->getFlip( $context ) ) {
159  $style = CSSJanus::transform( $style, true, false );
160  }
161  $style = CSSMin::remap( $style, false, $wgScriptPath, true );
162  if ( !isset( $styles[$media] ) ) {
163  $styles[$media] = array();
164  }
165  if ( strpos( $titleText, '*/' ) === false ) {
166  $style = "/* $titleText */\n" . $style;
167  }
168  $styles[$media][] = $style;
169  }
170  return $styles;
171  }
172 
177  public function getModifiedTime( ResourceLoaderContext $context ) {
178  $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
179  $mtimes = $this->getTitleMtimes( $context );
180  if ( count( $mtimes ) ) {
181  $modifiedTime = max( $modifiedTime, max( $mtimes ) );
182  }
183  $modifiedTime = max(
184  $modifiedTime,
185  $this->getMsgBlobMtime( $context->getLanguage() ),
186  $this->getDefinitionMtime( $context )
187  );
188  return $modifiedTime;
189  }
190 
196  public function getDefinitionSummary( ResourceLoaderContext $context ) {
197  return array(
198  'class' => get_class( $this ),
199  'pages' => $this->getPages( $context ),
200  );
201  }
202 
207  public function isKnownEmpty( ResourceLoaderContext $context ) {
208  return count( $this->getTitleMtimes( $context ) ) == 0;
209  }
210 
217  protected function getTitleMtimes( ResourceLoaderContext $context ) {
218  $dbr = $this->getDB();
219  if ( !$dbr ) {
220  // We're dealing with a subclass that doesn't have a DB
221  return array();
222  }
223 
224  $hash = $context->getHash();
225  if ( isset( $this->titleMtimes[$hash] ) ) {
226  return $this->titleMtimes[$hash];
227  }
228 
229  $this->titleMtimes[$hash] = array();
230  $batch = new LinkBatch;
231  foreach ( $this->getPages( $context ) as $titleText => $options ) {
232  $batch->addObj( Title::newFromText( $titleText ) );
233  }
234 
235  if ( !$batch->isEmpty() ) {
236  $res = $dbr->select( 'page',
237  array( 'page_namespace', 'page_title', 'page_touched' ),
238  $batch->constructSet( 'page', $dbr ),
239  __METHOD__
240  );
241  foreach ( $res as $row ) {
242  $title = Title::makeTitle( $row->page_namespace, $row->page_title );
243  $this->titleMtimes[$hash][$title->getPrefixedDBkey()] =
244  wfTimestamp( TS_UNIX, $row->page_touched );
245  }
246  }
247  return $this->titleMtimes[$hash];
248  }
249 }
Title\makeTitle
static & makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:398
ResourceLoaderContext
Object passed around to modules which contains information about the state of a specific loader reque...
Definition: ResourceLoaderContext.php:29
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:189
ResourceLoaderModule\getFlip
getFlip( $context)
Definition: ResourceLoaderModule.php:115
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
LinkBatch
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:30
ResourceLoaderWikiModule\$titleMtimes
$titleMtimes
Definition: ResourceLoaderWikiModule.php:40
ResourceLoaderWikiModule\getDB
getDB()
Get the Database object used in getTitleMTimes().
Definition: ResourceLoaderWikiModule.php:75
ResourceLoaderModule\ORIGIN_USER_SITEWIDE
const ORIGIN_USER_SITEWIDE
Definition: ResourceLoaderModule.php:44
CSSMin\remap
static remap( $source, $local, $remote, $embedData=true)
Remaps CSS URL paths and automatically embeds data URIs for CSS rules or url() values preceded by an ...
Definition: CSSMin.php:187
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3706
ResourceLoaderWikiModule\getStyles
getStyles(ResourceLoaderContext $context)
Definition: ResourceLoaderWikiModule.php:141
CONTENT_MODEL_CSS
const CONTENT_MODEL_CSS
Definition: Defines.php:285
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2530
ResourceLoaderWikiModule\getModifiedTime
getModifiedTime(ResourceLoaderContext $context)
Definition: ResourceLoaderWikiModule.php:177
wfDebugLog
wfDebugLog( $logGroup, $text, $dest='all')
Send a line to a supplementary debug log file, if configured, or main debug log if not.
Definition: GlobalFunctions.php:1087
ResourceLoaderWikiModule
Abstraction for resource loader modules which pull from wiki pages.
Definition: ResourceLoaderWikiModule.php:32
ResourceLoaderWikiModule\getScript
getScript(ResourceLoaderContext $context)
Definition: ResourceLoaderWikiModule.php:115
ResourceLoaderWikiModule\isKnownEmpty
isKnownEmpty(ResourceLoaderContext $context)
Definition: ResourceLoaderWikiModule.php:207
$dbr
$dbr
Definition: testCompression.php:48
IDBAccessObject\READ_NORMAL
const READ_NORMAL
Definition: IDBAccessObject.php:53
ResourceLoaderModule\getDefinitionMtime
getDefinitionMtime(ResourceLoaderContext $context)
Helper method for calculating when this module's definition summary was last changed.
Definition: ResourceLoaderModule.php:447
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
ResourceLoaderContext\getLanguage
getLanguage()
Definition: ResourceLoaderContext.php:143
$options
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:1530
Revision\newFromTitle
static newFromTitle( $title, $id=0, $flags=0)
Load either the current, or a specified, revision that's attached to a given title.
Definition: Revision.php:106
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
Revision\RAW
const RAW
Definition: Revision.php:74
ResourceLoaderModule\validateScriptFile
validateScriptFile( $fileName, $contents)
Validate a given script file; if valid returns the original source.
Definition: ResourceLoaderModule.php:541
$hash
return false to override stock group addition can be modified try getUserPermissionsErrors userCan checks are continued by internal code can override on output return false to not delete it return false to override the default password checks & $hash
Definition: hooks.txt:2702
ResourceLoaderModule\getMsgBlobMtime
getMsgBlobMtime( $lang)
Get the last modification timestamp of the message blob for this module in a given language.
Definition: ResourceLoaderModule.php:340
ResourceLoaderWikiModule\$origin
$origin
Definition: ResourceLoaderWikiModule.php:37
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:55
ResourceLoaderModule
Abstraction for resource loader modules, with name registration and maxage functionality.
Definition: ResourceLoaderModule.php:28
ResourceLoaderWikiModule\getTitleMtimes
getTitleMtimes(ResourceLoaderContext $context)
Get the modification times of all titles that would be loaded for a given context.
Definition: ResourceLoaderWikiModule.php:217
TS_UNIX
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
Definition: GlobalFunctions.php:2473
as
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
ResourceLoaderWikiModule\getDefinitionSummary
getDefinitionSummary(ResourceLoaderContext $context)
Get the definition summary for this module.
Definition: ResourceLoaderWikiModule.php:196
$batch
$batch
Definition: linkcache.txt:23
ResourceLoaderWikiModule\getPages
getPages(ResourceLoaderContext $context)
Subclasses should return an associative array of resources in the module.
CSSJanus\transform
static transform( $css, $swapLtrRtlInURL=false, $swapLeftRightInURL=false)
Transform an LTR stylesheet to RTL.
Definition: CSSJanus.php:139
CONTENT_MODEL_JAVASCRIPT
const CONTENT_MODEL_JAVASCRIPT
Definition: Defines.php:284
ResourceLoaderContext\getHash
getHash()
Definition: ResourceLoaderContext.php:231
ResourceLoaderWikiModule\getContent
getContent( $title)
Definition: ResourceLoaderWikiModule.php:83
$res
$res
Definition: database.txt:21