Example #1
0
 /**
  * Move page to a title which is either a redirect to the
  * source page or nonexistent
  *
  * @param $nt Title the page to move to, which should be a redirect or nonexistent
  * @param $reason String The reason for the move
  * @param $createRedirect Bool Whether to leave a redirect at the old title.  Ignored
  *   if the user doesn't have the suppressredirect right
  */
 private function moveToInternal(&$nt, $reason = '', $createRedirect = true)
 {
     global $wgUser, $wgContLang;
     $moveOverRedirect = $nt->exists();
     $commentMsg = $moveOverRedirect ? '1movedto2_redir' : '1movedto2';
     $comment = wfMsgForContent($commentMsg, $this->getPrefixedText(), $nt->getPrefixedText());
     if ($reason) {
         $comment .= wfMsgForContent('colon-separator') . $reason;
     }
     # Truncate for whole multibyte characters.
     $comment = $wgContLang->truncate($comment, 255);
     $oldid = $this->getArticleID();
     $latest = $this->getLatestRevID();
     $dbw = wfGetDB(DB_MASTER);
     if ($moveOverRedirect) {
         $rcts = $dbw->timestamp($nt->getEarliestRevTime());
         $newid = $nt->getArticleID();
         $newns = $nt->getNamespace();
         $newdbk = $nt->getDBkey();
         # Delete the old redirect. We don't save it to history since
         # by definition if we've got here it's rather uninteresting.
         # We have to remove it so that the next step doesn't trigger
         # a conflict on the unique namespace+title index...
         $dbw->delete('page', array('page_id' => $newid), __METHOD__);
         if (!$dbw->cascadingDeletes()) {
             $dbw->delete('revision', array('rev_page' => $newid), __METHOD__);
             global $wgUseTrackbacks;
             if ($wgUseTrackbacks) {
                 $dbw->delete('trackbacks', array('tb_page' => $newid), __METHOD__);
             }
             $dbw->delete('pagelinks', array('pl_from' => $newid), __METHOD__);
             $dbw->delete('imagelinks', array('il_from' => $newid), __METHOD__);
             $dbw->delete('categorylinks', array('cl_from' => $newid), __METHOD__);
             $dbw->delete('templatelinks', array('tl_from' => $newid), __METHOD__);
             $dbw->delete('externallinks', array('el_from' => $newid), __METHOD__);
             $dbw->delete('langlinks', array('ll_from' => $newid), __METHOD__);
             $dbw->delete('iwlinks', array('iwl_from' => $newid), __METHOD__);
             $dbw->delete('redirect', array('rd_from' => $newid), __METHOD__);
         }
         // If the target page was recently created, it may have an entry in recentchanges still
         $dbw->delete('recentchanges', array('rc_timestamp' => $rcts, 'rc_namespace' => $newns, 'rc_title' => $newdbk, 'rc_new' => 1), __METHOD__);
     }
     # Save a null revision in the page's history notifying of the move
     $nullRevision = Revision::newNullRevision($dbw, $oldid, $comment, true);
     if (!is_object($nullRevision)) {
         throw new MWException('No valid null revision produced in ' . __METHOD__);
     }
     $nullRevId = $nullRevision->insertOn($dbw);
     $now = wfTimestampNow();
     # Change the name of the target page:
     $dbw->update('page', array('page_touched' => $dbw->timestamp($now), 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey(), 'page_latest' => $nullRevId), array('page_id' => $oldid), __METHOD__);
     $nt->resetArticleID($oldid);
     $article = new Article($nt);
     wfRunHooks('NewRevisionFromEditComplete', array($article, $nullRevision, $latest, $wgUser));
     $article->setCachedLastEditTime($now);
     # Recreate the redirect, this time in the other direction.
     if ($createRedirect || !$wgUser->isAllowed('suppressredirect')) {
         $mwRedir = MagicWord::get('redirect');
         $redirectText = $mwRedir->getSynonym(0) . ' [[' . $nt->getPrefixedText() . "]]\n";
         $redirectArticle = new Article($this);
         $newid = $redirectArticle->insertOn($dbw);
         $redirectRevision = new Revision(array('page' => $newid, 'comment' => $comment, 'text' => $redirectText));
         $redirectRevision->insertOn($dbw);
         $redirectArticle->updateRevisionOn($dbw, $redirectRevision, 0);
         wfRunHooks('NewRevisionFromEditComplete', array($redirectArticle, $redirectRevision, false, $wgUser));
         # Now, we record the link from the redirect to the new title.
         # It should have no other outgoing links...
         $dbw->delete('pagelinks', array('pl_from' => $newid), __METHOD__);
         $dbw->insert('pagelinks', array('pl_from' => $newid, 'pl_namespace' => $nt->getNamespace(), 'pl_title' => $nt->getDBkey()), __METHOD__);
         $redirectSuppressed = false;
     } else {
         $this->resetArticleID(0);
         $redirectSuppressed = true;
     }
     # Log the move
     $log = new LogPage('move');
     $logType = $moveOverRedirect ? 'move_redir' : 'move';
     $log->addEntry($logType, $this, $reason, array(1 => $nt->getPrefixedText(), 2 => $redirectSuppressed));
     # Purge caches for old and new titles
     if ($moveOverRedirect) {
         # A simple purge is enough when moving over a redirect
         $nt->purgeSquid();
     } else {
         # Purge caches as per article creation, including any pages that link to this title
         Article::onArticleCreate($nt);
     }
     $this->purgeSquid();
 }