MediaWiki  REL1_22
ResourceLoaderContext.php
Go to the documentation of this file.
00001 <?php
00029 class ResourceLoaderContext {
00030 
00031     /* Protected Members */
00032 
00033     protected $resourceLoader;
00034     protected $request;
00035     protected $modules;
00036     protected $language;
00037     protected $direction;
00038     protected $skin;
00039     protected $user;
00040     protected $debug;
00041     protected $only;
00042     protected $version;
00043     protected $hash;
00044     protected $raw;
00045 
00046     /* Methods */
00047 
00052     public function __construct( $resourceLoader, WebRequest $request ) {
00053         global $wgDefaultSkin, $wgResourceLoaderDebug;
00054 
00055         $this->resourceLoader = $resourceLoader;
00056         $this->request = $request;
00057 
00058         // Interpret request
00059         // List of modules
00060         $modules = $request->getVal( 'modules' );
00061         $this->modules = $modules ? self::expandModuleNames( $modules ) : array();
00062         // Various parameters
00063         $this->skin = $request->getVal( 'skin' );
00064         $this->user = $request->getVal( 'user' );
00065         $this->debug = $request->getFuzzyBool( 'debug', $wgResourceLoaderDebug );
00066         $this->only = $request->getVal( 'only' );
00067         $this->version = $request->getVal( 'version' );
00068         $this->raw = $request->getFuzzyBool( 'raw' );
00069 
00070         $skinnames = Skin::getSkinNames();
00071         // If no skin is specified, or we don't recognize the skin, use the default skin
00072         if ( !$this->skin || !isset( $skinnames[$this->skin] ) ) {
00073             $this->skin = $wgDefaultSkin;
00074         }
00075     }
00076 
00084     public static function expandModuleNames( $modules ) {
00085         $retval = array();
00086         // For backwards compatibility with an earlier hack, replace ! with .
00087         $modules = str_replace( '!', '.', $modules );
00088         $exploded = explode( '|', $modules );
00089         foreach ( $exploded as $group ) {
00090             if ( strpos( $group, ',' ) === false ) {
00091                 // This is not a set of modules in foo.bar,baz notation
00092                 // but a single module
00093                 $retval[] = $group;
00094             } else {
00095                 // This is a set of modules in foo.bar,baz notation
00096                 $pos = strrpos( $group, '.' );
00097                 if ( $pos === false ) {
00098                     // Prefixless modules, i.e. without dots
00099                     $retval = array_merge( $retval, explode( ',', $group ) );
00100                 } else {
00101                     // We have a prefix and a bunch of suffixes
00102                     $prefix = substr( $group, 0, $pos ); // 'foo'
00103                     $suffixes = explode( ',', substr( $group, $pos + 1 ) ); // array( 'bar', 'baz' )
00104                     foreach ( $suffixes as $suffix ) {
00105                         $retval[] = "$prefix.$suffix";
00106                     }
00107                 }
00108             }
00109         }
00110         return $retval;
00111     }
00112 
00117     public static function newDummyContext() {
00118         return new self( null, new FauxRequest( array() ) );
00119     }
00120 
00124     public function getResourceLoader() {
00125         return $this->resourceLoader;
00126     }
00127 
00131     public function getRequest() {
00132         return $this->request;
00133     }
00134 
00138     public function getModules() {
00139         return $this->modules;
00140     }
00141 
00145     public function getLanguage() {
00146         if ( $this->language === null ) {
00147             global $wgLang;
00148             $this->language = $this->request->getVal( 'lang' );
00149             if ( !$this->language ) {
00150                 $this->language = $wgLang->getCode();
00151             }
00152         }
00153         return $this->language;
00154     }
00155 
00159     public function getDirection() {
00160         if ( $this->direction === null ) {
00161             $this->direction = $this->request->getVal( 'dir' );
00162             if ( !$this->direction ) {
00163                 # directionality based on user language (see bug 6100)
00164                 $this->direction = Language::factory( $this->getLanguage() )->getDir();
00165             }
00166         }
00167         return $this->direction;
00168     }
00169 
00173     public function getSkin() {
00174         return $this->skin;
00175     }
00176 
00180     public function getUser() {
00181         return $this->user;
00182     }
00183 
00187     public function getDebug() {
00188         return $this->debug;
00189     }
00190 
00194     public function getOnly() {
00195         return $this->only;
00196     }
00197 
00201     public function getVersion() {
00202         return $this->version;
00203     }
00204 
00208     public function getRaw() {
00209         return $this->raw;
00210     }
00211 
00215     public function shouldIncludeScripts() {
00216         return is_null( $this->only ) || $this->only === 'scripts';
00217     }
00218 
00222     public function shouldIncludeStyles() {
00223         return is_null( $this->only ) || $this->only === 'styles';
00224     }
00225 
00229     public function shouldIncludeMessages() {
00230         return is_null( $this->only ) || $this->only === 'messages';
00231     }
00232 
00236     public function getHash() {
00237         if ( !isset( $this->hash ) ) {
00238             $this->hash = implode( '|', array(
00239                 $this->getLanguage(), $this->getDirection(), $this->skin, $this->user,
00240                 $this->debug, $this->only, $this->version
00241             ) );
00242         }
00243         return $this->hash;
00244     }
00245 }