2010-09-04 04:00:09 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
2010-09-04 12:53:01 +00:00
|
|
|
*
|
2010-09-05 13:31:34 +00:00
|
|
|
* @file
|
2010-09-04 04:00:09 +00:00
|
|
|
* @author Trevor Parscal
|
|
|
|
|
* @author Roan Kattouw
|
|
|
|
|
*/
|
|
|
|
|
|
2010-09-24 17:31:40 +00:00
|
|
|
defined( 'MEDIAWIKI' ) || die( 1 );
|
|
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
2010-09-10 23:28:59 +00:00
|
|
|
* Abstraction for resource loader modules, with name registration and maxage functionality.
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
|
|
|
|
abstract class ResourceLoaderModule {
|
|
|
|
|
/* Protected Members */
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
protected $name = null;
|
2010-09-23 21:23:51 +00:00
|
|
|
|
|
|
|
|
// In-object cache for file dependencies
|
|
|
|
|
protected $fileDeps = array();
|
|
|
|
|
// In-object cache for message blob mtime
|
|
|
|
|
protected $msgBlobMtime = array();
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/* Methods */
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Get this module's name. This is set when the module is registered
|
|
|
|
|
* with ResourceLoader::register()
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @return Mixed: name (string) or null if no name was set
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
|
|
|
|
public function getName() {
|
|
|
|
|
return $this->name;
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Set this module's name. This is called by ResourceLodaer::register()
|
|
|
|
|
* when registering the module. Other code should not call this.
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @param $name String: name
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
|
|
|
|
public function setName( $name ) {
|
|
|
|
|
$this->name = $name;
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 10:53:57 +00:00
|
|
|
/**
|
|
|
|
|
* Get whether CSS for this module should be flipped
|
|
|
|
|
*/
|
|
|
|
|
public function getFlip( $context ) {
|
|
|
|
|
return $context->getDirection() === 'rtl';
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Get all JS for this module for a given language and skin.
|
|
|
|
|
* Includes all relevant JS except loader scripts.
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @param $context ResourceLoaderContext object
|
|
|
|
|
* @return String: JS
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
2010-09-11 03:26:15 +00:00
|
|
|
public function getScript( ResourceLoaderContext $context ) {
|
|
|
|
|
// Stub, override expected
|
|
|
|
|
return '';
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Get all CSS for this module for a given skin.
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @param $context ResourceLoaderContext object
|
2010-09-10 00:21:36 +00:00
|
|
|
* @return array: strings of CSS keyed by media type
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
2010-09-11 03:26:15 +00:00
|
|
|
public function getStyles( ResourceLoaderContext $context ) {
|
|
|
|
|
// Stub, override expected
|
|
|
|
|
return '';
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Get the messages needed for this module.
|
|
|
|
|
*
|
|
|
|
|
* To get a JSON blob with messages, use MessageBlobStore::get()
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
2010-09-04 04:00:09 +00:00
|
|
|
* @return array of message keys. Keys may occur more than once
|
|
|
|
|
*/
|
2010-09-11 03:26:15 +00:00
|
|
|
public function getMessages() {
|
|
|
|
|
// Stub, override expected
|
|
|
|
|
return array();
|
|
|
|
|
}
|
2010-09-20 21:23:29 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the group this module is in.
|
|
|
|
|
*
|
|
|
|
|
* @return string of group name
|
|
|
|
|
*/
|
|
|
|
|
public function getGroup() {
|
|
|
|
|
// Stub, override expected
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Get the loader JS for this module, if set.
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @return Mixed: loader JS (string) or false if no custom loader set
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
2010-09-11 03:26:15 +00:00
|
|
|
public function getLoaderScript() {
|
|
|
|
|
// Stub, override expected
|
2010-09-20 21:23:29 +00:00
|
|
|
return false;
|
2010-09-11 03:26:15 +00:00
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Get a list of modules this module depends on.
|
|
|
|
|
*
|
|
|
|
|
* Dependency information is taken into account when loading a module
|
|
|
|
|
* on the client side. When adding a module on the server side,
|
|
|
|
|
* dependency information is NOT taken into account and YOU are
|
|
|
|
|
* responsible for adding dependent modules as well. If you don't do
|
|
|
|
|
* this, the client side loader will send a second request back to the
|
|
|
|
|
* server to fetch the missing modules, which kind of defeats the
|
|
|
|
|
* purpose of the resource loader.
|
|
|
|
|
*
|
|
|
|
|
* To add dependencies dynamically on the client side, use a custom
|
|
|
|
|
* loader script, see getLoaderScript()
|
2010-09-05 13:31:34 +00:00
|
|
|
* @return Array of module names (strings)
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
2010-09-11 03:26:15 +00:00
|
|
|
public function getDependencies() {
|
|
|
|
|
// Stub, override expected
|
|
|
|
|
return array();
|
|
|
|
|
}
|
2010-09-23 21:23:51 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the files this module depends on indirectly for a given skin.
|
|
|
|
|
* Currently these are only image files referenced by the module's CSS.
|
|
|
|
|
*
|
|
|
|
|
* @param $skin String: skin name
|
|
|
|
|
* @return array of files
|
|
|
|
|
*/
|
|
|
|
|
public function getFileDependencies( $skin ) {
|
|
|
|
|
// Try in-object cache first
|
|
|
|
|
if ( isset( $this->fileDeps[$skin] ) ) {
|
|
|
|
|
return $this->fileDeps[$skin];
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-23 21:23:51 +00:00
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
|
|
|
|
$deps = $dbr->selectField( 'module_deps', 'md_deps', array(
|
|
|
|
|
'md_module' => $this->getName(),
|
|
|
|
|
'md_skin' => $skin,
|
|
|
|
|
), __METHOD__
|
|
|
|
|
);
|
|
|
|
|
if ( !is_null( $deps ) ) {
|
2010-09-24 17:19:27 +00:00
|
|
|
return $this->fileDeps[$skin] = (array) FormatJson::decode( $deps, true );
|
2010-09-23 21:23:51 +00:00
|
|
|
}
|
2010-09-24 17:19:27 +00:00
|
|
|
return array();
|
2010-09-23 21:23:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set preloaded file dependency information. Used so we can load this
|
|
|
|
|
* information for all modules at once.
|
|
|
|
|
* @param $skin string Skin name
|
|
|
|
|
* @param $deps array Array of file names
|
|
|
|
|
*/
|
|
|
|
|
public function setFileDependencies( $skin, $deps ) {
|
|
|
|
|
$this->fileDeps[$skin] = $deps;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the last modification timestamp of the message blob for this
|
|
|
|
|
* module in a given language.
|
|
|
|
|
* @param $lang string Language code
|
|
|
|
|
* @return int UNIX timestamp, or 0 if no blob found
|
|
|
|
|
*/
|
|
|
|
|
public function getMsgBlobMtime( $lang ) {
|
|
|
|
|
if ( !count( $this->getMessages() ) )
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
|
|
|
|
$msgBlobMtime = $dbr->selectField( 'msg_resource', 'mr_timestamp', array(
|
|
|
|
|
'mr_resource' => $this->getName(),
|
|
|
|
|
'mr_lang' => $lang
|
|
|
|
|
), __METHOD__
|
|
|
|
|
);
|
|
|
|
|
$this->msgBlobMtime[$lang] = $msgBlobMtime ? wfTimestamp( TS_UNIX, $msgBlobMtime ) : 0;
|
|
|
|
|
return $this->msgBlobMtime[$lang];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set a preloaded message blob last modification timestamp. Used so we
|
|
|
|
|
* can load this information for all modules at once.
|
|
|
|
|
* @param $lang string Language code
|
|
|
|
|
* @param $mtime int UNIX timestamp or 0 if there is no such blob
|
|
|
|
|
*/
|
|
|
|
|
public function setMsgBlobMtime( $lang, $mtime ) {
|
|
|
|
|
$this->msgBlobMtime[$lang] = $mtime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-09-11 03:26:15 +00:00
|
|
|
/* Abstract Methods */
|
|
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Get this module's last modification timestamp for a given
|
|
|
|
|
* combination of language, skin and debug mode flag. This is typically
|
|
|
|
|
* the highest of each of the relevant components' modification
|
|
|
|
|
* timestamps. Whenever anything happens that changes the module's
|
|
|
|
|
* contents for these parameters, the mtime should increase.
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @param $context ResourceLoaderContext object
|
2010-09-04 04:00:09 +00:00
|
|
|
* @return int UNIX timestamp
|
|
|
|
|
*/
|
|
|
|
|
public abstract function getModifiedTime( ResourceLoaderContext $context );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Module based on local JS/CSS files. This is the most common type of module.
|
|
|
|
|
*/
|
|
|
|
|
class ResourceLoaderFileModule extends ResourceLoaderModule {
|
|
|
|
|
/* Protected Members */
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
protected $scripts = array();
|
|
|
|
|
protected $styles = array();
|
|
|
|
|
protected $messages = array();
|
2010-09-20 21:23:29 +00:00
|
|
|
protected $group;
|
2010-09-04 04:00:09 +00:00
|
|
|
protected $dependencies = array();
|
|
|
|
|
protected $debugScripts = array();
|
|
|
|
|
protected $languageScripts = array();
|
|
|
|
|
protected $skinScripts = array();
|
|
|
|
|
protected $skinStyles = array();
|
|
|
|
|
protected $loaders = array();
|
|
|
|
|
protected $parameters = array();
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
// In-object cache for file dependencies
|
|
|
|
|
protected $fileDeps = array();
|
|
|
|
|
// In-object cache for mtime
|
|
|
|
|
protected $modifiedTime = array();
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/* Methods */
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Construct a new module from an options array.
|
|
|
|
|
*
|
|
|
|
|
* @param $options array Options array. If empty, an empty module will be constructed
|
2010-09-04 12:53:01 +00:00
|
|
|
*
|
2010-09-04 04:00:09 +00:00
|
|
|
* $options format:
|
|
|
|
|
* array(
|
|
|
|
|
* // Required module options (mutually exclusive)
|
|
|
|
|
* 'scripts' => 'dir/script.js' | array( 'dir/script1.js', 'dir/script2.js' ... ),
|
2010-09-20 21:23:29 +00:00
|
|
|
*
|
2010-09-04 04:00:09 +00:00
|
|
|
* // Optional module options
|
|
|
|
|
* 'languageScripts' => array(
|
|
|
|
|
* '[lang name]' => 'dir/lang.js' | '[lang name]' => array( 'dir/lang1.js', 'dir/lang2.js' ... )
|
|
|
|
|
* ...
|
|
|
|
|
* ),
|
|
|
|
|
* 'skinScripts' => 'dir/skin.js' | array( 'dir/skin1.js', 'dir/skin2.js' ... ),
|
|
|
|
|
* 'debugScripts' => 'dir/debug.js' | array( 'dir/debug1.js', 'dir/debug2.js' ... ),
|
2010-09-04 12:53:01 +00:00
|
|
|
*
|
2010-09-04 04:00:09 +00:00
|
|
|
* // Non-raw module options
|
|
|
|
|
* 'dependencies' => 'module' | array( 'module1', 'module2' ... )
|
|
|
|
|
* 'loaderScripts' => 'dir/loader.js' | array( 'dir/loader1.js', 'dir/loader2.js' ... ),
|
2010-09-10 00:21:36 +00:00
|
|
|
* 'styles' => 'dir/file.css' | array( 'dir/file1.css', 'dir/file2.css' ... ), |
|
|
|
|
|
* array( 'dir/file1.css' => array( 'media' => 'print' ) ),
|
2010-09-04 04:00:09 +00:00
|
|
|
* 'skinStyles' => array(
|
2010-09-10 00:21:36 +00:00
|
|
|
* '[skin name]' => 'dir/skin.css' | array( 'dir/skin1.css', 'dir/skin2.css' ... ) |
|
|
|
|
|
* array( 'dir/file1.css' => array( 'media' => 'print' )
|
2010-09-04 04:00:09 +00:00
|
|
|
* ...
|
|
|
|
|
* ),
|
|
|
|
|
* 'messages' => array( 'message1', 'message2' ... ),
|
2010-09-20 21:23:29 +00:00
|
|
|
* 'group' => 'stuff',
|
2010-09-04 04:00:09 +00:00
|
|
|
* )
|
|
|
|
|
*/
|
|
|
|
|
public function __construct( $options = array() ) {
|
|
|
|
|
foreach ( $options as $option => $value ) {
|
|
|
|
|
switch ( $option ) {
|
|
|
|
|
case 'scripts':
|
|
|
|
|
$this->scripts = (array)$value;
|
|
|
|
|
break;
|
|
|
|
|
case 'styles':
|
|
|
|
|
$this->styles = (array)$value;
|
|
|
|
|
break;
|
|
|
|
|
case 'messages':
|
|
|
|
|
$this->messages = (array)$value;
|
|
|
|
|
break;
|
2010-09-20 21:23:29 +00:00
|
|
|
case 'group':
|
|
|
|
|
$this->group = (string)$value;
|
|
|
|
|
break;
|
2010-09-04 04:00:09 +00:00
|
|
|
case 'dependencies':
|
|
|
|
|
$this->dependencies = (array)$value;
|
|
|
|
|
break;
|
|
|
|
|
case 'debugScripts':
|
|
|
|
|
$this->debugScripts = (array)$value;
|
|
|
|
|
break;
|
|
|
|
|
case 'languageScripts':
|
|
|
|
|
$this->languageScripts = (array)$value;
|
|
|
|
|
break;
|
|
|
|
|
case 'skinScripts':
|
|
|
|
|
$this->skinScripts = (array)$value;
|
|
|
|
|
break;
|
|
|
|
|
case 'skinStyles':
|
|
|
|
|
$this->skinStyles = (array)$value;
|
|
|
|
|
break;
|
|
|
|
|
case 'loaders':
|
|
|
|
|
$this->loaders = (array)$value;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Add script files to this module. In order to be valid, a module
|
|
|
|
|
* must contain at least one script file.
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @param $scripts Mixed: path to script file (string) or array of paths
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
|
|
|
|
public function addScripts( $scripts ) {
|
|
|
|
|
$this->scripts = array_merge( $this->scripts, (array)$scripts );
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Add style (CSS) files to this module.
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @param $styles Mixed: path to CSS file (string) or array of paths
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
|
|
|
|
public function addStyles( $styles ) {
|
|
|
|
|
$this->styles = array_merge( $this->styles, (array)$styles );
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Add messages to this module.
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @param $messages Mixed: message key (string) or array of message keys
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
|
|
|
|
public function addMessages( $messages ) {
|
|
|
|
|
$this->messages = array_merge( $this->messages, (array)$messages );
|
|
|
|
|
}
|
2010-09-20 21:23:29 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the group of this module.
|
|
|
|
|
*
|
|
|
|
|
* @param $group string group name
|
|
|
|
|
*/
|
|
|
|
|
public function setGroup( $group ) {
|
|
|
|
|
$this->group = $group;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Add dependencies. Dependency information is taken into account when
|
|
|
|
|
* loading a module on the client side. When adding a module on the
|
|
|
|
|
* server side, dependency information is NOT taken into account and
|
|
|
|
|
* YOU are responsible for adding dependent modules as well. If you
|
|
|
|
|
* don't do this, the client side loader will send a second request
|
|
|
|
|
* back to the server to fetch the missing modules, which kind of
|
|
|
|
|
* defeats the point of using the resource loader in the first place.
|
|
|
|
|
*
|
|
|
|
|
* To add dependencies dynamically on the client side, use a custom
|
|
|
|
|
* loader (see addLoaders())
|
|
|
|
|
*
|
2010-09-05 13:31:34 +00:00
|
|
|
* @param $dependencies Mixed: module name (string) or array of module names
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
|
|
|
|
public function addDependencies( $dependencies ) {
|
|
|
|
|
$this->dependencies = array_merge( $this->dependencies, (array)$dependencies );
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Add debug scripts to the module. These scripts are only included
|
|
|
|
|
* in debug mode.
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @param $scripts Mixed: path to script file (string) or array of paths
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
|
|
|
|
public function addDebugScripts( $scripts ) {
|
|
|
|
|
$this->debugScripts = array_merge( $this->debugScripts, (array)$scripts );
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Add language-specific scripts. These scripts are only included for
|
|
|
|
|
* a given language.
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @param $lang String: language code
|
|
|
|
|
* @param $scripts Mixed: path to script file (string) or array of paths
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
|
|
|
|
public function addLanguageScripts( $lang, $scripts ) {
|
|
|
|
|
$this->languageScripts = array_merge_recursive(
|
|
|
|
|
$this->languageScripts,
|
|
|
|
|
array( $lang => $scripts )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add skin-specific scripts. These scripts are only included for
|
|
|
|
|
* a given skin.
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @param $skin String: skin name, or 'default'
|
|
|
|
|
* @param $scripts Mixed: path to script file (string) or array of paths
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
|
|
|
|
public function addSkinScripts( $skin, $scripts ) {
|
|
|
|
|
$this->skinScripts = array_merge_recursive(
|
|
|
|
|
$this->skinScripts,
|
|
|
|
|
array( $skin => $scripts )
|
|
|
|
|
);
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Add skin-specific CSS. These CSS files are only included for a
|
|
|
|
|
* given skin. If there are no skin-specific CSS files for a skin,
|
|
|
|
|
* the files defined for 'default' will be used, if any.
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @param $skin String: skin name, or 'default'
|
|
|
|
|
* @param $scripts Mixed: path to CSS file (string) or array of paths
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
|
|
|
|
public function addSkinStyles( $skin, $scripts ) {
|
|
|
|
|
$this->skinStyles = array_merge_recursive(
|
|
|
|
|
$this->skinStyles,
|
|
|
|
|
array( $skin => $scripts )
|
|
|
|
|
);
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Add loader scripts. These scripts are loaded on every page and are
|
|
|
|
|
* responsible for registering this module using
|
|
|
|
|
* mediaWiki.loader.register(). If there are no loader scripts defined,
|
|
|
|
|
* the resource loader will register the module itself.
|
|
|
|
|
*
|
|
|
|
|
* Loader scripts are used to determine a module's dependencies
|
|
|
|
|
* dynamically on the client side (e.g. based on browser type/version).
|
|
|
|
|
* Note that loader scripts are included on every page, so they should
|
|
|
|
|
* be lightweight and use mediaWiki.loader.register()'s callback
|
|
|
|
|
* feature to defer dependency calculation.
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @param $scripts Mixed: path to script file (string) or array of paths
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
|
|
|
|
public function addLoaders( $scripts ) {
|
|
|
|
|
$this->loaders = array_merge( $this->loaders, (array)$scripts );
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
public function getScript( ResourceLoaderContext $context ) {
|
|
|
|
|
$retval = $this->getPrimaryScript() . "\n" .
|
|
|
|
|
$this->getLanguageScript( $context->getLanguage() ) . "\n" .
|
|
|
|
|
$this->getSkinScript( $context->getSkin() );
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
if ( $context->getDebug() ) {
|
|
|
|
|
$retval .= $this->getDebugScript();
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
return $retval;
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-10 00:21:36 +00:00
|
|
|
public function getStyles( ResourceLoaderContext $context ) {
|
|
|
|
|
$styles = array();
|
|
|
|
|
foreach ( $this->getPrimaryStyles() as $media => $style ) {
|
|
|
|
|
if ( !isset( $styles[$media] ) ) {
|
|
|
|
|
$styles[$media] = '';
|
|
|
|
|
}
|
|
|
|
|
$styles[$media] .= $style;
|
|
|
|
|
}
|
2010-09-10 18:42:26 +00:00
|
|
|
foreach ( $this->getSkinStyles( $context->getSkin() ) as $media => $style ) {
|
2010-09-10 00:21:36 +00:00
|
|
|
if ( !isset( $styles[$media] ) ) {
|
|
|
|
|
$styles[$media] = '';
|
|
|
|
|
}
|
2010-09-10 18:42:26 +00:00
|
|
|
$styles[$media] .= $style;
|
2010-09-10 00:21:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Collect referenced files
|
|
|
|
|
$files = array();
|
|
|
|
|
foreach ( $styles as $media => $style ) {
|
|
|
|
|
// Extract and store the list of referenced files
|
|
|
|
|
$files = array_merge( $files, CSSMin::getLocalFileReferences( $style ) );
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
// Only store if modified
|
|
|
|
|
if ( $files !== $this->getFileDependencies( $context->getSkin() ) ) {
|
|
|
|
|
$encFiles = FormatJson::encode( $files );
|
2010-09-17 11:45:49 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2010-09-04 04:00:09 +00:00
|
|
|
$dbw->replace( 'module_deps',
|
|
|
|
|
array( array( 'md_module', 'md_skin' ) ), array(
|
|
|
|
|
'md_module' => $this->getName(),
|
|
|
|
|
'md_skin' => $context->getSkin(),
|
|
|
|
|
'md_deps' => $encFiles,
|
|
|
|
|
)
|
|
|
|
|
);
|
2010-09-10 00:21:36 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
// Save into memcached
|
|
|
|
|
global $wgMemc;
|
2010-09-10 00:21:36 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
$key = wfMemcKey( 'resourceloader', 'module_deps', $this->getName(), $context->getSkin() );
|
|
|
|
|
$wgMemc->set( $key, $encFiles );
|
|
|
|
|
}
|
2010-09-10 00:21:36 +00:00
|
|
|
|
|
|
|
|
return $styles;
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
public function getMessages() {
|
|
|
|
|
return $this->messages;
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-20 21:23:29 +00:00
|
|
|
public function getGroup() {
|
|
|
|
|
return $this->group;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
public function getDependencies() {
|
|
|
|
|
return $this->dependencies;
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
public function getLoaderScript() {
|
|
|
|
|
if ( count( $this->loaders ) == 0 ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
return self::concatScripts( $this->loaders );
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Get the last modified timestamp of this module, which is calculated
|
|
|
|
|
* as the highest last modified timestamp of its constituent files and
|
|
|
|
|
* the files it depends on (see getFileDependencies()). Only files
|
|
|
|
|
* relevant to the given language and skin are taken into account, and
|
|
|
|
|
* files only relevant in debug mode are not taken into account when
|
|
|
|
|
* debug mode is off.
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @param $context ResourceLoaderContext object
|
|
|
|
|
* @return Integer: UNIX timestamp
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
|
|
|
|
public function getModifiedTime( ResourceLoaderContext $context ) {
|
|
|
|
|
if ( isset( $this->modifiedTime[$context->getHash()] ) ) {
|
|
|
|
|
return $this->modifiedTime[$context->getHash()];
|
|
|
|
|
}
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2010-09-10 00:21:36 +00:00
|
|
|
|
|
|
|
|
// Sort of nasty way we can get a flat list of files depended on by all styles
|
|
|
|
|
$styles = array();
|
|
|
|
|
foreach ( self::organizeFilesByOption( $this->styles, 'media', 'all' ) as $media => $styleFiles ) {
|
|
|
|
|
$styles = array_merge( $styles, $styleFiles );
|
|
|
|
|
}
|
|
|
|
|
$skinFiles = (array) self::getSkinFiles(
|
|
|
|
|
$context->getSkin(), self::organizeFilesByOption( $this->skinStyles, 'media', 'all' )
|
|
|
|
|
);
|
|
|
|
|
foreach ( $skinFiles as $media => $styleFiles ) {
|
|
|
|
|
$styles = array_merge( $styles, $styleFiles );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Final merge, this should result in a master list of dependent files
|
2010-09-04 04:00:09 +00:00
|
|
|
$files = array_merge(
|
|
|
|
|
$this->scripts,
|
2010-09-10 00:21:36 +00:00
|
|
|
$styles,
|
2010-09-04 04:00:09 +00:00
|
|
|
$context->getDebug() ? $this->debugScripts : array(),
|
|
|
|
|
isset( $this->languageScripts[$context->getLanguage()] ) ?
|
|
|
|
|
(array) $this->languageScripts[$context->getLanguage()] : array(),
|
|
|
|
|
(array) self::getSkinFiles( $context->getSkin(), $this->skinScripts ),
|
|
|
|
|
$this->loaders,
|
|
|
|
|
$this->getFileDependencies( $context->getSkin() )
|
|
|
|
|
);
|
2010-09-23 21:23:51 +00:00
|
|
|
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileIn( __METHOD__.'-filemtime' );
|
2010-09-04 04:00:09 +00:00
|
|
|
$filesMtime = max( array_map( 'filemtime', array_map( array( __CLASS__, 'remapFilename' ), $files ) ) );
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileOut( __METHOD__.'-filemtime' );
|
2010-09-23 21:23:51 +00:00
|
|
|
$this->modifiedTime[$context->getHash()] = max( $filesMtime, $this->getMsgBlobMtime( $context->getLanguage() ) );
|
2010-09-17 11:45:49 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2010-09-04 04:00:09 +00:00
|
|
|
return $this->modifiedTime[$context->getHash()];
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/* Protected Members */
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Get the primary JS for this module. This is pulled from the
|
|
|
|
|
* script files added through addScripts()
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @return String: JS
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
|
|
|
|
protected function getPrimaryScript() {
|
|
|
|
|
return self::concatScripts( $this->scripts );
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Get the primary CSS for this module. This is pulled from the CSS
|
|
|
|
|
* files added through addStyles()
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @return String: JS
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
2010-09-10 00:21:36 +00:00
|
|
|
protected function getPrimaryStyles() {
|
2010-09-04 04:00:09 +00:00
|
|
|
return self::concatStyles( $this->styles );
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Get the debug JS for this module. This is pulled from the script
|
|
|
|
|
* files added through addDebugScripts()
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @return String: JS
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
|
|
|
|
protected function getDebugScript() {
|
|
|
|
|
return self::concatScripts( $this->debugScripts );
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Get the language-specific JS for a given language. This is pulled
|
|
|
|
|
* from the language-specific script files added through addLanguageScripts()
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @return String: JS
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
|
|
|
|
protected function getLanguageScript( $lang ) {
|
|
|
|
|
if ( !isset( $this->languageScripts[$lang] ) ) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
return self::concatScripts( $this->languageScripts[$lang] );
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Get the skin-specific JS for a given skin. This is pulled from the
|
|
|
|
|
* skin-specific JS files added through addSkinScripts()
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @return String: JS
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
|
|
|
|
protected function getSkinScript( $skin ) {
|
|
|
|
|
return self::concatScripts( self::getSkinFiles( $skin, $this->skinScripts ) );
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Get the skin-specific CSS for a given skin. This is pulled from the
|
|
|
|
|
* skin-specific CSS files added through addSkinStyles()
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
2010-09-10 00:21:36 +00:00
|
|
|
* @return Array: list of CSS strings keyed by media type
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
2010-09-10 00:21:36 +00:00
|
|
|
protected function getSkinStyles( $skin ) {
|
2010-09-04 04:00:09 +00:00
|
|
|
return self::concatStyles( self::getSkinFiles( $skin, $this->skinStyles ) );
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Helper function to get skin-specific data from an array.
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @param $skin String: skin name
|
|
|
|
|
* @param $map Array: map of skin names to arrays
|
2010-09-04 04:00:09 +00:00
|
|
|
* @return $map[$skin] if set and non-empty, or $map['default'] if set, or an empty array
|
|
|
|
|
*/
|
|
|
|
|
protected static function getSkinFiles( $skin, $map ) {
|
|
|
|
|
$retval = array();
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
if ( isset( $map[$skin] ) && $map[$skin] ) {
|
|
|
|
|
$retval = $map[$skin];
|
|
|
|
|
} else if ( isset( $map['default'] ) ) {
|
|
|
|
|
$retval = $map['default'];
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
return $retval;
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Get the contents of a set of files and concatenate them, with
|
|
|
|
|
* newlines in between. Each file is used only once.
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @param $files Array of file names
|
|
|
|
|
* @return String: concatenated contents of $files
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
|
|
|
|
protected static function concatScripts( $files ) {
|
2010-09-17 11:45:49 +00:00
|
|
|
return implode( "\n",
|
|
|
|
|
array_map(
|
|
|
|
|
'file_get_contents',
|
|
|
|
|
array_map(
|
|
|
|
|
array( __CLASS__, 'remapFilename' ),
|
|
|
|
|
array_unique( (array) $files ) ) ) );
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-10 00:21:36 +00:00
|
|
|
protected static function organizeFilesByOption( $files, $option, $default ) {
|
|
|
|
|
$organizedFiles = array();
|
|
|
|
|
foreach ( (array) $files as $key => $value ) {
|
|
|
|
|
if ( is_int( $key ) ) {
|
|
|
|
|
// File name as the value
|
|
|
|
|
if ( !isset( $organizedFiles[$default] ) ) {
|
|
|
|
|
$organizedFiles[$default] = array();
|
|
|
|
|
}
|
|
|
|
|
$organizedFiles[$default][] = $value;
|
|
|
|
|
} else if ( is_array( $value ) ) {
|
|
|
|
|
// File name as the key, options array as the value
|
|
|
|
|
$media = isset( $value[$option] ) ? $value[$option] : $default;
|
|
|
|
|
if ( !isset( $organizedFiles[$media] ) ) {
|
|
|
|
|
$organizedFiles[$media] = array();
|
|
|
|
|
}
|
|
|
|
|
$organizedFiles[$media][] = $key;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $organizedFiles;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Get the contents of a set of CSS files, remap then and concatenate
|
|
|
|
|
* them, with newlines in between. Each file is used only once.
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @param $files Array of file names
|
2010-09-10 00:21:36 +00:00
|
|
|
* @return Array: list of concatenated and remapped contents of $files keyed by media type
|
2010-09-04 04:00:09 +00:00
|
|
|
*/
|
2010-09-10 00:21:36 +00:00
|
|
|
protected static function concatStyles( $styles ) {
|
|
|
|
|
$styles = self::organizeFilesByOption( $styles, 'media', 'all' );
|
|
|
|
|
foreach ( $styles as $media => $files ) {
|
|
|
|
|
$styles[$media] =
|
2010-09-17 11:45:49 +00:00
|
|
|
implode( "\n",
|
|
|
|
|
array_map(
|
|
|
|
|
array( __CLASS__, 'remapStyle' ),
|
|
|
|
|
array_unique( (array) $files ) ) );
|
2010-09-10 00:21:36 +00:00
|
|
|
}
|
|
|
|
|
return $styles;
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Remap a relative to $IP. Used as a callback for array_map()
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @param $file String: file name
|
2010-09-04 04:00:09 +00:00
|
|
|
* @return string $IP/$file
|
|
|
|
|
*/
|
|
|
|
|
protected static function remapFilename( $file ) {
|
|
|
|
|
global $IP;
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
return "$IP/$file";
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/**
|
|
|
|
|
* Get the contents of a CSS file and run it through CSSMin::remap().
|
|
|
|
|
* This wrapper is needed so we can use array_map() in concatStyles()
|
2010-09-05 13:31:34 +00:00
|
|
|
*
|
|
|
|
|
* @param $file String: file name
|
2010-09-04 04:00:09 +00:00
|
|
|
* @return string Remapped CSS
|
|
|
|
|
*/
|
|
|
|
|
protected static function remapStyle( $file ) {
|
2010-09-11 10:20:26 +00:00
|
|
|
global $wgUseDataURLs, $wgScriptPath;
|
|
|
|
|
return CSSMin::remap(
|
|
|
|
|
file_get_contents( self::remapFilename( $file ) ),
|
|
|
|
|
dirname( $file ),
|
|
|
|
|
$wgScriptPath . '/' . dirname( $file ),
|
|
|
|
|
$wgUseDataURLs
|
|
|
|
|
);
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-10 23:28:59 +00:00
|
|
|
/**
|
|
|
|
|
* Abstraction for resource loader modules which pull from wiki pages
|
|
|
|
|
*/
|
2010-09-10 21:40:42 +00:00
|
|
|
abstract class ResourceLoaderWikiModule extends ResourceLoaderModule {
|
|
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/* Protected Members */
|
2010-09-10 21:40:42 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
// In-object cache for modified time
|
2010-09-11 03:26:15 +00:00
|
|
|
protected $modifiedTime = array();
|
2010-09-10 21:40:42 +00:00
|
|
|
|
|
|
|
|
/* Abstract Protected Methods */
|
|
|
|
|
|
|
|
|
|
abstract protected function getPages( ResourceLoaderContext $context );
|
|
|
|
|
|
2010-09-11 07:33:16 +00:00
|
|
|
/* Protected Methods */
|
|
|
|
|
|
|
|
|
|
protected function getContent( $page, $ns ) {
|
|
|
|
|
if ( $ns === NS_MEDIAWIKI ) {
|
|
|
|
|
return wfMsgExt( $page, 'content' );
|
|
|
|
|
}
|
|
|
|
|
if ( $title = Title::newFromText( $page, $ns ) ) {
|
|
|
|
|
if ( $title->isValidCssJsSubpage() && $revision = Revision::newFromTitle( $title ) ) {
|
|
|
|
|
return $revision->getRawText();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-11 03:26:15 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
|
|
public function getScript( ResourceLoaderContext $context ) {
|
2010-09-11 07:33:16 +00:00
|
|
|
global $wgCanonicalNamespaceNames;
|
|
|
|
|
|
2010-09-11 03:26:15 +00:00
|
|
|
$scripts = '';
|
|
|
|
|
foreach ( $this->getPages( $context ) as $page => $options ) {
|
|
|
|
|
if ( $options['type'] === 'script' ) {
|
2010-09-11 07:33:16 +00:00
|
|
|
if ( $script = $this->getContent( $page, $options['ns'] ) ) {
|
|
|
|
|
$ns = $wgCanonicalNamespaceNames[$options['ns']];
|
|
|
|
|
$scripts .= "/*$ns:$page */\n$script\n";
|
|
|
|
|
}
|
2010-09-10 23:28:59 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-09-11 03:26:15 +00:00
|
|
|
return $scripts;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getStyles( ResourceLoaderContext $context ) {
|
2010-09-11 07:33:16 +00:00
|
|
|
global $wgCanonicalNamespaceNames;
|
|
|
|
|
|
2010-09-11 03:26:15 +00:00
|
|
|
$styles = array();
|
|
|
|
|
foreach ( $this->getPages( $context ) as $page => $options ) {
|
|
|
|
|
if ( $options['type'] === 'style' ) {
|
|
|
|
|
$media = isset( $options['media'] ) ? $options['media'] : 'all';
|
2010-09-11 07:33:16 +00:00
|
|
|
if ( $style = $this->getContent( $page, $options['ns'] ) ) {
|
|
|
|
|
if ( !isset( $styles[$media] ) ) {
|
|
|
|
|
$styles[$media] = '';
|
|
|
|
|
}
|
|
|
|
|
$ns = $wgCanonicalNamespaceNames[$options['ns']];
|
|
|
|
|
$styles[$media] .= "/* $ns:$page */\n$style\n";
|
2010-09-11 03:26:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-09-10 23:28:59 +00:00
|
|
|
}
|
|
|
|
|
return $styles;
|
|
|
|
|
}
|
2010-09-11 03:26:15 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
public function getModifiedTime( ResourceLoaderContext $context ) {
|
2010-09-11 03:26:15 +00:00
|
|
|
$hash = $context->getHash();
|
|
|
|
|
if ( isset( $this->modifiedTime[$hash] ) ) {
|
|
|
|
|
return $this->modifiedTime[$hash];
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
2010-09-14 09:23:43 +00:00
|
|
|
|
2010-09-11 03:26:15 +00:00
|
|
|
$titles = array();
|
|
|
|
|
foreach ( $this->getPages( $context ) as $page => $options ) {
|
2010-09-14 09:23:43 +00:00
|
|
|
$titles[$options['ns']][$page] = true;
|
2010-09-10 20:18:24 +00:00
|
|
|
}
|
2010-09-14 09:23:43 +00:00
|
|
|
|
2010-09-11 03:26:15 +00:00
|
|
|
$modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
|
2010-09-14 07:32:08 +00:00
|
|
|
|
2010-09-14 09:23:43 +00:00
|
|
|
if ( $titles ) {
|
|
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
|
|
|
|
$latest = $dbr->selectField( 'page', 'MAX(page_touched)',
|
|
|
|
|
$dbr->makeWhereFrom2d( $titles, 'page_namespace', 'page_title' ),
|
|
|
|
|
__METHOD__ );
|
|
|
|
|
|
|
|
|
|
if ( $latest ) {
|
|
|
|
|
$modifiedTime = wfTimestamp( TS_UNIX, $modifiedTime );
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-09-14 07:32:08 +00:00
|
|
|
|
2010-09-14 09:23:43 +00:00
|
|
|
return $this->modifiedTime[$hash] = $modifiedTime;
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
2010-09-10 21:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-09-11 03:26:15 +00:00
|
|
|
* Module for site customizations
|
2010-09-10 21:40:42 +00:00
|
|
|
*/
|
|
|
|
|
class ResourceLoaderSiteModule extends ResourceLoaderWikiModule {
|
|
|
|
|
|
|
|
|
|
/* Protected Methods */
|
|
|
|
|
|
|
|
|
|
protected function getPages( ResourceLoaderContext $context ) {
|
|
|
|
|
global $wgHandheldStyle;
|
|
|
|
|
|
|
|
|
|
$pages = array(
|
2010-09-11 07:33:16 +00:00
|
|
|
'Common.js' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'script' ),
|
|
|
|
|
'Common.css' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'style' ),
|
|
|
|
|
ucfirst( $context->getSkin() ) . '.js' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'script' ),
|
|
|
|
|
ucfirst( $context->getSkin() ) . '.css' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'style' ),
|
|
|
|
|
'Print.css' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'style', 'media' => 'print' ),
|
2010-09-10 21:40:42 +00:00
|
|
|
);
|
|
|
|
|
if ( $wgHandheldStyle ) {
|
2010-09-11 07:33:16 +00:00
|
|
|
$pages['Handheld.css'] = array( 'ns' => NS_MEDIAWIKI, 'type' => 'style', 'media' => 'handheld' );
|
2010-09-10 21:40:42 +00:00
|
|
|
}
|
|
|
|
|
return $pages;
|
|
|
|
|
}
|
2010-09-20 21:23:29 +00:00
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
|
|
public function getGroup() {
|
|
|
|
|
return 'site';
|
|
|
|
|
}
|
2010-09-11 03:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Module for user customizations
|
|
|
|
|
*/
|
|
|
|
|
class ResourceLoaderUserModule extends ResourceLoaderWikiModule {
|
|
|
|
|
|
|
|
|
|
/* Protected Methods */
|
|
|
|
|
|
|
|
|
|
protected function getPages( ResourceLoaderContext $context ) {
|
|
|
|
|
global $wgAllowUserCss;
|
|
|
|
|
|
|
|
|
|
if ( $context->getUser() && $wgAllowUserCss ) {
|
2010-09-11 07:33:16 +00:00
|
|
|
$username = $context->getUser();
|
2010-09-11 03:26:15 +00:00
|
|
|
return array(
|
2010-09-11 07:33:16 +00:00
|
|
|
"$username/common.js" => array( 'ns' => NS_USER, 'type' => 'script' ),
|
|
|
|
|
"$username/" . $context->getSkin() . '.js' => array( 'ns' => NS_USER, 'type' => 'script' ),
|
|
|
|
|
"$username/common.css" => array( 'ns' => NS_USER, 'type' => 'style' ),
|
|
|
|
|
"$username/" . $context->getSkin() . '.css' => array( 'ns' => NS_USER, 'type' => 'style' ),
|
2010-09-11 03:26:15 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return array();
|
|
|
|
|
}
|
2010-09-20 21:23:29 +00:00
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
|
|
public function getGroup() {
|
|
|
|
|
return 'user';
|
|
|
|
|
}
|
2010-09-11 03:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Module for user preference customizations
|
|
|
|
|
*/
|
2010-09-14 21:47:59 +00:00
|
|
|
class ResourceLoaderUserOptionsModule extends ResourceLoaderModule {
|
2010-09-11 03:26:15 +00:00
|
|
|
|
|
|
|
|
/* Protected Members */
|
|
|
|
|
|
|
|
|
|
protected $modifiedTime = array();
|
2010-09-10 21:40:42 +00:00
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
2010-09-11 03:26:15 +00:00
|
|
|
public function getModifiedTime( ResourceLoaderContext $context ) {
|
|
|
|
|
$hash = $context->getHash();
|
|
|
|
|
if ( isset( $this->modifiedTime[$hash] ) ) {
|
|
|
|
|
return $this->modifiedTime[$hash];
|
|
|
|
|
}
|
2010-09-14 07:49:24 +00:00
|
|
|
|
|
|
|
|
global $wgUser;
|
|
|
|
|
$username = $context->getUser();
|
|
|
|
|
// Avoid extra db query by using $wgUser if possible
|
|
|
|
|
$user = $wgUser->getName() === $username ? $wgUser : User::newFromName( $username );
|
|
|
|
|
|
|
|
|
|
if ( $user ) {
|
2010-09-11 07:33:16 +00:00
|
|
|
return $this->modifiedTime[$hash] = $user->getTouched();
|
|
|
|
|
} else {
|
2010-09-20 21:23:29 +00:00
|
|
|
return 1;
|
2010-09-11 07:33:16 +00:00
|
|
|
}
|
2010-09-10 21:40:42 +00:00
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-14 21:47:59 +00:00
|
|
|
public function getScript( ResourceLoaderContext $context ) {
|
|
|
|
|
$user = User::newFromName( $context->getUser() );
|
2010-09-17 11:45:49 +00:00
|
|
|
if ( $user instanceof User ) {
|
|
|
|
|
$options = FormatJson::encode( $user->getOptions() );
|
|
|
|
|
} else {
|
|
|
|
|
$options = FormatJson::encode( User::getDefaultOptions() );
|
|
|
|
|
}
|
2010-09-14 21:47:59 +00:00
|
|
|
return "mediaWiki.user.options.set( $options );";
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-10 18:42:26 +00:00
|
|
|
public function getStyles( ResourceLoaderContext $context ) {
|
2010-09-11 03:26:15 +00:00
|
|
|
global $wgAllowUserCssPrefs;
|
|
|
|
|
if ( $wgAllowUserCssPrefs ) {
|
|
|
|
|
$user = User::newFromName( $context->getUser() );
|
2010-09-14 21:47:59 +00:00
|
|
|
$options = $user instanceof User ? $user->getOptions() : User::getDefaultOptions();
|
|
|
|
|
|
2010-09-11 03:26:15 +00:00
|
|
|
$rules = array();
|
2010-09-14 21:47:59 +00:00
|
|
|
if ( $options['underline'] < 2 ) {
|
|
|
|
|
$rules[] = "a { text-decoration: " . ( $options['underline'] ? 'underline' : 'none' ) . "; }";
|
2010-09-11 03:26:15 +00:00
|
|
|
}
|
2010-09-14 21:47:59 +00:00
|
|
|
if ( $options['highlightbroken'] ) {
|
2010-09-11 03:26:15 +00:00
|
|
|
$rules[] = "a.new, #quickbar a.new { color: #CC2200; }\n";
|
|
|
|
|
} else {
|
|
|
|
|
$rules[] = "a.new, #quickbar a.new, a.stub, #quickbar a.stub { color: inherit; }";
|
|
|
|
|
$rules[] = "a.new:after, #quickbar a.new:after { content: '?'; color: #CC2200; }";
|
|
|
|
|
$rules[] = "a.stub:after, #quickbar a.stub:after { content: '!'; color: #772233; }";
|
|
|
|
|
}
|
2010-09-14 21:47:59 +00:00
|
|
|
if ( $options['justify'] ) {
|
2010-09-11 03:26:15 +00:00
|
|
|
$rules[] = "#article, #bodyContent, #mw_content { text-align: justify; }\n";
|
|
|
|
|
}
|
2010-09-14 21:47:59 +00:00
|
|
|
if ( !$options['showtoc'] ) {
|
2010-09-11 03:26:15 +00:00
|
|
|
$rules[] = "#toc { display: none; }\n";
|
|
|
|
|
}
|
2010-09-14 21:47:59 +00:00
|
|
|
if ( !$options['editsection'] ) {
|
2010-09-11 03:26:15 +00:00
|
|
|
$rules[] = ".editsection { display: none; }\n";
|
|
|
|
|
}
|
2010-09-14 21:47:59 +00:00
|
|
|
if ( $options['editfont'] !== 'default' ) {
|
|
|
|
|
$rules[] = "textarea { font-family: {$options['editfont']}; }\n";
|
2010-09-11 03:26:15 +00:00
|
|
|
}
|
|
|
|
|
return array( 'all' => implode( "\n", $rules ) );
|
2010-09-10 20:18:24 +00:00
|
|
|
}
|
2010-09-15 17:58:36 +00:00
|
|
|
return array();
|
2010-09-11 03:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getFlip( $context ) {
|
|
|
|
|
global $wgContLang;
|
|
|
|
|
|
|
|
|
|
return $wgContLang->getDir() !== $context->getDirection();
|
2010-09-10 18:42:26 +00:00
|
|
|
}
|
2010-09-20 21:23:29 +00:00
|
|
|
|
|
|
|
|
public function getGroup() {
|
|
|
|
|
return 'user';
|
|
|
|
|
}
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ResourceLoaderStartUpModule extends ResourceLoaderModule {
|
|
|
|
|
/* Protected Members */
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-11 03:26:15 +00:00
|
|
|
protected $modifiedTime = array();
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-11 03:26:15 +00:00
|
|
|
/* Protected Methods */
|
|
|
|
|
|
|
|
|
|
protected function getConfig( $context ) {
|
2010-09-17 11:45:49 +00:00
|
|
|
global $wgLoadScript, $wgScript, $wgStylePath, $wgScriptExtension,
|
|
|
|
|
$wgArticlePath, $wgScriptPath, $wgServer, $wgContLang, $wgBreakFrames,
|
|
|
|
|
$wgVariantArticlePath, $wgActionPaths, $wgUseAjax, $wgVersion,
|
|
|
|
|
$wgEnableAPI, $wgEnableWriteAPI, $wgDBname, $wgEnableMWSuggest,
|
|
|
|
|
$wgSitename, $wgFileExtensions;
|
2010-09-11 03:26:15 +00:00
|
|
|
|
|
|
|
|
// Pre-process information
|
|
|
|
|
$separatorTransTable = $wgContLang->separatorTransformTable();
|
|
|
|
|
$separatorTransTable = $separatorTransTable ? $separatorTransTable : array();
|
|
|
|
|
$compactSeparatorTransTable = array(
|
|
|
|
|
implode( "\t", array_keys( $separatorTransTable ) ),
|
|
|
|
|
implode( "\t", $separatorTransTable ),
|
|
|
|
|
);
|
|
|
|
|
$digitTransTable = $wgContLang->digitTransformTable();
|
|
|
|
|
$digitTransTable = $digitTransTable ? $digitTransTable : array();
|
|
|
|
|
$compactDigitTransTable = array(
|
|
|
|
|
implode( "\t", array_keys( $digitTransTable ) ),
|
|
|
|
|
implode( "\t", $digitTransTable ),
|
|
|
|
|
);
|
|
|
|
|
$mainPage = Title::newMainPage();
|
|
|
|
|
|
|
|
|
|
// Build list of variables
|
|
|
|
|
$vars = array(
|
|
|
|
|
'wgLoadScript' => $wgLoadScript,
|
|
|
|
|
'debug' => $context->getDebug(),
|
|
|
|
|
'skin' => $context->getSkin(),
|
|
|
|
|
'stylepath' => $wgStylePath,
|
|
|
|
|
'wgUrlProtocols' => wfUrlProtocols(),
|
|
|
|
|
'wgArticlePath' => $wgArticlePath,
|
|
|
|
|
'wgScriptPath' => $wgScriptPath,
|
|
|
|
|
'wgScriptExtension' => $wgScriptExtension,
|
|
|
|
|
'wgScript' => $wgScript,
|
|
|
|
|
'wgVariantArticlePath' => $wgVariantArticlePath,
|
|
|
|
|
'wgActionPaths' => $wgActionPaths,
|
|
|
|
|
'wgServer' => $wgServer,
|
|
|
|
|
'wgUserLanguage' => $context->getLanguage(),
|
|
|
|
|
'wgContentLanguage' => $wgContLang->getCode(),
|
|
|
|
|
'wgBreakFrames' => $wgBreakFrames,
|
|
|
|
|
'wgVersion' => $wgVersion,
|
|
|
|
|
'wgEnableAPI' => $wgEnableAPI,
|
|
|
|
|
'wgEnableWriteAPI' => $wgEnableWriteAPI,
|
|
|
|
|
'wgSeparatorTransformTable' => $compactSeparatorTransTable,
|
|
|
|
|
'wgDigitTransformTable' => $compactDigitTransTable,
|
|
|
|
|
'wgMainPageTitle' => $mainPage ? $mainPage->getPrefixedText() : null,
|
|
|
|
|
'wgFormattedNamespaces' => $wgContLang->getFormattedNamespaces(),
|
|
|
|
|
'wgNamespaceIds' => $wgContLang->getNamespaceIds(),
|
|
|
|
|
'wgSiteName' => $wgSitename,
|
|
|
|
|
'wgFileExtensions' => $wgFileExtensions,
|
|
|
|
|
);
|
|
|
|
|
if ( $wgContLang->hasVariants() ) {
|
|
|
|
|
$vars['wgUserVariant'] = $wgContLang->getPreferredVariant();
|
|
|
|
|
}
|
|
|
|
|
if ( $wgUseAjax && $wgEnableMWSuggest ) {
|
|
|
|
|
$vars['wgMWSuggestTemplate'] = SearchEngine::getMWSuggestTemplate();
|
|
|
|
|
$vars['wgDBname'] = $wgDBname;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $vars;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
/* Methods */
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
public function getScript( ResourceLoaderContext $context ) {
|
2010-09-11 21:55:21 +00:00
|
|
|
global $IP, $wgLoadScript;
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
$scripts = file_get_contents( "$IP/resources/startup.js" );
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
if ( $context->getOnly() === 'scripts' ) {
|
|
|
|
|
// Get all module registrations
|
|
|
|
|
$registration = ResourceLoader::getModuleRegistrations( $context );
|
|
|
|
|
// Build configuration
|
2010-09-11 03:26:15 +00:00
|
|
|
$config = FormatJson::encode( $this->getConfig( $context ) );
|
2010-09-04 04:00:09 +00:00
|
|
|
// Add a well-known start-up function
|
2010-09-17 11:45:49 +00:00
|
|
|
$scripts .= <<<JAVASCRIPT
|
|
|
|
|
window.startUp = function() {
|
|
|
|
|
$registration
|
|
|
|
|
mediaWiki.config.set( $config );
|
|
|
|
|
};
|
|
|
|
|
JAVASCRIPT;
|
2010-09-04 04:00:09 +00:00
|
|
|
// Build load query for jquery and mediawiki modules
|
2010-09-11 08:22:32 +00:00
|
|
|
$query = array(
|
|
|
|
|
'modules' => implode( '|', array( 'jquery', 'mediawiki' ) ),
|
|
|
|
|
'only' => 'scripts',
|
|
|
|
|
'lang' => $context->getLanguage(),
|
|
|
|
|
'skin' => $context->getSkin(),
|
|
|
|
|
'debug' => $context->getDebug() ? 'true' : 'false',
|
|
|
|
|
'version' => wfTimestamp( TS_ISO_8601, round( max(
|
|
|
|
|
ResourceLoader::getModule( 'jquery' )->getModifiedTime( $context ),
|
|
|
|
|
ResourceLoader::getModule( 'mediawiki' )->getModifiedTime( $context )
|
|
|
|
|
), -2 ) )
|
2010-09-04 04:00:09 +00:00
|
|
|
);
|
2010-09-11 08:22:32 +00:00
|
|
|
// Uniform query order
|
|
|
|
|
ksort( $query );
|
2010-09-04 04:00:09 +00:00
|
|
|
// Build HTML code for loading jquery and mediawiki modules
|
2010-09-11 08:22:32 +00:00
|
|
|
$loadScript = Html::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) );
|
2010-09-04 04:00:09 +00:00
|
|
|
// Add code to add jquery and mediawiki loading code; only if the current client is compatible
|
2010-09-17 11:45:49 +00:00
|
|
|
$scripts .= "if ( isCompatible() ) { document.write( " . FormatJson::encode( $loadScript ) . "); }\n";
|
2010-09-04 04:00:09 +00:00
|
|
|
// Delete the compatible function - it's not needed anymore
|
2010-09-17 11:45:49 +00:00
|
|
|
$scripts .= "delete window['isCompatible'];\n";
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
return $scripts;
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 04:00:09 +00:00
|
|
|
public function getModifiedTime( ResourceLoaderContext $context ) {
|
|
|
|
|
global $IP;
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-11 03:26:15 +00:00
|
|
|
$hash = $context->getHash();
|
|
|
|
|
if ( isset( $this->modifiedTime[$hash] ) ) {
|
|
|
|
|
return $this->modifiedTime[$hash];
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
2010-09-11 03:26:15 +00:00
|
|
|
$this->modifiedTime[$hash] = filemtime( "$IP/resources/startup.js" );
|
|
|
|
|
// ATTENTION!: Because of the line above, this is not going to cause infinite recursion - think carefully
|
|
|
|
|
// before making changes to this code!
|
|
|
|
|
$this->modifiedTime[$hash] = ResourceLoader::getHighestModifiedTime( $context );
|
|
|
|
|
return $this->modifiedTime[$hash];
|
2010-09-04 04:00:09 +00:00
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 10:53:57 +00:00
|
|
|
public function getFlip( $context ) {
|
|
|
|
|
global $wgContLang;
|
2010-09-04 12:53:01 +00:00
|
|
|
|
2010-09-04 10:53:57 +00:00
|
|
|
return $wgContLang->getDir() !== $context->getDirection();
|
|
|
|
|
}
|
2010-09-20 21:23:29 +00:00
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
|
|
public function getGroup() {
|
|
|
|
|
return 'startup';
|
|
|
|
|
}
|
2010-09-04 12:53:01 +00:00
|
|
|
}
|