Generic method to add stylesheets and scripts to the header from parser extensions...
authorTim Starling <tstarling@users.mediawiki.org>
Tue, 3 Apr 2007 21:58:18 +0000 (21:58 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Tue, 3 Apr 2007 21:58:18 +0000 (21:58 +0000)
includes/OutputPage.php
includes/ParserOutput.php

index c932f9c..009e89b 100644 (file)
@@ -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 .= "<script type=\"$wgJsMimeType\">/*<![CDATA[*/\n$script\n/*]]>*/</script>";
        }
 
-       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();
index 57a6ac5..fa2c1b6 100644 (file)
@@ -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 <head> 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 <head>. 
+        * 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;
+               }
+       }
 }
 
 ?>