Merge "jquery.suggestions: Improve comment about avoiding click interference"
[lhc/web/wiklou.git] / includes / MovePage.php
index de7da3f..936b94a 100644 (file)
@@ -64,21 +64,9 @@ class MovePage {
                        $status->fatal( 'spamprotectiontext' );
                }
 
-               # The move is allowed only if (1) the target doesn't exist, or
-               # (2) the target is a redirect to the source, and has no history
-               # (so we can undo bad moves right after they're done).
-
-               if ( $this->newTitle->getArticleID() ) { # Target exists; check for validity
-                       if ( !$this->isValidMoveTarget() ) {
-                               $status->fatal( 'articleexists' );
-                       }
-               } else {
-                       $tp = $this->newTitle->getTitleProtection();
-                       if ( $tp !== false ) {
-                               if ( !$user->isAllowed( $tp['permission'] ) ) {
-                                       $status->fatal( 'cantmove-titleprotected' );
-                               }
-                       }
+               $tp = $this->newTitle->getTitleProtection();
+               if ( $tp !== false && !$user->isAllowed( $tp['permission'] ) ) {
+                               $status->fatal( 'cantmove-titleprotected' );
                }
 
                Hooks::run( 'MovePageCheckPermissions',
@@ -125,6 +113,13 @@ class MovePage {
                        $status->fatal( 'badarticleerror' );
                }
 
+               # The move is allowed only if (1) the target doesn't exist, or
+               # (2) the target is a redirect to the source, and has no history
+               # (so we can undo bad moves right after they're done).
+               if ( $this->newTitle->getArticleID() && !$this->isValidMoveTarget() ) {
+                       $status->fatal( 'articleexists' );
+               }
+
                // Content model checks
                if ( !$wgContentHandlerUseDB &&
                        $this->oldTitle->getContentModel() !== $this->newTitle->getContentModel() ) {
@@ -252,12 +247,12 @@ class MovePage {
                        RepoGroup::singleton()->clearCache( $this->newTitle ); # clear false negative cache
                }
 
-               $dbw->begin( __METHOD__ ); # If $file was a LocalFile, its transaction would have closed our own.
+               $dbw->startAtomic( __METHOD__ );
                $pageid = $this->oldTitle->getArticleID( Title::GAID_FOR_UPDATE );
                $protected = $this->oldTitle->isProtected();
 
                // Do the actual move
-               $this->moveToInternal( $user, $this->newTitle, $reason, $createRedirect );
+               $nullRevision = $this->moveToInternal( $user, $this->newTitle, $reason, $createRedirect );
 
                // Refresh the sortkey for this row.  Be careful to avoid resetting
                // cl_timestamp, which may disturb time-based lists on some sites.
@@ -310,8 +305,8 @@ class MovePage {
                                __METHOD__,
                                array( 'IGNORE' )
                        );
-                       # Update the protection log
-                       $log = new LogPage( 'protect' );
+
+                       // Build comment for log
                        $comment = wfMessage(
                                'prot_1movedto2',
                                $this->oldTitle->getPrefixedText(),
@@ -320,14 +315,6 @@ class MovePage {
                        if ( $reason ) {
                                $comment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $reason;
                        }
-                       // @todo FIXME: $params?
-                       $logId = $log->addEntry(
-                               'move_prot',
-                               $this->newTitle,
-                               $comment,
-                               array( $this->oldTitle->getPrefixedText() ),
-                               $user
-                       );
 
                        // reread inserted pr_ids for log relation
                        $insertedPrIds = $dbw->select(
@@ -340,7 +327,18 @@ class MovePage {
                        foreach ( $insertedPrIds as $prid ) {
                                $logRelationsValues[] = $prid->pr_id;
                        }
-                       $log->addRelations( 'pr_id', $logRelationsValues, $logId );
+
+                       // Update the protection log
+                       $logEntry = new ManualLogEntry( 'protect', 'move_prot' );
+                       $logEntry->setTarget( $this->newTitle );
+                       $logEntry->setComment( $comment );
+                       $logEntry->setPerformer( $user );
+                       $logEntry->setParameters( array(
+                               '4::oldtitle' => $this->oldTitle->getPrefixedText(),
+                       ) );
+                       $logEntry->setRelations( array( 'pr_id' => $logRelationsValues ) );
+                       $logId = $logEntry->insert();
+                       $logEntry->publish( $logId );
                }
 
                // Update *_from_namespace fields as needed
@@ -371,12 +369,28 @@ class MovePage {
                        WatchedItem::duplicateEntries( $this->oldTitle, $this->newTitle );
                }
 
-               $dbw->commit( __METHOD__ );
-
                Hooks::run(
-                       'TitleMoveComplete',
-                       array( &$this->oldTitle, &$this->newTitle, &$user, $pageid, $redirid, $reason )
+                       'TitleMoveCompleting',
+                       array( $this->oldTitle, $this->newTitle, $user, $pageid, $redirid, $reason )
+               );
+
+               $dbw->endAtomic( __METHOD__ );
+
+               $params = array(
+                       &$this->oldTitle,
+                       &$this->newTitle,
+                       &$user,
+                       $pageid,
+                       $redirid,
+                       $reason,
+                       $nullRevision
                );
+               $dbw->onTransactionIdle( function () use ( $params, $dbw ) {
+                       // Keep each single hook handler atomic
+                       $dbw->setFlag( DBO_TRX ); // flag is automatically reset by DB layer
+                       Hooks::run( 'TitleMoveComplete', $params );
+               } );
+
                return Status::newGood();
        }
 
@@ -390,6 +404,7 @@ class MovePage {
         * @param string $reason The reason for the move
         * @param bool $createRedirect Whether to leave a redirect at the old title. Does not check
         *   if the user has the suppressredirect right
+        * @return Revision the revision created by the move
         * @throws MWException
         */
        private function moveToInternal( User $user, &$nt, $reason = '', $createRedirect = true ) {
@@ -421,6 +436,13 @@ class MovePage {
                        $redirectContent = null;
                }
 
+               // Figure out whether the content model is no longer the default
+               $oldDefault = ContentHandler::getDefaultModelFor( $this->oldTitle );
+               $contentModel = $this->oldTitle->getContentModel();
+               $newDefault = ContentHandler::getDefaultModelFor( $nt );
+               $defaultContentModelChanging = ( $oldDefault !== $newDefault
+                       && $oldDefault === $contentModel );
+
                // bug 57084: log_page should be the ID of the *moved* page
                $oldid = $this->oldTitle->getArticleID();
                $logTitle = clone $this->oldTitle;
@@ -498,6 +520,16 @@ class MovePage {
                $newpage->doEditUpdates( $nullRevision, $user,
                        array( 'changed' => false, 'moved' => true, 'oldcountable' => $oldcountable ) );
 
+               // If the default content model changes, we need to populate rev_content_model
+               if ( $defaultContentModelChanging ) {
+                       $dbw->update(
+                               'revision',
+                               array( 'rev_content_model' => $contentModel ),
+                               array( 'rev_page' => $nt->getArticleID(), 'rev_content_model IS NULL' ),
+                               __METHOD__
+                       );
+               }
+
                if ( !$moveOverRedirect ) {
                        WikiPage::onArticleCreate( $nt );
                }
@@ -529,5 +561,7 @@ class MovePage {
                # Log the move
                $logid = $logEntry->insert();
                $logEntry->publish( $logid );
+
+               return $nullRevision;
        }
 }