Follow-up r84814: revert redundant summary message addition.
[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',
155 'revmove-reasonfield', 60 ) . '</div>' .
156 Xml::inputLabel( wfMsg( 'revmove-titlefield' ), 'newTitle', 'revmove-titlefield', 20,
157 $this->mOldTitle->getPrefixedText() ) .
158 Html::hidden( 'ids', implode( ',', $this->mIds ) ) .
159 Xml::submitButton( wfMsg( 'revmove-submit' ),
160 array( 'name' => 'wpSubmit' ) ) .
161 Xml::closeElement( 'fieldset' ) . "\n" .
162 Xml::closeElement( 'form' ) . "\n";
163 $wgOut->addHTML( $out );
164 }
165
166 /**
167 * Show a list of selected revisions and check the input
168 */
169 protected function listItems() {
170 global $wgOut;
171
172 $wgOut->addHTML( "<ul>" );
173
174 $numRevisions = 0;
175
176 # No revisions specified at all
177 if ( $this->mIds == array() ) {
178 $wgOut->showErrorPage( 'revmove-norevisions-title', 'revmove-norevisions' );
179 return false;
180 }
181
182 for ( $this->mRevlist->reset(); $this->mRevlist->current(); $this->mRevlist->next() ) {
183 $item = $this->mRevlist->current();
184 $numRevisions++;
185 $wgOut->addHTML( $item->getHTML() );
186 }
187
188 # No valid revisions specified (e.g. only revs belonging to another page)
189 if ( $numRevisions == 0 ) {
190 $wgOut->showErrorPage( 'revmove-norevisions-title', 'revmove-norevisions' );
191 return false;
192 }
193
194 $wgOut->addHTML( "</ul>" );
195
196 return true;
197 }
198
199 /**
200 * Submit the posted changes (in $this->request).
201 *
202 * This function does some checks and then calls moveRevisions(), which does the real work
203 */
204 public function submit() {
205 global $wgUser, $wgOut;
206
207 # Confirm permissions
208 if ( !$this->mIsAllowedRevisionMove ) {
209 $permErrors = $this->mOldTitle->getUserPermissionsErrors( 'revisionmove', $this->user );
210 $wgOut->showPermissionsErrorPage( $permErrors, 'revisionmove' );
211 return false;
212 }
213
214 # Confirm Token
215 if ( !$wgUser->matchEditToken( $this->request->getVal( 'wpEditToken' ) ) ) {
216 $wgOut->showErrorPage( 'sessionfailure-title', 'sessionfailure' );
217 return false;
218 }
219
220 $this->mNewTitle = Title::newFromText( $this->request->getVal( 'newTitle' ) );
221 if ( !$this->mNewTitle instanceof Title ) {
222 $wgOut->showErrorPage( 'badtitle', 'badtitletext' );
223 return false;
224 }
225
226 if ( $this->mNewTitle->getPrefixedText() == $this->mOldTitle->getPrefixedText() ) {
227 $pagename = array( $this->mOldTitle->getPrefixedText() );
228 $wgOut->showErrorPage( 'revmove-nullmove-title', 'revmove-nullmove', $pagename );
229 return;
230 }
231
232 $this->moveRevisions();
233 }
234
235 /**
236 * This function actually move the revision. NEVER call this function, call submit()
237 */
238 protected function moveRevisions() {
239 $oldArticle = new Article( $this->mOldTitle );
240 $newArticle = new Article( $this->mNewTitle );
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' => $this->mIds,
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 $this->mNewTitle->getArticleID(),
270 array( 'rev_id' => $this->mIds )
271 );
272
273 # Compare the new page's page_latest against db query.
274 # If we create a new page, we have to update anyway
275
276 $currentNewPageRev = Revision::newFromId( $this->mNewTitle->getLatestRevID() );
277 if ( $this->createArticle || $timestampNewPage > $currentNewPageRev->getTimestamp() ) {
278 # we have to set page_latest to $timestampNewPage's revid
279 $this->updatePageLatest(
280 $this->mNewTitle,
281 $newArticle,
282 $timestampNewPage,
283 array( 'rev_id' => $this->mIds )
284 );
285 }
286
287 # Update the old page's page_latest field
288 $timestampOldPage = $this->queryLatestTimestamp(
289 $this->mOldTitle->getArticleID()
290 );
291
292 # If the timestamp is null that means the page doesn't have
293 # any revisions associated and should be deleted. In other words,
294 # someone misused revisionmove for the normal move function.
295 if ( is_null( $timestampOldPage ) ) {
296 $dbw->delete(
297 'page',
298 array( 'page_id' => $this->mOldTitle->getArticleID() ),
299 __METHOD__
300 );
301 } else {
302 # page_latest has to be updated
303 $currentOldPageRev = Revision::newFromId( $this->mOldTitle->getLatestRevID() );
304 if ( $timestampOldPage < $currentOldPageRev->getTimestamp() ) {
305 $this->updatePageLatest(
306 $this->mOldTitle,
307 $oldArticle,
308 $timestampOldPage
309 );
310 }
311 # Purge the old one only if it hasn't been deleted
312 $oldArticle->doPurge();
313 }
314
315 # All done, commit
316 $dbw->commit();
317
318 $this->logMove( $modifiedRevsNum );
319
320 # Purge new article
321 $newArticle->doPurge();
322
323 # If noting went wrong (i.e. returned), we are successful
324 $this->showSuccess( $modifiedRevsNum );
325 }
326
327 /**
328 * Query for the latest timestamp in order to update page_latest and
329 * page_timestamp.
330 * @param $articleId Integer page_id
331 * @param $conds array database conditions
332 *
333 * @return String timestamp
334 */
335 protected function queryLatestTimestamp( $articleId, $conds = array() ) {
336 $dbw = wfGetDB( DB_MASTER );
337 $timestampNewRow = $dbw->selectRow(
338 'revision',
339 'max(rev_timestamp) AS maxtime',
340 array_merge( array( 'rev_page' => $articleId ), $conds ),
341 __METHOD__
342 );
343 return $timestampNewRow->maxtime;
344 }
345
346 /**
347 * Updates page_latest and similar database fields (see Article::updateRevisionOn).
348 * Called two times, for the new and the old page
349 *
350 * @param $articleTitle Title object of the page
351 * @param $articleObj Article object of the page
352 * @param $timestamp to search for (use queryLatestTimestamp to get the latest)
353 * @param $conds array database conditions
354 *
355 * @return boolean indicating success
356 */
357 protected function updatePageLatest( $articleTitle, $articleObj, $timestamp, $conds = array() ) {
358 $dbw = wfGetDB( DB_MASTER );
359 # Query to find out the rev_id
360 $revisionRow = $dbw->selectRow(
361 'revision',
362 'rev_id',
363 array_merge( array(
364 'rev_timestamp' => $timestamp,
365 'rev_page' => $articleTitle->getArticleID(),
366 ), $conds ),
367 __METHOD__
368 );
369
370 # Update page_latest
371 $latestRev = Revision::newFromId( $revisionRow->rev_id );
372 return $articleObj->updateRevisionOn( $dbw, $latestRev, $articleTitle->getLatestRevID(), null,
373 /* set new page flag */ true );
374 }
375
376 /**
377 * Add a log entry for the revision move
378 */
379 protected function logMove( $modifiedRevsNum ) {
380 $paramArray = array(
381 $this->mNewTitle->getPrefixedText(),
382 $modifiedRevsNum
383 );
384
385 $log = new LogPage( 'move' );
386 $log->addEntry( 'move_rev', $this->mOldTitle, $this->mReason, $paramArray, $this->user );
387 }
388
389 protected function showSuccess( $modifiedRevsNum ) {
390 global $wgOut;
391
392 if ( $this->createArticle ) {
393 $wgOut->addWikiMsg( 'revmove-success-created', $modifiedRevsNum,
394 $this->mOldTitle->getPrefixedText(), $this->mNewTitle->getPrefixedText() );
395 } else {
396 $wgOut->addWikiMsg( 'revmove-success-existing', $modifiedRevsNum,
397 $this->mOldTitle->getPrefixedText(), $this->mNewTitle->getPrefixedText() );
398 }
399 }
400 }