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