[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/resourceloader/ -> ResourceLoaderContext.php (source)

   1  <?php
   2  /**
   3   * Context for resource loader modules.
   4   *
   5   * This program is free software; you can redistribute it and/or modify
   6   * it under the terms of the GNU General Public License as published by
   7   * the Free Software Foundation; either version 2 of the License, or
   8   * (at your option) any later version.
   9   *
  10   * This program is distributed in the hope that it will be useful,
  11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13   * GNU General Public License for more details.
  14   *
  15   * You should have received a copy of the GNU General Public License along
  16   * with this program; if not, write to the Free Software Foundation, Inc.,
  17   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18   * http://www.gnu.org/copyleft/gpl.html
  19   *
  20   * @file
  21   * @author Trevor Parscal
  22   * @author Roan Kattouw
  23   */
  24  
  25  /**
  26   * Object passed around to modules which contains information about the state
  27   * of a specific loader request
  28   */
  29  class ResourceLoaderContext {
  30      /* Protected Members */
  31  
  32      protected $resourceLoader;
  33      protected $request;
  34      protected $modules;
  35      protected $language;
  36      protected $direction;
  37      protected $skin;
  38      protected $user;
  39      protected $debug;
  40      protected $only;
  41      protected $version;
  42      protected $hash;
  43      protected $raw;
  44  
  45      /* Methods */
  46  
  47      /**
  48       * @param ResourceLoader $resourceLoader
  49       * @param WebRequest $request
  50       */
  51  	public function __construct( ResourceLoader $resourceLoader, WebRequest $request ) {
  52          $this->resourceLoader = $resourceLoader;
  53          $this->request = $request;
  54  
  55          // Interpret request
  56          // List of modules
  57          $modules = $request->getVal( 'modules' );
  58          $this->modules = $modules ? self::expandModuleNames( $modules ) : array();
  59          // Various parameters
  60          $this->skin = $request->getVal( 'skin' );
  61          $this->user = $request->getVal( 'user' );
  62          $this->debug = $request->getFuzzyBool(
  63              'debug', $resourceLoader->getConfig()->get( 'ResourceLoaderDebug' )
  64          );
  65          $this->only = $request->getVal( 'only' );
  66          $this->version = $request->getVal( 'version' );
  67          $this->raw = $request->getFuzzyBool( 'raw' );
  68  
  69          $skinnames = Skin::getSkinNames();
  70          // If no skin is specified, or we don't recognize the skin, use the default skin
  71          if ( !$this->skin || !isset( $skinnames[$this->skin] ) ) {
  72              $this->skin = $resourceLoader->getConfig()->get( 'DefaultSkin' );
  73          }
  74      }
  75  
  76      /**
  77       * Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to
  78       * an array of module names like array( 'jquery.foo', 'jquery.bar',
  79       * 'jquery.ui.baz', 'jquery.ui.quux' )
  80       * @param string $modules Packed module name list
  81       * @return array Array of module names
  82       */
  83  	public static function expandModuleNames( $modules ) {
  84          $retval = array();
  85          $exploded = explode( '|', $modules );
  86          foreach ( $exploded as $group ) {
  87              if ( strpos( $group, ',' ) === false ) {
  88                  // This is not a set of modules in foo.bar,baz notation
  89                  // but a single module
  90                  $retval[] = $group;
  91              } else {
  92                  // This is a set of modules in foo.bar,baz notation
  93                  $pos = strrpos( $group, '.' );
  94                  if ( $pos === false ) {
  95                      // Prefixless modules, i.e. without dots
  96                      $retval = array_merge( $retval, explode( ',', $group ) );
  97                  } else {
  98                      // We have a prefix and a bunch of suffixes
  99                      $prefix = substr( $group, 0, $pos ); // 'foo'
 100                      $suffixes = explode( ',', substr( $group, $pos + 1 ) ); // array( 'bar', 'baz' )
 101                      foreach ( $suffixes as $suffix ) {
 102                          $retval[] = "$prefix.$suffix";
 103                      }
 104                  }
 105              }
 106          }
 107          return $retval;
 108      }
 109  
 110      /**
 111       * Return a dummy ResourceLoaderContext object suitable for passing into
 112       * things that don't "really" need a context.
 113       * @return ResourceLoaderContext
 114       */
 115  	public static function newDummyContext() {
 116          return new self( new ResourceLoader(
 117              ConfigFactory::getDefaultInstance()->makeConfig( 'main' )
 118          ), new FauxRequest( array() ) );
 119      }
 120  
 121      /**
 122       * @return ResourceLoader
 123       */
 124  	public function getResourceLoader() {
 125          return $this->resourceLoader;
 126      }
 127  
 128      /**
 129       * @return WebRequest
 130       */
 131  	public function getRequest() {
 132          return $this->request;
 133      }
 134  
 135      /**
 136       * @return array
 137       */
 138  	public function getModules() {
 139          return $this->modules;
 140      }
 141  
 142      /**
 143       * @return string
 144       */
 145  	public function getLanguage() {
 146          if ( $this->language === null ) {
 147              // Must be a valid language code after this point (bug 62849)
 148              $this->language = RequestContext::sanitizeLangCode( $this->request->getVal( 'lang' ) );
 149          }
 150          return $this->language;
 151      }
 152  
 153      /**
 154       * @return string
 155       */
 156  	public function getDirection() {
 157          if ( $this->direction === null ) {
 158              $this->direction = $this->request->getVal( 'dir' );
 159              if ( !$this->direction ) {
 160                  // Determine directionality based on user language (bug 6100)
 161                  $this->direction = Language::factory( $this->getLanguage() )->getDir();
 162              }
 163          }
 164          return $this->direction;
 165      }
 166  
 167      /**
 168       * @return string|null
 169       */
 170  	public function getSkin() {
 171          return $this->skin;
 172      }
 173  
 174      /**
 175       * @return string|null
 176       */
 177  	public function getUser() {
 178          return $this->user;
 179      }
 180  
 181      /**
 182       * @return bool
 183       */
 184  	public function getDebug() {
 185          return $this->debug;
 186      }
 187  
 188      /**
 189       * @return string|null
 190       */
 191  	public function getOnly() {
 192          return $this->only;
 193      }
 194  
 195      /**
 196       * @return string|null
 197       */
 198  	public function getVersion() {
 199          return $this->version;
 200      }
 201  
 202      /**
 203       * @return bool
 204       */
 205  	public function getRaw() {
 206          return $this->raw;
 207      }
 208  
 209      /**
 210       * @return bool
 211       */
 212  	public function shouldIncludeScripts() {
 213          return is_null( $this->getOnly() ) || $this->getOnly() === 'scripts';
 214      }
 215  
 216      /**
 217       * @return bool
 218       */
 219  	public function shouldIncludeStyles() {
 220          return is_null( $this->getOnly() ) || $this->getOnly() === 'styles';
 221      }
 222  
 223      /**
 224       * @return bool
 225       */
 226  	public function shouldIncludeMessages() {
 227          return is_null( $this->getOnly() ) || $this->getOnly() === 'messages';
 228      }
 229  
 230      /**
 231       * @return string
 232       */
 233  	public function getHash() {
 234          if ( !isset( $this->hash ) ) {
 235              $this->hash = implode( '|', array(
 236                  $this->getLanguage(), $this->getDirection(), $this->getSkin(), $this->getUser(),
 237                  $this->getDebug(), $this->getOnly(), $this->getVersion()
 238              ) );
 239          }
 240          return $this->hash;
 241      }
 242  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1