Param documentation updates/added
[lhc/web/wiklou.git] / includes / specials / SpecialMergeHistory.php
1 <?php
2 /**
3 * Implements Special:MergeHistory
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 * merge article histories, with some restrictions
27 *
28 * @ingroup SpecialPage
29 */
30 class SpecialMergeHistory extends SpecialPage {
31 var $mAction, $mTarget, $mDest, $mTimestamp, $mTargetID, $mDestID, $mComment;
32 var $mTargetObj, $mDestObj;
33
34 public function __construct() {
35 parent::__construct( 'MergeHistory', 'mergehistory' );
36 }
37
38 /**
39 * @param $request WebRequest
40 * @return void
41 */
42 private function loadRequestParams( $request ) {
43 global $wgUser;
44
45 $this->mAction = $request->getVal( 'action' );
46 $this->mTarget = $request->getVal( 'target' );
47 $this->mDest = $request->getVal( 'dest' );
48 $this->mSubmitted = $request->getBool( 'submitted' );
49
50 $this->mTargetID = intval( $request->getVal( 'targetID' ) );
51 $this->mDestID = intval( $request->getVal( 'destID' ) );
52 $this->mTimestamp = $request->getVal( 'mergepoint' );
53 if( !preg_match( '/[0-9]{14}/', $this->mTimestamp ) ) {
54 $this->mTimestamp = '';
55 }
56 $this->mComment = $request->getText( 'wpComment' );
57
58 $this->mMerge = $request->wasPosted() && $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
59 // target page
60 if( $this->mSubmitted ) {
61 $this->mTargetObj = Title::newFromURL( $this->mTarget );
62 $this->mDestObj = Title::newFromURL( $this->mDest );
63 } else {
64 $this->mTargetObj = null;
65 $this->mDestObj = null;
66 }
67 $this->preCacheMessages();
68 }
69
70 /**
71 * As we use the same small set of messages in various methods and that
72 * they are called often, we call them once and save them in $this->message
73 */
74 function preCacheMessages() {
75 // Precache various messages
76 if( !isset( $this->message ) ) {
77 $this->message['last'] = wfMsgExt( 'last', array( 'escape' ) );
78 }
79 }
80
81 public function execute( $par ) {
82 global $wgOut, $wgRequest, $wgUser;
83
84 if ( wfReadOnly() ) {
85 $wgOut->readOnlyPage();
86 return;
87 }
88
89 if( !$this->userCanExecute( $wgUser ) ) {
90 $this->displayRestrictionError();
91 return;
92 }
93
94 $this->loadRequestParams( $wgRequest );
95
96 $this->setHeaders();
97 $this->outputHeader();
98
99 if( $this->mTargetID && $this->mDestID && $this->mAction == 'submit' && $this->mMerge ) {
100 return $this->merge();
101 }
102
103 if ( !$this->mSubmitted ) {
104 $this->showMergeForm();
105 return;
106 }
107
108 $errors = array();
109 if ( !$this->mTargetObj instanceof Title ) {
110 $errors[] = wfMsgExt( 'mergehistory-invalid-source', array( 'parse' ) );
111 } elseif( !$this->mTargetObj->exists() ) {
112 $errors[] = wfMsgExt( 'mergehistory-no-source', array( 'parse' ),
113 wfEscapeWikiText( $this->mTargetObj->getPrefixedText() )
114 );
115 }
116
117 if ( !$this->mDestObj instanceof Title ) {
118 $errors[] = wfMsgExt( 'mergehistory-invalid-destination', array( 'parse' ) );
119 } elseif( !$this->mDestObj->exists() ) {
120 $errors[] = wfMsgExt( 'mergehistory-no-destination', array( 'parse' ),
121 wfEscapeWikiText( $this->mDestObj->getPrefixedText() )
122 );
123 }
124
125 if ( $this->mTargetObj && $this->mDestObj && $this->mTargetObj->equals( $this->mDestObj ) ) {
126 $errors[] = wfMsgExt( 'mergehistory-same-destination', array( 'parse' ) );
127 }
128
129 if ( count( $errors ) ) {
130 $this->showMergeForm();
131 $wgOut->addHTML( implode( "\n", $errors ) );
132 } else {
133 $this->showHistory();
134 }
135
136 }
137
138 function showMergeForm() {
139 global $wgOut, $wgScript;
140
141 $wgOut->addWikiMsg( 'mergehistory-header' );
142
143 $wgOut->addHTML(
144 Xml::openElement( 'form', array(
145 'method' => 'get',
146 'action' => $wgScript ) ) .
147 '<fieldset>' .
148 Xml::element( 'legend', array(),
149 wfMsg( 'mergehistory-box' ) ) .
150 Html::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
151 Html::hidden( 'submitted', '1' ) .
152 Html::hidden( 'mergepoint', $this->mTimestamp ) .
153 Xml::openElement( 'table' ) .
154 '<tr>
155 <td>' . Xml::label( wfMsg( 'mergehistory-from' ), 'target' ) . '</td>
156 <td>' . Xml::input( 'target', 30, $this->mTarget, array( 'id' => 'target' ) ) . '</td>
157 </tr><tr>
158 <td>' . Xml::label( wfMsg( 'mergehistory-into' ), 'dest' ) . '</td>
159 <td>' . Xml::input( 'dest', 30, $this->mDest, array( 'id' => 'dest' ) ) . '</td>
160 </tr><tr><td>' .
161 Xml::submitButton( wfMsg( 'mergehistory-go' ) ) .
162 '</td></tr>' .
163 Xml::closeElement( 'table' ) .
164 '</fieldset>' .
165 '</form>'
166 );
167 }
168
169 private function showHistory() {
170 global $wgUser, $wgOut;
171
172 $this->sk = $wgUser->getSkin();
173
174 $wgOut->setPageTitle( wfMsg( 'mergehistory' ) );
175
176 $this->showMergeForm();
177
178 # List all stored revisions
179 $revisions = new MergeHistoryPager(
180 $this, array(), $this->mTargetObj, $this->mDestObj
181 );
182 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
183
184 $titleObj = $this->getTitle();
185 $action = $titleObj->getLocalURL( array( 'action' => 'submit' ) );
186 # Start the form here
187 $top = Xml::openElement(
188 'form',
189 array(
190 'method' => 'post',
191 'action' => $action,
192 'id' => 'merge'
193 )
194 );
195 $wgOut->addHTML( $top );
196
197 if( $haveRevisions ) {
198 # Format the user-visible controls (comment field, submission button)
199 # in a nice little table
200 $table =
201 Xml::openElement( 'fieldset' ) .
202 wfMsgExt( 'mergehistory-merge', array( 'parseinline' ),
203 $this->mTargetObj->getPrefixedText(), $this->mDestObj->getPrefixedText() ) .
204 Xml::openElement( 'table', array( 'id' => 'mw-mergehistory-table' ) ) .
205 '<tr>
206 <td class="mw-label">' .
207 Xml::label( wfMsg( 'mergehistory-reason' ), 'wpComment' ) .
208 '</td>
209 <td class="mw-input">' .
210 Xml::input( 'wpComment', 50, $this->mComment, array( 'id' => 'wpComment' ) ) .
211 '</td>
212 </tr>
213 <tr>
214 <td>&#160;</td>
215 <td class="mw-submit">' .
216 Xml::submitButton( wfMsg( 'mergehistory-submit' ), array( 'name' => 'merge', 'id' => 'mw-merge-submit' ) ) .
217 '</td>
218 </tr>' .
219 Xml::closeElement( 'table' ) .
220 Xml::closeElement( 'fieldset' );
221
222 $wgOut->addHTML( $table );
223 }
224
225 $wgOut->addHTML(
226 '<h2 id="mw-mergehistory">' .
227 wfMsgHtml( 'mergehistory-list' ) . "</h2>\n"
228 );
229
230 if( $haveRevisions ) {
231 $wgOut->addHTML( $revisions->getNavigationBar() );
232 $wgOut->addHTML( '<ul>' );
233 $wgOut->addHTML( $revisions->getBody() );
234 $wgOut->addHTML( '</ul>' );
235 $wgOut->addHTML( $revisions->getNavigationBar() );
236 } else {
237 $wgOut->addWikiMsg( 'mergehistory-empty' );
238 }
239
240 # Show relevant lines from the deletion log:
241 $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage::logName( 'merge' ) ) . "</h2>\n" );
242 LogEventsList::showLogExtract( $wgOut, 'merge', $this->mTargetObj->getPrefixedText() );
243
244 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
245 # Such would happen if a page was renamed after the form loaded, but before submit
246 $misc = Html::hidden( 'targetID', $this->mTargetObj->getArticleID() );
247 $misc .= Html::hidden( 'destID', $this->mDestObj->getArticleID() );
248 $misc .= Html::hidden( 'target', $this->mTarget );
249 $misc .= Html::hidden( 'dest', $this->mDest );
250 $misc .= Html::hidden( 'wpEditToken', $wgUser->editToken() );
251 $misc .= Xml::closeElement( 'form' );
252 $wgOut->addHTML( $misc );
253
254 return true;
255 }
256
257 function formatRevisionRow( $row ) {
258 global $wgLang;
259
260 $rev = new Revision( $row );
261
262 $stxt = '';
263 $last = $this->message['last'];
264
265 $ts = wfTimestamp( TS_MW, $row->rev_timestamp );
266 $checkBox = Xml::radio( 'mergepoint', $ts, false );
267
268 $pageLink = $this->sk->linkKnown(
269 $rev->getTitle(),
270 htmlspecialchars( $wgLang->timeanddate( $ts ) ),
271 array(),
272 array( 'oldid' => $rev->getId() )
273 );
274 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
275 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
276 }
277
278 # Last link
279 if( !$rev->userCan( Revision::DELETED_TEXT ) ) {
280 $last = $this->message['last'];
281 } elseif( isset( $this->prevId[$row->rev_id] ) ) {
282 $last = $this->sk->linkKnown(
283 $rev->getTitle(),
284 $this->message['last'],
285 array(),
286 array(
287 'diff' => $row->rev_id,
288 'oldid' => $this->prevId[$row->rev_id]
289 )
290 );
291 }
292
293 $userLink = $this->sk->revUserTools( $rev );
294
295 $size = $row->rev_len;
296 if( !is_null( $size ) ) {
297 $stxt = $this->sk->formatRevisionSize( $size );
298 }
299 $comment = $this->sk->revComment( $rev );
300
301 return "<li>$checkBox ($last) $pageLink . . $userLink $stxt $comment</li>";
302 }
303
304 /**
305 * Fetch revision text link if it's available to all users
306 * @return string
307 */
308 function getPageLink( $row, $titleObj, $ts, $target ) {
309 global $wgLang;
310
311 if( !$this->userCan( $row, Revision::DELETED_TEXT ) ) {
312 return '<span class="history-deleted">' .
313 $wgLang->timeanddate( $ts, true ) . '</span>';
314 } else {
315 $link = $this->sk->linkKnown(
316 $titleObj,
317 $wgLang->timeanddate( $ts, true ),
318 array(),
319 array(
320 'target' => $target,
321 'timestamp' => $ts
322 )
323 );
324 if( $this->isDeleted( $row, Revision::DELETED_TEXT ) ) {
325 $link = '<span class="history-deleted">' . $link . '</span>';
326 }
327 return $link;
328 }
329 }
330
331 function merge() {
332 global $wgOut;
333 # Get the titles directly from the IDs, in case the target page params
334 # were spoofed. The queries are done based on the IDs, so it's best to
335 # keep it consistent...
336 $targetTitle = Title::newFromID( $this->mTargetID );
337 $destTitle = Title::newFromID( $this->mDestID );
338 if( is_null( $targetTitle ) || is_null( $destTitle ) ) {
339 return false; // validate these
340 }
341 if( $targetTitle->getArticleId() == $destTitle->getArticleId() ) {
342 return false;
343 }
344 # Verify that this timestamp is valid
345 # Must be older than the destination page
346 $dbw = wfGetDB( DB_MASTER );
347 # Get timestamp into DB format
348 $this->mTimestamp = $this->mTimestamp ? $dbw->timestamp( $this->mTimestamp ) : '';
349 # Max timestamp should be min of destination page
350 $maxtimestamp = $dbw->selectField(
351 'revision',
352 'MIN(rev_timestamp)',
353 array( 'rev_page' => $this->mDestID ),
354 __METHOD__
355 );
356 # Destination page must exist with revisions
357 if( !$maxtimestamp ) {
358 $wgOut->addWikiMsg( 'mergehistory-fail' );
359 return false;
360 }
361 # Get the latest timestamp of the source
362 $lasttimestamp = $dbw->selectField(
363 array( 'page', 'revision' ),
364 'rev_timestamp',
365 array( 'page_id' => $this->mTargetID, 'page_latest = rev_id' ),
366 __METHOD__
367 );
368 # $this->mTimestamp must be older than $maxtimestamp
369 if( $this->mTimestamp >= $maxtimestamp ) {
370 $wgOut->addWikiMsg( 'mergehistory-fail' );
371 return false;
372 }
373 # Update the revisions
374 if( $this->mTimestamp ) {
375 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
376 $timestampLimit = wfTimestamp( TS_MW, $this->mTimestamp );
377 } else {
378 $timewhere = "rev_timestamp <= {$maxtimestamp}";
379 $timestampLimit = wfTimestamp( TS_MW, $lasttimestamp );
380 }
381 # Do the moving...
382 $dbw->update(
383 'revision',
384 array( 'rev_page' => $this->mDestID ),
385 array( 'rev_page' => $this->mTargetID, $timewhere ),
386 __METHOD__
387 );
388
389 $count = $dbw->affectedRows();
390 # Make the source page a redirect if no revisions are left
391 $haveRevisions = $dbw->selectField(
392 'revision',
393 'rev_timestamp',
394 array( 'rev_page' => $this->mTargetID ),
395 __METHOD__,
396 array( 'FOR UPDATE' )
397 );
398 if( !$haveRevisions ) {
399 if( $this->mComment ) {
400 $comment = wfMsgForContent(
401 'mergehistory-comment',
402 $targetTitle->getPrefixedText(),
403 $destTitle->getPrefixedText(),
404 $this->mComment
405 );
406 } else {
407 $comment = wfMsgForContent(
408 'mergehistory-autocomment',
409 $targetTitle->getPrefixedText(),
410 $destTitle->getPrefixedText()
411 );
412 }
413 $mwRedir = MagicWord::get( 'redirect' );
414 $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destTitle->getPrefixedText() . "]]\n";
415 $redirectArticle = new Article( $targetTitle );
416 $redirectRevision = new Revision( array(
417 'page' => $this->mTargetID,
418 'comment' => $comment,
419 'text' => $redirectText ) );
420 $redirectRevision->insertOn( $dbw );
421 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision );
422
423 # Now, we record the link from the redirect to the new title.
424 # It should have no other outgoing links...
425 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID ), __METHOD__ );
426 $dbw->insert( 'pagelinks',
427 array(
428 'pl_from' => $this->mDestID,
429 'pl_namespace' => $destTitle->getNamespace(),
430 'pl_title' => $destTitle->getDBkey() ),
431 __METHOD__
432 );
433 } else {
434 $targetTitle->invalidateCache(); // update histories
435 }
436 $destTitle->invalidateCache(); // update histories
437 # Check if this did anything
438 if( !$count ) {
439 $wgOut->addWikiMsg( 'mergehistory-fail' );
440 return false;
441 }
442 # Update our logs
443 $log = new LogPage( 'merge' );
444 $log->addEntry(
445 'merge', $targetTitle, $this->mComment,
446 array( $destTitle->getPrefixedText(), $timestampLimit )
447 );
448
449 $wgOut->addHTML(
450 wfMsgExt( 'mergehistory-success', array('parseinline'),
451 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count ) );
452
453 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
454
455 return true;
456 }
457 }
458
459 class MergeHistoryPager extends ReverseChronologicalPager {
460 public $mForm, $mConds;
461
462 function __construct( $form, $conds = array(), $source, $dest ) {
463 $this->mForm = $form;
464 $this->mConds = $conds;
465 $this->title = $source;
466 $this->articleID = $source->getArticleID();
467
468 $dbr = wfGetDB( DB_SLAVE );
469 $maxtimestamp = $dbr->selectField(
470 'revision',
471 'MIN(rev_timestamp)',
472 array( 'rev_page' => $dest->getArticleID() ),
473 __METHOD__
474 );
475 $this->maxTimestamp = $maxtimestamp;
476
477 parent::__construct();
478 }
479
480 function getStartBody() {
481 wfProfileIn( __METHOD__ );
482 # Do a link batch query
483 $this->mResult->seek( 0 );
484 $batch = new LinkBatch();
485 # Give some pointers to make (last) links
486 $this->mForm->prevId = array();
487 foreach ( $this->mResult as $row ) {
488 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->rev_user_text ) );
489 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->rev_user_text ) );
490
491 $rev_id = isset( $rev_id ) ? $rev_id : $row->rev_id;
492 if( $rev_id > $row->rev_id ) {
493 $this->mForm->prevId[$rev_id] = $row->rev_id;
494 } elseif( $rev_id < $row->rev_id ) {
495 $this->mForm->prevId[$row->rev_id] = $rev_id;
496 }
497
498 $rev_id = $row->rev_id;
499 }
500
501 $batch->execute();
502 $this->mResult->seek( 0 );
503
504 wfProfileOut( __METHOD__ );
505 return '';
506 }
507
508 function formatRow( $row ) {
509 return $this->mForm->formatRevisionRow( $row );
510 }
511
512 function getQueryInfo() {
513 $conds = $this->mConds;
514 $conds['rev_page'] = $this->articleID;
515 $conds[] = 'page_id = rev_page';
516 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
517 return array(
518 'tables' => array( 'revision', 'page' ),
519 'fields' => array(
520 'rev_minor_edit', 'rev_timestamp', 'rev_user', 'rev_user_text',
521 'rev_comment', 'rev_id', 'rev_page', 'rev_parent_id',
522 'rev_text_id', 'rev_len', 'rev_deleted'
523 ),
524 'conds' => $conds
525 );
526 }
527
528 function getIndexField() {
529 return 'rev_timestamp';
530 }
531 }