Clean up handling of custom CSS and JavaScript pages in Article::view():
authorRob Church <robchurch@users.mediawiki.org>
Fri, 29 Jun 2007 04:31:19 +0000 (04:31 +0000)
committerRob Church <robchurch@users.mediawiki.org>
Fri, 29 Jun 2007 04:31:19 +0000 (04:31 +0000)
* Don't use parser cache, output is garbled
* Run 'ShowRawCssJs' hook

RELEASE-NOTES
docs/hooks.txt
includes/Article.php
includes/Title.php

index 8c82a8d..0cfd0ba 100644 (file)
@@ -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 ==
 
index 6f188e9..3716629 100644 (file)
@@ -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
index 2877270..d2db32b 100644 (file)
@@ -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(
-                                       "<pre class=\"mw-code mw-{$matches[1]}\" dir=\"ltr\">"
-                                       .htmlspecialchars($this->mContent)."\n</pre>"
-                               );
-                       } 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 <pre> and don't parse
+                                       preg_match( '!\.(css|js)$!u', $this->mTitle->getText(), $m );
+                                       $wgOut->addHtml( "<pre class=\"mw-code mw-{$m[1]}\" dir=\"ltr\">\n" );
+                                       $wgOut->addHtml( htmlspecialchars( $text ) );
+                                       $wgOut->addHtml( "\n</pre>\n" );
+                               } else {
+                                       // Wrap hook output in a <div> with the right direction attribute
+                                       $wgOut->addHtml( "<div dir=\"ltr\">\n{$text}\n</div>" );
+                               }
+                       
+                       }
+                       
+                       elseif ( $rt = Title::newFromRedirect( $text ) ) {
                                # Display redirect
                                $imageDir = $wgContLang->isRTL() ? 'rtl' : 'ltr';
                                $imageUrl = $wgStylePath.'/common/images/redirect' . $imageDir . '.png';
index eae188c..34b9412 100644 (file)
@@ -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?