From 39bbe4cdf2086195cb5c267c17f3893840e043e8 Mon Sep 17 00:00:00 2001 From: Daniel Kinzler Date: Fri, 30 Mar 2012 12:46:42 +0000 Subject: [PATCH] clean up handling of JS/CSS pages --- includes/Article.php | 13 +-- includes/EditPage.php | 188 +++++++++++++++++++++--------------------- 2 files changed, 100 insertions(+), 101 deletions(-) diff --git a/includes/Article.php b/includes/Article.php index 5470dbcac7..037d66d275 100644 --- a/includes/Article.php +++ b/includes/Article.php @@ -605,7 +605,6 @@ class Article extends Page { # Pages containing custom CSS or JavaScript get special treatment if ( $this->getTitle()->isCssOrJsPage() || $this->getTitle()->isCssJsSubpage() ) { - #FIXME: use ContentHandler for specialized actions insetad. wfDebug( __METHOD__ . ": showing CSS/JS source\n" ); $this->showCssOrJsPage(); $outputDone = true; @@ -748,14 +747,16 @@ class Article extends Page { * This is hooked by SyntaxHighlight_GeSHi to do syntax highlighting of these * page views. */ - protected function showCssOrJsPage() { + protected function showCssOrJsPage( $showCacheHint = true ) { global $wgOut; - $dir = $this->getContext()->getLanguage()->getDir(); - $lang = $this->getContext()->getLanguage()->getCode(); + if ( $showCacheHint ) { + $dir = $this->getContext()->getLanguage()->getDir(); + $lang = $this->getContext()->getLanguage()->getCode(); - $wgOut->wrapWikiMsg( "
\n$1\n
", - 'clearyourcache' ); + $wgOut->wrapWikiMsg( "
\n$1\n
", + 'clearyourcache' ); + } // Give hooks a chance to customise the output if ( !Hooks::isRegistered('ShowRawCssJs') || wfRunHooks( 'ShowRawCssJs', array( $this->fetchContent(), $this->getTitle(), $wgOut ) ) ) { #FIXME: fetchContent() is deprecated #FIXME: hook is deprecated diff --git a/includes/EditPage.php b/includes/EditPage.php index e7ac95d26b..3bdab8f37e 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -1155,7 +1155,7 @@ class EditPage { # Check image redirect if ( $this->mTitle->getNamespace() == NS_FILE && - Title::newFromRedirect( $this->textbox1 ) instanceof Title && + Title::newFromRedirect( $this->textbox1 ) instanceof Title && #FIXME: use content handler to check for redirect !$wgUser->isAllowed( 'upload' ) ) { $code = $wgUser->isAnon() ? self::AS_IMAGE_REDIRECT_ANON : self::AS_IMAGE_REDIRECT_LOGGED; $status->setResult( false, $code ); @@ -1304,8 +1304,10 @@ class EditPage { return $status; } + $content = ContentHandler::makeContent( $this->textbox1, $this->getTitle(), $this->content_model, $this->content_format ); + # Handle the user preference to force summaries here. Check if it's not a redirect. - if ( !$this->allowBlankSummary && !Title::newFromRedirect( $this->textbox1 ) ) { + if ( !$this->allowBlankSummary && !$content->isRedirect() ) { if ( md5( $this->summary ) == $this->autoSumm ) { $this->missingSummary = true; $status->fatal( 'missingsummary' ); // or 'missingcommentheader' if $section == 'new'. Blegh @@ -1315,8 +1317,6 @@ class EditPage { } } - $content = ContentHandler::makeContent( $this->textbox1, $this->getTitle(), $this->content_model, $this->content_format ); - $result['sectionanchor'] = ''; if ( $this->section == 'new' ) { if ( $this->sectiontitle !== '' ) { @@ -2690,97 +2690,95 @@ HTML return $parsedNote; } - if ( $this->mTriedSave && !$this->mTokenOk ) { - if ( $this->mTokenOkExceptSuffix ) { - $note = wfMsg( 'token_suffix_mismatch' ); - } else { - $note = wfMsg( 'session_fail_preview' ); - } - } elseif ( $this->incompleteForm ) { - $note = wfMsg( 'edit_form_incomplete' ); - } else { - $note = wfMsg( 'previewnote' ); - } - - $parserOptions = ParserOptions::newFromUser( $wgUser ); - $parserOptions->setEditSection( false ); - $parserOptions->setTidy( true ); - $parserOptions->setIsPreview( true ); - $parserOptions->setIsSectionPreview( !is_null($this->section) && $this->section !== '' ); - - # don't parse non-wikitext pages, show message about preview - # XXX: stupid php bug won't let us use $this->getContextTitle()->isCssJsSubpage() here -- This note has been there since r3530. Sure the bug was fixed time ago? - - if ( $this->isCssJsSubpage || $this->mTitle->isCssOrJsPage() ) { #TODO: kill all special case handling for CSS/JS content! - if( $this->mTitle->isCssJsSubpage() ) { - $level = 'user'; - } elseif( $this->mTitle->isCssOrJsPage() ) { - $level = 'site'; - } else { - $level = false; - } - - # Used messages to make sure grep find them: - # Messages: usercsspreview, userjspreview, sitecsspreview, sitejspreview - if( $level ) { - #FIXME: move this crud into ContentHandler class! - if (preg_match( "/\\.css$/", $this->mTitle->getText() ) ) { - $previewtext = "
\n" . wfMsg( "{$level}csspreview" ) . "\n
"; - $class = "mw-code mw-css"; - } elseif (preg_match( "/\\.js$/", $this->mTitle->getText() ) ) { - $previewtext = "
\n" . wfMsg( "{$level}jspreview" ) . "\n
"; - $class = "mw-code mw-js"; - } else { - throw new MWException( 'A CSS/JS (sub)page but which is not css nor js!' ); - } - } #FIXME: else $previewtext is undefined! - - $parserOutput = $wgParser->parse( $previewtext, $this->mTitle, $parserOptions ); - $previewHTML = $parserOutput->mText; - $previewHTML .= "
\n" . htmlspecialchars( $this->textbox1 ) . "\n
\n"; #FIXME: use content object! - } else { - $rt = Title::newFromRedirectArray( $this->textbox1 ); - if ( $rt ) { - $previewHTML = $this->mArticle->viewRedirect( $rt, false ); - } else { - try { - $content = ContentHandler::makeContent( $this->textbox1, $this->getTitle(), $this->content_model, $this->content_format ); - - # If we're adding a comment, we need to show the - # summary as the headline - if ( $this->section == "new" && $this->summary != "" ) { - $content = $content->addSectionHeader( $this->summary ); - } - - $toparse_orig = $content->serialize( $this->content_format ); - $toparse = $toparse_orig; - wfRunHooks( 'EditPageGetPreviewText', array( $this, &$toparse ) ); - - if ( $toparse !== $toparse_orig ) { - #hook changed the text, create new Content object - $content = ContentHandler::makeContent( $toparse, $this->getTitle(), $this->content_model, $this->content_format ); - } - - wfRunHooks( 'EditPageGetPreviewContent', array( $this, &$content ) ); # FIXME: document new hook - - $parserOptions->enableLimitReport(); - - $content = $content->preSaveTransform( $this->mTitle, $wgUser, $parserOptions ); - $parserOutput = $content->getParserOutput( $this->mTitle, null, $parserOptions ); - - $previewHTML = $parserOutput->getText(); - $this->mParserOutput = $parserOutput; - $wgOut->addParserOutputNoText( $parserOutput ); - - if ( count( $parserOutput->getWarnings() ) ) { - $note .= "\n\n" . implode( "\n\n", $parserOutput->getWarnings() ); - } - } catch (MWContentSerializationException $ex) { - $note .= "\n\n" . wfMsg('content-failed-to-parse', $this->content_model, $this->content_format, $ex->getMessage() ); - $previewHTML = ''; - } - } - } + try { + $content = ContentHandler::makeContent( $this->textbox1, $this->getTitle(), $this->content_model, $this->content_format ); + + if ( $this->mTriedSave && !$this->mTokenOk ) { + if ( $this->mTokenOkExceptSuffix ) { + $note = wfMsg( 'token_suffix_mismatch' ); + } else { + $note = wfMsg( 'session_fail_preview' ); + } + } elseif ( $this->incompleteForm ) { + $note = wfMsg( 'edit_form_incomplete' ); + } elseif ( $this->isCssJsSubpage || $this->mTitle->isCssOrJsPage() ) { + # if this is a CSS or JS page used in the UI, show a special notice + # XXX: stupid php bug won't let us use $this->getContextTitle()->isCssJsSubpage() here -- This note has been there since r3530. Sure the bug was fixed time ago? + + if( $this->mTitle->isCssJsSubpage() ) { + $level = 'user'; + } elseif( $this->mTitle->isCssOrJsPage() ) { + $level = 'site'; + } else { + $level = false; + } + + if ( $content->getModelName() == CONTENT_MODEL_CSS ) { + $format = 'css'; + } elseif ( $content->getModelName() == CONTENT_MODEL_JAVASCRIPT ) { + $format = 'js'; + } else { + $format = false; + } + + # Used messages to make sure grep find them: + # Messages: usercsspreview, userjspreview, sitecsspreview, sitejspreview + if( $level && $format ) { + $note = "
" . wfMsg( "{$level}{$format}preview" ) . "
"; + } else { + $note = wfMsg( 'previewnote' ); + } + } else { + $note = wfMsg( 'previewnote' ); + } + + $parserOptions = ParserOptions::newFromUser( $wgUser ); + $parserOptions->setEditSection( false ); + $parserOptions->setTidy( true ); + $parserOptions->setIsPreview( true ); + $parserOptions->setIsSectionPreview( !is_null($this->section) && $this->section !== '' ); + + $rt = $content->getRedirectChain(); + + if ( $rt ) { + $previewHTML = $this->mArticle->viewRedirect( $rt, false ); + } else { + + # If we're adding a comment, we need to show the + # summary as the headline + if ( $this->section == "new" && $this->summary != "" ) { + $content = $content->addSectionHeader( $this->summary ); + } + + $toparse_orig = $content->serialize( $this->content_format ); + $toparse = $toparse_orig; + wfRunHooks( 'EditPageGetPreviewText', array( $this, &$toparse ) ); + + if ( $toparse !== $toparse_orig ) { + #hook changed the text, create new Content object + $content = ContentHandler::makeContent( $toparse, $this->getTitle(), $this->content_model, $this->content_format ); + } + + wfRunHooks( 'EditPageGetPreviewContent', array( $this, &$content ) ); # FIXME: document new hook + + $parserOptions->enableLimitReport(); + + #XXX: For CSS/JS pages, we should have called the ShowRawCssJs hook here. But it's now deprecated, so never mind + $content = $content->preSaveTransform( $this->mTitle, $wgUser, $parserOptions ); + $parserOutput = $content->getParserOutput( $this->mTitle, null, $parserOptions ); + + $previewHTML = $parserOutput->getText(); + $this->mParserOutput = $parserOutput; + $wgOut->addParserOutputNoText( $parserOutput ); + + if ( count( $parserOutput->getWarnings() ) ) { + $note .= "\n\n" . implode( "\n\n", $parserOutput->getWarnings() ); + } + } + } catch (MWContentSerializationException $ex) { + $note .= "\n\n" . wfMsg('content-failed-to-parse', $this->content_model, $this->content_format, $ex->getMessage() ); + $previewHTML = ''; + } if( $this->isConflict ) { $conflict = '

' . htmlspecialchars( wfMsg( 'previewconflict' ) ) . "

\n"; -- 2.20.1