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