MediaWiki
REL1_24
|
00001 <?php 00029 class ResourceLoaderContext { 00030 /* Protected Members */ 00031 00032 protected $resourceLoader; 00033 protected $request; 00034 protected $modules; 00035 protected $language; 00036 protected $direction; 00037 protected $skin; 00038 protected $user; 00039 protected $debug; 00040 protected $only; 00041 protected $version; 00042 protected $hash; 00043 protected $raw; 00044 00045 /* Methods */ 00046 00051 public function __construct( ResourceLoader $resourceLoader, WebRequest $request ) { 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( 00063 'debug', $resourceLoader->getConfig()->get( 'ResourceLoaderDebug' ) 00064 ); 00065 $this->only = $request->getVal( 'only' ); 00066 $this->version = $request->getVal( 'version' ); 00067 $this->raw = $request->getFuzzyBool( 'raw' ); 00068 00069 $skinnames = Skin::getSkinNames(); 00070 // If no skin is specified, or we don't recognize the skin, use the default skin 00071 if ( !$this->skin || !isset( $skinnames[$this->skin] ) ) { 00072 $this->skin = $resourceLoader->getConfig()->get( 'DefaultSkin' ); 00073 } 00074 } 00075 00083 public static function expandModuleNames( $modules ) { 00084 $retval = array(); 00085 $exploded = explode( '|', $modules ); 00086 foreach ( $exploded as $group ) { 00087 if ( strpos( $group, ',' ) === false ) { 00088 // This is not a set of modules in foo.bar,baz notation 00089 // but a single module 00090 $retval[] = $group; 00091 } else { 00092 // This is a set of modules in foo.bar,baz notation 00093 $pos = strrpos( $group, '.' ); 00094 if ( $pos === false ) { 00095 // Prefixless modules, i.e. without dots 00096 $retval = array_merge( $retval, explode( ',', $group ) ); 00097 } else { 00098 // We have a prefix and a bunch of suffixes 00099 $prefix = substr( $group, 0, $pos ); // 'foo' 00100 $suffixes = explode( ',', substr( $group, $pos + 1 ) ); // array( 'bar', 'baz' ) 00101 foreach ( $suffixes as $suffix ) { 00102 $retval[] = "$prefix.$suffix"; 00103 } 00104 } 00105 } 00106 } 00107 return $retval; 00108 } 00109 00115 public static function newDummyContext() { 00116 return new self( new ResourceLoader( 00117 ConfigFactory::getDefaultInstance()->makeConfig( 'main' ) 00118 ), 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 // Must be a valid language code after this point (bug 62849) 00148 $this->language = RequestContext::sanitizeLangCode( $this->request->getVal( 'lang' ) ); 00149 } 00150 return $this->language; 00151 } 00152 00156 public function getDirection() { 00157 if ( $this->direction === null ) { 00158 $this->direction = $this->request->getVal( 'dir' ); 00159 if ( !$this->direction ) { 00160 // Determine directionality based on user language (bug 6100) 00161 $this->direction = Language::factory( $this->getLanguage() )->getDir(); 00162 } 00163 } 00164 return $this->direction; 00165 } 00166 00170 public function getSkin() { 00171 return $this->skin; 00172 } 00173 00177 public function getUser() { 00178 return $this->user; 00179 } 00180 00184 public function getDebug() { 00185 return $this->debug; 00186 } 00187 00191 public function getOnly() { 00192 return $this->only; 00193 } 00194 00198 public function getVersion() { 00199 return $this->version; 00200 } 00201 00205 public function getRaw() { 00206 return $this->raw; 00207 } 00208 00212 public function shouldIncludeScripts() { 00213 return is_null( $this->getOnly() ) || $this->getOnly() === 'scripts'; 00214 } 00215 00219 public function shouldIncludeStyles() { 00220 return is_null( $this->getOnly() ) || $this->getOnly() === 'styles'; 00221 } 00222 00226 public function shouldIncludeMessages() { 00227 return is_null( $this->getOnly() ) || $this->getOnly() === 'messages'; 00228 } 00229 00233 public function getHash() { 00234 if ( !isset( $this->hash ) ) { 00235 $this->hash = implode( '|', array( 00236 $this->getLanguage(), $this->getDirection(), $this->getSkin(), $this->getUser(), 00237 $this->getDebug(), $this->getOnly(), $this->getVersion() 00238 ) ); 00239 } 00240 return $this->hash; 00241 } 00242 }