4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
21 * Special page allowing users with the appropriate permissions to
22 * move revisions of a page to a new target (either an existing page or not)
24 * The user selects revisions in the page history (HistoryPage.php),
25 * clicks on the submit button and gets this special page.
26 * A form is shown (showForm()) where the user has to enter a target page
27 * name and confirm the action with a post request & edit token.
28 * Then submit() is called, which does some checks and calls moveRevisions().
29 * If the target doesn't exist, a new page gets created. rev_page of the
30 * selected revisions is updated, after that it is determined whether page_latest
31 * of the target page and the source page require an update.
33 * **** NOTE: This feature is EXPERIMENTAL. ****
34 * **** Do not use on any productive system. ****
37 * @ingroup SpecialPage
40 /* TODO In case page_deleted gets introduced some day, use it.
41 * Currently it is possible with RevisionMove to make the latest revision
42 * of a page a RevisionDeleted one. When that happens, the user is presented
43 * an empty page with no error message whatsoever (in case he is not permitted
44 * to view deleted edits).
47 class SpecialRevisionMove
extends UnlistedSpecialPage
{
50 var $mOldTitle; # Title object.
51 var $mNewTitle; # Title object. Desired new title
52 var $request; # WebRequest object, $wgRequest by default
53 var $skin; # Skin object
54 var $user; # User object
57 var $mIds; # Array of Ids to look at
58 var $mRevlist; # RevDel_RevisionList object - borrowed from RevisionDelete
59 var $mReason; # User-supplied reason for performing the move operation
60 var $mSubmit; # Boolean: Is this a submitted request?
61 var $mIsAllowedRevisionMove = false;
63 public function __construct( $name = 'RevisionMove' ) {
64 parent
::__construct( $name );
68 * @param $par subpage part, standard special page parameter, is ignored here
70 * Mostly initializes variables and calls either showForm() or submit()
72 public function execute( $par ) {
73 global $wgUser, $wgOut, $wgSkin;
76 $this->outputHeader();
78 $this->mIsAllowedRevisionMove
= $wgUser->isAllowed( 'revisionmove' );
79 $this->user
= $wgUser;
80 $this->skin
= $wgUser->getSkin();
81 if ( !$this->request
instanceof WebRequest
) {
82 $this->request
= $GLOBALS['wgRequest'];
86 if ( $this->request
->getVal( 'action' ) == 'historysubmit' ) {
87 $this->mOldTitle
= Title
::newFromText( $this->request
->getVal( 'title' ) );
89 $this->mOldTitle
= Title
::newFromText( $this->request
->getVal( 'oldTitle' ) );
92 if ( !$this->mOldTitle
instanceof Title
) {
93 $wgOut->showErrorPage( 'revmove-badparam-title', 'revmove-badparam' );
97 $wgOut->setPagetitle( wfMsg( 'revisionmove', $this->mOldTitle
->getPrefixedText() ) );
98 $oldTitleLink = $this->skin
->link( $this->mOldTitle
);
99 $wgOut->setSubtitle( wfMsg( 'revisionmove-backlink', $oldTitleLink ) );
101 $this->mReason
= $this->request
->getText( 'wpReason' );
103 # TODO maybe not needed here? Copied from SpecialRevisiondelete.php.
104 # Keep for now, allow different inputs
105 # Handle our many different possible input types for ids
106 $ids = $this->request
->getVal( 'ids' );
107 if ( !is_null( $ids ) ) {
108 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
109 $this->mIds
= explode( ',', $ids );
112 $this->mIds
= array_keys( $this->request
->getArray( 'ids', array() ) );
114 $this->mIds
= array_unique( array_filter( $this->mIds
) );
116 if ( is_null ( $this->mIds
) ) {
117 $wgOut->showErrorPage( 'revmove-badparam-title', 'revmove-badparam' );
120 $this->mRevlist
= new RevDel_RevisionList( $this, $this->mOldTitle
, $this->mIds
);
122 # Decide what to do: Show the form, or submit the changes
123 if ( $this->request
->wasPosted() ) {
132 * Show a list of items that we will operate on and a field for the target name
134 public function showForm() {
135 global $wgOut, $wgUser;
137 if ( !$this->mIsAllowedRevisionMove
) {
138 $permErrors = $this->mOldTitle
->getUserPermissionsErrors( 'revisionmove', $this->user
);
139 $wgOut->showPermissionsErrorPage( $permErrors, 'revisionmove' );
143 $wgOut->addWikiMsg( 'revmove-explain', $this->mOldTitle
->getPrefixedText() );
144 $listNotEmpty = $this->listItems();
145 if ( !$listNotEmpty ) {
146 return; # we're done, we already displayed an error earlier
149 $out = Xml
::openElement( 'form', array( 'method' => 'post',
150 'action' => $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) ),
151 'id' => 'mw-revmove-form' ) ) .
152 Xml
::fieldset( wfMsg( 'revmove-legend' ) ) .
153 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
154 Xml
::hidden( 'oldTitle', $this->mOldTitle
->getPrefixedText() ) .
155 '<div>' . Xml
::inputLabel( wfMsg( 'revmove-reasonfield' ), 'wpReason', 'revmove-reasonfield', 60 ) . '</div>' .
156 Xml
::inputLabel( wfMsg( 'revmove-titlefield' ), 'newTitle', 'revmove-titlefield', 20, $this->mOldTitle
->getPrefixedText() ) .
157 Xml
::hidden( 'ids', implode( ',', $this->mIds
) ) .
158 Xml
::submitButton( wfMsg( 'revmove-submit' ),
159 array( 'name' => 'wpSubmit' ) ) .
160 Xml
::closeElement( 'fieldset' ) . "\n" .
161 Xml
::closeElement( 'form' ) . "\n";
162 $wgOut->addHTML( $out );
166 * Show a list of selected revisions and check the input
168 protected function listItems() {
171 $wgOut->addHTML( "<ul>" );
175 # No revisions specified at all
176 if ( $this->mIds
== array() ) {
177 $wgOut->showErrorPage( 'revmove-norevisions-title', 'revmove-norevisions' );
181 for ( $this->mRevlist
->reset(); $this->mRevlist
->current(); $this->mRevlist
->next() ) {
182 $item = $this->mRevlist
->current();
184 $wgOut->addHTML( $item->getHTML() );
187 # No valid revisions specified (e.g. only revs belonging to another page)
188 if( $numRevisions == 0 ) {
189 $wgOut->showErrorPage( 'revmove-norevisions-title', 'revmove-norevisions' );
193 $wgOut->addHTML( "</ul>" );
198 * Submit the posted changes (in $this->request).
200 * This function does some checks and then calls moveRevisions(), which does the real work
202 public function submit() {
203 global $wgUser, $wgOut;
205 # Confirm permissions
206 if ( !$this->mIsAllowedRevisionMove
) {
207 $permErrors = $this->mOldTitle
->getUserPermissionsErrors( 'revisionmove', $this->user
);
208 $wgOut->showPermissionsErrorPage( $permErrors, 'revisionmove' );
212 if( !$wgUser->matchEditToken( $this->request
->getVal( 'wpEditToken' ) ) ) {
213 $wgOut->showErrorPage( 'sessionfailure-title', 'sessionfailure' );
217 $this->mNewTitle
= Title
::newFromText( $this->request
->getVal( 'newTitle' ) );
218 if ( !$this->mNewTitle
instanceof Title
) {
219 $wgOut->showErrorPage( 'badtitle', 'badtitletext' );
223 if ( $this->mNewTitle
->getPrefixedText() == $this->mOldTitle
->getPrefixedText() ) {
224 $pagename = array( $this->mOldTitle
->getPrefixedText() );
225 $wgOut->showErrorPage( 'revmove-nullmove-title', 'revmove-nullmove', $pagename );
229 $this->moveRevisions();
233 * This function actually move the revision. NEVER call this function, call submit()
235 protected function moveRevisions() {
238 $oldArticle = new Article( $this->mOldTitle
);
239 $newArticle = new Article( $this->mNewTitle
);
241 $idstring = implode( ", ", $this->mIds
);
243 # Get DB connection and begin transaction
244 $dbw = wfGetDB( DB_MASTER
);
247 # Check if the target exists. If not, try creating it
248 if ( !$this->mNewTitle
->exists() ) {
249 $newArticle->insertOn( $dbw );
250 $this->createArticle
= true;
252 $this->createArticle
= false;
255 # This is where the magic happens:
256 # Update revision table
257 $dbw->update( 'revision',
258 array( 'rev_page' => $this->mNewTitle
->getArticleID() ),
260 'rev_id IN (' . $idstring . ')',
261 'rev_page' => $this->mOldTitle
->getArticleID(),
265 $modifiedRevsNum = $dbw->affectedRows();
267 # Check if we need to update page_latest
268 # Get the latest version of the revisions we are moving
269 $timestampNewPage = $this->queryLatestTimestamp(
271 $this->mNewTitle
->getArticleID(),
272 array( 'rev_id IN (' . $idstring . ')' )
275 # Compare the new page's page_latest against db query.
276 # If we create a new page, we have to update anyway
278 $currentNewPageRev = Revision
::newFromId( $this->mNewTitle
->getLatestRevID() );
279 if ( $this->createArticle ||
$timestampNewPage > $currentNewPageRev->getTimestamp() ) {
280 # we have to set page_latest to $timestampNewPage's revid
281 $this->updatePageLatest(
286 array( 'rev_id IN (' . $idstring . ')' )
290 # Update the old page's page_latest field
291 $timestampOldPage = $this->queryLatestTimestamp(
293 $this->mOldTitle
->getArticleID()
296 # If the timestamp is null that means the page doesn't have
297 # any revisions associated and should be deleted. In other words,
298 # someone misused revisionmove for the normal move function.
299 if ( is_null( $timestampOldPage ) ) {
302 array( 'page_id = ' . $this->mOldTitle
->getArticleID() ),
305 $deletedOldPage = true;
307 # page_latest has to be updated
308 $currentOldPageRev = Revision
::newFromId( $this->mOldTitle
->getLatestRevID() );
309 if ( $timestampOldPage < $currentOldPageRev->getTimestamp() ) {
310 $this->updatePageLatest(
317 # Purge the old one only if it hasn't been deleted
318 $oldArticle->doPurge();
324 $this->logMove( $modifiedRevsNum );
327 $newArticle->doPurge();
329 # If noting went wrong (i.e. returned), we are successful
330 $this->showSuccess( $modifiedRevsNum );
334 * Query for the latest timestamp in order to update page_latest and
336 * @param &$dbw Database object (Master)
337 * @param $articleId Integer page_id
338 * @param $conds array database conditions
340 * @return String timestamp
342 protected function queryLatestTimestamp( &$dbw, $articleId, $conds = array() ) {
343 $timestampNewRow = $dbw->selectRow(
345 'max(rev_timestamp) as maxtime',
346 array_merge( array( 'rev_page' => $articleId ), $conds ),
349 return $timestampNewRow->maxtime
;
353 * Updates page_latest and similar database fields (see Article::updateRevisionOn).
354 * Called two times, for the new and the old page
356 * @param &$dbw Database object (Master)
357 * @param $articleTitle Title object of the page
358 * @param $articleObj Article object of the page
359 * @param $timestamp to search for (use queryLatestTimestamp to get the latest)
360 * @param $conds array database conditions
362 * @return boolean indicating success
364 protected function updatePageLatest( &$dbw, $articleTitle, &$articleObj, $timestamp, $conds = array() ) {
365 # Query to find out the rev_id
366 $revisionRow = $dbw->selectRow(
370 'rev_timestamp' => $timestamp,
371 'rev_page' => $articleTitle->getArticleID(),
376 $latestRev = Revision
::newFromId( $revisionRow->rev_id
);
377 return $articleObj->updateRevisionOn( $dbw, $latestRev, $articleTitle->getLatestRevID(), null, /* set new page flag */ true );
381 * Add a log entry for the revision move
383 protected function logMove( $modifiedRevsNum ) {
385 $this->mNewTitle
->getPrefixedText(),
388 $log = new LogPage( 'move' );
389 $log->addEntry( 'move_rev', $this->mOldTitle
, $this->mReason
, $paramArray, $this->user
);
393 protected function showSuccess( $modifiedRevsNum ) {
396 if ( $this->createArticle
) {
397 $wgOut->addWikiMsg( 'revmove-success-created', $modifiedRevsNum,
398 $this->mOldTitle
->getPrefixedText(), $this->mNewTitle
->getPrefixedText() );
400 $wgOut->addWikiMsg( 'revmove-success-existing', $modifiedRevsNum,
401 $this->mOldTitle
->getPrefixedText(), $this->mNewTitle
->getPrefixedText() );