Correct the address of the FSF in some of the GPL headers
[lhc/web/wiklou.git] / includes / specials / SpecialRevisionMove.php
1 <?php
2 /**
3 *
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.
8 *
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.
13 *
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
18 */
19
20 /**
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)
23 *
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.
32 *
33 * **** NOTE: This feature is EXPERIMENTAL. ****
34 * **** Do not use on any productive system. ****
35 *
36 * @file
37 * @ingroup SpecialPage
38 */
39
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).
45 */
46
47 class SpecialRevisionMove extends UnlistedSpecialPage {
48
49 # common objects
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
55
56 # variables
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;
62
63 public function __construct( $name = 'RevisionMove' ) {
64 parent::__construct( $name );
65 }
66
67 /**
68 * @param $par subpage part, standard special page parameter, is ignored here
69 *
70 * Mostly initializes variables and calls either showForm() or submit()
71 */
72 public function execute( $par ) {
73 global $wgUser, $wgOut, $wgSkin;
74
75 $this->setHeaders();
76 $this->outputHeader();
77
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'];
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 /**
132 * Show a list of items that we will operate on and a field for the target name
133 */
134 public function showForm() {
135 global $wgOut, $wgUser;
136
137 if ( !$this->mIsAllowedRevisionMove ) {
138 $permErrors = $this->mOldTitle->getUserPermissionsErrors( 'revisionmove', $this->user );
139 $wgOut->showPermissionsErrorPage( $permErrors, 'revisionmove' );
140 return false;
141 }
142
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
147 }
148
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 );
163 }
164
165 /**
166 * Show a list of selected revisions and check the input
167 */
168 protected function listItems() {
169 global $wgOut;
170
171 $wgOut->addHTML( "<ul>" );
172
173 $numRevisions = 0;
174
175 # No revisions specified at all
176 if ( $this->mIds == array() ) {
177 $wgOut->showErrorPage( 'revmove-norevisions-title', 'revmove-norevisions' );
178 return false;
179 }
180
181 for ( $this->mRevlist->reset(); $this->mRevlist->current(); $this->mRevlist->next() ) {
182 $item = $this->mRevlist->current();
183 $numRevisions++;
184 $wgOut->addHTML( $item->getHTML() );
185 }
186
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' );
190 return false;
191 }
192
193 $wgOut->addHTML( "</ul>" );
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 # Confirm Token
212 if( !$wgUser->matchEditToken( $this->request->getVal( 'wpEditToken' ) ) ) {
213 $wgOut->showErrorPage( 'sessionfailure-title', 'sessionfailure' );
214 return false;
215 }
216
217 $this->mNewTitle = Title::newFromText( $this->request->getVal( 'newTitle' ) );
218 if ( !$this->mNewTitle instanceof Title ) {
219 $wgOut->showErrorPage( 'badtitle', 'badtitletext' );
220 return false;
221 }
222
223 if ( $this->mNewTitle->getPrefixedText() == $this->mOldTitle->getPrefixedText() ) {
224 $pagename = array( $this->mOldTitle->getPrefixedText() );
225 $wgOut->showErrorPage( 'revmove-nullmove-title', 'revmove-nullmove', $pagename );
226 return;
227 }
228
229 $this->moveRevisions();
230 }
231
232 /**
233 * This function actually move the revision. NEVER call this function, call submit()
234 */
235 protected function moveRevisions() {
236 global $wgOut;
237
238 $oldArticle = new Article( $this->mOldTitle );
239 $newArticle = new Article( $this->mNewTitle );
240
241 $idstring = implode( ", ", $this->mIds );
242
243 # Get DB connection and begin transaction
244 $dbw = wfGetDB( DB_MASTER );
245 $dbw->begin();
246
247 # Check if the target exists. If not, try creating it
248 if ( !$this->mNewTitle->exists() ) {
249 $newArticle->insertOn( $dbw );
250 $this->createArticle = true;
251 } else {
252 $this->createArticle = false;
253 }
254
255 # This is where the magic happens:
256 # Update revision table
257 $dbw->update( 'revision',
258 array( 'rev_page' => $this->mNewTitle->getArticleID() ),
259 array(
260 'rev_id IN (' . $idstring . ')',
261 'rev_page' => $this->mOldTitle->getArticleID(),
262 ),
263 __METHOD__
264 );
265 $modifiedRevsNum = $dbw->affectedRows();
266
267 # Check if we need to update page_latest
268 # Get the latest version of the revisions we are moving
269 $timestampNewPage = $this->queryLatestTimestamp(
270 $dbw,
271 $this->mNewTitle->getArticleID(),
272 array( 'rev_id IN (' . $idstring . ')' )
273 );
274
275 # Compare the new page's page_latest against db query.
276 # If we create a new page, we have to update anyway
277
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(
282 $dbw,
283 $this->mNewTitle,
284 $newArticle,
285 $timestampNewPage,
286 array( 'rev_id IN (' . $idstring . ')' )
287 );
288 }
289
290 # Update the old page's page_latest field
291 $timestampOldPage = $this->queryLatestTimestamp(
292 $dbw,
293 $this->mOldTitle->getArticleID()
294 );
295
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 ) ) {
300 $dbw->delete(
301 'page',
302 array( 'page_id = ' . $this->mOldTitle->getArticleID() ),
303 __METHOD__
304 );
305 $deletedOldPage = true;
306 } else {
307 # page_latest has to be updated
308 $currentOldPageRev = Revision::newFromId( $this->mOldTitle->getLatestRevID() );
309 if ( $timestampOldPage < $currentOldPageRev->getTimestamp() ) {
310 $this->updatePageLatest(
311 $dbw,
312 $this->mOldTitle,
313 $oldArticle,
314 $timestampOldPage
315 );
316 }
317 # Purge the old one only if it hasn't been deleted
318 $oldArticle->doPurge();
319 }
320
321 # All done, commit
322 $dbw->commit();
323
324 $this->logMove( $modifiedRevsNum );
325
326 # Purge new article
327 $newArticle->doPurge();
328
329 # If noting went wrong (i.e. returned), we are successful
330 $this->showSuccess( $modifiedRevsNum );
331 }
332
333 /**
334 * Query for the latest timestamp in order to update page_latest and
335 * page_timestamp.
336 * @param &$dbw Database object (Master)
337 * @param $articleId Integer page_id
338 * @param $conds array database conditions
339 *
340 * @return String timestamp
341 */
342 protected function queryLatestTimestamp( &$dbw, $articleId, $conds = array() ) {
343 $timestampNewRow = $dbw->selectRow(
344 'revision',
345 'max(rev_timestamp) as maxtime',
346 array_merge( array( 'rev_page' => $articleId ), $conds ),
347 __METHOD__
348 );
349 return $timestampNewRow->maxtime;
350 }
351
352 /**
353 * Updates page_latest and similar database fields (see Article::updateRevisionOn).
354 * Called two times, for the new and the old page
355 *
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
361 *
362 * @return boolean indicating success
363 */
364 protected function updatePageLatest( &$dbw, $articleTitle, &$articleObj, $timestamp, $conds = array() ) {
365 # Query to find out the rev_id
366 $revisionRow = $dbw->selectRow(
367 'revision',
368 'rev_id',
369 array_merge( array(
370 'rev_timestamp' => $timestamp,
371 'rev_page' => $articleTitle->getArticleID(),
372 ), $conds ),
373 __METHOD__
374 );
375 # Update page_latest
376 $latestRev = Revision::newFromId( $revisionRow->rev_id );
377 return $articleObj->updateRevisionOn( $dbw, $latestRev, $articleTitle->getLatestRevID(), null, /* set new page flag */ true );
378 }
379
380 /**
381 * Add a log entry for the revision move
382 */
383 protected function logMove( $modifiedRevsNum ) {
384 $paramArray = array(
385 $this->mNewTitle->getPrefixedText(),
386 $modifiedRevsNum
387 );
388 $log = new LogPage( 'move' );
389 $log->addEntry( 'move_rev', $this->mOldTitle, $this->mReason, $paramArray, $this->user );
390
391 }
392
393 protected function showSuccess( $modifiedRevsNum ) {
394 global $wgOut;
395
396 if ( $this->createArticle ) {
397 $wgOut->addWikiMsg( 'revmove-success-created', $modifiedRevsNum,
398 $this->mOldTitle->getPrefixedText(), $this->mNewTitle->getPrefixedText() );
399 } else {
400 $wgOut->addWikiMsg( 'revmove-success-existing', $modifiedRevsNum,
401 $this->mOldTitle->getPrefixedText(), $this->mNewTitle->getPrefixedText() );
402 }
403 }
404 }