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