- if( $targetTitle->getArticleId() == $destTitle->getArticleId() )
- return false;
- # Verify that this timestamp is valid
- # Must be older than the destination page
- $dbw = wfGetDB( DB_MASTER );
- # Get timestamp into DB format
- $this->mTimestamp = $this->mTimestamp ? $dbw->timestamp($this->mTimestamp) : '';
- # Max timestamp should be min of destination page
- $maxtimestamp = $dbw->selectField( 'revision', 'MIN(rev_timestamp)',
- array('rev_page' => $this->mDestID ),
- __METHOD__ );
- # Destination page must exist with revisions
- if( !$maxtimestamp ) {
- $wgOut->addWikiMsg('mergehistory-fail');
- return false;
- }
- # Get the latest timestamp of the source
- $lasttimestamp = $dbw->selectField( array('page','revision'),
- 'rev_timestamp',
- array('page_id' => $this->mTargetID, 'page_latest = rev_id' ),
- __METHOD__ );
- # $this->mTimestamp must be older than $maxtimestamp
- if( $this->mTimestamp >= $maxtimestamp ) {
- $wgOut->addWikiMsg('mergehistory-fail');
- return false;
- }
- # Update the revisions
- if( $this->mTimestamp ) {
- $timewhere = "rev_timestamp <= {$this->mTimestamp}";
- $TimestampLimit = wfTimestamp(TS_MW,$this->mTimestamp);
- } else {
- $timewhere = "rev_timestamp <= {$maxtimestamp}";
- $TimestampLimit = wfTimestamp(TS_MW,$lasttimestamp);
- }
- # Do the moving...
- $dbw->update( 'revision',
- array( 'rev_page' => $this->mDestID ),
- array( 'rev_page' => $this->mTargetID,
- $timewhere ),
- __METHOD__ );
-
- $count = $dbw->affectedRows();
- # Make the source page a redirect if no revisions are left
- $haveRevisions = $dbw->selectField( 'revision',
- 'rev_timestamp',
- array( 'rev_page' => $this->mTargetID ),
- __METHOD__,
- array( 'FOR UPDATE' ) );
- if( !$haveRevisions ) {
- if( $this->mComment ) {
- $comment = wfMsgForContent( 'mergehistory-comment', $targetTitle->getPrefixedText(),
- $destTitle->getPrefixedText(), $this->mComment );
- } else {
- $comment = wfMsgForContent( 'mergehistory-autocomment', $targetTitle->getPrefixedText(),
- $destTitle->getPrefixedText() );
- }
- $mwRedir = MagicWord::get( 'redirect' );
- $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destTitle->getPrefixedText() . "]]\n";
- $redirectArticle = new Article( $targetTitle );
- $redirectRevision = new Revision( array(
- 'page' => $this->mTargetID,
- 'comment' => $comment,
- 'text' => $redirectText ) );
- $redirectRevision->insertOn( $dbw );
- $redirectArticle->updateRevisionOn( $dbw, $redirectRevision );
-
- # 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' => $this->mDestID ), __METHOD__ );
- $dbw->insert( 'pagelinks',
- array(
- 'pl_from' => $this->mDestID,
- 'pl_namespace' => $destTitle->getNamespace(),
- 'pl_title' => $destTitle->getDBkey() ),
- __METHOD__ );
- } else {
- $targetTitle->invalidateCache(); // update histories