2010-10-19 18:25:42 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2012-04-30 07:16:10 +00:00
|
|
|
* Abstraction for resource loader modules which pull from wiki pages.
|
|
|
|
|
*
|
2010-10-19 18:25:42 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @author Trevor Parscal
|
|
|
|
|
* @author Roan Kattouw
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Abstraction for resource loader modules which pull from wiki pages
|
2011-10-14 08:06:54 +00:00
|
|
|
*
|
|
|
|
|
* This can only be used for wiki pages in the MediaWiki and User namespaces,
|
|
|
|
|
* because of its dependence on the functionality of
|
2011-12-13 11:54:02 +00:00
|
|
|
* Title::isCssJsSubpage.
|
2010-10-19 18:25:42 +00:00
|
|
|
*/
|
|
|
|
|
abstract class ResourceLoaderWikiModule extends ResourceLoaderModule {
|
2011-10-14 08:06:54 +00:00
|
|
|
|
2010-10-19 18:25:42 +00:00
|
|
|
/* Protected Members */
|
2011-02-04 16:39:17 +00:00
|
|
|
|
|
|
|
|
# Origin is user-supplied code
|
|
|
|
|
protected $origin = self::ORIGIN_USER_SITEWIDE;
|
2011-10-14 08:06:54 +00:00
|
|
|
|
2011-02-19 17:07:05 +00:00
|
|
|
// In-object cache for title mtimes
|
|
|
|
|
protected $titleMtimes = array();
|
2011-10-14 08:06:54 +00:00
|
|
|
|
2010-10-19 18:25:42 +00:00
|
|
|
/* Abstract Protected Methods */
|
2011-10-14 08:06:54 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $context ResourceLoaderContext
|
|
|
|
|
*/
|
2010-10-19 18:25:42 +00:00
|
|
|
abstract protected function getPages( ResourceLoaderContext $context );
|
2011-10-14 08:06:54 +00:00
|
|
|
|
2010-10-19 18:25:42 +00:00
|
|
|
/* Protected Methods */
|
2011-10-14 08:06:54 +00:00
|
|
|
|
2011-08-02 15:47:30 +00:00
|
|
|
/**
|
|
|
|
|
* Get the Database object used in getTitleMTimes(). Defaults to the local slave DB
|
2011-11-02 15:04:34 +00:00
|
|
|
* but subclasses may want to override this to return a remote DB object, or to return
|
|
|
|
|
* null if getTitleMTimes() shouldn't access the DB at all.
|
2011-10-14 08:06:54 +00:00
|
|
|
*
|
2011-08-02 15:47:30 +00:00
|
|
|
* NOTE: This ONLY works for getTitleMTimes() and getModifiedTime(), NOT FOR ANYTHING ELSE.
|
|
|
|
|
* In particular, it doesn't work for getting the content of JS and CSS pages. That functionality
|
|
|
|
|
* will use the local DB irrespective of the return value of this method.
|
2011-10-14 08:06:54 +00:00
|
|
|
*
|
2011-11-02 15:04:34 +00:00
|
|
|
* @return DatabaseBase|null
|
2011-08-02 15:47:30 +00:00
|
|
|
*/
|
|
|
|
|
protected function getDB() {
|
|
|
|
|
return wfGetDB( DB_SLAVE );
|
|
|
|
|
}
|
2011-02-08 23:09:22 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $title Title
|
|
|
|
|
* @return null|string
|
|
|
|
|
*/
|
2011-02-08 06:34:38 +00:00
|
|
|
protected function getContent( $title ) {
|
|
|
|
|
if ( $title->getNamespace() === NS_MEDIAWIKI ) {
|
2012-04-26 17:23:02 +00:00
|
|
|
// The first "true" is to use the database, the second is to use the content langue
|
|
|
|
|
// and the last one is to specify the message key already contains the language in it ("/de", etc.)
|
|
|
|
|
$text = MessageCache::singleton()->get( $title->getDBkey(), true, true, true );
|
|
|
|
|
return $text === false ? '' : $text;
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
2011-08-02 16:00:42 +00:00
|
|
|
if ( !$title->isCssJsSubpage() && !$title->isCssOrJsPage() ) {
|
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2012-08-30 03:04:30 +00:00
|
|
|
$revision = Revision::newFromTitle( $title, false, Revision::READ_NORMAL );
|
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
|
|
|
if ( !$revision ) {
|
|
|
|
|
return null;
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
|
|
|
return $revision->getRawText();
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
2011-10-14 08:06:54 +00:00
|
|
|
|
2010-10-19 18:25:42 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
2011-05-21 17:45:20 +00:00
|
|
|
/**
|
|
|
|
|
* @param $context ResourceLoaderContext
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2010-10-19 18:25:42 +00:00
|
|
|
public function getScript( ResourceLoaderContext $context ) {
|
|
|
|
|
$scripts = '';
|
2011-02-08 06:34:38 +00:00
|
|
|
foreach ( $this->getPages( $context ) as $titleText => $options ) {
|
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
|
|
|
if ( $options['type'] !== 'script' ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2011-02-08 06:34:38 +00:00
|
|
|
$title = Title::newFromText( $titleText );
|
2011-08-10 14:23:25 +00:00
|
|
|
if ( !$title || $title->isRedirect() ) {
|
2011-02-08 06:34:38 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$script = $this->getContent( $title );
|
|
|
|
|
if ( strval( $script ) !== '' ) {
|
2011-07-06 21:48:09 +00:00
|
|
|
$script = $this->validateScriptFile( $titleText, $script );
|
2011-02-08 06:34:38 +00:00
|
|
|
if ( strpos( $titleText, '*/' ) === false ) {
|
|
|
|
|
$scripts .= "/* $titleText */\n";
|
|
|
|
|
}
|
|
|
|
|
$scripts .= $script . "\n";
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $scripts;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-21 17:45:20 +00:00
|
|
|
/**
|
|
|
|
|
* @param $context ResourceLoaderContext
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2010-10-19 18:25:42 +00:00
|
|
|
public function getStyles( ResourceLoaderContext $context ) {
|
2011-02-11 22:57:32 +00:00
|
|
|
global $wgScriptPath;
|
2011-10-14 08:06:54 +00:00
|
|
|
|
2010-10-19 18:25:42 +00:00
|
|
|
$styles = array();
|
2011-02-08 06:34:38 +00:00
|
|
|
foreach ( $this->getPages( $context ) as $titleText => $options ) {
|
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
|
|
|
if ( $options['type'] !== 'style' ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2011-02-08 06:34:38 +00:00
|
|
|
$title = Title::newFromText( $titleText );
|
2011-08-10 14:23:25 +00:00
|
|
|
if ( !$title || $title->isRedirect() ) {
|
2011-02-08 06:34:38 +00:00
|
|
|
continue;
|
2011-10-14 08:06:54 +00:00
|
|
|
}
|
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
|
|
|
$media = isset( $options['media'] ) ? $options['media'] : 'all';
|
2011-02-08 06:34:38 +00:00
|
|
|
$style = $this->getContent( $title );
|
|
|
|
|
if ( strval( $style ) === '' ) {
|
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2010-12-16 19:31:48 +00:00
|
|
|
if ( $this->getFlip( $context ) ) {
|
|
|
|
|
$style = CSSJanus::transform( $style, true, false );
|
|
|
|
|
}
|
2011-02-11 22:57:32 +00:00
|
|
|
$style = CSSMin::remap( $style, false, $wgScriptPath, true );
|
* Introduced Xml::encodeJsCall(), to replace the awkward repetitive code that was doing the same thing throughout the resource loader with varying degrees of security and correctness.
* Modified Xml::encodeJsVar() to allow it to pass through JS expressions without encoding, using a special object.
* In ResourceLoader::makeModuleResponse(), renamed $messages to $messagesBlob to make it clear that it's JSON-encoded, not an array.
* Fixed MessageBlobStore to store {} for an empty message array instead of [].
* In ResourceLoader::makeMessageSetScript(), fixed call to non-existent function mediaWiki.msg.set.
* For security, changed the calling convention of makeMessageSetScript() and makeLoaderImplementScript() to require explicit object construction of XmlJsCode() before interpreting their input as JS code.
* Documented several ResourceLoader static functions.
* In ResourceLoaderWikiModule, for readability, reduced the indenting level by flipping some if blocks and adding continue statements.
* In makeCustomLoaderScript(), allow non-numeric $version. The only caller I can find is already sending a non-numeric $version, presumably it was broken. Luckily there aren't any loader scripts in existence, I had to make one to test it.
* wfGetDb -> wfGetDB
* Added an extra line break in the startup module output, for readability.
* In ResourceLoaderStartUpModule::getModuleRegistrations(), fixed another assignment expression
2010-11-04 07:53:37 +00:00
|
|
|
if ( !isset( $styles[$media] ) ) {
|
|
|
|
|
$styles[$media] = '';
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
2011-02-08 06:34:38 +00:00
|
|
|
if ( strpos( $titleText, '*/' ) === false ) {
|
|
|
|
|
$styles[$media] .= "/* $titleText */\n";
|
|
|
|
|
}
|
|
|
|
|
$styles[$media] .= $style . "\n";
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
|
|
|
|
return $styles;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-21 17:45:20 +00:00
|
|
|
/**
|
|
|
|
|
* @param $context ResourceLoaderContext
|
|
|
|
|
* @return int|mixed
|
|
|
|
|
*/
|
2010-10-19 18:25:42 +00:00
|
|
|
public function getModifiedTime( ResourceLoaderContext $context ) {
|
|
|
|
|
$modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
|
2011-02-19 17:07:05 +00:00
|
|
|
$mtimes = $this->getTitleMtimes( $context );
|
|
|
|
|
if ( count( $mtimes ) ) {
|
|
|
|
|
$modifiedTime = max( $modifiedTime, max( $mtimes ) );
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
2012-03-07 19:33:37 +00:00
|
|
|
$modifiedTime = max( $modifiedTime, $this->getMsgBlobMtime( $context->getLanguage() ) );
|
2011-02-08 06:34:38 +00:00
|
|
|
return $modifiedTime;
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
2011-05-21 17:45:20 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $context ResourceLoaderContext
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2011-02-19 17:07:05 +00:00
|
|
|
public function isKnownEmpty( ResourceLoaderContext $context ) {
|
|
|
|
|
return count( $this->getTitleMtimes( $context ) ) == 0;
|
|
|
|
|
}
|
2011-02-18 00:33:45 +00:00
|
|
|
|
2011-02-19 17:07:05 +00:00
|
|
|
/**
|
|
|
|
|
* Get the modification times of all titles that would be loaded for
|
|
|
|
|
* a given context.
|
|
|
|
|
* @param $context ResourceLoaderContext: Context object
|
|
|
|
|
* @return array( prefixed DB key => UNIX timestamp ), nonexistent titles are dropped
|
|
|
|
|
*/
|
|
|
|
|
protected function getTitleMtimes( ResourceLoaderContext $context ) {
|
2011-11-02 15:04:34 +00:00
|
|
|
$dbr = $this->getDB();
|
|
|
|
|
if ( !$dbr ) {
|
|
|
|
|
// We're dealing with a subclass that doesn't have a DB
|
|
|
|
|
return array();
|
|
|
|
|
}
|
2012-08-30 03:04:30 +00:00
|
|
|
|
2011-02-19 17:07:05 +00:00
|
|
|
$hash = $context->getHash();
|
|
|
|
|
if ( isset( $this->titleMtimes[$hash] ) ) {
|
|
|
|
|
return $this->titleMtimes[$hash];
|
|
|
|
|
}
|
2011-10-14 08:06:54 +00:00
|
|
|
|
2011-02-19 17:07:05 +00:00
|
|
|
$this->titleMtimes[$hash] = array();
|
|
|
|
|
$batch = new LinkBatch;
|
|
|
|
|
foreach ( $this->getPages( $context ) as $titleText => $options ) {
|
|
|
|
|
$batch->addObj( Title::newFromText( $titleText ) );
|
|
|
|
|
}
|
2011-10-14 08:06:54 +00:00
|
|
|
|
2011-02-19 17:07:05 +00:00
|
|
|
if ( !$batch->isEmpty() ) {
|
|
|
|
|
$res = $dbr->select( 'page',
|
|
|
|
|
array( 'page_namespace', 'page_title', 'page_touched' ),
|
|
|
|
|
$batch->constructSet( 'page', $dbr ),
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
$title = Title::makeTitle( $row->page_namespace, $row->page_title );
|
|
|
|
|
$this->titleMtimes[$hash][$title->getPrefixedDBkey()] =
|
|
|
|
|
wfTimestamp( TS_UNIX, $row->page_touched );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $this->titleMtimes[$hash];
|
|
|
|
|
}
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|