(bug 37755) Set robot meta tags for 'view source' pages
[lhc/web/wiklou.git] / includes / EditPage.php
index 461997f..0e92754 100644 (file)
@@ -219,7 +219,7 @@ class EditPage {
        var $textbox1 = '', $textbox2 = '', $summary = '', $nosummary = false;
        var $edittime = '', $section = '', $sectiontitle = '', $starttime = '';
        var $oldid = 0, $editintro = '', $scrolltop = null, $bot = true;
-       var $content_model = null, $content_format = null;
+       var $contentModel = null, $contentFormat = null;
 
        # Placeholders for text injection by hooks (must be HTML)
        # extensions should take care to _append_ to the present value
@@ -253,10 +253,10 @@ class EditPage {
                $this->mArticle = $article;
                $this->mTitle = $article->getTitle();
 
-               $this->content_model = $this->mTitle->getContentModel();
+               $this->contentModel = $this->mTitle->getContentModel();
 
-               $handler = ContentHandler::getForModelID( $this->content_model );
-               $this->content_format = $handler->getDefaultFormat(); #NOTE: should be overridden by format of actual revision
+               $handler = ContentHandler::getForModelID( $this->contentModel );
+               $this->contentFormat = $handler->getDefaultFormat();
        }
 
        /**
@@ -285,7 +285,7 @@ class EditPage {
 
        /**
         * Get the context title object.
-        * If not set, $wgTitle will be returned. This behavior might changed in
+        * If not set, $wgTitle will be returned. This behavior might change in
         * the future to return $this->mTitle instead.
         *
         * @return Title object
@@ -381,7 +381,6 @@ class EditPage {
                $this->isCssSubpage         = $this->mTitle->isCssSubpage();
                $this->isJsSubpage          = $this->mTitle->isJsSubpage();
                $this->isWrongCaseCssJsPage = $this->isWrongCaseCssJsPage();
-               $this->isNew                = !$this->mTitle->exists() || $this->section == 'new';
 
                # Show applicable editing introductions
                if ( $this->formtype == 'initial' || $this->firsttime ) {
@@ -456,6 +455,7 @@ class EditPage {
         * @since 1.19
         * @param $permErrors Array of permissions errors, as returned by
         *                    Title::getUserPermissionsErrors().
+        * @throws PermissionsError
         */
        protected function displayPermissionsError( array $permErrors ) {
                global $wgRequest, $wgOut;
@@ -477,6 +477,7 @@ class EditPage {
                        throw new PermissionsError( $action, $permErrors );
                }
 
+               $wgOut->setRobotPolicy( 'noindex,nofollow' );
                $wgOut->setPageTitle( wfMessage( 'viewsource-title', $this->getContextTitle()->getPrefixedText() ) );
                $wgOut->addBacklinkSubtitle( $this->getContextTitle() );
                $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $permErrors, 'edit' ) );
