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