MediaWiki  REL1_19
ResourceLoaderContext.php
Go to the documentation of this file.
00001 <?php
00027 class ResourceLoaderContext {
00028 
00029         /* Protected Members */
00030 
00031         protected $resourceLoader;
00032         protected $request;
00033         protected $modules;
00034         protected $language;
00035         protected $direction;
00036         protected $skin;
00037         protected $user;
00038         protected $debug;
00039         protected $only;
00040         protected $version;
00041         protected $hash;
00042 
00043         /* Methods */
00044 
00049         public function __construct( $resourceLoader, WebRequest $request ) {
00050                 global $wgDefaultSkin, $wgResourceLoaderDebug;
00051 
00052                 $this->resourceLoader = $resourceLoader;
00053                 $this->request = $request;
00054 
00055                 // Interpret request
00056                 // List of modules
00057                 $modules = $request->getVal( 'modules' );
00058                 $this->modules   = $modules ? self::expandModuleNames( $modules ) : array();
00059                 // Various parameters
00060                 $this->skin      = $request->getVal( 'skin' );
00061                 $this->user      = $request->getVal( 'user' );
00062                 $this->debug     = $request->getFuzzyBool( 'debug', $wgResourceLoaderDebug );
00063                 $this->only      = $request->getVal( 'only' );
00064                 $this->version   = $request->getVal( 'version' );
00065 
00066                 $skinnames = Skin::getSkinNames();
00067                 // If no skin is specified, or we don't recognize the skin, use the default skin
00068                 if ( !$this->skin || !isset( $skinnames[$this->skin] ) ) {
00069                         $this->skin = $wgDefaultSkin;
00070                 }
00071         }
00072 
00080         public static function expandModuleNames( $modules ) {
00081                 $retval = array();
00082                 // For backwards compatibility with an earlier hack, replace ! with .
00083                 $modules = str_replace( '!', '.', $modules );
00084                 $exploded = explode( '|', $modules );
00085                 foreach ( $exploded as $group ) {
00086                         if ( strpos( $group, ',' ) === false ) {
00087                                 // This is not a set of modules in foo.bar,baz notation
00088                                 // but a single module
00089                                 $retval[] = $group;
00090                         } else {
00091                                 // This is a set of modules in foo.bar,baz notation
00092                                 $pos = strrpos( $group, '.' );
00093                                 if ( $pos === false ) {
00094                                         // Prefixless modules, i.e. without dots
00095                                         $retval = explode( ',', $group );
00096                                 } else {
00097                                         // We have a prefix and a bunch of suffixes
00098                                         $prefix = substr( $group, 0, $pos ); // 'foo'
00099                                         $suffixes = explode( ',', substr( $group, $pos + 1 ) ); // array( 'bar', 'baz' )
00100                                         foreach ( $suffixes as $suffix ) {
00101                                                 $retval[] = "$prefix.$suffix";
00102                                         }
00103                                 }
00104                         }
00105                 }
00106                 return $retval;
00107         }
00108 
00113         public static function newDummyContext() {
00114                 return new self( null, new FauxRequest( array() ) );
00115         }
00116 
00120         public function getResourceLoader() {
00121                 return $this->resourceLoader;
00122         }
00123 
00127         public function getRequest() {
00128                 return $this->request;
00129         }
00130 
00134         public function getModules() {
00135                 return $this->modules;
00136         }
00137 
00141         public function getLanguage() {
00142                 if ( $this->language === null ) {
00143                         global $wgLang;
00144                         $this->language  = $this->request->getVal( 'lang' );
00145                         if ( !$this->language ) {
00146                                 $this->language = $wgLang->getCode();
00147                         }
00148                 }
00149                 return $this->language;
00150         }
00151 
00155         public function getDirection() {
00156                 if ( $this->direction === null ) {
00157                         $this->direction = $this->request->getVal( 'dir' );
00158                         if ( !$this->direction ) {
00159                                 # directionality based on user language (see bug 6100)
00160                                 $this->direction = Language::factory( $this->language )->getDir();
00161                         }
00162                 }
00163                 return $this->direction;
00164         }
00165 
00169         public function getSkin() {
00170                 return $this->skin;
00171         }
00172 
00176         public function getUser() {
00177                 return $this->user;
00178         }
00179 
00183         public function getDebug() {
00184                 return $this->debug;
00185         }
00186 
00190         public function getOnly() {
00191                 return $this->only;
00192         }
00193 
00197         public function getVersion() {
00198                 return $this->version;
00199         }
00200 
00204         public function shouldIncludeScripts() {
00205                 return is_null( $this->only ) || $this->only === 'scripts';
00206         }
00207 
00211         public function shouldIncludeStyles() {
00212                 return is_null( $this->only ) || $this->only === 'styles';
00213         }
00214 
00218         public function shouldIncludeMessages() {
00219                 return is_null( $this->only ) || $this->only === 'messages';
00220         }
00221 
00225         public function getHash() {
00226                 if ( !isset( $this->hash ) ) {
00227                         $this->hash = implode( '|', array(
00228                                 $this->getLanguage(), $this->getDirection(), $this->skin, $this->user,
00229                                 $this->debug, $this->only, $this->version
00230                         ) );
00231                 }
00232                 return $this->hash;
00233         }
00234 }