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