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