Ejemplo n.º 1
0
 /** @dataProvider devBoxReplaceAssetServerProvider */
 public function testDevBoxReplaceAssetServer($url, $regex)
 {
     $this->mockGlobalVariable('wgDevelEnvironment', true);
     $replacedUrl = wfReplaceAssetServer($url);
     if ($regex) {
         $this->assertEquals(true, preg_match($regex, $replacedUrl));
     } else {
         $this->assertEquals($url, $replacedUrl);
     }
 }
Ejemplo n.º 2
0
 /**
  * Returns the link tags for stylesheets to be output for this template as an array
  *
  * @return array an array with the following format:
  * array( 'url' => 'asset, null if inlined code', 'tag' => 'the original tag found in the HTML output' );
  */
 public function getStyles()
 {
     $this->wf->profileIn(__METHOD__);
     //there are a number of extension that use addScript to append link tags for stylesheets, need to include those too
     $stylesTags = $this->wg->out->buildCssLinks() . $this->wg->out->getHeadItems() . $this->wg->out->getScriptsOnly();
     $am = F::build('AssetsManager', array(), 'getInstance');
     $matches = array();
     $res = array();
     //find all the link tags, including conditionals
     preg_match_all(self::LINK_REGEX, $stylesTags, $matches);
     if (!empty($matches[0])) {
         foreach ($matches[0] as $m) {
             $hrefMatch = array();
             //find the src if set
             preg_match('/<link[^>]+href=["\'\\s]?([^"\'>\\s]+)["\'\\s]?[^>]*>/im', $m, $hrefMatch);
             if (!empty($hrefMatch[1]) && $am->checkAssetUrlForSkin($hrefMatch[1], $this)) {
                 //fix HTML::element's expansion of ampersands in the src attribute
                 // todo: do we really need this trick? I notice URLs that are not properly encoded in the head element
                 $url = str_replace('&amp;', '&', $hrefMatch[1]);
                 // apply domain sharding
                 $url = wfReplaceAssetServer($url);
                 $res[] = array('url' => $url, 'tag' => str_replace('&amp;', '&', $m));
             } elseif (empty($hrefMatch[1]) && !$this->strictAssetUrlCheck) {
                 $res[] = array('url' => null, 'tag' => $m);
             }
         }
     }
     //find all the inline style tags, including conditionals
     preg_match_all(self::STYLE_REGEX, $stylesTags, $matches);
     if (!empty($matches[0])) {
         foreach ($matches[0] as $m) {
             if (!$this->strictAssetUrlCheck) {
                 //only non-strict skins accept inline elements
                 $res[] = array('url' => null, 'tag' => $m);
             }
         }
     }
     $this->wf->profileOut(__METHOD__);
     return $res;
 }
 /**
  * @param $path string
  * @return string
  */
 protected function getRemotePath($path, ResourceLoaderContext $context)
 {
     switch (self::getFileType($path)) {
         case self::FILE_TYPE_SASS:
             return AssetsManager::getInstance()->getSassLocalURL($path, false);
             break;
         default:
             $url = "{$this->remoteBasePath}/{$path}";
             // apply domain sharding
             $url = wfReplaceAssetServer($url);
             return $url;
     }
 }
 public static function onAlternateResourceLoaderURL(&$loadScript, &$query, &$url, $modules)
 {
     $resourceLoaderInstance = self::getResourceLoaderInstance();
     $source = false;
     foreach ($modules as $moduleName) {
         $module = $resourceLoaderInstance->getModule($moduleName);
         if (!$module) {
             continue;
         }
         $moduleSource = $module->getSource();
         if ($source === false) {
             // first module is being inspected
             $source = $moduleSource;
         } elseif ($source !== $moduleSource) {
             // if there are at least two different sources used just fall back
             // to use local source
             $source = 'local';
             break;
         }
     }
     if (empty($source)) {
         $source = 'local';
     }
     $sources = $resourceLoaderInstance->getSources();
     $loadScript = $sources[$source]['loadScript'];
     // this is triggered only when $wgEnableResourceLoaderRewrites is set
     if (substr($loadScript, -1) == '/') {
         $loadQuery = $query;
         $modules = $loadQuery['modules'];
         unset($loadQuery['modules']);
         $params = urlencode(http_build_query($loadQuery));
         $url = $loadScript . "{$params}/{$modules}";
         $url = wfExpandUrl($url, PROTO_RELATIVE);
         // apply domain sharding
         $url = wfReplaceAssetServer($url);
     } else {
         // just a copy&paste from ResourceLoader::makeLoaderURL :-(
         $url = wfExpandUrl(wfAppendQuery($loadScript, $query) . '&*', PROTO_RELATIVE);
     }
     // apply domain sharding
     $url = wfReplaceAssetServer($url);
     return false;
 }
Ejemplo n.º 5
0
 /**
  * @author Inez Korczyński <*****@*****.**>
  * @return array Array of one or many URLs
  */
 private function getGroupURL($groupName, $params, $prefix, $combine, $minify)
 {
     wfProfileIn(__METHOD__);
     //lazy loading of AssetsConfig
     $this->loadConfig();
     // pass noexternals mode (BugId:28143)
     global $wgNoExternals;
     if (!empty($wgNoExternals)) {
         $params['noexternals'] = 1;
     }
     $assets = $this->mAssetsConfig->resolve($groupName, $this->mCombine, $this->mMinify, $params);
     $URLs = array();
     if ($combine !== null ? $combine : $this->mCombine) {
         // "minify" is a special parameter that can be set only when initialising object and can not be overwritten per request
         if ($minify !== null ? !$minify : !$this->mMinify) {
             $params['minify'] = false;
         } else {
             unset($params['minify']);
         }
         // check for an #external_ URL being in the package (BugId:9522)
         $isEmpty = true;
         foreach ($assets as $asset) {
             if (substr($asset, 0, 10) == '#external_') {
                 $URLs[] = substr($asset, 10);
             } else {
                 $isEmpty = false;
             }
         }
         // add info about noexternals mode to AssetsManager URL (BugId:28143)
         global $wgNoExternals;
         if (!empty($wgNoExternals)) {
             $params['noexternals'] = 1;
         }
         // When AssetsManager works in "combine" mode return URL to the combined package
         if (!$isEmpty) {
             $url = $prefix . $this->getAMLocalURL('group', $groupName, $params);
             // apply domain sharding
             $url = wfReplaceAssetServer($url);
             $URLs[] = $url;
         }
     } else {
         foreach ($assets as $asset) {
             if (substr($asset, 0, 10) == '#external_') {
                 $url = substr($asset, 10);
             } else {
                 if (Http::isValidURI($asset)) {
                     $url = $asset;
                 } else {
                     // We always need to use common host for static assets since it has
                     // the information about the slot which is otherwise not available
                     // in varnish (BugId: 33905)
                     $url = $this->getOneCommonURL($asset, $minify);
                 }
             }
             // apply domain sharding
             $url = wfReplaceAssetServer($url);
             $URLs[] = $url;
         }
     }
     $this->mGeneratedUrls += array_fill_keys($URLs, $groupName);
     wfProfileOut(__METHOD__);
     return $URLs;
 }