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