From 0b5389d98d611d490a604baa798e9e98f5f1b395 Mon Sep 17 00:00:00 2001 From: Alexandre Emsenhuber Date: Tue, 22 Sep 2009 16:52:40 +0000 Subject: [PATCH] * (bug 20631) Preview of personal JavaScript and CSS pages now works again * introduced SkinTemplate::$useHeadElement as switch for backward compatibility for extension skins using the old way of generating the element: ** false (default): no change from previous version ** true: 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 | 25 +++++++------ includes/Skin.php | 6 ++-- includes/SkinTemplate.php | 76 +++++++++++++++++++++++++-------------- skins/Chick.php | 8 ++--- skins/Modern.php | 19 +++++----- skins/MonoBook.php | 33 +++++++---------- skins/MySkin.php | 2 +- skins/Simple.php | 2 +- skins/Vector.php | 4 +-- 9 files changed, 96 insertions(+), 79 deletions(-) diff --git a/includes/OutputPage.php b/includes/OutputPage.php index 204163acfb..a719c7c453 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -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; diff --git a/includes/Skin.php b/includes/Skin.php index 49da137513..ebdc78a0dc 100644 --- a/includes/Skin.php +++ b/includes/Skin.php @@ -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 = "/**/"; + $out->addInlineStyle( $wgRequest->getText( 'wpTextbox1' ) ); } else { $out->addStyle( self::makeUrl( $this->userpage . '/' . $this->getSkinName() .'.css', 'action=raw&ctype=text/css' ) ); diff --git a/includes/SkinTemplate.php b/includes/SkinTemplate.php index cbf9b14c39..4042514122 100644 --- a/includes/SkinTemplate.php +++ b/includes/SkinTemplate.php @@ -87,6 +87,12 @@ class SkinTemplate extends Skin { */ var $template = 'QuickTemplate'; + /** + * Whether this skin use OutputPage::headElement() to generate the + * 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 ) ) ) { diff --git a/skins/Chick.php b/skins/Chick.php index 35d83937c4..c8eb81527c 100644 --- a/skins/Chick.php +++ b/skins/Chick.php @@ -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 ); diff --git a/skins/Modern.php b/skins/Modern.php index add02419c4..87687adeb0 100644 --- a/skins/Modern.php +++ b/skins/Modern.php @@ -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 + ); } } @@ -59,13 +68,7 @@ class ModernTemplate extends QuickTemplate { // Suppress warnings to prevent notices about missing indexes in $this->data wfSuppressWarnings(); - $wgOut->addScript( << - -HTML - ); - - echo $wgOut->headElement( $this->skin ); + $this->html( 'headelement' ); ?>data['body_ondblclick']) { ?> ondblclick="text('body_ondblclick') ?>" data['body_onload' ]) { ?> onload="text('body_onload') ?>" class="mediawiki text('dir') ?> text('pageclass') ?> text('skinnameclass') ?>"> diff --git a/skins/MonoBook.php b/skins/MonoBook.php index b816e57612..9218295074 100644 --- a/skins/MonoBook.php +++ b/skins/MonoBook.php @@ -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 + ); } } @@ -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 - ); - - echo $wgOut->headElement( $this->skin ); - + $this->html( 'headelement' ); ?>data['body_ondblclick']) { ?> ondblclick="text('body_ondblclick') ?>" data['body_onload']) { ?> onload="text('body_onload') ?>" class="mediawiki text('dir'); $this->text('capitalizeallnouns') ?> text('pageclass') ?> text('skinnameclass') ?>"> diff --git a/skins/MySkin.php b/skins/MySkin.php index 4f5de8bf04..8859a71ad6 100644 --- a/skins/MySkin.php +++ b/skins/MySkin.php @@ -16,5 +16,5 @@ if( !defined( 'MEDIAWIKI' ) ) */ class SkinMySkin extends SkinTemplate { var $skinname = 'myskin', $stylename = 'myskin', - $template = 'MonoBookTemplate'; + $template = 'MonoBookTemplate', $useHeadElement = true; } diff --git a/skins/Simple.php b/skins/Simple.php index 13cc59f4c8..b636d5c964 100644 --- a/skins/Simple.php +++ b/skins/Simple.php @@ -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' ); diff --git a/skins/Vector.php b/skins/Vector.php index 4af108f440..f65d29ad3f 100644 --- a/skins/Vector.php +++ b/skins/Vector.php @@ -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' ); ?> data['body_ondblclick'] ): ?> ondblclick="text( 'body_ondblclick' ) ?>" data['body_onload'] ): ?> onload="text( 'body_onload' ) ?>" class="mediawiki text( 'dir' ) ?> text( 'pageclass' ) ?> text( 'skinnameclass' ) ?>" dir="text( 'dir' ) ?>">
-- 2.20.1