Big oops - merged to wrong branch.
[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 deletion log:
230 $out->addHTML( '<h2>' . htmlspecialchars( LogPage::logName( 'merge' ) ) . "</h2>\n" );
231 LogEventsList::showLogExtract( $out, 'merge', $this->mTargetObj );
232
233 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
234 # Such would happen if a page was renamed after the form loaded, but before submit
235 $misc = Html::hidden( 'targetID', $this->mTargetObj->getArticleID() );
236 $misc .= Html::hidden( 'destID', $this->mDestObj->getArticleID() );
237 $misc .= Html::hidden( 'target', $this->mTarget );
238 $misc .= Html::hidden( 'dest', $this->mDest );
239 $misc .= Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
240 $misc .= Xml::closeElement( 'form' );
241 $out->addHTML( $misc );
242
243 return true;
244 }
245
246 function formatRevisionRow( $row ) {
247 $rev = new Revision( $row );
248
249 $stxt = '';
250 $last = $this->message['last'];
251
252 $ts = wfTimestamp( TS_MW, $row->rev_timestamp );
253 $checkBox = Xml::radio( 'mergepoint', $ts, false );
254
255 $pageLink = Linker::linkKnown(
256 $rev->getTitle(),
257 htmlspecialchars( $this->getLanguage()->timeanddate( $ts ) ),
258 array(),
259 array( 'oldid' => $rev->getId() )
260 );
261 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
262 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
263 }
264
265 # Last link
266 if( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
267 $last = $this->message['last'];
268 } elseif( isset( $this->prevId[$row->rev_id] ) ) {
269 $last = Linker::linkKnown(
270 $rev->getTitle(),
271 $this->message['last'],
272 array(),
273 array(
274 'diff' => $row->rev_id,
275 'oldid' => $this->prevId[$row->rev_id]
276 )
277 );
278 }
279
280 $userLink = Linker::revUserTools( $rev );
281
282 $size = $row->rev_len;
283 if( !is_null( $size ) ) {
284 $stxt = Linker::formatRevisionSize( $size );
285 }
286 $comment = Linker::revComment( $rev );
287
288 $revisionRow = $this->msg( 'mergehistory-revisionrow' )->rawParams( $checkBox, $last, $pageLink, $userLink, $stxt, $comment)->escaped();
289 $revisionRow .= "<li>" . $line . "</li>";
290
291 return $revisionRow;
292 }
293
294 function merge() {
295 # Get the titles directly from the IDs, in case the target page params
296 # were spoofed. The queries are done based on the IDs, so it's best to
297 # keep it consistent...
298 $targetTitle = Title::newFromID( $this->mTargetID );
299 $destTitle = Title::newFromID( $this->mDestID );
300 if( is_null( $targetTitle ) || is_null( $destTitle ) ) {
301 return false; // validate these
302 }
303 if( $targetTitle->getArticleID() == $destTitle->getArticleID() ) {
304 return false;
305 }
306 # Verify that this timestamp is valid
307 # Must be older than the destination page
308 $dbw = wfGetDB( DB_MASTER );
309 # Get timestamp into DB format
310 $this->mTimestamp = $this->mTimestamp ? $dbw->timestamp( $this->mTimestamp ) : '';
311 # Max timestamp should be min of destination page
312 $maxtimestamp = $dbw->selectField(
313 'revision',
314 'MIN(rev_timestamp)',
315 array( 'rev_page' => $this->mDestID ),
316 __METHOD__
317 );
318 # Destination page must exist with revisions
319 if( !$maxtimestamp ) {
320 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
321 return false;
322 }
323 # Get the latest timestamp of the source
324 $lasttimestamp = $dbw->selectField(
325 array( 'page', 'revision' ),
326 'rev_timestamp',
327 array( 'page_id' => $this->mTargetID, 'page_latest = rev_id' ),
328 __METHOD__
329 );
330 # $this->mTimestamp must be older than $maxtimestamp
331 if( $this->mTimestamp >= $maxtimestamp ) {
332 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
333 return false;
334 }
335 # Update the revisions
336 if( $this->mTimestamp ) {
337 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
338 $timestampLimit = wfTimestamp( TS_MW, $this->mTimestamp );
339 } else {
340 $timewhere = "rev_timestamp <= {$maxtimestamp}";
341 $timestampLimit = wfTimestamp( TS_MW, $lasttimestamp );
342 }
343 # Do the moving...
344 $dbw->update(
345 'revision',
346 array( 'rev_page' => $this->mDestID ),
347 array( 'rev_page' => $this->mTargetID, $timewhere ),
348 __METHOD__
349 );
350
351 $count = $dbw->affectedRows();
352 # Make the source page a redirect if no revisions are left
353 $haveRevisions = $dbw->selectField(
354 'revision',
355 'rev_timestamp',
356 array( 'rev_page' => $this->mTargetID ),
357 __METHOD__,
358 array( 'FOR UPDATE' )
359 );
360 if( !$haveRevisions ) {
361 if( $this->mComment ) {
362 $comment = $this->msg(
363 'mergehistory-comment',
364 $targetTitle->getPrefixedText(),
365 $destTitle->getPrefixedText(),
366 $this->mComment
367 )->inContentLanguage()->text();
368 } else {
369 $comment = $this->msg(
370 'mergehistory-autocomment',
371 $targetTitle->getPrefixedText(),
372 $destTitle->getPrefixedText()
373 )->inContentLanguage()->text();
374 }
375 $mwRedir = MagicWord::get( 'redirect' );
376 $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destTitle->getPrefixedText() . "]]\n";
377 $redirectPage = WikiPage::factory( $targetTitle );
378 $redirectRevision = new Revision( array(
379 'page' => $this->mTargetID,
380 'comment' => $comment,
381 'text' => $redirectText ) );
382 $redirectRevision->insertOn( $dbw );
383 $redirectPage->updateRevisionOn( $dbw, $redirectRevision );
384
385 # Now, we record the link from the redirect to the new title.
386 # It should have no other outgoing links...
387 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID ), __METHOD__ );
388 $dbw->insert( 'pagelinks',
389 array(
390 'pl_from' => $this->mDestID,
391 'pl_namespace' => $destTitle->getNamespace(),
392 'pl_title' => $destTitle->getDBkey() ),
393 __METHOD__
394 );
395 } else {
396 $targetTitle->invalidateCache(); // update histories
397 }
398 $destTitle->invalidateCache(); // update histories
399 # Check if this did anything
400 if( !$count ) {
401 $this->getOutput()->addWikiMsg( 'mergehistory-fail' );
402 return false;
403 }
404 # Update our logs
405 $log = new LogPage( 'merge' );
406 $log->addEntry(
407 'merge', $targetTitle, $this->mComment,
408 array( $destTitle->getPrefixedText(), $timestampLimit )
409 );
410
411 $this->getOutput()->addWikiMsg( 'mergehistory-success',
412 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count );
413
414 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
415
416 return true;
417 }
418 }
419
420 class MergeHistoryPager extends ReverseChronologicalPager {
421 public $mForm, $mConds;
422
423 function __construct( $form, $conds = array(), $source, $dest ) {
424 $this->mForm = $form;
425 $this->mConds = $conds;
426 $this->title = $source;
427 $this->articleID = $source->getArticleID();
428
429 $dbr = wfGetDB( DB_SLAVE );
430 $maxtimestamp = $dbr->selectField(
431 'revision',
432 'MIN(rev_timestamp)',
433 array( 'rev_page' => $dest->getArticleID() ),
434 __METHOD__
435 );
436 $this->maxTimestamp = $maxtimestamp;
437
438 parent::__construct( $form->getContext() );
439 }
440
441 function getStartBody() {
442 wfProfileIn( __METHOD__ );
443 # Do a link batch query
444 $this->mResult->seek( 0 );
445 $batch = new LinkBatch();
446 # Give some pointers to make (last) links
447 $this->mForm->prevId = array();
448 foreach ( $this->mResult as $row ) {
449 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
450 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
451
452 $rev_id = isset( $rev_id ) ? $rev_id : $row->rev_id;
453 if( $rev_id > $row->rev_id ) {
454 $this->mForm->prevId[$rev_id] = $row->rev_id;
455 } elseif( $rev_id < $row->rev_id ) {
456 $this->mForm->prevId[$row->rev_id] = $rev_id;
457 }
458
459 $rev_id = $row->rev_id;
460 }
461
462 $batch->execute();
463 $this->mResult->seek( 0 );
464
465 wfProfileOut( __METHOD__ );
466 return '';
467 }
468
469 function formatRow( $row ) {
470 return $this->mForm->formatRevisionRow( $row );
471 }
472
473 function getQueryInfo() {
474 $conds = $this->mConds;
475 $conds['rev_page'] = $this->articleID;
476 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
477 return array(
478 'tables' => array( 'revision', 'page', 'user' ),
479 'fields' => array_merge( Revision::selectFields(), Revision::selectUserFields() ),
480 'conds' => $conds,
481 'join_conds' => array(
482 'page' => Revision::pageJoinCond(),
483 'user' => Revision::userJoinCond() )
484 );
485 }
486
487 function getIndexField() {
488 return 'rev_timestamp';
489 }
490 }