Merge "Cache processed stylesheets during the parse"

This commit is contained in:
jenkins-bot 2018-02-02 02:45:31 +00:00 committed by Gerrit Code Review
commit 7b025c809a
2 changed files with 37 additions and 4 deletions

View file

@ -126,6 +126,7 @@ class TemplateStylesHooks {
*/
public static function onParserFirstCallInit( &$parser ) {
$parser->setHook( 'templatestyles', 'TemplateStylesHooks::handleTag' );
$parser->extTemplateStylesCache = new MapCacheLRU( 100 ); // 100 is arbitrary
return true;
}
@ -178,6 +179,14 @@ class TemplateStylesHooks {
return true;
}
/**
* Clear our cache when the parser is reset
* @param Parser $parser
*/
public static function onParserClearState( Parser $parser ) {
$parser->extTemplateStylesCache->clear();
}
/**
* Parser hook for `<templatestyles>`
* @param string $text Contents of the tag (ignored).
@ -239,13 +248,32 @@ class TemplateStylesHooks {
'</strong>';
}
// For the moment just output the styles inline.
// @todo: If T160563 happens, it would be good to convert this to use that.
// If the revision actually has an ID, cache based on that.
// Otherwise, cache by hash.
if ( $rev->getId() ) {
$cacheKey = 'r' . $rev->getId();
} else {
$cacheKey = sha1( $content->getNativeData() );
}
// Include any non-default wrapper class in the cache key too
$wrapClass = $parser->getOptions()->getWrapOutputClass();
if ( $wrapClass === false ) {
$wrapClass = 'mw-parser-output';
}
if ( $wrapClass !== 'mw-parser-output' ) {
$cacheKey .= '/' . $wrapClass;
}
// Already cached?
if ( $parser->extTemplateStylesCache->has( $cacheKey ) ) {
return $parser->extTemplateStylesCache->get( $cacheKey );
}
$status = $content->sanitize( [
'flip' => $parser->getTargetLanguage()->getDir() !== $wgContLang->getDir(),
'minify' => !ResourceLoader::inDebugMode(),
'class' => $parser->getOptions()->getWrapOutputClass(),
'class' => $wrapClass,
] );
$style = $status->isOk() ? $status->getValue() : '/* Fatal error, no CSS will be output */';
@ -274,7 +302,9 @@ class TemplateStylesHooks {
// Return the inline <style>, which the Parser will wrap in a 'general'
// strip marker.
return Html::inlineStyle( $marker );
$ret = Html::inlineStyle( $marker );
$parser->extTemplateStylesCache->set( $cacheKey, $ret );
return $ret;
}
}

View file

@ -35,6 +35,9 @@
"ParserFirstCallInit": [
"TemplateStylesHooks::onParserFirstCallInit"
],
"ParserClearState": [
"TemplateStylesHooks::onParserClearState"
],
"ParserAfterTidy": [
"TemplateStylesHooks::onParserAfterTidy"
],