MediaWiki
REL1_23
|
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 $exploded = explode( '|', $modules ); 00087 foreach ( $exploded as $group ) { 00088 if ( strpos( $group, ',' ) === false ) { 00089 // This is not a set of modules in foo.bar,baz notation 00090 // but a single module 00091 $retval[] = $group; 00092 } else { 00093 // This is a set of modules in foo.bar,baz notation 00094 $pos = strrpos( $group, '.' ); 00095 if ( $pos === false ) { 00096 // Prefixless modules, i.e. without dots 00097 $retval = array_merge( $retval, explode( ',', $group ) ); 00098 } else { 00099 // We have a prefix and a bunch of suffixes 00100 $prefix = substr( $group, 0, $pos ); // 'foo' 00101 $suffixes = explode( ',', substr( $group, $pos + 1 ) ); // array( 'bar', 'baz' ) 00102 foreach ( $suffixes as $suffix ) { 00103 $retval[] = "$prefix.$suffix"; 00104 } 00105 } 00106 } 00107 } 00108 return $retval; 00109 } 00110 00115 public static function newDummyContext() { 00116 return new self( null, new FauxRequest( array() ) ); 00117 } 00118 00122 public function getResourceLoader() { 00123 return $this->resourceLoader; 00124 } 00125 00129 public function getRequest() { 00130 return $this->request; 00131 } 00132 00136 public function getModules() { 00137 return $this->modules; 00138 } 00139 00143 public function getLanguage() { 00144 if ( $this->language === null ) { 00145 // Must be a valid language code after this point (bug 62849) 00146 $this->language = RequestContext::sanitizeLangCode( $this->request->getVal( 'lang' ) ); 00147 } 00148 return $this->language; 00149 } 00150 00154 public function getDirection() { 00155 if ( $this->direction === null ) { 00156 $this->direction = $this->request->getVal( 'dir' ); 00157 if ( !$this->direction ) { 00158 // Determine directionality based on user language (bug 6100) 00159 $this->direction = Language::factory( $this->getLanguage() )->getDir(); 00160 } 00161 } 00162 return $this->direction; 00163 } 00164 00168 public function getSkin() { 00169 return $this->skin; 00170 } 00171 00175 public function getUser() { 00176 return $this->user; 00177 } 00178 00182 public function getDebug() { 00183 return $this->debug; 00184 } 00185 00189 public function getOnly() { 00190 return $this->only; 00191 } 00192 00196 public function getVersion() { 00197 return $this->version; 00198 } 00199 00203 public function getRaw() { 00204 return $this->raw; 00205 } 00206 00210 public function shouldIncludeScripts() { 00211 return is_null( $this->only ) || $this->only === 'scripts'; 00212 } 00213 00217 public function shouldIncludeStyles() { 00218 return is_null( $this->only ) || $this->only === 'styles'; 00219 } 00220 00224 public function shouldIncludeMessages() { 00225 return is_null( $this->only ) || $this->only === 'messages'; 00226 } 00227 00231 public function getHash() { 00232 if ( !isset( $this->hash ) ) { 00233 $this->hash = implode( '|', array( 00234 $this->getLanguage(), $this->getDirection(), $this->skin, $this->user, 00235 $this->debug, $this->only, $this->version 00236 ) ); 00237 } 00238 return $this->hash; 00239 } 00240 }