3 * Special page allowing users with the appropriate permissions to
4 * move revisions of a page to a new target (either an existing page or not)
6 * The user selects revisions in the page history (HistoryPage.php),
7 * clicks on the submit button and gets this special page.
8 * A form is shown (showForm()) where the user has to enter a target page
9 * name and confirm the action with a post request & edit token.
10 * Then submit() is called, which does some checks and calls moveRevisions().
11 * If the target doesn't exist, a new page gets created. rev_page of the
12 * selected revisions is updated, after that it is determined whether page_latest
13 * of the target page and the source page require an update.
15 * **** NOTE: This feature is EXPERIMENTAL. ****
16 * **** Do not use on any productive system. ****
19 * @ingroup SpecialPage
22 /* TODO In case page_deleted gets introduced some day, use it.
23 * Currently it is possible with RevisionMove to make the latest revision
24 * of a page a RevisionDeleted one. When that happens, the user is presented
25 * an empty page with no error message whatsoever (in case he is not permitted
26 * to view deleted edits).
29 class SpecialRevisionMove
extends UnlistedSpecialPage
{
32 var $mOldTitle; # Title object.
33 var $mNewTitle; # Title object. Desired new title
34 var $request; # WebRequest object, $wgRequest by default
35 var $skin; # Skin object
36 var $user; # User object
39 var $mIds; # Array of Ids to look at
40 var $mRevlist; # RevDel_RevisionList object - borrowed from RevisionDelete
41 var $mReason; # User-supplied reason for performing the move operation
42 var $mSubmit; # Boolean: Is this a submitted request?
43 var $mIsAllowedRevisionMove = false;
45 public function __construct( $name = 'RevisionMove' ) {
46 parent
::__construct( $name );
50 * @param $par subpage part, standard special page parameter, is ignored here
52 * Mostly initializes variables and calls either showForm() or submit()
54 public function execute( $par ) {
55 global $wgUser, $wgOut, $wgSkin;
58 $this->outputHeader();
60 $this->mIsAllowedRevisionMove
= $wgUser->isAllowed( 'revisionmove' );
61 $this->user
= $wgUser;
62 $this->skin
= $wgUser->getSkin();
63 if ( !$this->request
instanceof WebRequest
) {
64 $this->request
= $GLOBALS['wgRequest'];
68 if ( $this->request
->getVal( 'action' ) == 'historysubmit' ) {
69 $this->mOldTitle
= Title
::newFromText( $this->request
->getVal( 'title' ) );
71 $this->mOldTitle
= Title
::newFromText( $this->request
->getVal( 'oldTitle' ) );
74 if ( !$this->mOldTitle
instanceof Title
) {
75 $wgOut->showErrorPage( 'revmove-badparam-title', 'revmove-badparam' );
79 $wgOut->setPagetitle( wfMsg( 'revisionmove', $this->mOldTitle
->getPrefixedText() ) );
80 $oldTitleLink = $this->skin
->link( $this->mOldTitle
);
81 $wgOut->setSubtitle( wfMsg( 'revisionmove-backlink', $oldTitleLink ) );
83 $this->mReason
= $this->request
->getText( 'wpReason' );
85 # TODO maybe not needed here? Copied from SpecialRevisiondelete.php.
86 # Keep for now, allow different inputs
87 # Handle our many different possible input types for ids
88 $ids = $this->request
->getVal( 'ids' );
89 if ( !is_null( $ids ) ) {
90 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
91 $this->mIds
= explode( ',', $ids );
94 $this->mIds
= array_keys( $this->request
->getArray( 'ids', array() ) );
96 $this->mIds
= array_unique( array_filter( $this->mIds
) );
98 if ( is_null ( $this->mIds
) ) {
99 $wgOut->showErrorPage( 'revmove-badparam-title', 'revmove-badparam' );
102 $this->mRevlist
= new RevDel_RevisionList( $this, $this->mOldTitle
, $this->mIds
);
104 # Decide what to do: Show the form, or submit the changes
105 if ( $this->request
->wasPosted() ) {
114 * Show a list of items that we will operate on and a field for the target name
116 public function showForm() {
117 global $wgOut, $wgUser;
119 if ( !$this->mIsAllowedRevisionMove
) {
120 $permErrors = $this->mOldTitle
->getUserPermissionsErrors( 'revisionmove', $this->user
);
121 $wgOut->showPermissionsErrorPage( $permErrors, 'revisionmove' );
125 $wgOut->addWikiMsg( 'revmove-explain', $this->mOldTitle
->getPrefixedText() );
126 $listNotEmpty = $this->listItems();
127 if ( !$listNotEmpty ) {
128 return; # we're done, we already displayed an error earlier
131 $out = Xml
::openElement( 'form', array( 'method' => 'post',
132 'action' => $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) ),
133 'id' => 'mw-revmove-form' ) ) .
134 Xml
::fieldset( wfMsg( 'revmove-legend' ) ) .
135 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
136 Xml
::hidden( 'oldTitle', $this->mOldTitle
->getPrefixedText() ) .
137 '<div>' . Xml
::inputLabel( wfMsg( 'revmove-reasonfield' ), 'wpReason', 'revmove-reasonfield', 60 ) . '</div>' .
138 Xml
::inputLabel( wfMsg( 'revmove-titlefield' ), 'newTitle', 'revmove-titlefield', 20, $this->mOldTitle
->getPrefixedText() ) .
139 Xml
::hidden( 'ids', implode( ',', $this->mIds
) ) .
140 Xml
::submitButton( wfMsg( 'revmove-submit' ),
141 array( 'name' => 'wpSubmit' ) ) .
142 Xml
::closeElement( 'fieldset' ) . "\n" .
143 Xml
::closeElement( 'form' ) . "\n";
144 $wgOut->addHTML( $out );
148 * Show a list of selected revisions and check the input
150 protected function listItems() {
153 $wgOut->addHTML( "<ul>" );
157 # No revisions specified at all
158 if ( $this->mIds
== array() ) {
159 $wgOut->showErrorPage( 'revmove-norevisions-title', 'revmove-norevisions' );
163 for ( $this->mRevlist
->reset(); $this->mRevlist
->current(); $this->mRevlist
->next() ) {
164 $item = $this->mRevlist
->current();
166 $wgOut->addHTML( $item->getHTML() );
169 # No valid revisions specified (e.g. only revs belonging to another page)
170 if( $numRevisions == 0 ) {
171 $wgOut->showErrorPage( 'revmove-norevisions-title', 'revmove-norevisions' );
175 $wgOut->addHTML( "</ul>" );
180 * Submit the posted changes (in $this->request).
182 * This function does some checks and then calls moveRevisions(), which does the real work
184 public function submit() {
185 global $wgUser, $wgOut;
187 # Confirm permissions
188 if ( !$this->mIsAllowedRevisionMove
) {
189 $permErrors = $this->mOldTitle
->getUserPermissionsErrors( 'revisionmove', $this->user
);
190 $wgOut->showPermissionsErrorPage( $permErrors, 'revisionmove' );
194 if( !$wgUser->matchEditToken( $this->request
->getVal( 'wpEditToken' ) ) ) {
195 $wgOut->showErrorPage( 'sessionfailure-title', 'sessionfailure' );
199 $this->mNewTitle
= Title
::newFromText( $this->request
->getVal( 'newTitle' ) );
200 if ( !$this->mNewTitle
instanceof Title
) {
201 $wgOut->showErrorPage( 'badtitle', 'badtitletext' );
205 if ( $this->mNewTitle
->getPrefixedText() == $this->mOldTitle
->getPrefixedText() ) {
206 $pagename = array( $this->mOldTitle
->getPrefixedText() );
207 $wgOut->showErrorPage( 'revmove-nullmove-title', 'revmove-nullmove', $pagename );
211 $this->moveRevisions();
215 * This function actually move the revision. NEVER call this function, call submit()
217 protected function moveRevisions() {
220 $oldArticle = new Article( $this->mOldTitle
);
221 $newArticle = new Article( $this->mNewTitle
);
223 $idstring = implode( ", ", $this->mIds
);
225 # Get DB connection and begin transaction
226 $dbw = wfGetDB( DB_MASTER
);
229 # Check if the target exists. If not, try creating it
230 if ( !$this->mNewTitle
->exists() ) {
231 $newArticle->insertOn( $dbw );
232 $this->createArticle
= true;
234 $this->createArticle
= false;
237 # This is where the magic happens:
238 # Update revision table
239 $dbw->update( 'revision',
240 array( 'rev_page' => $this->mNewTitle
->getArticleID() ),
242 'rev_id IN (' . $idstring . ')',
243 'rev_page' => $this->mOldTitle
->getArticleID(),
247 $modifiedRevsNum = $dbw->affectedRows();
249 # Check if we need to update page_latest
250 # Get the latest version of the revisions we are moving
251 $timestampNewPage = $this->queryLatestTimestamp(
253 $this->mNewTitle
->getArticleID(),
254 array( 'rev_id IN (' . $idstring . ')' )
257 # Compare the new page's page_latest against db query.
258 # If we create a new page, we have to update anyway
260 $currentNewPageRev = Revision
::newFromId( $this->mNewTitle
->getLatestRevID() );
261 if ( $this->createArticle ||
$timestampNewPage > $currentNewPageRev->getTimestamp() ) {
262 # we have to set page_latest to $timestampNewPage's revid
263 $this->updatePageLatest(
268 array( 'rev_id IN (' . $idstring . ')' )
272 # Update the old page's page_latest field
273 $timestampOldPage = $this->queryLatestTimestamp(
275 $this->mOldTitle
->getArticleID()
278 # If the timestamp is null that means the page doesn't have
279 # any revisions associated and should be deleted. In other words,
280 # someone misused revisionmove for the normal move function.
281 if ( is_null( $timestampOldPage ) ) {
284 array( 'page_id = ' . $this->mOldTitle
->getArticleID() ),
287 $deletedOldPage = true;
289 # page_latest has to be updated
290 $currentOldPageRev = Revision
::newFromId( $this->mOldTitle
->getLatestRevID() );
291 if ( $timestampOldPage < $currentOldPageRev->getTimestamp() ) {
292 $this->updatePageLatest(
299 # Purge the old one only if it hasn't been deleted
300 $oldArticle->doPurge();
306 $this->logMove( $modifiedRevsNum );
309 $newArticle->doPurge();
311 # If noting went wrong (i.e. returned), we are successful
312 $this->showSuccess( $modifiedRevsNum );
316 * Query for the latest timestamp in order to update page_latest and
318 * @param &$dbw Database object (Master)
319 * @param $articleId Integer page_id
320 * @param $conds array database conditions
322 * @return String timestamp
324 protected function queryLatestTimestamp( &$dbw, $articleId, $conds = array() ) {
325 $timestampNewRow = $dbw->selectRow(
327 'max(rev_timestamp) as maxtime',
328 array_merge( array( 'rev_page' => $articleId ), $conds ),
331 return $timestampNewRow->maxtime
;
335 * Updates page_latest and similar database fields (see Article::updateRevisionOn).
336 * Called two times, for the new and the old page
338 * @param &$dbw Database object (Master)
339 * @param $articleTitle Title object of the page
340 * @param $articleObj Article object of the page
341 * @param $timestamp to search for (use queryLatestTimestamp to get the latest)
342 * @param $conds array database conditions
344 * @return boolean indicating success
346 protected function updatePageLatest( &$dbw, $articleTitle, &$articleObj, $timestamp, $conds = array() ) {
347 # Query to find out the rev_id
348 $revisionRow = $dbw->selectRow(
352 'rev_timestamp' => $timestamp,
353 'rev_page' => $articleTitle->getArticleID(),
358 $latestRev = Revision
::newFromId( $revisionRow->rev_id
);
359 return $articleObj->updateRevisionOn( $dbw, $latestRev, $articleTitle->getLatestRevID(), null, /* set new page flag */ true );
363 * Add a log entry for the revision move
365 protected function logMove( $modifiedRevsNum ) {
367 $this->mNewTitle
->getPrefixedText(),
370 $log = new LogPage( 'move' );
371 $log->addEntry( 'move_rev', $this->mOldTitle
, $this->mReason
, $paramArray, $this->user
);
375 protected function showSuccess( $modifiedRevsNum ) {
378 if ( $this->createArticle
) {
379 $wgOut->addWikiMsg( 'revmove-success-created', $modifiedRevsNum,
380 $this->mOldTitle
->getPrefixedText(), $this->mNewTitle
->getPrefixedText() );
382 $wgOut->addWikiMsg( 'revmove-success-existing', $modifiedRevsNum,
383 $this->mOldTitle
->getPrefixedText(), $this->mNewTitle
->getPrefixedText() );