/**
  * Get contents of a javascript file for inline use.
  *
  * Roughly based MediaWiki core methods:
  * - ResourceLoader::filter()
  * - ResourceLoaderFileModule::readScriptFiles()
  *
  * @param string $name Path to file relative to /modules/inline/
  * @return string Minified script
  * @throws Exception If file doesn't exist
  */
 protected static function getInlineScript($name)
 {
     // Get file
     $filePath = __DIR__ . '/../../modules/inline/' . $name;
     if (!file_exists($filePath)) {
         throw new Exception(__METHOD__ . ": file not found: \"{$filePath}\"");
     }
     $contents = file_get_contents($filePath);
     // Try minified from cache
     $key = wfMemcKey('centralauth', 'minify-js', md5($contents));
     $cache = wfGetCache(CACHE_ANYTHING);
     $cacheEntry = $cache->get($key);
     if (is_string($cacheEntry)) {
         return $cacheEntry;
     }
     // Compute new value
     $result = '';
     try {
         $result = JavaScriptMinifier::minify($contents) . "\n/* cache key: {$key} */";
         $cache->set($key, $result);
     } catch (Exception $e) {
         MWExceptionHandler::logException($e);
         wfDebugLog('CentralAuth', __METHOD__ . ": minification failed for {$name}: {$e}");
         $result = ResourceLoader::formatException($e) . "\n" . $contents;
     }
     return $result;
 }
コード例 #2
0
 /**
  * Get the raw vector CSS, flipping if needed
  *
  * @todo Possibly get rid of this function and use ResourceLoader in the manner it was
  *   designed to be used in, rather than just grabbing a list of filenames from it,
  *   and not properly handling such details as media types in module definitions.
  *
  * @param string $dir 'ltr' or 'rtl'
  * @return String
  */
 public function getCSS($dir)
 {
     // All CSS files these modules reference will be concatenated in sequence
     // and loaded as one file.
     $moduleNames = array('mediawiki.legacy.shared', 'skins.common.interface', 'skins.vector.styles', 'mediawiki.legacy.config');
     $prepend = '';
     $css = '';
     $resourceLoader = new ResourceLoader();
     foreach ($moduleNames as $moduleName) {
         /** @var ResourceLoaderFileModule $module */
         $module = $resourceLoader->getModule($moduleName);
         $cssFileNames = $module->getAllStyleFiles();
         wfSuppressWarnings();
         foreach ($cssFileNames as $cssFileName) {
             if (!file_exists($cssFileName)) {
                 $prepend .= ResourceLoader::makeComment("Unable to find {$cssFileName}.");
                 continue;
             }
             if (!is_readable($cssFileName)) {
                 $prepend .= ResourceLoader::makeComment("Unable to read {$cssFileName}. " . "Please check file permissions.");
                 continue;
             }
             try {
                 if (preg_match('/\\.less$/', $cssFileName)) {
                     // Run the LESS compiler for *.less files (bug 55589)
                     $compiler = ResourceLoader::getLessCompiler();
                     $cssFileContents = $compiler->compileFile($cssFileName);
                 } else {
                     // Regular CSS file
                     $cssFileContents = file_get_contents($cssFileName);
                 }
                 if ($cssFileContents) {
                     // Rewrite URLs, though don't bother embedding images. While static image
                     // files may be cached, CSS returned by this function is definitely not.
                     $cssDirName = dirname($cssFileName);
                     $css .= CSSMin::remap($cssFileContents, $cssDirName, '..' . str_replace(array($GLOBALS['IP'], DIRECTORY_SEPARATOR), array('', '/'), $cssDirName), false);
                 } else {
                     $prepend .= ResourceLoader::makeComment("Unable to read {$cssFileName}.");
                 }
             } catch (Exception $e) {
                 $prepend .= ResourceLoader::formatException($e);
             }
             $css .= "\n";
         }
         wfRestoreWarnings();
     }
     $css = $prepend . $css;
     if ($dir == 'rtl') {
         $css = CSSJanus::transform($css, true);
     }
     return $css;
 }