Remove redundant null-handling for Title::newFromText
[lhc/web/wiklou.git] / includes / EditPage.php
index 89b484c..2bddc3e 100644 (file)
@@ -537,6 +537,20 @@ class EditPage {
                        return;
                }
 
+               $revision = $this->mArticle->getRevisionFetched();
+               // Disallow editing revisions with content models different from the current one
+               if ( $revision && $revision->getContentModel() !== $this->contentModel ) {
+                       $this->displayViewSourcePage(
+                               $this->getContentObject(),
+                               wfMessage(
+                                       'contentmodelediterror',
+                                       $revision->getContentModel(),
+                                       $this->contentModel
+                               )->plain()
+                       );
+                       return;
+               }
+
                $this->isConflict = false;
                // css / js subpages of user pages get a special treatment
                $this->isCssJsSubpage = $this->mTitle->isCssJsSubpage();
@@ -647,6 +661,20 @@ class EditPage {
                        throw new PermissionsError( $action, $permErrors );
                }
 
+               $this->displayViewSourcePage(
+                       $content,
+                       $wgOut->formatPermissionsErrorMessage( $permErrors, 'edit' )
+               );
+       }
+
+       /**
+        * Display a read-only View Source page
+        * @param Content $content content object
+        * @param string $errorMessage additional wikitext error message to display
+        */
+       protected function displayViewSourcePage( Content $content, $errorMessage = '' ) {
+               global $wgOut;
+
                Hooks::run( 'EditPage::showReadOnlyForm:initial', array( $this, &$wgOut ) );
 
                $wgOut->setRobotPolicy( 'noindex,nofollow' );
@@ -658,8 +686,10 @@ class EditPage {
                $wgOut->addHTML( $this->editFormPageTop );
                $wgOut->addHTML( $this->editFormTextTop );
 
-               $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $permErrors, 'edit' ) );
-               $wgOut->addHTML( "<hr />\n" );
+               if ( $errorMessage !== '' ) {
+                       $wgOut->addWikiText( $errorMessage );
+                       $wgOut->addHTML( "<hr />\n" );
+               }
 
                # If the user made changes, preserve them when showing the markup
                # (This happens when a user is blocked during edit, for instance)
@@ -667,7 +697,13 @@ class EditPage {
                        $text = $this->textbox1;
                        $wgOut->addWikiMsg( 'viewyourtext' );
                } else {
-                       $text = $this->toEditText( $content );
+                       try {
+                               $text = $this->toEditText( $content );
+                       } catch ( MWException $e ) {
+                               # Serialize using the default format if the content model is not supported
+                               # (e.g. for an old revision with a different model)
+                               $text = $content->serialize();
+                       }
                        $wgOut->addWikiMsg( 'viewsourcetext' );
                }
 
