2015-12-30 04:53:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Copyright © 2015 Geoffrey Mon <geofbot@gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
2020-04-28 22:54:18 +00:00
|
|
|
|
|
|
|
|
use MediaWiki\Content\IContentHandlerFactory;
|
2020-05-13 21:30:46 +00:00
|
|
|
use MediaWiki\EditPage\SpamChecker;
|
2020-08-25 20:09:46 +00:00
|
|
|
use MediaWiki\HookContainer\HookContainer;
|
|
|
|
|
use MediaWiki\HookContainer\HookRunner;
|
2016-04-11 10:41:39 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2020-11-11 21:10:47 +00:00
|
|
|
use MediaWiki\Page\WikiPageFactory;
|
2020-04-28 22:54:18 +00:00
|
|
|
use MediaWiki\Permissions\PermissionManager;
|
2020-03-29 21:16:50 +00:00
|
|
|
use MediaWiki\Revision\MutableRevisionRecord;
|
2020-04-28 22:54:18 +00:00
|
|
|
use MediaWiki\Revision\RevisionStore;
|
2020-03-29 21:16:50 +00:00
|
|
|
use MediaWiki\Revision\SlotRecord;
|
2017-02-10 18:09:05 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2020-04-28 22:54:18 +00:00
|
|
|
use Wikimedia\Rdbms\ILoadBalancer;
|
2020-01-10 00:00:51 +00:00
|
|
|
use Wikimedia\Timestamp\TimestampException;
|
2015-12-30 04:53:34 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handles the backend logic of merging the histories of two
|
|
|
|
|
* pages.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.27
|
|
|
|
|
*/
|
|
|
|
|
class MergeHistory {
|
|
|
|
|
|
2019-02-26 13:02:50 +00:00
|
|
|
/** Maximum number of revisions that can be merged at once */
|
2020-05-11 00:48:27 +00:00
|
|
|
public const REVISION_LIMIT = 5000;
|
2015-12-30 04:53:34 +00:00
|
|
|
|
|
|
|
|
/** @var Title Page from which history will be merged */
|
|
|
|
|
protected $source;
|
|
|
|
|
|
|
|
|
|
/** @var Title Page to which history will be merged */
|
|
|
|
|
protected $dest;
|
|
|
|
|
|
2016-09-26 22:40:07 +00:00
|
|
|
/** @var IDatabase Database that we are using */
|
2015-12-30 04:53:34 +00:00
|
|
|
protected $dbw;
|
|
|
|
|
|
|
|
|
|
/** @var MWTimestamp Maximum timestamp that we can use (oldest timestamp of dest) */
|
|
|
|
|
protected $maxTimestamp;
|
|
|
|
|
|
|
|
|
|
/** @var string SQL WHERE condition that selects source revisions to insert into destination */
|
|
|
|
|
protected $timeWhere;
|
|
|
|
|
|
2016-03-24 08:44:09 +00:00
|
|
|
/** @var MWTimestamp|bool Timestamp upto which history from the source will be merged */
|
2015-12-30 04:53:34 +00:00
|
|
|
protected $timestampLimit;
|
|
|
|
|
|
2017-08-20 11:20:59 +00:00
|
|
|
/** @var int Number of revisions merged (for Special:MergeHistory success message) */
|
2015-12-30 04:53:34 +00:00
|
|
|
protected $revisionsMerged;
|
|
|
|
|
|
2020-04-28 22:54:18 +00:00
|
|
|
/** @var PermissionManager */
|
|
|
|
|
private $permManager;
|
|
|
|
|
|
|
|
|
|
/** @var IContentHandlerFactory */
|
|
|
|
|
private $contentHandlerFactory;
|
|
|
|
|
|
|
|
|
|
/** @var RevisionStore */
|
|
|
|
|
private $revisionStore;
|
|
|
|
|
|
|
|
|
|
/** @var WatchedItemStoreInterface */
|
|
|
|
|
private $watchedItemStore;
|
|
|
|
|
|
2020-05-13 21:30:46 +00:00
|
|
|
/** @var SpamChecker */
|
|
|
|
|
private $spamChecker;
|
|
|
|
|
|
2020-08-25 20:09:46 +00:00
|
|
|
/** @var HookRunner */
|
|
|
|
|
private $hookRunner;
|
|
|
|
|
|
2020-11-11 21:10:47 +00:00
|
|
|
/** @var WikiPageFactory */
|
|
|
|
|
private $wikiPageFactory;
|
|
|
|
|
|
2015-12-30 04:53:34 +00:00
|
|
|
/**
|
2020-04-28 22:54:18 +00:00
|
|
|
* Since 1.35 dependencies are injected and not providing them is hard deprecated; use the
|
|
|
|
|
* MergeHistoryFactory service
|
|
|
|
|
*
|
2015-12-30 04:53:34 +00:00
|
|
|
* @param Title $source Page from which history will be merged
|
|
|
|
|
* @param Title $dest Page to which history will be merged
|
2016-03-24 08:44:09 +00:00
|
|
|
* @param string|bool $timestamp Timestamp up to which history from the source will be merged
|
2020-04-28 22:54:18 +00:00
|
|
|
* @param ILoadBalancer|null $loadBalancer
|
|
|
|
|
* @param PermissionManager|null $permManager
|
|
|
|
|
* @param IContentHandlerFactory|null $contentHandlerFactory
|
|
|
|
|
* @param RevisionStore|null $revisionStore
|
|
|
|
|
* @param WatchedItemStoreInterface|null $watchedItemStore
|
2020-05-13 21:30:46 +00:00
|
|
|
* @param SpamChecker|null $spamChecker
|
2020-08-25 20:09:46 +00:00
|
|
|
* @param HookContainer|null $hookContainer
|
2020-11-11 21:10:47 +00:00
|
|
|
* @param WikiPageFactory|null $wikiPageFactory
|
2015-12-30 04:53:34 +00:00
|
|
|
*/
|
2020-04-28 22:54:18 +00:00
|
|
|
public function __construct(
|
|
|
|
|
Title $source,
|
|
|
|
|
Title $dest,
|
|
|
|
|
$timestamp = false,
|
|
|
|
|
ILoadBalancer $loadBalancer = null,
|
|
|
|
|
PermissionManager $permManager = null,
|
|
|
|
|
IContentHandlerFactory $contentHandlerFactory = null,
|
|
|
|
|
RevisionStore $revisionStore = null,
|
2020-05-13 21:30:46 +00:00
|
|
|
WatchedItemStoreInterface $watchedItemStore = null,
|
2020-08-25 20:09:46 +00:00
|
|
|
SpamChecker $spamChecker = null,
|
2020-11-11 21:10:47 +00:00
|
|
|
HookContainer $hookContainer = null,
|
|
|
|
|
WikiPageFactory $wikiPageFactory = null
|
2020-04-28 22:54:18 +00:00
|
|
|
) {
|
|
|
|
|
if ( $loadBalancer === null ) {
|
Introduce wfDeprecatedMsg()
Deprecating something means to say something nasty about it, or to draw
its character into question. For example, "this function is lazy and good
for nothing". Deprecatory remarks by a developer are generally taken as a
warning that violence will soon be done against the function in question.
Other developers are thus warned to avoid associating with the deprecated
function.
However, since wfDeprecated() was introduced, it has become obvious that
the targets of deprecation are not limited to functions. Developers can
deprecate literally anything: a parameter, a return value, a file
format, Mondays, the concept of being, etc. wfDeprecated() requires
every deprecatory statement to begin with "use of", leading to some
awkward sentences. For example, one might say: "Use of your mouth to
cough without it being covered by your arm is deprecated since 2020."
So, introduce wfDeprecatedMsg(), which allows deprecation messages to be
specified in plain text, with the caller description being optionally
appended. Migrate incorrect or gramatically awkward uses of wfDeprecated()
to wfDeprecatedMsg().
Change-Id: Ib3dd2fe37677d98425d0f3692db5c9e988943ae8
2020-06-12 04:18:35 +00:00
|
|
|
wfDeprecatedMsg( 'Direct construction of ' . __CLASS__ .
|
|
|
|
|
' was deprecated in MediaWiki 1.35', '1.35' );
|
2020-04-28 22:54:18 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
|
|
|
|
|
|
$loadBalancer = $services->getDBLoadBalancer();
|
|
|
|
|
$permManager = $services->getPermissionManager();
|
|
|
|
|
$contentHandlerFactory = $services->getContentHandlerFactory();
|
|
|
|
|
$revisionStore = $services->getRevisionStore();
|
|
|
|
|
$watchedItemStore = $services->getWatchedItemStore();
|
2020-05-13 21:30:46 +00:00
|
|
|
$spamChecker = $services->getSpamChecker();
|
2020-08-25 20:09:46 +00:00
|
|
|
$hookContainer = $services->getHookContainer();
|
2020-11-11 21:10:47 +00:00
|
|
|
$wikiPageFactory = $services->getWikiPageFactory();
|
2020-04-28 22:54:18 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-30 04:53:34 +00:00
|
|
|
// Save the parameters
|
|
|
|
|
$this->source = $source;
|
|
|
|
|
$this->dest = $dest;
|
|
|
|
|
|
|
|
|
|
// Get the database
|
2020-04-28 22:54:18 +00:00
|
|
|
$this->dbw = $loadBalancer->getConnection( DB_MASTER );
|
|
|
|
|
|
|
|
|
|
$this->permManager = $permManager;
|
|
|
|
|
$this->contentHandlerFactory = $contentHandlerFactory;
|
|
|
|
|
$this->revisionStore = $revisionStore;
|
|
|
|
|
$this->watchedItemStore = $watchedItemStore;
|
2020-05-13 21:30:46 +00:00
|
|
|
$this->spamChecker = $spamChecker;
|
2020-08-25 20:09:46 +00:00
|
|
|
$this->hookRunner = new HookRunner( $hookContainer );
|
2020-11-11 21:10:47 +00:00
|
|
|
$this->wikiPageFactory = $wikiPageFactory;
|
2015-12-30 04:53:34 +00:00
|
|
|
|
|
|
|
|
// Max timestamp should be min of destination page
|
|
|
|
|
$firstDestTimestamp = $this->dbw->selectField(
|
|
|
|
|
'revision',
|
|
|
|
|
'MIN(rev_timestamp)',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'rev_page' => $this->dest->getArticleID() ],
|
2015-12-30 04:53:34 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
$this->maxTimestamp = new MWTimestamp( $firstDestTimestamp );
|
|
|
|
|
|
|
|
|
|
// Get the timestamp pivot condition
|
|
|
|
|
try {
|
|
|
|
|
if ( $timestamp ) {
|
|
|
|
|
// If we have a requested timestamp, use the
|
|
|
|
|
// latest revision up to that point as the insertion point
|
|
|
|
|
$mwTimestamp = new MWTimestamp( $timestamp );
|
|
|
|
|
$lastWorkingTimestamp = $this->dbw->selectField(
|
|
|
|
|
'revision',
|
|
|
|
|
'MAX(rev_timestamp)',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2016-10-29 16:16:39 +00:00
|
|
|
'rev_timestamp <= ' .
|
|
|
|
|
$this->dbw->addQuotes( $this->dbw->timestamp( $mwTimestamp ) ),
|
2015-12-30 04:53:34 +00:00
|
|
|
'rev_page' => $this->source->getArticleID()
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2015-12-30 04:53:34 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
$mwLastWorkingTimestamp = new MWTimestamp( $lastWorkingTimestamp );
|
|
|
|
|
|
|
|
|
|
$timeInsert = $mwLastWorkingTimestamp;
|
|
|
|
|
$this->timestampLimit = $mwLastWorkingTimestamp;
|
|
|
|
|
} else {
|
|
|
|
|
// If we don't, merge entire source page history into the
|
|
|
|
|
// beginning of destination page history
|
|
|
|
|
|
|
|
|
|
// Get the latest timestamp of the source
|
|
|
|
|
$lastSourceTimestamp = $this->dbw->selectField(
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'page', 'revision' ],
|
2015-12-30 04:53:34 +00:00
|
|
|
'rev_timestamp',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'page_id' => $this->source->getArticleID(),
|
2015-12-30 04:53:34 +00:00
|
|
|
'page_latest = rev_id'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2015-12-30 04:53:34 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
$lasttimestamp = new MWTimestamp( $lastSourceTimestamp );
|
|
|
|
|
|
|
|
|
|
$timeInsert = $this->maxTimestamp;
|
|
|
|
|
$this->timestampLimit = $lasttimestamp;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 16:16:39 +00:00
|
|
|
$this->timeWhere = "rev_timestamp <= " .
|
|
|
|
|
$this->dbw->addQuotes( $this->dbw->timestamp( $timeInsert ) );
|
2015-12-30 04:53:34 +00:00
|
|
|
} catch ( TimestampException $ex ) {
|
|
|
|
|
// The timestamp we got is screwed up and merge cannot continue
|
|
|
|
|
// This should be detected by $this->isValidMerge()
|
|
|
|
|
$this->timestampLimit = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the number of revisions that will be moved
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getRevisionCount() {
|
|
|
|
|
$count = $this->dbw->selectRowCount( 'revision', '1',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'rev_page' => $this->source->getArticleID(), $this->timeWhere ],
|
2015-12-30 04:53:34 +00:00
|
|
|
__METHOD__,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'LIMIT' => self::REVISION_LIMIT + 1 ]
|
2015-12-30 04:53:34 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the number of revisions that were moved
|
|
|
|
|
* Used in the SpecialMergeHistory success message
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getMergedRevisionCount() {
|
|
|
|
|
return $this->revisionsMerged;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if the merge is possible
|
|
|
|
|
* @param User $user
|
|
|
|
|
* @param string $reason
|
|
|
|
|
* @return Status
|
|
|
|
|
*/
|
|
|
|
|
public function checkPermissions( User $user, $reason ) {
|
|
|
|
|
$status = new Status();
|
|
|
|
|
|
|
|
|
|
// Check if user can edit both pages
|
|
|
|
|
$errors = wfMergeErrorArrays(
|
2020-04-28 22:54:18 +00:00
|
|
|
$this->permManager->getPermissionErrors( 'edit', $user, $this->source ),
|
|
|
|
|
$this->permManager->getPermissionErrors( 'edit', $user, $this->dest )
|
2015-12-30 04:53:34 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Convert into a Status object
|
|
|
|
|
if ( $errors ) {
|
|
|
|
|
foreach ( $errors as $error ) {
|
2018-06-08 02:58:35 +00:00
|
|
|
$status->fatal( ...$error );
|
2015-12-30 04:53:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Anti-spam
|
2020-05-13 21:30:46 +00:00
|
|
|
if ( $this->spamChecker->checkSummary( $reason ) !== false ) {
|
2015-12-30 04:53:34 +00:00
|
|
|
// This is kind of lame, won't display nice
|
|
|
|
|
$status->fatal( 'spamprotectiontext' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check mergehistory permission
|
2020-04-28 22:54:18 +00:00
|
|
|
if ( !$this->permManager->userHasRight( $user, 'mergehistory' ) ) {
|
2015-12-30 04:53:34 +00:00
|
|
|
// User doesn't have the right to merge histories
|
|
|
|
|
$status->fatal( 'mergehistory-fail-permission' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Does various sanity checks that the merge is
|
|
|
|
|
* valid. Only things based on the two pages
|
|
|
|
|
* should be checked here.
|
|
|
|
|
*
|
|
|
|
|
* @return Status
|
|
|
|
|
*/
|
|
|
|
|
public function isValidMerge() {
|
|
|
|
|
$status = new Status();
|
|
|
|
|
|
|
|
|
|
// If either article ID is 0, then revisions cannot be reliably selected
|
|
|
|
|
if ( $this->source->getArticleID() === 0 ) {
|
|
|
|
|
$status->fatal( 'mergehistory-fail-invalid-source' );
|
|
|
|
|
}
|
|
|
|
|
if ( $this->dest->getArticleID() === 0 ) {
|
|
|
|
|
$status->fatal( 'mergehistory-fail-invalid-dest' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure page aren't the same
|
|
|
|
|
if ( $this->source->equals( $this->dest ) ) {
|
|
|
|
|
$status->fatal( 'mergehistory-fail-self-merge' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure the timestamp is valid
|
|
|
|
|
if ( !$this->timestampLimit ) {
|
|
|
|
|
$status->fatal( 'mergehistory-fail-bad-timestamp' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// $this->timestampLimit must be older than $this->maxTimestamp
|
|
|
|
|
if ( $this->timestampLimit > $this->maxTimestamp ) {
|
|
|
|
|
$status->fatal( 'mergehistory-fail-timestamps-overlap' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check that there are not too many revisions to move
|
|
|
|
|
if ( $this->timestampLimit && $this->getRevisionCount() > self::REVISION_LIMIT ) {
|
|
|
|
|
$status->fatal( 'mergehistory-fail-toobig', Message::numParam( self::REVISION_LIMIT ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Actually attempt the history move
|
|
|
|
|
*
|
|
|
|
|
* @todo if all versions of page A are moved to B and then a user
|
|
|
|
|
* tries to do a reverse-merge via the "unmerge" log link, then page
|
|
|
|
|
* A will still be a redirect (as it was after the original merge),
|
|
|
|
|
* though it will have the old revisions back from before (as expected).
|
|
|
|
|
* The user may have to "undo" the redirect manually to finish the "unmerge".
|
|
|
|
|
* Maybe this should delete redirects at the source page of merges?
|
|
|
|
|
*
|
|
|
|
|
* @param User $user
|
|
|
|
|
* @param string $reason
|
|
|
|
|
* @return Status status of the history merge
|
|
|
|
|
*/
|
|
|
|
|
public function merge( User $user, $reason = '' ) {
|
|
|
|
|
$status = new Status();
|
|
|
|
|
|
|
|
|
|
// Check validity and permissions required for merge
|
|
|
|
|
$validCheck = $this->isValidMerge(); // Check this first to check for null pages
|
|
|
|
|
if ( !$validCheck->isOK() ) {
|
|
|
|
|
return $validCheck;
|
|
|
|
|
}
|
|
|
|
|
$permCheck = $this->checkPermissions( $user, $reason );
|
|
|
|
|
if ( !$permCheck->isOK() ) {
|
|
|
|
|
return $permCheck;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-02 18:02:54 +00:00
|
|
|
$this->dbw->startAtomic( __METHOD__ );
|
|
|
|
|
|
2015-12-30 04:53:34 +00:00
|
|
|
$this->dbw->update(
|
|
|
|
|
'revision',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'rev_page' => $this->dest->getArticleID() ],
|
|
|
|
|
[ 'rev_page' => $this->source->getArticleID(), $this->timeWhere ],
|
2015-12-30 04:53:34 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Check if this did anything
|
|
|
|
|
$this->revisionsMerged = $this->dbw->affectedRows();
|
|
|
|
|
if ( $this->revisionsMerged < 1 ) {
|
2018-07-02 18:02:54 +00:00
|
|
|
$this->dbw->endAtomic( __METHOD__ );
|
2015-12-30 04:53:34 +00:00
|
|
|
$status->fatal( 'mergehistory-fail-no-change' );
|
2018-07-02 18:02:54 +00:00
|
|
|
|
2015-12-30 04:53:34 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-10 15:24:24 +00:00
|
|
|
// Update denormalized revactor_page too
|
|
|
|
|
$this->dbw->update(
|
|
|
|
|
'revision_actor_temp',
|
|
|
|
|
[ 'revactor_page' => $this->dest->getArticleID() ],
|
|
|
|
|
[
|
|
|
|
|
'revactor_page' => $this->source->getArticleID(),
|
|
|
|
|
// Slightly hacky, but should work given the values assigned in this class
|
|
|
|
|
str_replace( 'rev_timestamp', 'revactor_timestamp', $this->timeWhere )
|
|
|
|
|
],
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
|
2018-07-02 18:02:54 +00:00
|
|
|
$haveRevisions = $this->dbw->lockForUpdate(
|
2015-12-30 04:53:34 +00:00
|
|
|
'revision',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'rev_page' => $this->source->getArticleID() ],
|
2018-07-02 18:02:54 +00:00
|
|
|
__METHOD__
|
2015-12-30 04:53:34 +00:00
|
|
|
);
|
2020-03-29 21:16:50 +00:00
|
|
|
|
Improve handling of content models that do not support redirect.
To properly support content models that do not support redirects
during historymerge we have to do some cleanup, else after merging
all revisions of a page, a corrupted page will be left with a page id
(because it was not deleted) but no live revision (because they have
been all merged to the destination page).
This will lead to cascade of exceptions in Wikipage, RevisionStore,
RevisionStoreRecord, PoolWorkArticleView as well other various paths
that will attempt to interact with these, because page and revision
mismatch is considered a logic error almost everywhere.
The failure does not happen for content models that support redirects
because they are immediately creating new (latest) revision for the
old corrupted page. But we cannot require all content models to support
redirects, may not be feasible and can hinder forward compatibility.
This patch fixes this for content models that do not support redirect.
Now after merging all revisions of a page to another page, and the
source content model does not support redirect, empty content will be
created to aid proper deletion of the page afterwards.
Creating the content before deletion is necessary, else proper
deletion is not possible because many calls to revision-related methods
will throw exception during the deletion if we just use the original
corrupted page which does not have proper revisions now.
Bug: T93469
Bug: T263340
Change-Id: I07109445288633e3ddece4190f0c1c2b10372384
2020-09-28 09:08:00 +00:00
|
|
|
// Update source page, histories and invalidate caches
|
2015-12-30 04:53:34 +00:00
|
|
|
if ( !$haveRevisions ) {
|
|
|
|
|
if ( $reason ) {
|
|
|
|
|
$reason = wfMessage(
|
|
|
|
|
'mergehistory-comment',
|
|
|
|
|
$this->source->getPrefixedText(),
|
|
|
|
|
$this->dest->getPrefixedText(),
|
|
|
|
|
$reason
|
|
|
|
|
)->inContentLanguage()->text();
|
|
|
|
|
} else {
|
|
|
|
|
$reason = wfMessage(
|
|
|
|
|
'mergehistory-autocomment',
|
|
|
|
|
$this->source->getPrefixedText(),
|
|
|
|
|
$this->dest->getPrefixedText()
|
|
|
|
|
)->inContentLanguage()->text();
|
|
|
|
|
}
|
|
|
|
|
|
Improve handling of content models that do not support redirect.
To properly support content models that do not support redirects
during historymerge we have to do some cleanup, else after merging
all revisions of a page, a corrupted page will be left with a page id
(because it was not deleted) but no live revision (because they have
been all merged to the destination page).
This will lead to cascade of exceptions in Wikipage, RevisionStore,
RevisionStoreRecord, PoolWorkArticleView as well other various paths
that will attempt to interact with these, because page and revision
mismatch is considered a logic error almost everywhere.
The failure does not happen for content models that support redirects
because they are immediately creating new (latest) revision for the
old corrupted page. But we cannot require all content models to support
redirects, may not be feasible and can hinder forward compatibility.
This patch fixes this for content models that do not support redirect.
Now after merging all revisions of a page to another page, and the
source content model does not support redirect, empty content will be
created to aid proper deletion of the page afterwards.
Creating the content before deletion is necessary, else proper
deletion is not possible because many calls to revision-related methods
will throw exception during the deletion if we just use the original
corrupted page which does not have proper revisions now.
Bug: T93469
Bug: T263340
Change-Id: I07109445288633e3ddece4190f0c1c2b10372384
2020-09-28 09:08:00 +00:00
|
|
|
$this->updateSourcePage( $status, $user, $reason );
|
2020-03-29 21:16:50 +00:00
|
|
|
|
2015-12-30 04:53:34 +00:00
|
|
|
} else {
|
Improve handling of content models that do not support redirect.
To properly support content models that do not support redirects
during historymerge we have to do some cleanup, else after merging
all revisions of a page, a corrupted page will be left with a page id
(because it was not deleted) but no live revision (because they have
been all merged to the destination page).
This will lead to cascade of exceptions in Wikipage, RevisionStore,
RevisionStoreRecord, PoolWorkArticleView as well other various paths
that will attempt to interact with these, because page and revision
mismatch is considered a logic error almost everywhere.
The failure does not happen for content models that support redirects
because they are immediately creating new (latest) revision for the
old corrupted page. But we cannot require all content models to support
redirects, may not be feasible and can hinder forward compatibility.
This patch fixes this for content models that do not support redirect.
Now after merging all revisions of a page to another page, and the
source content model does not support redirect, empty content will be
created to aid proper deletion of the page afterwards.
Creating the content before deletion is necessary, else proper
deletion is not possible because many calls to revision-related methods
will throw exception during the deletion if we just use the original
corrupted page which does not have proper revisions now.
Bug: T93469
Bug: T263340
Change-Id: I07109445288633e3ddece4190f0c1c2b10372384
2020-09-28 09:08:00 +00:00
|
|
|
$this->source->invalidateCache();
|
2015-12-30 04:53:34 +00:00
|
|
|
}
|
Improve handling of content models that do not support redirect.
To properly support content models that do not support redirects
during historymerge we have to do some cleanup, else after merging
all revisions of a page, a corrupted page will be left with a page id
(because it was not deleted) but no live revision (because they have
been all merged to the destination page).
This will lead to cascade of exceptions in Wikipage, RevisionStore,
RevisionStoreRecord, PoolWorkArticleView as well other various paths
that will attempt to interact with these, because page and revision
mismatch is considered a logic error almost everywhere.
The failure does not happen for content models that support redirects
because they are immediately creating new (latest) revision for the
old corrupted page. But we cannot require all content models to support
redirects, may not be feasible and can hinder forward compatibility.
This patch fixes this for content models that do not support redirect.
Now after merging all revisions of a page to another page, and the
source content model does not support redirect, empty content will be
created to aid proper deletion of the page afterwards.
Creating the content before deletion is necessary, else proper
deletion is not possible because many calls to revision-related methods
will throw exception during the deletion if we just use the original
corrupted page which does not have proper revisions now.
Bug: T93469
Bug: T263340
Change-Id: I07109445288633e3ddece4190f0c1c2b10372384
2020-09-28 09:08:00 +00:00
|
|
|
$this->dest->invalidateCache();
|
2015-12-30 04:53:34 +00:00
|
|
|
|
Improve handling of content models that do not support redirect.
To properly support content models that do not support redirects
during historymerge we have to do some cleanup, else after merging
all revisions of a page, a corrupted page will be left with a page id
(because it was not deleted) but no live revision (because they have
been all merged to the destination page).
This will lead to cascade of exceptions in Wikipage, RevisionStore,
RevisionStoreRecord, PoolWorkArticleView as well other various paths
that will attempt to interact with these, because page and revision
mismatch is considered a logic error almost everywhere.
The failure does not happen for content models that support redirects
because they are immediately creating new (latest) revision for the
old corrupted page. But we cannot require all content models to support
redirects, may not be feasible and can hinder forward compatibility.
This patch fixes this for content models that do not support redirect.
Now after merging all revisions of a page to another page, and the
source content model does not support redirect, empty content will be
created to aid proper deletion of the page afterwards.
Creating the content before deletion is necessary, else proper
deletion is not possible because many calls to revision-related methods
will throw exception during the deletion if we just use the original
corrupted page which does not have proper revisions now.
Bug: T93469
Bug: T263340
Change-Id: I07109445288633e3ddece4190f0c1c2b10372384
2020-09-28 09:08:00 +00:00
|
|
|
// Duplicate watchers of the old article to the new article
|
2020-04-28 22:54:18 +00:00
|
|
|
$this->watchedItemStore->duplicateAllAssociatedEntries( $this->source, $this->dest );
|
2016-04-11 10:41:39 +00:00
|
|
|
|
2015-12-30 04:53:34 +00:00
|
|
|
// Update our logs
|
|
|
|
|
$logEntry = new ManualLogEntry( 'merge', 'merge' );
|
|
|
|
|
$logEntry->setPerformer( $user );
|
|
|
|
|
$logEntry->setComment( $reason );
|
|
|
|
|
$logEntry->setTarget( $this->source );
|
2016-02-17 09:09:32 +00:00
|
|
|
$logEntry->setParameters( [
|
2015-12-30 04:53:34 +00:00
|
|
|
'4::dest' => $this->dest->getPrefixedText(),
|
|
|
|
|
'5::mergepoint' => $this->timestampLimit->getTimestamp( TS_MW )
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2015-12-30 04:53:34 +00:00
|
|
|
$logId = $logEntry->insert();
|
|
|
|
|
$logEntry->publish( $logId );
|
|
|
|
|
|
2020-08-25 20:09:46 +00:00
|
|
|
$this->hookRunner->onArticleMergeComplete( $this->source, $this->dest );
|
2015-12-30 04:53:34 +00:00
|
|
|
|
2018-07-02 18:02:54 +00:00
|
|
|
$this->dbw->endAtomic( __METHOD__ );
|
|
|
|
|
|
2015-12-30 04:53:34 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
Improve handling of content models that do not support redirect.
To properly support content models that do not support redirects
during historymerge we have to do some cleanup, else after merging
all revisions of a page, a corrupted page will be left with a page id
(because it was not deleted) but no live revision (because they have
been all merged to the destination page).
This will lead to cascade of exceptions in Wikipage, RevisionStore,
RevisionStoreRecord, PoolWorkArticleView as well other various paths
that will attempt to interact with these, because page and revision
mismatch is considered a logic error almost everywhere.
The failure does not happen for content models that support redirects
because they are immediately creating new (latest) revision for the
old corrupted page. But we cannot require all content models to support
redirects, may not be feasible and can hinder forward compatibility.
This patch fixes this for content models that do not support redirect.
Now after merging all revisions of a page to another page, and the
source content model does not support redirect, empty content will be
created to aid proper deletion of the page afterwards.
Creating the content before deletion is necessary, else proper
deletion is not possible because many calls to revision-related methods
will throw exception during the deletion if we just use the original
corrupted page which does not have proper revisions now.
Bug: T93469
Bug: T263340
Change-Id: I07109445288633e3ddece4190f0c1c2b10372384
2020-09-28 09:08:00 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Do various cleanup work and updates to the source page. This method
|
|
|
|
|
* will only be called if no revision is remaining on the page.
|
|
|
|
|
*
|
|
|
|
|
* At the end, there would be either a redirect page or a deleted page,
|
|
|
|
|
* depending on whether the content model of the page supports redirects or not.
|
|
|
|
|
*
|
|
|
|
|
* @param Status $status
|
|
|
|
|
* @param User $user
|
|
|
|
|
* @param string $reason
|
|
|
|
|
*
|
|
|
|
|
* @return Status
|
|
|
|
|
*/
|
|
|
|
|
private function updateSourcePage( $status, $user, $reason ) {
|
|
|
|
|
$deleteSource = false;
|
|
|
|
|
$sourceModel = $this->source->getContentModel();
|
|
|
|
|
$contentHandler = $this->contentHandlerFactory->getContentHandler( $sourceModel );
|
|
|
|
|
|
|
|
|
|
if ( !$contentHandler->supportsRedirects() ) {
|
|
|
|
|
$deleteSource = true;
|
|
|
|
|
$newContent = $contentHandler->makeEmptyContent();
|
|
|
|
|
} else {
|
|
|
|
|
$msg = wfMessage( 'mergehistory-redirect-text' )->inContentLanguage()->plain();
|
|
|
|
|
$newContent = $contentHandler->makeRedirectContent( $this->dest, $msg );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !$newContent instanceof Content ) {
|
|
|
|
|
// Handler supports redirect but cannot create new redirect content?
|
|
|
|
|
// Not possible to proceed without Content.
|
|
|
|
|
|
|
|
|
|
// @todo. Remove this once there's no evidence it's happening or if it's
|
|
|
|
|
// determined all violating handlers have been fixed.
|
|
|
|
|
// This is mostly kept because previous code was also blindly checking
|
|
|
|
|
// existing of the Content for both content models that supports redirects
|
|
|
|
|
// and those that that don't, so it's hard to know what it was masking.
|
|
|
|
|
$logger = MediaWiki\Logger\LoggerFactory::getInstance( 'ContentHandler' );
|
|
|
|
|
$logger->warning(
|
|
|
|
|
'ContentHandler for {model} says it supports redirects but failed '
|
|
|
|
|
. 'to return Content object from ContentHandler::makeRedirectContent().'
|
|
|
|
|
. ' {value} returned instead.',
|
|
|
|
|
[
|
|
|
|
|
'value' => gettype( $newContent ),
|
|
|
|
|
'model' => $sourceModel
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
|
"ContentHandler for '$sourceModel' supports redirects" .
|
|
|
|
|
' but cannot create redirect content during History merge.'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// T263340/T93469: Create revision record to also serve as the page revision.
|
|
|
|
|
// This revision will be used to create page content. If the source page's
|
|
|
|
|
// content model supports redirects, then it will be the redirect content.
|
|
|
|
|
// If the content model does not supports redirect, this content will aid
|
|
|
|
|
// proper deletion of the page below.
|
|
|
|
|
$comment = CommentStoreComment::newUnsavedComment( $reason );
|
|
|
|
|
$newRevRecord = new MutableRevisionRecord( $this->source );
|
|
|
|
|
$newRevRecord->setContent( SlotRecord::MAIN, $newContent );
|
|
|
|
|
$newRevRecord->setPageId( $this->source->getArticleID() );
|
|
|
|
|
$newRevRecord->setComment( $comment );
|
|
|
|
|
$newRevRecord->setUser( $user );
|
|
|
|
|
$newRevRecord->setTimestamp( wfTimestampNow() );
|
|
|
|
|
|
|
|
|
|
$insertedRevRecord = $this->revisionStore->insertRevisionOn( $newRevRecord, $this->dbw );
|
|
|
|
|
|
2020-11-11 21:10:47 +00:00
|
|
|
$newPage = $this->wikiPageFactory->newFromTitle( $this->source );
|
Improve handling of content models that do not support redirect.
To properly support content models that do not support redirects
during historymerge we have to do some cleanup, else after merging
all revisions of a page, a corrupted page will be left with a page id
(because it was not deleted) but no live revision (because they have
been all merged to the destination page).
This will lead to cascade of exceptions in Wikipage, RevisionStore,
RevisionStoreRecord, PoolWorkArticleView as well other various paths
that will attempt to interact with these, because page and revision
mismatch is considered a logic error almost everywhere.
The failure does not happen for content models that support redirects
because they are immediately creating new (latest) revision for the
old corrupted page. But we cannot require all content models to support
redirects, may not be feasible and can hinder forward compatibility.
This patch fixes this for content models that do not support redirect.
Now after merging all revisions of a page to another page, and the
source content model does not support redirect, empty content will be
created to aid proper deletion of the page afterwards.
Creating the content before deletion is necessary, else proper
deletion is not possible because many calls to revision-related methods
will throw exception during the deletion if we just use the original
corrupted page which does not have proper revisions now.
Bug: T93469
Bug: T263340
Change-Id: I07109445288633e3ddece4190f0c1c2b10372384
2020-09-28 09:08:00 +00:00
|
|
|
$newPage->updateRevisionOn( $this->dbw, $insertedRevRecord );
|
|
|
|
|
|
|
|
|
|
if ( !$deleteSource ) {
|
|
|
|
|
// We have created a redirect page so let's
|
|
|
|
|
// record the link from the page to the new title.
|
|
|
|
|
// It should have no other outgoing links...
|
|
|
|
|
$this->dbw->delete(
|
|
|
|
|
'pagelinks',
|
|
|
|
|
[ 'pl_from' => $this->dest->getArticleID() ],
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
$this->dbw->insert( 'pagelinks',
|
|
|
|
|
[
|
|
|
|
|
'pl_from' => $this->dest->getArticleID(),
|
|
|
|
|
'pl_from_namespace' => $this->dest->getNamespace(),
|
|
|
|
|
'pl_namespace' => $this->dest->getNamespace(),
|
|
|
|
|
'pl_title' => $this->dest->getDBkey() ],
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// T263340/T93469: Delete the source page to prevent errors because its
|
|
|
|
|
// revisions are now tied to a different title and its content model
|
|
|
|
|
// does not support redirects, so we cannot leave a new revision on it.
|
|
|
|
|
// This deletion does not depend on userright but may still fails. If it
|
|
|
|
|
// fails, it will be communicated in the status reponse.
|
|
|
|
|
$reason = wfMessage( 'mergehistory-source-deleted-reason' )->inContentLanguage()->plain();
|
|
|
|
|
$deletionStatus = $newPage->doDeleteArticleReal( $reason, $user );
|
|
|
|
|
$status->merge( $deletionStatus );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
2015-12-30 04:53:34 +00:00
|
|
|
}
|