From d3e07758d4ea14fbc0410dd2878bc8652390f75c Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Tue, 3 Apr 2007 21:58:18 +0000 Subject: [PATCH] Generic method to add stylesheets and scripts to the header from parser extensions. Using a hook is inefficient since it requires loading the code on a parser cache hit. This feature saves the header text to the parser cache, and writes it to the output with no hook being called. Ugly getScript() hack to make it work with third-party skins. --- includes/OutputPage.php | 16 +++++++++++++++- includes/ParserOutput.php | 23 +++++++++++++++++++---- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/includes/OutputPage.php b/includes/OutputPage.php index c932f9cf91..009e89b9c0 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -47,6 +47,7 @@ class OutputPage { $this->mParserOptions = null; $this->mSquidMaxage = 0; $this->mScripts = ''; + $this->mHeadItems = array(); $this->mETag = false; $this->mRevisionId = null; $this->mNewSectionLink = false; @@ -80,7 +81,18 @@ class OutputPage { $this->mScripts .= ""; } - function getScript() { return $this->mScripts; } + function getScript() { + return $this->mScripts . $this->getHeadItems(); + } + + function getHeadItems() { + $s = ''; + foreach ( $this->mHeadItems as $item ) { + $s .= $item; + } + return $s; + } + function setETag($tag) { $this->mETag = $tag; } function setArticleBodyOnly($only) { $this->mArticleBodyOnly = $only; } @@ -355,6 +367,7 @@ class OutputPage { $this->mSubtitle .= $parserOutput->mSubtitle ; } $this->mNoGallery = $parserOutput->getNoGallery(); + $this->mHeadItems = array_merge( $this->mHeadItems, $parserOutput->mHeadItems ); wfRunHooks( 'OutputPageParserOutput', array( &$this, $parserOutput ) ); } @@ -1101,6 +1114,7 @@ class OutputPage { $ret .= $sk->getHeadScripts(); $ret .= $this->mScripts; $ret .= $sk->getUserStyles(); + $ret .= $this->getHeadItems(); if ($wgUseTrackbacks && $this->isArticleRelated()) $ret .= $wgTitle->trackbackRDF(); diff --git a/includes/ParserOutput.php b/includes/ParserOutput.php index 57a6ac547a..fa2c1b61e3 100644 --- a/includes/ParserOutput.php +++ b/includes/ParserOutput.php @@ -15,10 +15,11 @@ class ParserOutput $mTemplates, # 2-D map of NS/DBK to ID for the template references. ID=zero for broken. $mImages, # DB keys of the images used, in the array key only $mExternalLinks, # External link URLs, in the key only - $mHTMLtitle, # Display HTML title - $mSubtitle, # Additional subtitle - $mNewSection, # Show a new section link? - $mNoGallery; # No gallery on category page? (__NOGALLERY__) + $mHTMLtitle, # Display HTML title + $mSubtitle, # Additional subtitle + $mNewSection, # Show a new section link? + $mNoGallery, # No gallery on category page? (__NOGALLERY__) + $mHeadItems; # Items to put in the section function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(), $containsOldMagic = false, $titletext = '' ) @@ -38,6 +39,7 @@ class ParserOutput $this->mSubtitle = "" ; $this->mNewSection = false; $this->mNoGallery = false; + $this->mHeadItems = array(); } function getText() { return $this->mText; } @@ -112,6 +114,19 @@ class ParserOutput !isset( $this->mVersion ) || version_compare( $this->mVersion, Parser::VERSION, "lt" ); } + + /** + * Add some text to the . + * If $tag is set, the section with that tag will only be included once + * in a given page. + */ + function addHeadItem( $section, $tag = false ) { + if ( $tag !== false ) { + $this->mHeadItems[$tag] = $section; + } else { + $this->mHeadItems[] = $section; + } + } } ?> -- 2.20.1