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