* (bug 20631) Preview of personal JavaScript and CSS pages now works again
authorAlexandre Emsenhuber <ialex@users.mediawiki.org>
Tue, 22 Sep 2009 16:52:40 +0000 (16:52 +0000)
committerAlexandre Emsenhuber <ialex@users.mediawiki.org>
Tue, 22 Sep 2009 16:52:40 +0000 (16:52 +0000)
* introduced SkinTemplate::$useHeadElement as switch for backward compatibility for extension skins using the old way of generating the <head> element:
** false (default): no change from previous version
** true: <head> specific items set in SkinTemplate::outputPage() are no longer generated (avoid double execution of some functions) and the result of OutputPage::headElement() is stored in the 'headelement' item
* updated all core skin to use this new method, some extensions using MonoBookTemplate but not extending SkinMonoBook (or for other core skins) will need to set $useHeadElement to true to work properly though
* Made Skin::userCanPreview() public since it's needed in OutputPage::getHeadScripts()
* Pass the Skin object from OutputPage::headElement() to OutputPage::getHeadScripts() rather than getting it from $wgUser

includes/OutputPage.php
includes/Skin.php
includes/SkinTemplate.php
skins/Chick.php
skins/Modern.php
skins/MonoBook.php
skins/MySkin.php
skins/Simple.php
skins/Vector.php

