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