@@ -587,12 +588,13 @@ class EditPage {
         * @param $request WebRequest
         */
        function importFormData( &$request ) {
-               global $wgLang, $wgUser;
+               global $wgContLang, $wgUser;
 
                wfProfileIn( __METHOD__ );
 
                # Section edit can come from either the form or a link
                $this->section = $request->getVal( 'wpSection', $request->getVal( 'section' ) );
+               $this->isNew = !$this->mTitle->exists() || $this->section == 'new';
 
                if ( $request->wasPosted() ) {
                        # These fields need to be checked for encoding.
@@ -611,7 +613,7 @@ class EditPage {
                        }
 
                        # Truncate for whole multibyte characters
-                       $this->summary = $wgLang->truncate( $request->getText( 'wpSummary' ), 255 );
+                       $this->summary = $wgContLang->truncate( $request->getText( 'wpSummary' ), 255 );
 
                        # If the summary consists of a heading, e.g. '==Foobar==', extract the title from the
                        # header syntax, e.g. 'Foobar'. This is mainly an issue when we are using wpSummary for
@@ -623,7 +625,7 @@ class EditPage {
                        # currently doing double duty as both edit summary and section title. Right now this
                        # is just to allow API edits to work around this limitation, but this should be
                        # incorporated into the actual edit form when EditPage is rewritten (Bugs 18654, 26312).
-                       $this->sectiontitle = $wgLang->truncate( $request->getText( 'wpSectionTitle' ), 255 );
+                       $this->sectiontitle = $wgContLang->truncate( $request->getText( 'wpSectionTitle' ), 255 );
                        $this->sectiontitle = preg_replace( '/^\s*=+\s*(.*?)\s*=+\s*$/', '$1', $this->sectiontitle );
 
                        $this->edittime = $request->getVal( 'wpEdittime' );
@@ -736,8 +738,8 @@ class EditPage {
                $this->nosummary = $request->getBool( 'nosummary' );
 
                $content_handler = ContentHandler::getForTitle( $this->mTitle );
-               $this->content_model = $request->getText( 'model', $content_handler->getModelID() ); #may be overridden by revision
-               $this->content_format = $request->getText( 'format', $content_handler->getDefaultFormat() ); #may be overridden by revision
+               $this->contentModel = $request->getText( 'model', $content_handler->getModelID() ); #may be overridden by revision
+               $this->contentFormat = $request->getText( 'format', $content_handler->getDefaultFormat() ); #may be overridden by revision
 
                #TODO: check if the desired model is allowed in this namespace, and if a transition from the page's current model to the new model is allowed
                #TODO: check if the desired content model supports the given content format!
@@ -805,11 +807,10 @@ class EditPage {
         * @param $def_text string
         * @return mixed string on success, $def_text for invalid sections
         * @private
-        * @deprecated since 1.21
-        * @todo: deprecated, replace usage everywhere
+        * @deprecated since 1.21, get WikiPage::getContent() instead.
         */
        function getContent( $def_text = false ) {
-               wfDeprecated( __METHOD__, '1.21' );
+               ContentHandler::deprecated( __METHOD__, '1.21' );
 
                if ( $def_text !== null && $def_text !== false && $def_text !== '' ) {
                        $def_content = $this->toEditContent( $def_text );
@@ -823,7 +824,14 @@ class EditPage {
                return $this->toEditText( $content );
        }
 
-       private function getContentObject( $def_content = null ) {
+       /**
+        * @param Content|false $def_content The default value to return
+        *
+        * @return mixed Content on success, $def_content for invalid sections
+        *
+        * @since 1.21
+        */
+       protected function getContentObject( $def_content = null ) {
                global $wgOut, $wgRequest;
 
                wfProfileIn( __METHOD__ );
@@ -942,8 +950,8 @@ class EditPage {
                }
                $revision = $this->mArticle->getRevisionFetched();
                if ( $revision === null ) {
-                       if ( !$this->content_model ) $this->content_model = $this->getTitle()->getContentModel();
-                       $handler = ContentHandler::getForModelID( $this->content_model );
+                       if ( !$this->contentModel ) $this->contentModel = $this->getTitle()->getContentModel();
+                       $handler = ContentHandler::getForModelID( $this->contentModel );
 
                        return $handler->makeEmptyContent();
                }
@@ -957,21 +965,21 @@ class EditPage {
         * content object is returned instead of null.
         *
         * @since 1.21
-        * @return string
+        * @return Content
         */
-       private function getCurrentContent() {
+       protected function getCurrentContent() {
                $rev = $this->mArticle->getRevision();
                $content = $rev ? $rev->getContent( Revision::RAW ) : null;
 
                if ( $content  === false || $content === null ) {
-                       if ( !$this->content_model ) $this->content_model = $this->getTitle()->getContentModel();
-                       $handler = ContentHandler::getForModelID( $this->content_model );
+                       if ( !$this->contentModel ) $this->contentModel = $this->getTitle()->getContentModel();
+                       $handler = ContentHandler::getForModelID( $this->contentModel );
 
                        return $handler->makeEmptyContent();
                } else {
                        # nasty side-effect, but needed for consistency
-                       $this->content_model = $rev->getContentModel();
-                       $this->content_format = $rev->getContentFormat();
+                       $this->contentModel = $rev->getContentModel();
+                       $this->contentFormat = $rev->getContentFormat();
 
                        return $content;
                }
@@ -982,10 +990,10 @@ class EditPage {
         * Use this method before edit() to preload some text into the edit box
         *
         * @param $text string
-        * @deprecated since 1.21
+        * @deprecated since 1.21, use setPreloadedContent() instead.
         */
        public function setPreloadedText( $text ) {
-               wfDeprecated( __METHOD__, "1.21" );
+               ContentHandler::deprecated( __METHOD__, "1.21" );
 
                $content = $this->toEditContent( $text );
 
@@ -1000,7 +1008,7 @@ class EditPage {
         * @since 1.21
         */
        public function setPreloadedContent( Content $content ) {
-               $this->mPreloadedContent = $content;
+               $this->mPreloadContent = $content;
        }
 
        /**
@@ -1013,8 +1021,8 @@ class EditPage {
         *
         * @deprecated since 1.21, use getPreloadedContent() instead
         */
-       protected function getPreloadedText( $preload ) { #NOTE: B/C only, replace usage!
-               wfDeprecated( __METHOD__, "1.21" );
+       protected function getPreloadedText( $preload ) {
+               ContentHandler::deprecated( __METHOD__, "1.21" );
 
                $content = $this->getPreloadedContent( $preload );
                $text = $this->toEditText( $content );
@@ -1032,7 +1040,7 @@ class EditPage {
         *
         * @since 1.21
         */
-       protected function getPreloadedContent( $preload ) { #@todo: use this!
+       protected function getPreloadedContent( $preload ) {
                global $wgUser;
 
                if ( !empty( $this->mPreloadContent ) ) {
@@ -1084,6 +1092,7 @@ class EditPage {
 
        /**
         * Attempt submission
+        * @throws UserBlockedError|ReadOnlyError|ThrottledError|PermissionsError
         * @return bool false if output is done, true if the rest of the form should be displayed
         */
        function attemptSave() {
@@ -1212,7 +1221,7 @@ class EditPage {
                        # Construct Content object
                        $textbox_content = $this->toEditContent( $this->textbox1 );
                } catch (MWContentSerializationException $ex) {
-                       $status->fatal( 'content-failed-to-parse', $this->content_model, $this->content_format, $ex->getMessage() );
+                       $status->fatal( 'content-failed-to-parse', $this->contentModel, $this->contentFormat, $ex->getMessage() );
                        $status->value = self::AS_PARSE_ERROR;
                        wfProfileOut( __METHOD__ );
                        return $status;
@@ -1420,7 +1429,8 @@ class EditPage {
                                                $this->isConflict = false;
                                                wfDebug( __METHOD__ . ": conflict suppressed; new section\n" );
                                        }
-                               } elseif ( $this->section == '' && Revision::userWasLastToEdit( DB_MASTER,  $this->mTitle->getArticleID(), $wgUser->getId(), $this->edittime ) ) {
+                               } elseif ( $this->section == '' && Revision::userWasLastToEdit( DB_MASTER,  $this->mTitle->getArticleID(),
+                                                       $wgUser->getId(), $this->edittime ) ) {
                                        # Suppress edit conflict with self, except for section edits where merging is required.
                                        wfDebug( __METHOD__ . ": Suppressing edit conflict, same user.\n" );
                                        $this->isConflict = false;
@@ -1441,8 +1451,7 @@ class EditPage {
                                wfDebug( __METHOD__ . ": conflict! getting section '{$this->section}' for time '{$this->edittime}'"
                                                . " (article time '{$timestamp}')\n" );
 
-                               $content = $this->mArticle->replaceSectionContent( $this->section, $textbox_content,
-                                                                                                                                       $sectionTitle, $this->edittime );
+                               $content = $this->mArticle->replaceSectionContent( $this->section, $textbox_content, $sectionTitle, $this->edittime );
                        } else {
                                wfDebug( __METHOD__ . ": getting section '{$this->section}'\n" );
                                $content = $this->mArticle->replaceSectionContent( $this->section, $textbox_content, $sectionTitle );
@@ -1584,7 +1593,7 @@ class EditPage {
                        ( $bot ? EDIT_FORCE_BOT : 0 );
 
                        $doEditStatus = $this->mArticle->doEditContent( $content, $this->summary, $flags,
-                                                                                                                       false, null, $this->content_format );
+                                                                                                                       false, null, $this->contentFormat );
 
                if ( $doEditStatus->isOK() ) {
                                $result['redirect'] = $content->isRedirect();
@@ -1635,7 +1644,7 @@ class EditPage {
         * @deprecated since 1.21, use mergeChangesIntoContent() instead
         */
        function mergeChangesInto( &$editText ){
-               wfDebug( __METHOD__, "1.21" );
+               ContentHandler::deprecated( __METHOD__, "1.21" );
 
                $editContent = $this->toEditContent( $editText );
 
@@ -1656,7 +1665,7 @@ class EditPage {
         * @parma $editText string
         *
         * @return bool
-        * @since since 1.21
+        * @since since 1.WD
         */
        private function mergeChangesIntoContent( &$editContent ){
                wfProfileIn( __METHOD__ );
@@ -1903,7 +1912,7 @@ class EditPage {
                                                                . ContentHandler::getLocalizedName( $content->getModel() ) );
                }
 
-               return $content->serialize( $this->content_format );
+               return $content->serialize( $this->contentFormat );
        }
 
        /**
@@ -1913,15 +1922,20 @@ class EditPage {
         * an exception will be raised. Set $this->allowNonTextContent to true to allow editing of non-textual
         * content.
         *
-        * @param String $text Text to unserialize
-        * @return Content the content object created from $text
+        * @param String|null|false $text Text to unserialize
+        * @return Content The content object created from $text. If $text was false or null, false resp. null will be
+        *                 returned instead.
         *
         * @throws MWException if unserializing the text results in a Content object that is not an instance of TextContent
         *          and $this->allowNonTextContent is not true.
         */
        protected function toEditContent( $text ) {
+               if ( $text === false || $text === null ) {
+                       return $text;
+               }
+
                $content = ContentHandler::makeContent( $text, $this->getTitle(),
-                       $this->content_model, $this->content_format );
+                       $this->contentModel, $this->contentFormat );
 
                if ( !$this->allowNonTextContent && !( $content instanceof TextContent ) ) {
                        throw new MWException( "This content model can not be edited as text: "
@@ -2045,8 +2059,8 @@ class EditPage {
 
                $wgOut->addHTML( Html::hidden( 'oldid', $this->oldid ) );
 
-               $wgOut->addHTML( Html::hidden( 'format', $this->content_format ) );
-               $wgOut->addHTML( Html::hidden( 'model', $this->content_model ) );
+               $wgOut->addHTML( Html::hidden( 'format', $this->contentFormat ) );
+               $wgOut->addHTML( Html::hidden( 'model', $this->contentModel ) );
 
                if ( $this->section == 'new' ) {
                        $this->showSummaryInput( true, $this->summary );
@@ -2097,7 +2111,7 @@ class EditPage {
                                $this->showConflict();
                        } catch ( MWContentSerializationException $ex ) {
                                // this can't really happen, but be nice if it does.
-                               $msg = wfMessage( 'content-failed-to-parse', $this->content_model, $this->content_format, $ex->getMessage() );
+                               $msg = wfMessage( 'content-failed-to-parse', $this->contentModel, $this->contentFormat, $ex->getMessage() );
                                $wgOut->addWikiText( '<div class="error">' . $msg->text() . '</div>');
                        }
                }
@@ -2530,7 +2544,7 @@ HTML
                        try {
                                $this->showDiff();
                        } catch ( MWContentSerializationException $ex ) {
-                               $msg = wfMessage( 'content-failed-to-parse', $this->content_model, $this->content_format, $ex->getMessage() );
+                               $msg = wfMessage( 'content-failed-to-parse', $this->contentModel, $this->contentFormat, $ex->getMessage() );
                                $wgOut->addWikiText( '<div class="error">' . $msg->text() . '</div>');
                        }
                }
@@ -2583,8 +2597,8 @@ HTML
                $textboxContent = $this->toEditContent( $this->textbox1 );
 
                $newContent = $this->mArticle->replaceSectionContent(
-                                                                                       $this->section, $textboxContent,
-                                                                                       $this->summary, $this->edittime );
+                                                       $this->section, $textboxContent,
+                                                       $this->summary, $this->edittime );
 
                ContentHandler::runLegacyHooks( 'EditPageGetDiffText', array( $this, &$newContent ) );
                wfRunHooks( 'EditPageGetDiffContent', array( $this, &$newContent ) );
@@ -2718,10 +2732,10 @@ HTML
                        $content1 = $this->toEditContent( $this->textbox1 );
                        $content2 = $this->toEditContent( $this->textbox2 );
 
-                       $handler = ContentHandler::getForModelID( $this->content_model );
+                       $handler = ContentHandler::getForModelID( $this->contentModel );
                        $de = $handler->createDifferenceEngine( $this->mArticle->getContext() );
                        $de->setContent( $content2, $content1 );
-                       $de->showDiff( 
+                       $de->showDiff(
                                wfMessage( 'yourtext' )->parse(),
                                wfMessage( 'storedversion' )->text()
                        );
@@ -2821,6 +2835,7 @@ HTML
 
        /**
         * Get the rendered text for previewing.
+        * @throws MWException
         * @return string
         */
        function getPreviewText() {
@@ -2864,7 +2879,6 @@ HTML
 
                        $parserOptions = $this->mArticle->makeParserOptions( $this->mArticle->getContext() );
                        $parserOptions->setEditSection( false );
-                       $parserOptions->setTidy( true );
                        $parserOptions->setIsPreview( true );
                        $parserOptions->setIsSectionPreview( !is_null($this->section) && $this->section !== '' );
 
@@ -2904,7 +2918,7 @@ HTML
 
                                # If we're adding a comment, we need to show the
                                # summary as the headline
-                               if ( $this->section == "new" && $this->summary != "" ) {
+                               if ( $this->section === "new" && $this->summary !== "" ) {
                                        $content = $content->addSectionHeader( $this->summary );
                                }
 
@@ -2928,8 +2942,8 @@ HTML
                                        $note .= "\n\n" . implode( "\n\n", $parserOutput->getWarnings() );
                                }
                        }
-               } catch (MWContentSerializationException $ex) {
-                       $m = wfMessage('content-failed-to-parse', $this->content_model, $this->content_format, $ex->getMessage() );
+               } catch ( MWContentSerializationException $ex ) {
+                       $m = wfMessage('content-failed-to-parse', $this->contentModel, $this->contentFormat, $ex->getMessage() );
                        $note .= "\n\n" . $m->parse();
                        $previewHTML = '';
                }