]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/specials/SpecialMergeHistory.php
MediaWiki 1.16.1-scripts
[autoinstallsdev/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;
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 && $this->mDestObj && $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, $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( array( '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                         $table =
167                                 Xml::openElement( 'fieldset' ) .
168                                 wfMsgExt( 'mergehistory-merge', array('parseinline'),
169                                         $this->mTargetObj->getPrefixedText(), $this->mDestObj->getPrefixedText() ) .
170                                 Xml::openElement( 'table', array( 'id' => 'mw-mergehistory-table' ) ) .
171                                         "<tr>
172                                                 <td class='mw-label'>" .
173                                                         Xml::label( wfMsg( 'mergehistory-reason' ), 'wpComment' ) .
174                                                 "</td>
175                                                 <td class='mw-input'>" .
176                                                         Xml::input( 'wpComment', 50, $this->mComment, array('id' => 'wpComment') ) .
177                                                 "</td>
178                                         </tr>
179                                         <tr>
180                                                 <td>&nbsp;</td>
181                                                 <td class='mw-submit'>" .
182                                                         Xml::submitButton( wfMsg( 'mergehistory-submit' ), array( 'name' => 'merge', 'id' => 'mw-merge-submit' ) ) .
183                                                 "</td>
184                                         </tr>" .
185                                 Xml::closeElement( 'table' ) .
186                                 Xml::closeElement( 'fieldset' );
187
188                         $wgOut->addHTML( $table );
189                 }
190
191                 $wgOut->addHTML( "<h2 id=\"mw-mergehistory\">" . wfMsgHtml( "mergehistory-list" ) . "</h2>\n" );
192
193                 if( $haveRevisions ) {
194                         $wgOut->addHTML( $revisions->getNavigationBar() );
195                         $wgOut->addHTML( "<ul>" );
196                         $wgOut->addHTML( $revisions->getBody() );
197                         $wgOut->addHTML( "</ul>" );
198                         $wgOut->addHTML( $revisions->getNavigationBar() );
199                 } else {
200                         $wgOut->addWikiMsg( "mergehistory-empty" );
201                 }
202
203                 # Show relevant lines from the deletion log:
204                 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'merge' ) ) . "</h2>\n" );
205                 LogEventsList::showLogExtract( $wgOut, 'merge', $this->mTargetObj->getPrefixedText() );
206
207                 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
208                 # Such would happen if a page was renamed after the form loaded, but before submit
209                 $misc = Xml::hidden( 'targetID', $this->mTargetObj->getArticleID() );
210                 $misc .= Xml::hidden( 'destID', $this->mDestObj->getArticleID() );
211                 $misc .= Xml::hidden( 'target', $this->mTarget );
212                 $misc .= Xml::hidden( 'dest', $this->mDest );
213                 $misc .= Xml::hidden( 'wpEditToken', $wgUser->editToken() );
214                 $misc .= Xml::closeElement( 'form' );
215                 $wgOut->addHTML( $misc );
216
217                 return true;
218         }
219
220         function formatRevisionRow( $row ) {
221                 global $wgLang;
222
223                 $rev = new Revision( $row );
224
225                 $stxt = '';
226                 $last = $this->message['last'];
227
228                 $ts = wfTimestamp( TS_MW, $row->rev_timestamp );
229                 $checkBox = Xml::radio( "mergepoint", $ts, false );
230
231                 $pageLink = $this->sk->linkKnown(
232                         $rev->getTitle(),
233                         htmlspecialchars( $wgLang->timeanddate( $ts ) ),
234                         array(),
235                         array( 'oldid' => $rev->getId() )
236                 );
237                 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
238                         $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
239                 }
240
241                 # Last link
242                 if( !$rev->userCan( Revision::DELETED_TEXT ) )
243                         $last = $this->message['last'];
244                 else if( isset($this->prevId[$row->rev_id]) )
245                         $last = $this->sk->linkKnown(
246                                 $rev->getTitle(),
247                                 $this->message['last'],
248                                 array(),
249                                 array(
250                                         'diff' => $row->rev_id,
251                                         'oldid' => $this->prevId[$row->rev_id]
252                                 )
253                         );
254
255                 $userLink = $this->sk->revUserTools( $rev );
256
257                 if(!is_null($size = $row->rev_len)) {
258                         $stxt = $this->sk->formatRevisionSize( $size );
259                 }
260                 $comment = $this->sk->revComment( $rev );
261
262                 return "<li>$checkBox ($last) $pageLink . . $userLink $stxt $comment</li>";
263         }
264
265         /**
266          * Fetch revision text link if it's available to all users
267          * @return string
268          */
269         function getPageLink( $row, $titleObj, $ts, $target ) {
270                 global $wgLang;
271
272                 if( !$this->userCan($row, Revision::DELETED_TEXT) ) {
273                         return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
274                 } else {
275                         $link = $this->sk->linkKnown(
276                                 $titleObj,
277                                 $wgLang->timeanddate( $ts, true ),
278                                 array(),
279                                 array(
280                                         'target' => $target,
281                                         'timestamp' => $ts
282                                 )
283                         );
284                         if( $this->isDeleted($row, Revision::DELETED_TEXT) )
285                                 $link = '<span class="history-deleted">' . $link . '</span>';
286                         return $link;
287                 }
288         }
289
290         function merge() {
291                 global $wgOut;
292                 # Get the titles directly from the IDs, in case the target page params
293                 # were spoofed. The queries are done based on the IDs, so it's best to
294                 # keep it consistent...
295                 $targetTitle = Title::newFromID( $this->mTargetID );
296                 $destTitle = Title::newFromID( $this->mDestID );
297                 if( is_null($targetTitle) || is_null($destTitle) )
298                         return false; // validate these
299                 if( $targetTitle->getArticleId() == $destTitle->getArticleId() )
300                         return false;
301                 # Verify that this timestamp is valid
302                 # Must be older than the destination page
303                 $dbw = wfGetDB( DB_MASTER );
304                 # Get timestamp into DB format
305                 $this->mTimestamp = $this->mTimestamp ? $dbw->timestamp($this->mTimestamp) : '';
306                 # Max timestamp should be min of destination page
307                 $maxtimestamp = $dbw->selectField( 'revision', 'MIN(rev_timestamp)',
308                         array('rev_page' => $this->mDestID ),
309                         __METHOD__ );
310                 # Destination page must exist with revisions
311                 if( !$maxtimestamp ) {
312                         $wgOut->addWikiMsg('mergehistory-fail');
313                         return false;
314                 }
315                 # Get the latest timestamp of the source
316                 $lasttimestamp = $dbw->selectField( array('page','revision'),
317                         'rev_timestamp',
318                         array('page_id' => $this->mTargetID, 'page_latest = rev_id' ),
319                         __METHOD__ );
320                 # $this->mTimestamp must be older than $maxtimestamp
321                 if( $this->mTimestamp >= $maxtimestamp ) {
322                         $wgOut->addWikiMsg('mergehistory-fail');
323                         return false;
324                 }
325                 # Update the revisions
326                 if( $this->mTimestamp ) {
327                         $timewhere = "rev_timestamp <= {$this->mTimestamp}";
328                         $TimestampLimit = wfTimestamp(TS_MW,$this->mTimestamp);
329                 } else {
330                         $timewhere = "rev_timestamp <= {$maxtimestamp}";
331                         $TimestampLimit = wfTimestamp(TS_MW,$lasttimestamp);
332                 }
333                 # Do the moving...
334                 $dbw->update( 'revision',
335                         array( 'rev_page' => $this->mDestID ),
336                         array( 'rev_page' => $this->mTargetID,
337                                 $timewhere ),
338                         __METHOD__ );
339
340                 $count = $dbw->affectedRows();
341                 # Make the source page a redirect if no revisions are left
342                 $haveRevisions = $dbw->selectField( 'revision',
343                         'rev_timestamp',
344                         array( 'rev_page' => $this->mTargetID  ),
345                         __METHOD__,
346                         array( 'FOR UPDATE' ) );
347                 if( !$haveRevisions ) {
348                         if( $this->mComment ) {
349                                 $comment = wfMsgForContent( 'mergehistory-comment', $targetTitle->getPrefixedText(),
350                                         $destTitle->getPrefixedText(), $this->mComment );
351                         } else {
352                                 $comment = wfMsgForContent( 'mergehistory-autocomment', $targetTitle->getPrefixedText(),
353                                         $destTitle->getPrefixedText() );
354                         }
355                         $mwRedir = MagicWord::get( 'redirect' );
356                         $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destTitle->getPrefixedText() . "]]\n";
357                         $redirectArticle = new Article( $targetTitle );
358                         $redirectRevision = new Revision( array(
359                                 'page'    => $this->mTargetID,
360                                 'comment' => $comment,
361                                 'text'    => $redirectText ) );
362                         $redirectRevision->insertOn( $dbw );
363                         $redirectArticle->updateRevisionOn( $dbw, $redirectRevision );
364
365                         # Now, we record the link from the redirect to the new title.
366                         # It should have no other outgoing links...
367                         $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mDestID ), __METHOD__ );
368                         $dbw->insert( 'pagelinks',
369                                 array(
370                                         'pl_from'      => $this->mDestID,
371                                         'pl_namespace' => $destTitle->getNamespace(),
372                                         'pl_title'     => $destTitle->getDBkey() ),
373                                 __METHOD__ );
374                 } else {
375                         $targetTitle->invalidateCache(); // update histories
376                 }
377                 $destTitle->invalidateCache(); // update histories
378                 # Check if this did anything
379                 if( !$count ) {
380                         $wgOut->addWikiMsg('mergehistory-fail');
381                         return false;
382                 }
383                 # Update our logs
384                 $log = new LogPage( 'merge' );
385                 $log->addEntry( 'merge', $targetTitle, $this->mComment,
386                         array($destTitle->getPrefixedText(),$TimestampLimit) );
387
388                 $wgOut->addHTML( wfMsgExt( 'mergehistory-success', array('parseinline'),
389                         $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count ) );
390
391                 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
392
393                 return true;
394         }
395 }
396
397 class MergeHistoryPager extends ReverseChronologicalPager {
398         public $mForm, $mConds;
399
400         function __construct( $form, $conds = array(), $source, $dest ) {
401                 $this->mForm = $form;
402                 $this->mConds = $conds;
403                 $this->title = $source;
404                 $this->articleID = $source->getArticleID();
405
406                 $dbr = wfGetDB( DB_SLAVE );
407                 $maxtimestamp = $dbr->selectField( 'revision', 'MIN(rev_timestamp)',
408                         array('rev_page' => $dest->getArticleID() ),
409                         __METHOD__ );
410                 $this->maxTimestamp = $maxtimestamp;
411
412                 parent::__construct();
413         }
414
415         function getStartBody() {
416                 wfProfileIn( __METHOD__ );
417                 # Do a link batch query
418                 $this->mResult->seek( 0 );
419                 $batch = new LinkBatch();
420                 # Give some pointers to make (last) links
421                 $this->mForm->prevId = array();
422                 while( $row = $this->mResult->fetchObject() ) {
423                         $batch->addObj( Title::makeTitleSafe( NS_USER, $row->rev_user_text ) );
424                         $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->rev_user_text ) );
425
426                         $rev_id = isset($rev_id) ? $rev_id : $row->rev_id;
427                         if( $rev_id > $row->rev_id )
428                                 $this->mForm->prevId[$rev_id] = $row->rev_id;
429                         else if( $rev_id < $row->rev_id )
430                                 $this->mForm->prevId[$row->rev_id] = $rev_id;
431
432                         $rev_id = $row->rev_id;
433                 }
434
435                 $batch->execute();
436                 $this->mResult->seek( 0 );
437
438                 wfProfileOut( __METHOD__ );
439                 return '';
440         }
441
442         function formatRow( $row ) {
443                 $block = new Block;
444                 return $this->mForm->formatRevisionRow( $row );
445         }
446
447         function getQueryInfo() {
448                 $conds = $this->mConds;
449                 $conds['rev_page'] = $this->articleID;
450                 $conds[] = 'page_id = rev_page';
451                 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
452                 return array(
453                         'tables' => array('revision','page'),
454                         'fields' => array( 'rev_minor_edit', 'rev_timestamp', 'rev_user', 'rev_user_text', 'rev_comment',
455                                  'rev_id', 'rev_page', 'rev_parent_id', 'rev_text_id', 'rev_len', 'rev_deleted' ),
456                         'conds' => $conds
457                 );
458         }
459
460         function getIndexField() {
461                 return 'rev_timestamp';
462         }
463 }