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