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