3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
19 * @author Trevor Parscal
20 * @author Roan Kattouw
23 defined( 'MEDIAWIKI' ) ||
die( 1 );
26 * Abstraction for resource loader modules, with name registration and maxage functionality.
28 abstract class ResourceLoaderModule
{
30 /* Protected Members */
32 protected $name = null;
34 // In-object cache for file dependencies
35 protected $fileDeps = array();
36 // In-object cache for message blob mtime
37 protected $msgBlobMtime = array();
42 * Get this module's name. This is set when the module is registered
43 * with ResourceLoader::register()
45 * @return Mixed: name (string) or null if no name was set
47 public function getName() {
52 * Set this module's name. This is called by ResourceLodaer::register()
53 * when registering the module. Other code should not call this.
55 * @param $name String: name
57 public function setName( $name ) {
62 * Get whether CSS for this module should be flipped
64 public function getFlip( $context ) {
65 return $context->getDirection() === 'rtl';
69 * Get all JS for this module for a given language and skin.
70 * Includes all relevant JS except loader scripts.
72 * @param $context ResourceLoaderContext object
75 public function getScript( ResourceLoaderContext
$context ) {
76 // Stub, override expected
81 * Get all CSS for this module for a given skin.
83 * @param $context ResourceLoaderContext object
84 * @return array: strings of CSS keyed by media type
86 public function getStyles( ResourceLoaderContext
$context ) {
87 // Stub, override expected
92 * Get the messages needed for this module.
94 * To get a JSON blob with messages, use MessageBlobStore::get()
96 * @return array of message keys. Keys may occur more than once
98 public function getMessages() {
99 // Stub, override expected
104 * Get the group this module is in.
106 * @return string of group name
108 public function getGroup() {
109 // Stub, override expected
114 * Get the loader JS for this module, if set.
116 * @return Mixed: loader JS (string) or false if no custom loader set
118 public function getLoaderScript() {
119 // Stub, override expected
124 * Get a list of modules this module depends on.
126 * Dependency information is taken into account when loading a module
127 * on the client side. When adding a module on the server side,
128 * dependency information is NOT taken into account and YOU are
129 * responsible for adding dependent modules as well. If you don't do
130 * this, the client side loader will send a second request back to the
131 * server to fetch the missing modules, which kind of defeats the
132 * purpose of the resource loader.
134 * To add dependencies dynamically on the client side, use a custom
135 * loader script, see getLoaderScript()
136 * @return Array of module names (strings)
138 public function getDependencies() {
139 // Stub, override expected
144 * Get the files this module depends on indirectly for a given skin.
145 * Currently these are only image files referenced by the module's CSS.
147 * @param $skin String: skin name
148 * @return array of files
150 public function getFileDependencies( $skin ) {
151 // Try in-object cache first
152 if ( isset( $this->fileDeps
[$skin] ) ) {
153 return $this->fileDeps
[$skin];
156 $dbr = wfGetDB( DB_SLAVE
);
157 $deps = $dbr->selectField( 'module_deps', 'md_deps', array(
158 'md_module' => $this->getName(),
162 if ( !is_null( $deps ) ) {
163 return $this->fileDeps
[$skin] = (array) FormatJson
::decode( $deps, true );
165 return $this->fileDeps
[$skin] = array();
169 * Set preloaded file dependency information. Used so we can load this
170 * information for all modules at once.
171 * @param $skin string Skin name
172 * @param $deps array Array of file names
174 public function setFileDependencies( $skin, $deps ) {
175 $this->fileDeps
[$skin] = $deps;
179 * Get the last modification timestamp of the message blob for this
180 * module in a given language.
181 * @param $lang string Language code
182 * @return int UNIX timestamp, or 0 if no blob found
184 public function getMsgBlobMtime( $lang ) {
185 if ( !count( $this->getMessages() ) )
188 $dbr = wfGetDB( DB_SLAVE
);
189 $msgBlobMtime = $dbr->selectField( 'msg_resource', 'mr_timestamp', array(
190 'mr_resource' => $this->getName(),
194 $this->msgBlobMtime
[$lang] = $msgBlobMtime ?
wfTimestamp( TS_UNIX
, $msgBlobMtime ) : 0;
195 return $this->msgBlobMtime
[$lang];
199 * Set a preloaded message blob last modification timestamp. Used so we
200 * can load this information for all modules at once.
201 * @param $lang string Language code
202 * @param $mtime int UNIX timestamp or 0 if there is no such blob
204 public function setMsgBlobMtime( $lang, $mtime ) {
205 $this->msgBlobMtime
[$lang] = $mtime;
208 /* Abstract Methods */
211 * Get this module's last modification timestamp for a given
212 * combination of language, skin and debug mode flag. This is typically
213 * the highest of each of the relevant components' modification
214 * timestamps. Whenever anything happens that changes the module's
215 * contents for these parameters, the mtime should increase.
217 * @param $context ResourceLoaderContext object
218 * @return int UNIX timestamp
220 public abstract function getModifiedTime( ResourceLoaderContext
$context );
224 * Module based on local JS/CSS files. This is the most common type of module.
226 class ResourceLoaderFileModule
extends ResourceLoaderModule
{
227 /* Protected Members */
229 protected $scripts = array();
230 protected $styles = array();
231 protected $messages = array();
233 protected $dependencies = array();
234 protected $debugScripts = array();
235 protected $languageScripts = array();
236 protected $skinScripts = array();
237 protected $skinStyles = array();
238 protected $loaders = array();
239 protected $parameters = array();
241 // In-object cache for file dependencies
242 protected $fileDeps = array();
243 // In-object cache for mtime
244 protected $modifiedTime = array();
249 * Construct a new module from an options array.
251 * @param $options array Options array. If empty, an empty module will be constructed
255 * // Required module options (mutually exclusive)
256 * 'scripts' => 'dir/script.js' | array( 'dir/script1.js', 'dir/script2.js' ... ),
258 * // Optional module options
259 * 'languageScripts' => array(
260 * '[lang name]' => 'dir/lang.js' | '[lang name]' => array( 'dir/lang1.js', 'dir/lang2.js' ... )
263 * 'skinScripts' => 'dir/skin.js' | array( 'dir/skin1.js', 'dir/skin2.js' ... ),
264 * 'debugScripts' => 'dir/debug.js' | array( 'dir/debug1.js', 'dir/debug2.js' ... ),
266 * // Non-raw module options
267 * 'dependencies' => 'module' | array( 'module1', 'module2' ... )
268 * 'loaderScripts' => 'dir/loader.js' | array( 'dir/loader1.js', 'dir/loader2.js' ... ),
269 * 'styles' => 'dir/file.css' | array( 'dir/file1.css', 'dir/file2.css' ... ), |
270 * array( 'dir/file1.css' => array( 'media' => 'print' ) ),
271 * 'skinStyles' => array(
272 * '[skin name]' => 'dir/skin.css' | array( 'dir/skin1.css', 'dir/skin2.css' ... ) |
273 * array( 'dir/file1.css' => array( 'media' => 'print' )
276 * 'messages' => array( 'message1', 'message2' ... ),
277 * 'group' => 'stuff',
280 public function __construct( $options = array() ) {
281 foreach ( $options as $option => $value ) {
284 $this->scripts
= (array)$value;
287 $this->styles
= (array)$value;
290 $this->messages
= (array)$value;
293 $this->group
= (string)$value;
296 $this->dependencies
= (array)$value;
299 $this->debugScripts
= (array)$value;
301 case 'languageScripts':
302 $this->languageScripts
= (array)$value;
305 $this->skinScripts
= (array)$value;
308 $this->skinStyles
= (array)$value;
311 $this->loaders
= (array)$value;
318 * Add script files to this module. In order to be valid, a module
319 * must contain at least one script file.
321 * @param $scripts Mixed: path to script file (string) or array of paths
323 public function addScripts( $scripts ) {
324 $this->scripts
= array_merge( $this->scripts
, (array)$scripts );
328 * Add style (CSS) files to this module.
330 * @param $styles Mixed: path to CSS file (string) or array of paths
332 public function addStyles( $styles ) {
333 $this->styles
= array_merge( $this->styles
, (array)$styles );
337 * Add messages to this module.
339 * @param $messages Mixed: message key (string) or array of message keys
341 public function addMessages( $messages ) {
342 $this->messages
= array_merge( $this->messages
, (array)$messages );
346 * Sets the group of this module.
348 * @param $group string group name
350 public function setGroup( $group ) {
351 $this->group
= $group;
355 * Add dependencies. Dependency information is taken into account when
356 * loading a module on the client side. When adding a module on the
357 * server side, dependency information is NOT taken into account and
358 * YOU are responsible for adding dependent modules as well. If you
359 * don't do this, the client side loader will send a second request
360 * back to the server to fetch the missing modules, which kind of
361 * defeats the point of using the resource loader in the first place.
363 * To add dependencies dynamically on the client side, use a custom
364 * loader (see addLoaders())
366 * @param $dependencies Mixed: module name (string) or array of module names
368 public function addDependencies( $dependencies ) {
369 $this->dependencies
= array_merge( $this->dependencies
, (array)$dependencies );
373 * Add debug scripts to the module. These scripts are only included
376 * @param $scripts Mixed: path to script file (string) or array of paths
378 public function addDebugScripts( $scripts ) {
379 $this->debugScripts
= array_merge( $this->debugScripts
, (array)$scripts );
383 * Add language-specific scripts. These scripts are only included for
386 * @param $lang String: language code
387 * @param $scripts Mixed: path to script file (string) or array of paths
389 public function addLanguageScripts( $lang, $scripts ) {
390 $this->languageScripts
= array_merge_recursive(
391 $this->languageScripts
,
392 array( $lang => $scripts )
397 * Add skin-specific scripts. These scripts are only included for
400 * @param $skin String: skin name, or 'default'
401 * @param $scripts Mixed: path to script file (string) or array of paths
403 public function addSkinScripts( $skin, $scripts ) {
404 $this->skinScripts
= array_merge_recursive(
406 array( $skin => $scripts )
411 * Add skin-specific CSS. These CSS files are only included for a
412 * given skin. If there are no skin-specific CSS files for a skin,
413 * the files defined for 'default' will be used, if any.
415 * @param $skin String: skin name, or 'default'
416 * @param $scripts Mixed: path to CSS file (string) or array of paths
418 public function addSkinStyles( $skin, $scripts ) {
419 $this->skinStyles
= array_merge_recursive(
421 array( $skin => $scripts )
426 * Add loader scripts. These scripts are loaded on every page and are
427 * responsible for registering this module using
428 * mediaWiki.loader.register(). If there are no loader scripts defined,
429 * the resource loader will register the module itself.
431 * Loader scripts are used to determine a module's dependencies
432 * dynamically on the client side (e.g. based on browser type/version).
433 * Note that loader scripts are included on every page, so they should
434 * be lightweight and use mediaWiki.loader.register()'s callback
435 * feature to defer dependency calculation.
437 * @param $scripts Mixed: path to script file (string) or array of paths
439 public function addLoaders( $scripts ) {
440 $this->loaders
= array_merge( $this->loaders
, (array)$scripts );
443 public function getScript( ResourceLoaderContext
$context ) {
444 $retval = $this->getPrimaryScript() . "\n" .
445 $this->getLanguageScript( $context->getLanguage() ) . "\n" .
446 $this->getSkinScript( $context->getSkin() );
448 if ( $context->getDebug() ) {
449 $retval .= $this->getDebugScript();
455 public function getStyles( ResourceLoaderContext
$context ) {
457 foreach ( $this->getPrimaryStyles() as $media => $style ) {
458 if ( !isset( $styles[$media] ) ) {
459 $styles[$media] = '';
461 $styles[$media] .= $style;
463 foreach ( $this->getSkinStyles( $context->getSkin() ) as $media => $style ) {
464 if ( !isset( $styles[$media] ) ) {
465 $styles[$media] = '';
467 $styles[$media] .= $style;
470 // Collect referenced files
472 foreach ( $styles as $media => $style ) {
473 // Extract and store the list of referenced files
474 $files = array_merge( $files, CSSMin
::getLocalFileReferences( $style ) );
477 // Only store if modified
478 if ( $files !== $this->getFileDependencies( $context->getSkin() ) ) {
479 $encFiles = FormatJson
::encode( $files );
480 $dbw = wfGetDB( DB_MASTER
);
481 $dbw->replace( 'module_deps',
482 array( array( 'md_module', 'md_skin' ) ), array(
483 'md_module' => $this->getName(),
484 'md_skin' => $context->getSkin(),
485 'md_deps' => $encFiles,
489 // Save into memcached
492 $key = wfMemcKey( 'resourceloader', 'module_deps', $this->getName(), $context->getSkin() );
493 $wgMemc->set( $key, $encFiles );
499 public function getMessages() {
500 return $this->messages
;
503 public function getGroup() {
507 public function getDependencies() {
508 return $this->dependencies
;
511 public function getLoaderScript() {
512 if ( count( $this->loaders
) == 0 ) {
516 return self
::concatScripts( $this->loaders
);
520 * Get the last modified timestamp of this module, which is calculated
521 * as the highest last modified timestamp of its constituent files and
522 * the files it depends on (see getFileDependencies()). Only files
523 * relevant to the given language and skin are taken into account, and
524 * files only relevant in debug mode are not taken into account when
527 * @param $context ResourceLoaderContext object
528 * @return Integer: UNIX timestamp
530 public function getModifiedTime( ResourceLoaderContext
$context ) {
531 if ( isset( $this->modifiedTime
[$context->getHash()] ) ) {
532 return $this->modifiedTime
[$context->getHash()];
534 wfProfileIn( __METHOD__
);
536 // Sort of nasty way we can get a flat list of files depended on by all styles
538 foreach ( self
::organizeFilesByOption( $this->styles
, 'media', 'all' ) as $media => $styleFiles ) {
539 $styles = array_merge( $styles, $styleFiles );
541 $skinFiles = (array) self
::getSkinFiles(
542 $context->getSkin(), self
::organizeFilesByOption( $this->skinStyles
, 'media', 'all' )
544 foreach ( $skinFiles as $media => $styleFiles ) {
545 $styles = array_merge( $styles, $styleFiles );
548 // Final merge, this should result in a master list of dependent files
549 $files = array_merge(
552 $context->getDebug() ?
$this->debugScripts
: array(),
553 isset( $this->languageScripts
[$context->getLanguage()] ) ?
554 (array) $this->languageScripts
[$context->getLanguage()] : array(),
555 (array) self
::getSkinFiles( $context->getSkin(), $this->skinScripts
),
557 $this->getFileDependencies( $context->getSkin() )
560 wfProfileIn( __METHOD__
.'-filemtime' );
561 $filesMtime = max( array_map( 'filemtime', array_map( array( __CLASS__
, 'remapFilename' ), $files ) ) );
562 wfProfileOut( __METHOD__
.'-filemtime' );
563 $this->modifiedTime
[$context->getHash()] = max( $filesMtime, $this->getMsgBlobMtime( $context->getLanguage() ) );
564 wfProfileOut( __METHOD__
);
565 return $this->modifiedTime
[$context->getHash()];
568 /* Protected Members */
571 * Get the primary JS for this module. This is pulled from the
572 * script files added through addScripts()
576 protected function getPrimaryScript() {
577 return self
::concatScripts( $this->scripts
);
581 * Get the primary CSS for this module. This is pulled from the CSS
582 * files added through addStyles()
586 protected function getPrimaryStyles() {
587 return self
::concatStyles( $this->styles
);
591 * Get the debug JS for this module. This is pulled from the script
592 * files added through addDebugScripts()
596 protected function getDebugScript() {
597 return self
::concatScripts( $this->debugScripts
);
601 * Get the language-specific JS for a given language. This is pulled
602 * from the language-specific script files added through addLanguageScripts()
606 protected function getLanguageScript( $lang ) {
607 if ( !isset( $this->languageScripts
[$lang] ) ) {
610 return self
::concatScripts( $this->languageScripts
[$lang] );
614 * Get the skin-specific JS for a given skin. This is pulled from the
615 * skin-specific JS files added through addSkinScripts()
619 protected function getSkinScript( $skin ) {
620 return self
::concatScripts( self
::getSkinFiles( $skin, $this->skinScripts
) );
624 * Get the skin-specific CSS for a given skin. This is pulled from the
625 * skin-specific CSS files added through addSkinStyles()
627 * @return Array: list of CSS strings keyed by media type
629 protected function getSkinStyles( $skin ) {
630 return self
::concatStyles( self
::getSkinFiles( $skin, $this->skinStyles
) );
634 * Helper function to get skin-specific data from an array.
636 * @param $skin String: skin name
637 * @param $map Array: map of skin names to arrays
638 * @return $map[$skin] if set and non-empty, or $map['default'] if set, or an empty array
640 protected static function getSkinFiles( $skin, $map ) {
643 if ( isset( $map[$skin] ) && $map[$skin] ) {
644 $retval = $map[$skin];
645 } else if ( isset( $map['default'] ) ) {
646 $retval = $map['default'];
653 * Get the contents of a set of files and concatenate them, with
654 * newlines in between. Each file is used only once.
656 * @param $files Array of file names
657 * @return String: concatenated contents of $files
659 protected static function concatScripts( $files ) {
660 return implode( "\n",
664 array( __CLASS__
, 'remapFilename' ),
665 array_unique( (array) $files ) ) ) );
668 protected static function organizeFilesByOption( $files, $option, $default ) {
669 $organizedFiles = array();
670 foreach ( (array) $files as $key => $value ) {
671 if ( is_int( $key ) ) {
672 // File name as the value
673 if ( !isset( $organizedFiles[$default] ) ) {
674 $organizedFiles[$default] = array();
676 $organizedFiles[$default][] = $value;
677 } else if ( is_array( $value ) ) {
678 // File name as the key, options array as the value
679 $media = isset( $value[$option] ) ?
$value[$option] : $default;
680 if ( !isset( $organizedFiles[$media] ) ) {
681 $organizedFiles[$media] = array();
683 $organizedFiles[$media][] = $key;
686 return $organizedFiles;
690 * Get the contents of a set of CSS files, remap then and concatenate
691 * them, with newlines in between. Each file is used only once.
693 * @param $files Array of file names
694 * @return Array: list of concatenated and remapped contents of $files keyed by media type
696 protected static function concatStyles( $styles ) {
697 $styles = self
::organizeFilesByOption( $styles, 'media', 'all' );
698 foreach ( $styles as $media => $files ) {
702 array( __CLASS__
, 'remapStyle' ),
703 array_unique( (array) $files ) ) );
709 * Remap a relative to $IP. Used as a callback for array_map()
711 * @param $file String: file name
712 * @return string $IP/$file
714 protected static function remapFilename( $file ) {
721 * Get the contents of a CSS file and run it through CSSMin::remap().
722 * This wrapper is needed so we can use array_map() in concatStyles()
724 * @param $file String: file name
725 * @return string Remapped CSS
727 protected static function remapStyle( $file ) {
728 global $wgUseDataURLs, $wgScriptPath;
729 return CSSMin
::remap(
730 file_get_contents( self
::remapFilename( $file ) ),
732 $wgScriptPath . '/' . dirname( $file ),
739 * Abstraction for resource loader modules which pull from wiki pages
741 abstract class ResourceLoaderWikiModule
extends ResourceLoaderModule
{
743 /* Protected Members */
745 // In-object cache for modified time
746 protected $modifiedTime = array();
748 /* Abstract Protected Methods */
750 abstract protected function getPages( ResourceLoaderContext
$context );
752 /* Protected Methods */
754 protected function getContent( $page, $ns ) {
755 if ( $ns === NS_MEDIAWIKI
) {
756 return wfMsgExt( $page, 'content' );
758 if ( $title = Title
::newFromText( $page, $ns ) ) {
759 if ( $title->isValidCssJsSubpage() && $revision = Revision
::newFromTitle( $title ) ) {
760 return $revision->getRawText();
768 public function getScript( ResourceLoaderContext
$context ) {
769 global $wgCanonicalNamespaceNames;
772 foreach ( $this->getPages( $context ) as $page => $options ) {
773 if ( $options['type'] === 'script' ) {
774 if ( $script = $this->getContent( $page, $options['ns'] ) ) {
775 $ns = $wgCanonicalNamespaceNames[$options['ns']];
776 $scripts .= "/*$ns:$page */\n$script\n";
783 public function getStyles( ResourceLoaderContext
$context ) {
784 global $wgCanonicalNamespaceNames;
787 foreach ( $this->getPages( $context ) as $page => $options ) {
788 if ( $options['type'] === 'style' ) {
789 $media = isset( $options['media'] ) ?
$options['media'] : 'all';
790 if ( $style = $this->getContent( $page, $options['ns'] ) ) {
791 if ( !isset( $styles[$media] ) ) {
792 $styles[$media] = '';
794 $ns = $wgCanonicalNamespaceNames[$options['ns']];
795 $styles[$media] .= "/* $ns:$page */\n$style\n";
802 public function getModifiedTime( ResourceLoaderContext
$context ) {
803 $hash = $context->getHash();
804 if ( isset( $this->modifiedTime
[$hash] ) ) {
805 return $this->modifiedTime
[$hash];
809 foreach ( $this->getPages( $context ) as $page => $options ) {
810 $titles[$options['ns']][$page] = true;
813 $modifiedTime = 1; // wfTimestamp() interprets 0 as "now"
816 $dbr = wfGetDB( DB_SLAVE
);
817 $latest = $dbr->selectField( 'page', 'MAX(page_touched)',
818 $dbr->makeWhereFrom2d( $titles, 'page_namespace', 'page_title' ),
822 $modifiedTime = wfTimestamp( TS_UNIX
, $modifiedTime );
826 return $this->modifiedTime
[$hash] = $modifiedTime;
831 * Module for site customizations
833 class ResourceLoaderSiteModule
extends ResourceLoaderWikiModule
{
835 /* Protected Methods */
837 protected function getPages( ResourceLoaderContext
$context ) {
838 global $wgHandheldStyle;
841 'Common.js' => array( 'ns' => NS_MEDIAWIKI
, 'type' => 'script' ),
842 'Common.css' => array( 'ns' => NS_MEDIAWIKI
, 'type' => 'style' ),
843 ucfirst( $context->getSkin() ) . '.js' => array( 'ns' => NS_MEDIAWIKI
, 'type' => 'script' ),
844 ucfirst( $context->getSkin() ) . '.css' => array( 'ns' => NS_MEDIAWIKI
, 'type' => 'style' ),
845 'Print.css' => array( 'ns' => NS_MEDIAWIKI
, 'type' => 'style', 'media' => 'print' ),
847 if ( $wgHandheldStyle ) {
848 $pages['Handheld.css'] = array( 'ns' => NS_MEDIAWIKI
, 'type' => 'style', 'media' => 'handheld' );
855 public function getGroup() {
861 * Module for user customizations
863 class ResourceLoaderUserModule
extends ResourceLoaderWikiModule
{
865 /* Protected Methods */
867 protected function getPages( ResourceLoaderContext
$context ) {
868 global $wgAllowUserCss;
870 if ( $context->getUser() && $wgAllowUserCss ) {
871 $username = $context->getUser();
873 "$username/common.js" => array( 'ns' => NS_USER
, 'type' => 'script' ),
874 "$username/" . $context->getSkin() . '.js' => array( 'ns' => NS_USER
, 'type' => 'script' ),
875 "$username/common.css" => array( 'ns' => NS_USER
, 'type' => 'style' ),
876 "$username/" . $context->getSkin() . '.css' => array( 'ns' => NS_USER
, 'type' => 'style' ),
884 public function getGroup() {
890 * Module for user preference customizations
892 class ResourceLoaderUserOptionsModule
extends ResourceLoaderModule
{
894 /* Protected Members */
896 protected $modifiedTime = array();
900 public function getModifiedTime( ResourceLoaderContext
$context ) {
901 $hash = $context->getHash();
902 if ( isset( $this->modifiedTime
[$hash] ) ) {
903 return $this->modifiedTime
[$hash];
908 if ( $context->getUser() === $wgUser->getName() ) {
909 return $this->modifiedTime
[$hash] = $wgUser->getTouched();
915 public function getScript( ResourceLoaderContext
$context ) {
918 // Verify identity -- this is a private module
919 if ( $context->getUser() === $wgUser->getName() ) {
920 $options = FormatJson
::encode( $wgUser->getOptions() );
922 $options = FormatJson
::encode( User
::getDefaultOptions() );
924 return "mediaWiki.user.options.set( $options );";
927 public function getStyles( ResourceLoaderContext
$context ) {
928 global $wgUser, $wgAllowUserCssPrefs;
930 if ( $wgAllowUserCssPrefs ) {
931 // Verify identity -- this is a private module
932 if ( $context->getUser() === $wgUser->getName() ) {
933 $options = FormatJson
::encode( $wgUser->getOptions() );
935 $options = FormatJson
::encode( User
::getDefaultOptions() );
940 if ( $options['underline'] < 2 ) {
941 $rules[] = "a { text-decoration: " . ( $options['underline'] ?
'underline' : 'none' ) . "; }";
943 if ( $options['highlightbroken'] ) {
944 $rules[] = "a.new, #quickbar a.new { color: #CC2200; }\n";
946 $rules[] = "a.new, #quickbar a.new, a.stub, #quickbar a.stub { color: inherit; }";
947 $rules[] = "a.new:after, #quickbar a.new:after { content: '?'; color: #CC2200; }";
948 $rules[] = "a.stub:after, #quickbar a.stub:after { content: '!'; color: #772233; }";
950 if ( $options['justify'] ) {
951 $rules[] = "#article, #bodyContent, #mw_content { text-align: justify; }\n";
953 if ( !$options['showtoc'] ) {
954 $rules[] = "#toc { display: none; }\n";
956 if ( !$options['editsection'] ) {
957 $rules[] = ".editsection { display: none; }\n";
959 if ( $options['editfont'] !== 'default' ) {
960 $rules[] = "textarea { font-family: {$options['editfont']}; }\n";
962 return array( 'all' => implode( "\n", $rules ) );
967 public function getFlip( $context ) {
970 return $wgContLang->getDir() !== $context->getDirection();
973 public function getGroup() {
978 class ResourceLoaderStartUpModule
extends ResourceLoaderModule
{
979 /* Protected Members */
981 protected $modifiedTime = array();
983 /* Protected Methods */
985 protected function getConfig( $context ) {
986 global $wgLoadScript, $wgScript, $wgStylePath, $wgScriptExtension,
987 $wgArticlePath, $wgScriptPath, $wgServer, $wgContLang, $wgBreakFrames,
988 $wgVariantArticlePath, $wgActionPaths, $wgUseAjax, $wgVersion,
989 $wgEnableAPI, $wgEnableWriteAPI, $wgDBname, $wgEnableMWSuggest,
990 $wgSitename, $wgFileExtensions;
992 // Pre-process information
993 $separatorTransTable = $wgContLang->separatorTransformTable();
994 $separatorTransTable = $separatorTransTable ?
$separatorTransTable : array();
995 $compactSeparatorTransTable = array(
996 implode( "\t", array_keys( $separatorTransTable ) ),
997 implode( "\t", $separatorTransTable ),
999 $digitTransTable = $wgContLang->digitTransformTable();
1000 $digitTransTable = $digitTransTable ?
$digitTransTable : array();
1001 $compactDigitTransTable = array(
1002 implode( "\t", array_keys( $digitTransTable ) ),
1003 implode( "\t", $digitTransTable ),
1005 $mainPage = Title
::newMainPage();
1007 // Build list of variables
1009 'wgLoadScript' => $wgLoadScript,
1010 'debug' => $context->getDebug(),
1011 'skin' => $context->getSkin(),
1012 'stylepath' => $wgStylePath,
1013 'wgUrlProtocols' => wfUrlProtocols(),
1014 'wgArticlePath' => $wgArticlePath,
1015 'wgScriptPath' => $wgScriptPath,
1016 'wgScriptExtension' => $wgScriptExtension,
1017 'wgScript' => $wgScript,
1018 'wgVariantArticlePath' => $wgVariantArticlePath,
1019 'wgActionPaths' => $wgActionPaths,
1020 'wgServer' => $wgServer,
1021 'wgUserLanguage' => $context->getLanguage(),
1022 'wgContentLanguage' => $wgContLang->getCode(),
1023 'wgBreakFrames' => $wgBreakFrames,
1024 'wgVersion' => $wgVersion,
1025 'wgEnableAPI' => $wgEnableAPI,
1026 'wgEnableWriteAPI' => $wgEnableWriteAPI,
1027 'wgSeparatorTransformTable' => $compactSeparatorTransTable,
1028 'wgDigitTransformTable' => $compactDigitTransTable,
1029 'wgMainPageTitle' => $mainPage ?
$mainPage->getPrefixedText() : null,
1030 'wgFormattedNamespaces' => $wgContLang->getFormattedNamespaces(),
1031 'wgNamespaceIds' => $wgContLang->getNamespaceIds(),
1032 'wgSiteName' => $wgSitename,
1033 'wgFileExtensions' => $wgFileExtensions,
1035 if ( $wgContLang->hasVariants() ) {
1036 $vars['wgUserVariant'] = $wgContLang->getPreferredVariant();
1038 if ( $wgUseAjax && $wgEnableMWSuggest ) {
1039 $vars['wgMWSuggestTemplate'] = SearchEngine
::getMWSuggestTemplate();
1040 $vars['wgDBname'] = $wgDBname;
1047 * Gets registration code for all modules
1049 * @param $context ResourceLoaderContext object
1050 * @return String: JavaScript code for registering all modules with the client loader
1052 public static function getModuleRegistrations( ResourceLoaderContext
$context ) {
1053 wfProfileIn( __METHOD__
);
1056 $registrations = array();
1057 foreach ( ResourceLoader
::getModules() as $name => $module ) {
1058 // Support module loader scripts
1059 if ( ( $loader = $module->getLoaderScript() ) !== false ) {
1060 $deps = FormatJson
::encode( $module->getDependencies() );
1061 $group = FormatJson
::encode( $module->getGroup() );
1062 $version = wfTimestamp( TS_ISO_8601
, round( $module->getModifiedTime( $context ), -2 ) );
1063 $out .= ResourceLoader
::makeCustomLoaderScript( $name, $version, $deps, $group, $loader );
1065 // Automatically register module
1067 // Modules without dependencies or a group pass two arguments (name, timestamp) to
1068 // mediaWiki.loader.register()
1069 if ( !count( $module->getDependencies() && $module->getGroup() === null ) ) {
1070 $registrations[] = array( $name, $module->getModifiedTime( $context ) );
1072 // Modules with dependencies but no group pass three arguments (name, timestamp, dependencies)
1073 // to mediaWiki.loader.register()
1074 else if ( $module->getGroup() === null ) {
1075 $registrations[] = array(
1076 $name, $module->getModifiedTime( $context ), $module->getDependencies() );
1078 // Modules with dependencies pass four arguments (name, timestamp, dependencies, group)
1079 // to mediaWiki.loader.register()
1081 $registrations[] = array(
1082 $name, $module->getModifiedTime( $context ), $module->getDependencies(), $module->getGroup() );
1086 $out .= ResourceLoader
::makeLoaderRegisterScript( $registrations );
1088 wfProfileOut( __METHOD__
);
1094 public function getScript( ResourceLoaderContext
$context ) {
1095 global $IP, $wgLoadScript;
1097 $out = file_get_contents( "$IP/resources/startup.js" );
1098 if ( $context->getOnly() === 'scripts' ) {
1099 // Build load query for jquery and mediawiki modules
1101 'modules' => implode( '|', array( 'jquery', 'mediawiki' ) ),
1102 'only' => 'scripts',
1103 'lang' => $context->getLanguage(),
1104 'skin' => $context->getSkin(),
1105 'debug' => $context->getDebug() ?
'true' : 'false',
1106 'version' => wfTimestamp( TS_ISO_8601
, round( max(
1107 ResourceLoader
::getModule( 'jquery' )->getModifiedTime( $context ),
1108 ResourceLoader
::getModule( 'mediawiki' )->getModifiedTime( $context )
1111 // Ensure uniform query order
1115 $configuration = FormatJson
::encode( $this->getConfig( $context ) );
1116 $registrations = self
::getModuleRegistrations( $context );
1117 $out .= "window.startUp = function() {\n\t$registrations\n\tmediaWiki.config.set( $configuration );\n};";
1119 // Conditional script injection
1120 $scriptTag = Xml
::escapeJsString( Html
::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) ) );
1121 $out .= "if ( isCompatible() ) {\n\tdocument.write( '$scriptTag' );\n}\ndelete window['isCompatible'];";
1127 public function getModifiedTime( ResourceLoaderContext
$context ) {
1130 $hash = $context->getHash();
1131 if ( isset( $this->modifiedTime
[$hash] ) ) {
1132 return $this->modifiedTime
[$hash];
1134 $this->modifiedTime
[$hash] = filemtime( "$IP/resources/startup.js" );
1136 // ATTENTION!: Because of the line above, this is not going to cause infinite recursion - think carefully
1137 // before making changes to this code!
1138 return $this->modifiedTime
[$hash] = ResourceLoader
::getHighestModifiedTime( $context );
1141 public function getFlip( $context ) {
1144 return $wgContLang->getDir() !== $context->getDirection();
1149 public function getGroup() {