From 51c3e790e1011c98c76a65ae18ba7888c924a49c Mon Sep 17 00:00:00 2001 From: Rob Church Date: Fri, 29 Jun 2007 04:31:19 +0000 Subject: [PATCH] Clean up handling of custom CSS and JavaScript pages in Article::view(): * Don't use parser cache, output is garbled * Run 'ShowRawCssJs' hook --- RELEASE-NOTES | 4 +++- docs/hooks.txt | 7 ++++++- includes/Article.php | 46 ++++++++++++++++++++++++++------------------ includes/Title.php | 11 +++++++++++ 4 files changed, 47 insertions(+), 21 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 8c82a8d146..0cfd0ba2c0 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -112,7 +112,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN * Introduce 'UserEffectiveGroups' hook; see docs/hooks.txt for more information * (bug 10387) Detect and handle '.php5' extension environments at install time Patch by Edward Z. Yang. - +* Introduce 'ShowRawCssJs' hook; see docs/hooks.txt for more information == Bugfixes since 1.10 == @@ -238,6 +238,8 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN Special:Confirmemail * Fix read permission check for unreadable page titles which are numerically equivalent to a whitelisted title +* Don't use garbled parser cache output when viewing custom CSS or JavaScript + pages == API changes since 1.10 == diff --git a/docs/hooks.txt b/docs/hooks.txt index 6f188e931a..371662934a 100644 --- a/docs/hooks.txt +++ b/docs/hooks.txt @@ -656,9 +656,14 @@ $content_actions: The array of content actions Can be used to set custom CSS/JS $out: OutputPage object +'ShowRawCssJs': When presenting raw CSS and JavaScript during page views +&$text: Text being shown +$title: Title of the custom script/stylesheet page +$output: Current OutputPage object + 'AjaxAddScript': Called in output page just before the initialisation of the javascript ajax engine. The hook is only called when ajax is enabled ( $wgUseAjax = true; ). More hooks might be available but undocumented, you can execute -./maintenance/findhooks.php to find hidden one. +./maintenance/findhooks.php to find hidden one. \ No newline at end of file diff --git a/includes/Article.php b/includes/Article.php index 28772706b6..d2db32b238 100644 --- a/includes/Article.php +++ b/includes/Article.php @@ -684,10 +684,12 @@ class Article { } # Should the parser cache be used? - $pcache = $wgEnableParserCache && - intval( $wgUser->getOption( 'stubthreshold' ) ) == 0 && - $this->exists() && - empty( $oldid ); + $pcache = $wgEnableParserCache + && intval( $wgUser->getOption( 'stubthreshold' ) ) == 0 + && $this->exists() + && empty( $oldid ) + && !$this->mTitle->isCssOrJsPage() + && !$this->mTitle->isCssJsSubpage(); wfDebug( 'Article::view using parser cache: ' . ($pcache ? 'yes' : 'no' ) . "\n" ); if ( $wgUser->getOption( 'stubthreshold' ) ) { wfIncrStats( 'pcache_miss_stub' ); @@ -777,21 +779,27 @@ class Article { } if( !$outputDone ) { $wgOut->setRevisionId( $this->getRevIdFetched() ); - // Wrap site/user css/js in pre and don't parse. User pages need - // to be subpages, site pages just need to end in ".css" or ".js". - - // @todo: use $this->mTitle->isCssJsSubpage() when php is fixed/ - // a workaround is found. - if ( - ($ns == NS_USER and preg_match('#/\w+\.(css|js)$#',$this->mTitle->getDBkey(),$matches)) - or ($ns == NS_MEDIAWIKI and preg_match('/.(css|js)$/', $this->mTitle->getDBkey(), $matches)) - ) { - $wgOut->addWikiText( wfMsg('clearyourcache')); - $wgOut->addHTML( - "
"
-					.htmlspecialchars($this->mContent)."\n
" - ); - } else if ( $rt = Title::newFromRedirect( $text ) ) { + + // Pages containing custom CSS or JavaScript get special treatment + if( $this->mTitle->isCssOrJsPage() || $this->mTitle->isCssJsSubpage() ) { + $wgOut->addHtml( wfMsgExt( 'clearyourcache', 'parse' ) ); + $text = $this->mContent; + + // Give hooks a chance to do formatting... + if( wfRunHooks( 'ShowRawCssJs', array( &$text, $this->mTitle, $wgOut ) ) ) { + // Wrap the whole lot in a
 and don't parse
+					preg_match( '!\.(css|js)$!u', $this->mTitle->getText(), $m );
+					$wgOut->addHtml( "
\n" );
+					$wgOut->addHtml( htmlspecialchars( $text ) );
+					$wgOut->addHtml( "\n
\n" ); + } else { + // Wrap hook output in a
with the right direction attribute + $wgOut->addHtml( "
\n{$text}\n
" ); + } + + } + + elseif ( $rt = Title::newFromRedirect( $text ) ) { # Display redirect $imageDir = $wgContLang->isRTL() ? 'rtl' : 'ltr'; $imageUrl = $wgStylePath.'/common/images/redirect' . $imageDir . '.png'; diff --git a/includes/Title.php b/includes/Title.php index eae188cfcb..34b94126ae 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -1214,6 +1214,17 @@ class Title { return false; } } + + /** + * Could this page contain custom CSS or JavaScript, based + * on the title? + * + * @return bool + */ + public function isCssOrJsPage() { + return $this->mNamespace == NS_MEDIAWIKI + && preg_match( '!\.(?:css|js)$!u', $this->mTextform ) > 0; + } /** * Is this a .css or .js subpage of a user page? -- 2.20.1