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