Anomie has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/394369 )

Change subject: Cache processed stylesheets during the parse
......................................................................

Cache processed stylesheets during the parse

If we've already processed a stylesheet once, there's no point in
processing it again.

Change-Id: I83f7aab82cc7674037974b0de43ccae6c77ff39f
---
M TemplateStylesHooks.php
M extension.json
2 files changed, 37 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/TemplateStyles 
refs/changes/69/394369/1

diff --git a/TemplateStylesHooks.php b/TemplateStylesHooks.php
index 4742cab..5587a46 100644
--- a/TemplateStylesHooks.php
+++ b/TemplateStylesHooks.php
@@ -126,6 +126,7 @@
         */
        public static function onParserFirstCallInit( &$parser ) {
                $parser->setHook( 'templatestyles', 
'TemplateStylesHooks::handleTag' );
+               $parser->extTemplateStylesCache = new MapCacheLRU( 100 ); // 
100 is arbitrary
                return true;
        }
 
@@ -176,6 +177,14 @@
                        return false;
                }
                return true;
+       }
+
+       /**
+        * Clear our cache when the parser is reset
+        * @param Parser $parser
+        */
+       public static function onParserClearState( Parser $parser ) {
+               $parser->extTemplateStylesCache->clear();
        }
 
        /**
@@ -235,13 +244,32 @@
                                '</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 */';
 
@@ -270,7 +298,9 @@
 
                // 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;
        }
 
 }
diff --git a/extension.json b/extension.json
index 181d409..77f75f5 100644
--- a/extension.json
+++ b/extension.json
@@ -35,6 +35,9 @@
                "ParserFirstCallInit": [
                        "TemplateStylesHooks::onParserFirstCallInit"
                ],
+               "ParserClearState": [
+                       "TemplateStylesHooks::onParserClearState"
+               ],
                "ParserAfterTidy": [
                        "TemplateStylesHooks::onParserAfterTidy"
                ],

-- 
To view, visit https://gerrit.wikimedia.org/r/394369
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I83f7aab82cc7674037974b0de43ccae6c77ff39f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/TemplateStyles
Gerrit-Branch: master
Gerrit-Owner: Anomie <bjor...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to