fix E_STRICT
[lhc/web/wiklou.git] / includes / specials / SpecialRevisionMove.php
1 <?php
2 /**
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)
5 *
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.
14 *
15 * **** NOTE: This feature is EXPERIMENTAL. ****
16 * **** Do not use on any productive system. ****
17 *
18 * @file
19 * @ingroup SpecialPage
20 */
21
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).
27 */
28
29 class SpecialRevisionMove extends UnlistedSpecialPage {
30
31 # common objects
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
37
38 # variables
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;
44
45 public function __construct( $name = 'RevisionMove' ) {
46 parent::__construct( $name );
47 }
48
49 /**
50 * @param $par subpage part, standard special page parameter, is ignored here
51 *
52 * Mostly initializes variables and calls either showForm() or submit()
53 */
54 public function execute( $par ) {
55 global $wgUser, $wgOut, $wgSkin;
56
57 $this->setHeaders();
58 $this->outputHeader();
59
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'];
65 }
66
67 # Get correct title
68 if ( $this->request->getVal( 'action' ) == 'historysubmit' ) {
69 $this->mOldTitle = Title::newFromText( $this->request->getVal( 'title' ) );
70 } else {
71 $this->mOldTitle = Title::newFromText( $this->request->getVal( 'oldTitle' ) );
72 }
73
74 if ( !$this->mOldTitle instanceof Title ) {
75 $wgOut->showErrorPage( 'revmove-badparam-title', 'revmove-badparam' );
76 return;
77 }
78
79 $wgOut->setPagetitle( wfMsg( 'revisionmove', $this->mOldTitle->getPrefixedText() ) );
80 $oldTitleLink = $this->skin->link( $this->mOldTitle );
81 $wgOut->setSubtitle( wfMsg( 'revisionmove-backlink', $oldTitleLink ) );
82
83 $this->mReason = $this->request->getText( 'wpReason' );
84
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 );
92 } else {
93 # Array input
94 $this->mIds = array_keys( $this->request->getArray( 'ids', array() ) );
95 }
96 $this->mIds = array_unique( array_filter( $this->mIds ) );
97
98 if ( is_null ( $this->mIds ) ) {
99 $wgOut->showErrorPage( 'revmove-badparam-title', 'revmove-badparam' );
100 return;
101 }
102 $this->mRevlist = new RevDel_RevisionList( $this, $this->mOldTitle, $this->mIds );
103
104 # Decide what to do: Show the form, or submit the changes
105 if ( $this->request->wasPosted() ) {
106 $this->submit();
107 } else {
108 $this->showForm();
109 }
110
111 }
112
113 /**
114 * Show a list of items that we will operate on and a field for the target name
115 */
116 public function showForm() {
117 global $wgOut, $wgUser;
118
119 if ( !$this->mIsAllowedRevisionMove ) {
120 $permErrors = $this->mOldTitle->getUserPermissionsErrors( 'revisionmove', $this->user );
121 $wgOut->showPermissionsErrorPage( $permErrors, 'revisionmove' );
122 return false;
123 }
124
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
129 }
130
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 );
145 }
146
147 /**
148 * Show a list of selected revisions and check the input
149 */
150 protected function listItems() {
151 global $wgOut;
152
153 $wgOut->addHTML( "<ul>" );
154
155 $numRevisions = 0;
156
157 # No revisions specified at all
158 if ( $this->mIds == array() ) {
159 $wgOut->showErrorPage( 'revmove-norevisions-title', 'revmove-norevisions' );
160 return false;
161 }
162
163 for ( $this->mRevlist->reset(); $this->mRevlist->current(); $this->mRevlist->next() ) {
164 $item = $this->mRevlist->current();
165 $numRevisions++;
166 $wgOut->addHTML( $item->getHTML() );
167 }
168
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' );
172 return false;
173 }
174
175 $wgOut->addHTML( "</ul>" );
176 return true;
177 }
178
179 /**
180 * Submit the posted changes (in $this->request).
181 *
182 * This function does some checks and then calls moveRevisions(), which does the real work
183 */
184 public function submit() {
185 global $wgUser, $wgOut;
186
187 # Confirm permissions
188 if ( !$this->mIsAllowedRevisionMove ) {
189 $permErrors = $this->mOldTitle->getUserPermissionsErrors( 'revisionmove', $this->user );
190 $wgOut->showPermissionsErrorPage( $permErrors, 'revisionmove' );
191 return false;
192 }
193 # Confirm Token
194 if( !$wgUser->matchEditToken( $this->request->getVal( 'wpEditToken' ) ) ) {
195 $wgOut->showErrorPage( 'sessionfailure-title', 'sessionfailure' );
196 return false;
197 }
198
199 $this->mNewTitle = Title::newFromText( $this->request->getVal( 'newTitle' ) );
200 if ( !$this->mNewTitle instanceof Title ) {
201 $wgOut->showErrorPage( 'badtitle', 'badtitletext' );
202 return false;
203 }
204
205 if ( $this->mNewTitle->getPrefixedText() == $this->mOldTitle->getPrefixedText() ) {
206 $pagename = array( $this->mOldTitle->getPrefixedText() );
207 $wgOut->showErrorPage( 'revmove-nullmove-title', 'revmove-nullmove', $pagename );
208 return;
209 }
210
211 $this->moveRevisions();
212 }
213
214 /**
215 * This function actually move the revision. NEVER call this function, call submit()
216 */
217 protected function moveRevisions() {
218 global $wgOut;
219
220 $oldArticle = new Article( $this->mOldTitle );
221 $newArticle = new Article( $this->mNewTitle );
222
223 $idstring = implode( ", ", $this->mIds );
224
225 # Get DB connection and begin transaction
226 $dbw = wfGetDB( DB_MASTER );
227 $dbw->begin();
228
229 # Check if the target exists. If not, try creating it
230 if ( !$this->mNewTitle->exists() ) {
231 $newArticle->insertOn( $dbw );
232 $this->createArticle = true;
233 } else {
234 $this->createArticle = false;
235 }
236
237 # This is where the magic happens:
238 # Update revision table
239 $dbw->update( 'revision',
240 array( 'rev_page' => $this->mNewTitle->getArticleID() ),
241 array(
242 'rev_id IN (' . $idstring . ')',
243 'rev_page' => $this->mOldTitle->getArticleID(),
244 ),
245 __METHOD__
246 );
247 $modifiedRevsNum = $dbw->affectedRows();
248
249 # Check if we need to update page_latest
250 # Get the latest version of the revisions we are moving
251 $timestampNewPage = $this->queryLatestTimestamp(
252 $dbw,
253 $this->mNewTitle->getArticleID(),
254 array( 'rev_id IN (' . $idstring . ')' )
255 );
256
257 # Compare the new page's page_latest against db query.
258 # If we create a new page, we have to update anyway
259
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(
264 $dbw,
265 $this->mNewTitle,
266 $newArticle,
267 $timestampNewPage,
268 array( 'rev_id IN (' . $idstring . ')' )
269 );
270 }
271
272 # Update the old page's page_latest field
273 $timestampOldPage = $this->queryLatestTimestamp(
274 $dbw,
275 $this->mOldTitle->getArticleID()
276 );
277
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 ) ) {
282 $dbw->delete(
283 'page',
284 array( 'page_id = ' . $this->mOldTitle->getArticleID() ),
285 __METHOD__
286 );
287 $deletedOldPage = true;
288 } else {
289 # page_latest has to be updated
290 $currentOldPageRev = Revision::newFromId( $this->mOldTitle->getLatestRevID() );
291 if ( $timestampOldPage < $currentOldPageRev->getTimestamp() ) {
292 $this->updatePageLatest(
293 $dbw,
294 $this->mOldTitle,
295 $oldArticle,
296 $timestampOldPage
297 );
298 }
299 # Purge the old one only if it hasn't been deleted
300 $oldArticle->doPurge();
301 }
302
303 # All done, commit
304 $dbw->commit();
305
306 $this->logMove( $modifiedRevsNum );
307
308 # Purge new article
309 $newArticle->doPurge();
310
311 # If noting went wrong (i.e. returned), we are successful
312 $this->showSuccess( $modifiedRevsNum );
313 }
314
315 /**
316 * Query for the latest timestamp in order to update page_latest and
317 * page_timestamp.
318 * @param &$dbw Database object (Master)
319 * @param $articleId Integer page_id
320 * @param $conds array database conditions
321 *
322 * @return String timestamp
323 */
324 protected function queryLatestTimestamp( &$dbw, $articleId, $conds = array() ) {
325 $timestampNewRow = $dbw->selectRow(
326 'revision',
327 'max(rev_timestamp) as maxtime',
328 array_merge( array( 'rev_page' => $articleId ), $conds ),
329 __METHOD__
330 );
331 return $timestampNewRow->maxtime;
332 }
333
334 /**
335 * Updates page_latest and similar database fields (see Article::updateRevisionOn).
336 * Called two times, for the new and the old page
337 *
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
343 *
344 * @return boolean indicating success
345 */
346 protected function updatePageLatest( &$dbw, $articleTitle, &$articleObj, $timestamp, $conds = array() ) {
347 # Query to find out the rev_id
348 $revisionRow = $dbw->selectRow(
349 'revision',
350 'rev_id',
351 array_merge( array(
352 'rev_timestamp' => $timestamp,
353 'rev_page' => $articleTitle->getArticleID(),
354 ), $conds ),
355 __METHOD__
356 );
357 # Update page_latest
358 $latestRev = Revision::newFromId( $revisionRow->rev_id );
359 return $articleObj->updateRevisionOn( $dbw, $latestRev, $articleTitle->getLatestRevID(), null, /* set new page flag */ true );
360 }
361
362 /**
363 * Add a log entry for the revision move
364 */
365 protected function logMove( $modifiedRevsNum ) {
366 $paramArray = array(
367 $this->mNewTitle->getPrefixedText(),
368 $modifiedRevsNum
369 );
370 $log = new LogPage( 'move' );
371 $log->addEntry( 'move_rev', $this->mOldTitle, $this->mReason, $paramArray, $this->user );
372
373 }
374
375 protected function showSuccess( $modifiedRevsNum ) {
376 global $wgOut;
377
378 if ( $this->createArticle ) {
379 $wgOut->addWikiMsg( 'revmove-success-created', $modifiedRevsNum,
380 $this->mOldTitle->getPrefixedText(), $this->mNewTitle->getPrefixedText() );
381 } else {
382 $wgOut->addWikiMsg( 'revmove-success-existing', $modifiedRevsNum,
383 $this->mOldTitle->getPrefixedText(), $this->mNewTitle->getPrefixedText() );
384 }
385 }
386 }