@@ -980,7 +1016,7 @@ class EditPage {
                global $wgUser;
                $this->edittime = $this->mArticle->getTimestamp();
 
-               $content = $this->getContentObject( false ); #TODO: track content object?!
+               $content = $this->getContentObject( false ); # TODO: track content object?!
                if ( $content === false ) {
                        return false;
                }
@@ -1228,7 +1264,7 @@ class EditPage {
                $title = Title::newFromText( $preload );
                # Check for existence to avoid getting MediaWiki:Noarticletext
                if ( $title === null || !$title->exists() || !$title->userCan( 'read', $wgUser ) ) {
-                       //TODO: somehow show a warning to the user!
+                       // TODO: somehow show a warning to the user!
                        return $handler->makeEmptyContent();
                }
 
@@ -1237,7 +1273,7 @@ class EditPage {
                        $title = $page->getRedirectTarget();
                        # Same as before
                        if ( $title === null || !$title->exists() || !$title->userCan( 'read', $wgUser ) ) {
-                               //TODO: somehow show a warning to the user!
+                               // TODO: somehow show a warning to the user!
                                return $handler->makeEmptyContent();
                        }
                        $page = WikiPage::factory( $title );
@@ -1247,7 +1283,7 @@ class EditPage {
                $content = $page->getContent( Revision::RAW );
 
                if ( !$content ) {
-                       //TODO: somehow show a warning to the user!
+                       // TODO: somehow show a warning to the user!
                        return $handler->makeEmptyContent();
                }
 
@@ -1255,7 +1291,7 @@ class EditPage {
                        $converted = $content->convert( $handler->getModelID() );
 
                        if ( !$converted ) {
-                               //TODO: somehow show a warning to the user!
+                               // TODO: somehow show a warning to the user!
                                wfDebug( "Attempt to preload incompatible content: " .
                                        "can't convert " . $content->getModel() .
                                        " to " . $handler->getModelID() );
@@ -1312,7 +1348,7 @@ class EditPage {
                }
 
                $response = RequestContext::getMain()->getRequest()->response();
-               $response->setcookie( $postEditKey, $val, time() + self::POST_EDIT_COOKIE_DURATION, array(
+               $response->setCookie( $postEditKey, $val, time() + self::POST_EDIT_COOKIE_DURATION, array(
                        'httpOnly' => false,
                ) );
        }
@@ -1942,7 +1978,7 @@ class EditPage {
                        return $status;
                }
 
-               $flags = EDIT_DEFER_UPDATES | EDIT_AUTOSUMMARY |
+               $flags = EDIT_AUTOSUMMARY |
                        ( $new ? EDIT_NEW : EDIT_UPDATE ) |
                        ( ( $this->minoredit && !$this->isNew ) ? EDIT_MINOR : 0 ) |
                        ( $bot ? EDIT_FORCE_BOT : 0 );
@@ -2005,7 +2041,7 @@ class EditPage {
        }
 
        /**
-        * @param Title $title
+        * @param User $user
         * @param string $oldModel
         * @param string $newModel
         * @param string $reason
@@ -2023,26 +2059,26 @@ class EditPage {
                $log->publish( $logid );
        }
 
-
        /**
         * Register the change of watch status
         */
        protected function updateWatchlist() {
                global $wgUser;
 
-               if ( $wgUser->isLoggedIn()
-                       && $this->watchthis != $wgUser->isWatched( $this->mTitle, WatchedItem::IGNORE_USER_RIGHTS )
-               ) {
-                       $fname = __METHOD__;
-                       $title = $this->mTitle;
-                       $watch = $this->watchthis;
-
-                       // Do this in its own transaction to reduce contention...
-                       $dbw = wfGetDB( DB_MASTER );
-                       $dbw->onTransactionIdle( function () use ( $dbw, $title, $watch, $wgUser, $fname ) {
-                               WatchAction::doWatchOrUnwatch( $watch, $title, $wgUser );
-                       } );
+               if ( !$wgUser->isLoggedIn() ) {
+                       return;
                }
+
+               $user = $wgUser;
+               $title = $this->mTitle;
+               $watch = $this->watchthis;
+               // Do this in its own transaction to reduce contention...
+               DeferredUpdates::addCallableUpdate( function () use ( $user, $title, $watch ) {
+                       if ( $watch == $user->isWatched( $title, WatchedItem::IGNORE_USER_RIGHTS ) ) {
+                               return; // nothing to change
+                       }
+                       WatchAction::doWatchOrUnwatch( $watch, $title, $user );
+               } );
        }
 
        /**
@@ -2188,6 +2224,8 @@ class EditPage {
                }
 
                # Use the title defined by DISPLAYTITLE magic word when present
+               # NOTE: getDisplayTitle() returns HTML while getPrefixedText() returns plain text.
+               #       setPageTitle() treats the input as wikitext, which should be safe in either case.
                $displayTitle = isset( $this->mParserOutput ) ? $this->mParserOutput->getDisplayTitle() : false;
                if ( $displayTitle === false ) {
                        $displayTitle = $contextTitle->getPrefixedText();
@@ -2514,7 +2552,7 @@ class EditPage {
                # user preference is active, pass a hidden tag as wpIgnoreBlankSummary. This will stop the
                # user being bounced back more than once in the event that a summary
                # is not required.
-               #####
+               # ####
                # For a bit more sophisticated detection of blank summaries, hash the
                # automatic one and pass that in the hidden field wpAutoSummary.
                if ( $this->missingSummary || ( $this->section == 'new' && $this->nosummary ) ) {
@@ -2681,7 +2719,7 @@ class EditPage {
 
                        if ( $this->section != '' && $this->section != 'new' ) {
                                if ( !$this->summary && !$this->preview && !$this->diff ) {
-                                       $sectionTitle = self::extractSectionTitle( $this->textbox1 ); //FIXME: use Content object
+                                       $sectionTitle = self::extractSectionTitle( $this->textbox1 ); // FIXME: use Content object
                                        if ( $sectionTitle !== false ) {
                                                $this->summary = "/* $sectionTitle */ ";
                                        }