index 204163a..a719c7c 100644 (file)
@@ -1724,7 +1724,7 @@ class OutputPage {
         *
         * @param $sk Skin The given Skin
         */
-       public function headElement( Skin $sk , $includeStyle = true ) {
+       public function headElement( Skin $sk, $includeStyle = true ) {
                global $wgDocType, $wgDTD, $wgContLanguageCode, $wgOutputEncoding, $wgMimeType;
                global $wgXhtmlDefaultNamespace, $wgXhtmlNamespaces;
                global $wgContLang, $wgUseTrackbacks, $wgStyleVersion, $wgEnableScriptLoader, $wgHtml5;
@@ -1764,7 +1764,7 @@ class OutputPage {
                $ret .= implode( "\n", array(
                        $this->getHeadLinks(),
                        $this->buildCssLinks(),
-                       $this->getHeadScripts(),
+                       $this->getHeadScripts( $sk ),
                        $this->getHeadItems(),
                ));
                if( $sk->usercss ){
@@ -1786,19 +1786,24 @@ class OutputPage {
         *
         * also adds userjs to the end if enabled:
        */
-       function getHeadScripts() {
-               global $wgUser, $wgJsMimeType;
-               $sk = $wgUser->getSkin();
+       function getHeadScripts( Skin $sk ) {
+               global $wgUser, $wgRequest, $wgJsMimeType;
 
                $vars = Skin::makeGlobalVariablesScript( $sk->getSkinName() );
 
                //add user js if enabled:
                if( $this->isUserJsAllowed() && $wgUser->isLoggedIn() ) {
-                       $userpage = $wgUser->getUserPage();
-                       $userjs = Skin::makeUrl(
-                               $userpage->getPrefixedText() . '/' . $sk->getSkinName() . '.js',
-                               'action=raw&ctype=' . $wgJsMimeType );
-                       $this->addScriptFile( $userjs );
+                       $action = $wgRequest->getVal( 'action', 'view' );
+                       if( $this->mTitle->isJsSubpage() and $sk->userCanPreview( $action ) ) {
+                               # XXX: additional security check/prompt?
+                               $this->addInlineScript( $wgRequest->getText( 'wpTextbox1' ) );
+                       } else {
+                               $userpage = $wgUser->getUserPage();
+                               $userjs = Skin::makeUrl(
+                                       $userpage->getPrefixedText() . '/' . $sk->getSkinName() . '.js',
+                                       'action=raw&ctype=' . $wgJsMimeType );
+                               $this->addScriptFile( $userjs );
+                       }
                }
 
                return $vars . "\n" . $this->mScripts;
index 49da137..ebdc78a 100644 (file)
@@ -463,9 +463,8 @@ class Skin extends Linker {
         *
         * @param string $action
         * @return bool
-        * @private
         */
-       function userCanPreview( $action ) {
+       public function userCanPreview( $action ) {
                global $wgRequest, $wgUser;
 
                if( $action != 'submit' )
@@ -637,9 +636,8 @@ END;
                        $action = $wgRequest->getVal( 'action' );
                        # If we're previewing the CSS page, use it
                        if( $this->mTitle->isCssSubpage() && $this->userCanPreview( $action ) ) {
-                               $previewCss = $wgRequest->getText( 'wpTextbox1' );
                                // @FIXME: properly escape the cdata!
-                               $this->usercss = "/*<![CDATA[*/\n" . $previewCss . "/*]]>*/";
+                               $out->addInlineStyle( $wgRequest->getText( 'wpTextbox1' ) );
                        } else {
                                $out->addStyle( self::makeUrl( $this->userpage . '/' . $this->getSkinName() .'.css',
                                        'action=raw&ctype=text/css' ) );
index cbf9b14..4042514 100644 (file)
@@ -87,6 +87,12 @@ class SkinTemplate extends Skin {
         */
        var $template = 'QuickTemplate';
 
+       /**
+        * Whether this skin use OutputPage::headElement() to generate the <head>
+        * tag
+        */
+       var $useHeadElement = false;
+
        /**#@-*/
 
        /**
@@ -171,12 +177,48 @@ class SkinTemplate extends Skin {
                        $this->userpageUrlDetails = self::makeKnownUrlDetails( $this->userpage );
                }
 
-               $this->userjs = $this->userjsprev = false;
-               $this->setupUserCss( $out );
-               $this->setupUserJs( $out->isUserJsAllowed() );
                $this->titletxt = $this->mTitle->getPrefixedText();
                wfProfileOut( __METHOD__ . '-stuff' );
 
+               wfProfileIn( __METHOD__ . '-stuff-head' );
+               if ( $this->useHeadElement ) {
+                       $pagecss = $this->setupPageCss();
+                       if( $pagecss )
+                               $out->addInlineStyle( $pagecss );
+               } else {
+                       $this->setupUserCss( $out );
+
+                       $tpl->set( 'pagecss', $this->setupPageCss() );
+                       $tpl->setRef( 'usercss', $this->usercss );
+
+                       $this->userjs = $this->userjsprev = false;
+                       $this->setupUserJs( $out->isUserJsAllowed() );
+                       $tpl->setRef( 'userjs', $this->userjs );
+                       $tpl->setRef( 'userjsprev', $this->userjsprev );
+
+                       if( $wgUseSiteJs ) {
+                               $jsCache = $this->loggedin ? '&smaxage=0' : '';
+                               $tpl->set( 'jsvarurl',
+                                                 self::makeUrl( '-',
+                                                                               "action=raw$jsCache&gen=js&useskin=" .
+                                                                               urlencode( $this->getSkinName() ) ) );
+                       } else {
+                               $tpl->set( 'jsvarurl', false );
+                       }
+
+                       $tpl->setRef( 'xhtmldefaultnamespace', $wgXhtmlDefaultNamespace );
+                       $tpl->set( 'xhtmlnamespaces', $wgXhtmlNamespaces );
+                       $tpl->set( 'headlinks', $out->getHeadLinks() );
+                       $tpl->set( 'csslinks', $out->buildCssLinks() );
+
+                       if( $wgUseTrackbacks && $out->isArticleRelated() ) {
+                               $tpl->set( 'trackbackhtml', $out->getTitle()->trackbackRDF() );
+                       } else {
+                               $tpl->set( 'trackbackhtml', null );
+                       }
+               }
+               wfProfileOut( __METHOD__ . '-stuff-head' );
+
                wfProfileIn( __METHOD__ . '-stuff2' );
                $tpl->set( 'title', $out->getPageTitle() );
                $tpl->set( 'pagetitle', $out->getHTMLTitle() );
@@ -224,19 +266,10 @@ class SkinTemplate extends Skin {
                } else {
                        $tpl->set( 'feeds', false );
                }
-               if( $wgUseTrackbacks && $out->isArticleRelated() ) {
-                       $tpl->set( 'trackbackhtml', $out->getTitle()->trackbackRDF() );
-               } else {
-                       $tpl->set( 'trackbackhtml', null );
-               }
 
-               $tpl->setRef( 'xhtmldefaultnamespace', $wgXhtmlDefaultNamespace );
-               $tpl->set( 'xhtmlnamespaces', $wgXhtmlNamespaces );
                $tpl->setRef( 'mimetype', $wgMimeType );
                $tpl->setRef( 'jsmimetype', $wgJsMimeType );
                $tpl->setRef( 'charset', $wgOutputEncoding );
-               $tpl->set( 'headlinks', $out->getHeadLinks() );
-               $tpl->set( 'csslinks', $out->buildCssLinks() );
                $tpl->setRef( 'wgScript', $wgScript );
                $tpl->setRef( 'skinname', $this->skinname );
                $tpl->set( 'skinclass', get_class( $this ) );
@@ -271,19 +304,6 @@ class SkinTemplate extends Skin {
                $tpl->setRef( 'userpageurl', $this->userpageUrlDetails['href'] );
                $tpl->set( 'userlang', $wgLang->getCode() );
                $tpl->set( 'userlangattributes', 'lang="' . $wgLang->getCode() . '" xml:lang="' . $wgLang->getCode() . '"' );
-               $tpl->set( 'pagecss', $this->setupPageCss() );
-               $tpl->setRef( 'usercss', $this->usercss );
-               $tpl->setRef( 'userjs', $this->userjs );
-               $tpl->setRef( 'userjsprev', $this->userjsprev );
-               if( $wgUseSiteJs ) {
-                       $jsCache = $this->loggedin ? '&smaxage=0' : '';
-                       $tpl->set( 'jsvarurl',
-                               self::makeUrl( '-',
-                                       "action=raw$jsCache&gen=js&useskin=" .
-                                               urlencode( $this->getSkinName() ) ) );
-               } else {
-                       $tpl->set( 'jsvarurl', false );
-               }
 
                $newtalks = $wgUser->getNewMessageLinks();
 
@@ -463,7 +483,11 @@ class SkinTemplate extends Skin {
                $tpl->set( 'nav_urls', $this->buildNavUrls() );
 
                // Set the head scripts near the end, in case the above actions resulted in added scripts
-               $tpl->set( 'headscripts', $out->getScript() );
+               if ( $this->useHeadElement ) {
+                       $tpl->set( 'headelement', $out->headElement( $this ) );
+               } else {
+                       $tpl->set( 'headscripts', $out->getScript() );
+               }
 
                // original version by hansm
                if( !wfRunHooks( 'SkinTemplateOutputPageBeforeExec', array( &$this, &$tpl ) ) ) {
index 35d8393..c8eb815 100644 (file)
@@ -18,12 +18,8 @@ require_once( dirname(__FILE__) . '/MonoBook.php' );
  * @ingroup Skins
  */
 class SkinChick extends SkinTemplate {
-       function initPage( OutputPage $out ) {
-               parent::initPage( $out );
-               $this->skinname  = 'chick';
-               $this->stylename = 'chick';
-               $this->template  = 'MonoBookTemplate';
-       }
+       var $skinname = 'chick', $stylename = 'chick',
+       $template = 'MonoBookTemplate', $useHeadElement = true;
 
        function setupSkinUserCss( OutputPage $out ){
                parent::setupSkinUserCss( $out );
index add0241..87687ad 100644 (file)
@@ -17,7 +17,7 @@ if( !defined( 'MEDIAWIKI' ) )
  */
 class SkinModern extends SkinTemplate {
        var $skinname = 'modern', $stylename = 'modern',
-               $template = 'ModernTemplate';
+               $template = 'ModernTemplate', $useHeadElement = true;
 
        /*
         * We don't like the default getPoweredBy, the icon clashes with the 
@@ -29,11 +29,20 @@ class SkinModern extends SkinTemplate {
        }
 
        function setupSkinUserCss( OutputPage $out ){
+               global $wgStyleVersion, $wgJsMimeType, $wgStylePath;
+
                // Do not call parent::setupSkinUserCss(), we have our own print style
                $out->addStyle( 'common/shared.css', 'screen' );
                $out->addStyle( 'modern/main.css', 'screen' );
                $out->addStyle( 'modern/print.css', 'print' );
                $out->addStyle( 'modern/rtl.css', 'screen', '', 'rtl' );
+
+               $path = htmlspecialchars( $wgStylePath );
+               $out->addScript( <<<HTML
+<!--[if lt IE 7]><script type="$wgJsMimeType" src="$path/common/IEFixes.js?$wgStyleVersion"></script>
+       <meta http-equiv="imagetoolbar" content="no" /><![endif]-->
+HTML
+               );
        }
 }
 
@@ -59,13 +68,7 @@ class ModernTemplate extends QuickTemplate {
                // Suppress warnings to prevent notices about missing indexes in $this->data
                wfSuppressWarnings();
 
-               $wgOut->addScript( <<<HTML
-<!--[if lt IE 7]><script type="$wgJsMimeType" src="$path/common/IEFixes.js?$wgStyleVersion"></script>
-       <meta http-equiv="imagetoolbar" content="no" /><![endif]-->
-HTML
-               );
-
-               echo $wgOut->headElement( $this->skin );
+               $this->html( 'headelement' );
 ?><body<?php if($this->data['body_ondblclick']) { ?> ondblclick="<?php $this->text('body_ondblclick') ?>"<?php } ?>
 <?php if($this->data['body_onload'    ]) { ?> onload="<?php     $this->text('body_onload')     ?>"<?php } ?>
  class="mediawiki <?php $this->text('dir') ?> <?php $this->text('pageclass') ?> <?php $this->text('skinnameclass') ?>">
index b816e57..9218295 100644 (file)
@@ -21,10 +21,10 @@ if( !defined( 'MEDIAWIKI' ) )
 class SkinMonoBook extends SkinTemplate {
        /** Using monobook. */
        var $skinname = 'monobook', $stylename = 'monobook',
-               $template = 'MonoBookTemplate';
+               $template = 'MonoBookTemplate', $useHeadElement = true;
 
        function setupSkinUserCss( OutputPage $out ) {
-               global $wgHandheldStyle;
+               global $wgHandheldStyle, $wgStyleVersion, $wgJsMimeType, $wgStylePath;
 
                parent::setupSkinUserCss( $out );
 
@@ -42,14 +42,13 @@ class SkinMonoBook extends SkinTemplate {
 
                $out->addStyle( 'monobook/rtl.css', 'screen', '', 'rtl' );
 
-
-               // @todo We can move this to the parent once we update all the skins
-               if( isset( $this->pagecss ) &&  $this->pagecss )
-                       $out->addInlineStyle( $this->pagecss );
-
-               if( isset( $this->usercss ) &&  $this->usercss )
-                       $out->addInlineStyle( $this->usercss );
-
+               # FIXME: What is this?  Should it apply to all skins?
+               $path = htmlspecialchars( $wgStylePath );
+               $out->addScript( <<<HTML
+<!--[if lt IE 7]><script type="$wgJsMimeType" src="$path/common/IEFixes.js?$wgStyleVersion"></script>
+       <meta http-equiv="imagetoolbar" content="no" /><![endif]-->
+HTML
+               );
        }
 }
 
@@ -68,23 +67,15 @@ class MonoBookTemplate extends QuickTemplate {
         * @access private
         */
        function execute() {
-               global $wgRequest, $wgOut, $wgStyleVersion, $wgJsMimeType, $wgStylePath;
+               global $wgRequest;
+
                $this->skin = $skin = $this->data['skin'];
                $action = $wgRequest->getText( 'action' );
 
                // Suppress warnings to prevent notices about missing indexes in $this->data
                wfSuppressWarnings();
 
-               # FIXME: What is this?  Should it apply to all skins?
-               $path = htmlspecialchars( $wgStylePath );
-               $wgOut->addScript( <<<HTML
-<!--[if lt IE 7]><script type="$wgJsMimeType" src="$path/common/IEFixes.js?$wgStyleVersion"></script>
-       <meta http-equiv="imagetoolbar" content="no" /><![endif]-->
-HTML
-               );
-
-               echo $wgOut->headElement( $this->skin );
-
+               $this->html( 'headelement' );
 ?><body<?php if($this->data['body_ondblclick']) { ?> ondblclick="<?php $this->text('body_ondblclick') ?>"<?php } ?>
 <?php if($this->data['body_onload']) { ?> onload="<?php $this->text('body_onload') ?>"<?php } ?>
  class="mediawiki <?php $this->text('dir'); $this->text('capitalizeallnouns') ?> <?php $this->text('pageclass') ?> <?php $this->text('skinnameclass') ?>">
index 4f5de8b..8859a71 100644 (file)
@@ -16,5 +16,5 @@ if( !defined( 'MEDIAWIKI' ) )
  */
 class SkinMySkin extends SkinTemplate {
        var $skinname = 'myskin', $stylename = 'myskin',
-               $template = 'MonoBookTemplate';
+               $template = 'MonoBookTemplate', $useHeadElement = true;
 }
index 13cc59f..b636d5c 100644 (file)
@@ -19,7 +19,7 @@ require_once( dirname(__FILE__) . '/MonoBook.php' );
  */
 class SkinSimple extends SkinTemplate {
        var $skinname = 'simple', $stylename = 'simple',
-               $template = 'MonoBookTemplate';
+               $template = 'MonoBookTemplate', $useHeadElement = true;
 
        function setupSkinUserCss( OutputPage $out ){
                $out->addStyle( 'simple/main.css', 'screen' );
index 4af108f..f65d29a 100644 (file)
@@ -19,7 +19,7 @@ class SkinVector extends SkinTemplate {
 
        /* Functions */
        var $skinname = 'vector', $stylename = 'vector',
-               $template = 'VectorTemplate';
+               $template = 'VectorTemplate', $useHeadElement = true;
 
        /**
         * Initializes output page and sets up skin-specific parameters
@@ -459,7 +459,7 @@ class VectorTemplate extends QuickTemplate {
                                array_reverse( $this->data['personal_urls'] );
                }
                // Output HTML Page
-               echo $wgOut->headElement( $this->skin );
+               $this->html( 'headelement' );
 ?>
        <body<?php if ( $this->data['body_ondblclick'] ): ?> ondblclick="<?php $this->text( 'body_ondblclick' ) ?>"<?php endif; ?> <?php if ( $this->data['body_onload'] ): ?> onload="<?php $this->text( 'body_onload' ) ?>"<?php endif; ?> class="mediawiki <?php $this->text( 'dir' ) ?> <?php $this->text( 'pageclass' ) ?> <?php $this->text( 'skinnameclass' ) ?>" dir="<?php $this->text( 'dir' ) ?>">
                <div id="page-base" class="noprint"></div>