Follow-up r79964: Use the existing message 'parentheses' instead of introducing an...
[lhc/web/wiklou.git] / includes / specials / SpecialRevisionMove.php
1 <?php
2 /**
3 * Implements Special:RevisionMove
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Special page allowing users with the appropriate permissions to
26 * move revisions of a page to a new target (either an existing page or not)
27 *
28 * The user selects revisions in the page history (HistoryPage.php),
29 * clicks on the submit button and gets this special page.
30 * A form is shown (showForm()) where the user has to enter a target page
31 * name and confirm the action with a post request & edit token.
32 * Then submit() is called, which does some checks and calls moveRevisions().
33 * If the target doesn't exist, a new page gets created. rev_page of the
34 * selected revisions is updated, after that it is determined whether page_latest
35 * of the target page and the source page require an update.
36 *
37 * **** NOTE: This feature is EXPERIMENTAL. ****
38 * **** Do not use on any productive system. ****
39 *
40 * @ingroup SpecialPage
41 * @todo In case page_deleted gets introduced some day, use it.
42 * Currently it is possible with RevisionMove to make the latest revision
43 * of a page a RevisionDeleted one. When that happens, the user is presented
44 * an empty page with no error message whatsoever (in case he is not permitted
45 * to view deleted edits).
46 */
47 class SpecialRevisionMove extends UnlistedSpecialPage {
48 # common objects
49 var $mOldTitle; # Title object.
50 var $mNewTitle; # Title object. Desired new title
51 var $request; # WebRequest object, $wgRequest by default
52 var $skin; # Skin object
53 var $user; # User object
54
55 # variables
56 var $mIds; # Array of Ids to look at
57 var $mRevlist; # RevDel_RevisionList object - borrowed from RevisionDelete
58 var $mReason; # User-supplied reason for performing the move operation
59 var $mSubmit; # Boolean: Is this a submitted request?
60 var $mIsAllowedRevisionMove = false;
61
62 public function __construct( $name = 'RevisionMove' ) {
63 parent::__construct( $name );
64 }
65
66 /**
67 * @param $par subpage part, standard special page parameter, is ignored here
68 *
69 * Mostly initializes variables and calls either showForm() or submit()
70 */
71 public function execute( $par ) {
72 global $wgUser, $wgOut;
73
74 $this->setHeaders();
75 $this->outputHeader();
76
77 $this->mIsAllowedRevisionMove = $wgUser->isAllowed( 'revisionmove' );
78 $this->user = $wgUser;
79 $this->skin = $wgUser->getSkin();
80
81 if ( !$this->request instanceof WebRequest ) {
82 $this->request = $GLOBALS['wgRequest'];
83 }
84
85 # Get correct title
86 if ( $this->request->getVal( 'action' ) == 'historysubmit' ) {
87 $this->mOldTitle = Title::newFromText( $this->request->getVal( 'title' ) );
88 } else {
89 $this->mOldTitle = Title::newFromText( $this->request->getVal( 'oldTitle' ) );
90 }
91
92 if ( !$this->mOldTitle instanceof Title ) {
93 $wgOut->showErrorPage( 'revmove-badparam-title', 'revmove-badparam' );
94 return;
95 }
96
97 $wgOut->setPagetitle( wfMsg( 'revisionmove', $this->mOldTitle->getPrefixedText() ) );
98 $oldTitleLink = $this->skin->link( $this->mOldTitle );
99 $wgOut->setSubtitle( wfMsg( 'revisionmove-backlink', $oldTitleLink ) );
100
101 $this->mReason = $this->request->getText( 'wpReason' );
102
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 );
110 } else {
111 # Array input
112 $this->mIds = array_keys( $this->request->getArray( 'ids', array() ) );
113 }
114 $this->mIds = array_unique( array_filter( $this->mIds ) );
115
116 if ( is_null ( $this->mIds ) ) {
117 $wgOut->showErrorPage( 'revmove-badparam-title', 'revmove-badparam' );
118 return;
119 }
120 $this->mRevlist = new RevDel_RevisionList( $this, $this->mOldTitle, $this->mIds );
121
122 # Decide what to do: Show the form, or submit the changes
123 if ( $this->request->wasPosted() ) {
124 $this->submit();
125 } else {
126 $this->showForm();
127 }
128 }
129
130 /**
131 * Show a list of items that we will operate on and a field for the target name
132 */
133 public function showForm() {
134 global $wgOut, $wgUser;
135
136 if ( !$this->mIsAllowedRevisionMove ) {
137 $permErrors = $this->mOldTitle->getUserPermissionsErrors( 'revisionmove', $this->user );
138 $wgOut->showPermissionsErrorPage( $permErrors, 'revisionmove' );
139 return false;
140 }
141
142 $wgOut->addWikiMsg( 'revmove-explain', $this->mOldTitle->getPrefixedText() );
143 $listNotEmpty = $this->listItems();
144 if ( !$listNotEmpty ) {
145 return; # we're done, we already displayed an error earlier
146 }
147
148 $out = Xml::openElement( 'form', array( 'method' => 'post',
149 'action' => $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) ),
150 'id' => 'mw-revmove-form' ) ) .
151 Xml::fieldset( wfMsg( 'revmove-legend' ) ) .
152 Html::hidden( 'wpEditToken', $wgUser->editToken() ) .
153 Html::hidden( 'oldTitle', $this->mOldTitle->getPrefixedText() ) .
154 '<div>' . Xml::inputLabel( wfMsg( 'revmove-reasonfield' ), 'wpReason', 'revmove-reasonfield', 60 ) . '</div>' .
155 Xml::inputLabel( wfMsg( 'revmove-titlefield' ), 'newTitle', 'revmove-titlefield', 20, $this->mOldTitle->getPrefixedText() ) .
156 Html::hidden( 'ids', implode( ',', $this->mIds ) ) .
157 Xml::submitButton( wfMsg( 'revmove-submit' ),
158 array( 'name' => 'wpSubmit' ) ) .
159 Xml::closeElement( 'fieldset' ) . "\n" .
160 Xml::closeElement( 'form' ) . "\n";
161 $wgOut->addHTML( $out );
162 }
163
164 /**
165 * Show a list of selected revisions and check the input
166 */
167 protected function listItems() {
168 global $wgOut;
169
170 $wgOut->addHTML( "<ul>" );
171
172 $numRevisions = 0;
173
174 # No revisions specified at all
175 if ( $this->mIds == array() ) {
176 $wgOut->showErrorPage( 'revmove-norevisions-title', 'revmove-norevisions' );
177 return false;
178 }
179
180 for ( $this->mRevlist->reset(); $this->mRevlist->current(); $this->mRevlist->next() ) {
181 $item = $this->mRevlist->current();
182 $numRevisions++;
183 $wgOut->addHTML( $item->getHTML() );
184 }
185
186 # No valid revisions specified (e.g. only revs belonging to another page)
187 if ( $numRevisions == 0 ) {
188 $wgOut->showErrorPage( 'revmove-norevisions-title', 'revmove-norevisions' );
189 return false;
190 }
191
192 $wgOut->addHTML( "</ul>" );
193
194 return true;
195 }
196
197 /**
198 * Submit the posted changes (in $this->request).
199 *
200 * This function does some checks and then calls moveRevisions(), which does the real work
201 */
202 public function submit() {
203 global $wgUser, $wgOut;
204
205 # Confirm permissions
206 if ( !$this->mIsAllowedRevisionMove ) {
207 $permErrors = $this->mOldTitle->getUserPermissionsErrors( 'revisionmove', $this->user );
208 $wgOut->showPermissionsErrorPage( $permErrors, 'revisionmove' );
209 return false;
210 }
211
212 # Confirm Token
213 if ( !$wgUser->matchEditToken( $this->request->getVal( 'wpEditToken' ) ) ) {
214 $wgOut->showErrorPage( 'sessionfailure-title', 'sessionfailure' );
215 return false;
216 }
217
218 $this->mNewTitle = Title::newFromText( $this->request->getVal( 'newTitle' ) );
219 if ( !$this->mNewTitle instanceof Title ) {
220 $wgOut->showErrorPage( 'badtitle', 'badtitletext' );
221 return false;
222 }
223
224 if ( $this->mNewTitle->getPrefixedText() == $this->mOldTitle->getPrefixedText() ) {
225 $pagename = array( $this->mOldTitle->getPrefixedText() );
226 $wgOut->showErrorPage( 'revmove-nullmove-title', 'revmove-nullmove', $pagename );
227 return;
228 }
229
230 $this->moveRevisions();
231 }
232
233 /**
234 * This function actually move the revision. NEVER call this function, call submit()
235 */
236 protected function moveRevisions() {
237 $oldArticle = new Article( $this->mOldTitle );
238 $newArticle = new Article( $this->mNewTitle );
239
240 $idstring = implode( ", ", $this->mIds );
241
242 # Get DB connection and begin transaction
243 $dbw = wfGetDB( DB_MASTER );
244 $dbw->begin();
245
246 # Check if the target exists. If not, try creating it
247 if ( !$this->mNewTitle->exists() ) {
248 $newArticle->insertOn( $dbw );
249 $this->createArticle = true;
250 } else {
251 $this->createArticle = false;
252 }
253
254 # This is where the magic happens:
255 # Update revision table
256 $dbw->update( 'revision',
257 array( 'rev_page' => $this->mNewTitle->getArticleID() ),
258 array(
259 'rev_id IN (' . $idstring . ')',
260 'rev_page' => $this->mOldTitle->getArticleID(),
261 ),
262 __METHOD__
263 );
264 $modifiedRevsNum = $dbw->affectedRows();
265
266 # Check if we need to update page_latest
267 # Get the latest version of the revisions we are moving
268 $timestampNewPage = $this->queryLatestTimestamp(
269 $dbw,
270 $this->mNewTitle->getArticleID(),
271 array( 'rev_id IN (' . $idstring . ')' )
272 );
273
274 # Compare the new page's page_latest against db query.
275 # If we create a new page, we have to update anyway
276
277 $currentNewPageRev = Revision::newFromId( $this->mNewTitle->getLatestRevID() );
278 if ( $this->createArticle || $timestampNewPage > $currentNewPageRev->getTimestamp() ) {
279 # we have to set page_latest to $timestampNewPage's revid
280 $this->updatePageLatest(
281 $dbw,
282 $this->mNewTitle,
283 $newArticle,
284 $timestampNewPage,
285 array( 'rev_id IN (' . $idstring . ')' )
286 );
287 }
288
289 # Update the old page's page_latest field
290 $timestampOldPage = $this->queryLatestTimestamp(
291 $dbw,
292 $this->mOldTitle->getArticleID()
293 );
294
295 # If the timestamp is null that means the page doesn't have
296 # any revisions associated and should be deleted. In other words,
297 # someone misused revisionmove for the normal move function.
298 if ( is_null( $timestampOldPage ) ) {
299 $dbw->delete(
300 'page',
301 array( 'page_id = ' . $this->mOldTitle->getArticleID() ),
302 __METHOD__
303 );
304 } else {
305 # page_latest has to be updated
306 $currentOldPageRev = Revision::newFromId( $this->mOldTitle->getLatestRevID() );
307 if ( $timestampOldPage < $currentOldPageRev->getTimestamp() ) {
308 $this->updatePageLatest(
309 $dbw,
310 $this->mOldTitle,
311 $oldArticle,
312 $timestampOldPage
313 );
314 }
315 # Purge the old one only if it hasn't been deleted
316 $oldArticle->doPurge();
317 }
318
319 # All done, commit
320 $dbw->commit();
321
322 $this->logMove( $modifiedRevsNum );
323
324 # Purge new article
325 $newArticle->doPurge();
326
327 # If noting went wrong (i.e. returned), we are successful
328 $this->showSuccess( $modifiedRevsNum );
329 }
330
331 /**
332 * Query for the latest timestamp in order to update page_latest and
333 * page_timestamp.
334 * @param &$dbw Database object (Master)
335 * @param $articleId Integer page_id
336 * @param $conds array database conditions
337 *
338 * @return String timestamp
339 */
340 protected function queryLatestTimestamp( &$dbw, $articleId, $conds = array() ) {
341 $timestampNewRow = $dbw->selectRow(
342 'revision',
343 'max(rev_timestamp) AS maxtime',
344 array_merge( array( 'rev_page' => $articleId ), $conds ),
345 __METHOD__
346 );
347 return $timestampNewRow->maxtime;
348 }
349
350 /**
351 * Updates page_latest and similar database fields (see Article::updateRevisionOn).
352 * Called two times, for the new and the old page
353 *
354 * @param &$dbw Database object (Master)
355 * @param $articleTitle Title object of the page
356 * @param $articleObj Article object of the page
357 * @param $timestamp to search for (use queryLatestTimestamp to get the latest)
358 * @param $conds array database conditions
359 *
360 * @return boolean indicating success
361 */
362 protected function updatePageLatest( &$dbw, $articleTitle, &$articleObj, $timestamp, $conds = array() ) {
363 # Query to find out the rev_id
364 $revisionRow = $dbw->selectRow(
365 'revision',
366 'rev_id',
367 array_merge( array(
368 'rev_timestamp' => $timestamp,
369 'rev_page' => $articleTitle->getArticleID(),
370 ), $conds ),
371 __METHOD__
372 );
373
374 # Update page_latest
375 $latestRev = Revision::newFromId( $revisionRow->rev_id );
376 return $articleObj->updateRevisionOn( $dbw, $latestRev, $articleTitle->getLatestRevID(), null, /* set new page flag */ true );
377 }
378
379 /**
380 * Add a log entry for the revision move
381 */
382 protected function logMove( $modifiedRevsNum ) {
383 $paramArray = array(
384 $this->mNewTitle->getPrefixedText(),
385 $modifiedRevsNum
386 );
387
388 $log = new LogPage( 'move' );
389 $log->addEntry( 'move_rev', $this->mOldTitle, $this->mReason, $paramArray, $this->user );
390 }
391
392 protected function showSuccess( $modifiedRevsNum ) {
393 global $wgOut;
394
395 if ( $this->createArticle ) {
396 $wgOut->addWikiMsg( 'revmove-success-created', $modifiedRevsNum,
397 $this->mOldTitle->getPrefixedText(), $this->mNewTitle->getPrefixedText() );
398 } else {
399 $wgOut->addWikiMsg( 'revmove-success-existing', $modifiedRevsNum,
400 $this->mOldTitle->getPrefixedText(), $this->mNewTitle->getPrefixedText() );
401 }
402 }
403 }