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.
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.
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
21 * Special page allowing users with the appropriate permissions to
22 * merge article histories, with some restrictions
24 * @ingroup SpecialPage
26 class SpecialMergeHistory
extends SpecialPage
{
27 var $mAction, $mTarget, $mDest, $mTimestamp, $mTargetID, $mDestID, $mComment;
28 var $mTargetObj, $mDestObj;
30 public function __construct() {
31 parent
::__construct( 'MergeHistory', 'mergehistory' );
34 private function loadRequestParams( $request ) {
36 $this->mAction
= $request->getVal( 'action' );
37 $this->mTarget
= $request->getVal( 'target' );
38 $this->mDest
= $request->getVal( 'dest' );
39 $this->mSubmitted
= $request->getBool( 'submitted' );
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
= '';
47 $this->mComment
= $request->getText( 'wpComment' );
49 $this->mMerge
= $request->wasPosted() && $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
51 if( $this->mSubmitted
) {
52 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
53 $this->mDestObj
= Title
::newFromURL( $this->mDest
);
55 $this->mTargetObj
= null;
56 $this->mDestObj
= null;
58 $this->preCacheMessages();
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
65 function preCacheMessages() {
66 // Precache various messages
67 if( !isset( $this->message
) ) {
68 $this->message
['last'] = wfMsgExt( 'last', array( 'escape' ) );
72 function execute( $par ) {
73 global $wgOut, $wgRequest, $wgUser;
76 $wgOut->readOnlyPage();
80 if( !$this->userCanExecute( $wgUser ) ) {
81 $this->displayRestrictionError();
85 $this->loadRequestParams( $wgRequest );
88 $this->outputHeader();
90 if( $this->mTargetID
&& $this->mDestID
&& $this->mAction
=="submit" && $this->mMerge
) {
91 return $this->merge();
94 if ( !$this->mSubmitted
) {
95 $this->showMergeForm();
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() )
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() )
116 if ( $this->mTargetObj
&& $this->mDestObj
&& $this->mTargetObj
->equals( $this->mDestObj
) ) {
117 $errors[] = wfMsgExt( 'mergehistory-same-destination', array( 'parse' ) );
120 if ( count( $errors ) ) {
121 $this->showMergeForm();
122 $wgOut->addHTML( implode( "\n", $errors ) );
124 $this->showHistory();
129 function showMergeForm() {
130 global $wgOut, $wgScript;
132 $wgOut->addWikiMsg( 'mergehistory-header' );
135 Xml
::openElement( 'form', array(
137 'action' => $wgScript ) ) .
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' ) .
146 <td>".Xml
::label( wfMsg( 'mergehistory-from' ), 'target' )."</td>
147 <td>".Xml
::input( 'target', 30, $this->mTarget
, array('id'=>'target') )."</td>
149 <td>".Xml
::label( wfMsg( 'mergehistory-into' ), 'dest' )."</td>
150 <td>".Xml
::input( 'dest', 30, $this->mDest
, array('id'=>'dest') )."</td>
152 Xml
::submitButton( wfMsg( 'mergehistory-go' ) ) .
154 Xml
::closeElement( 'table' ) .
159 private function showHistory() {
160 global $wgLang, $wgUser, $wgOut;
162 $this->sk
= $wgUser->getSkin();
164 $wgOut->setPagetitle( wfMsg( "mergehistory" ) );
166 $this->showMergeForm();
168 # List all stored revisions
169 $revisions = new MergeHistoryPager( $this, array(), $this->mTargetObj
, $this->mDestObj
);
170 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
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 );
178 if( $haveRevisions ) {
179 # Format the user-visible controls (comment field, submission button)
180 # in a nice little 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' ) ) .
187 <td class='mw-label'>" .
188 Xml
::label( wfMsg( 'mergehistory-reason' ), 'wpComment' ) .
190 <td class='mw-input'>" .
191 Xml
::input( 'wpComment', 50, $this->mComment
, array('id' => 'wpComment') ) .
196 <td class='mw-submit'>" .
197 Xml
::submitButton( wfMsg( 'mergehistory-submit' ), array( 'name' => 'merge', 'id' => 'mw-merge-submit' ) ) .
200 Xml
::closeElement( 'table' ) .
201 Xml
::closeElement( 'fieldset' );
203 $wgOut->addHTML( $table );
206 $wgOut->addHTML( "<h2 id=\"mw-mergehistory\">" . wfMsgHtml( "mergehistory-list" ) . "</h2>\n" );
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() );
215 $wgOut->addWikiMsg( "mergehistory-empty" );
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() );
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 );
235 function formatRevisionRow( $row ) {
238 $rev = new Revision( $row );
241 $last = $this->message
['last'];
243 $ts = wfTimestamp( TS_MW
, $row->rev_timestamp
);
244 $checkBox = Xml
::radio( "mergepoint", $ts, false );
246 $pageLink = $this->sk
->linkKnown(
248 htmlspecialchars( $wgLang->timeanddate( $ts ) ),
250 array( 'oldid' => $rev->getId() )
252 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
253 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
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(
262 $this->message
['last'],
265 'diff' => $row->rev_id
,
266 'oldid' => $this->prevId
[$row->rev_id
]
270 $userLink = $this->sk
->revUserTools( $rev );
272 if(!is_null($size = $row->rev_len
)) {
273 $stxt = $this->sk
->formatRevisionSize( $size );
275 $comment = $this->sk
->revComment( $rev );
277 return "<li>$checkBox ($last) $pageLink . . $userLink $stxt $comment</li>";
281 * Fetch revision text link if it's available to all users
284 function getPageLink( $row, $titleObj, $ts, $target ) {
287 if( !$this->userCan($row, Revision
::DELETED_TEXT
) ) {
288 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
290 $link = $this->sk
->linkKnown(
292 $wgLang->timeanddate( $ts, true ),
299 if( $this->isDeleted($row, Revision
::DELETED_TEXT
) )
300 $link = '<span class="history-deleted">' . $link . '</span>';
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() )
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
),
325 # Destination page must exist with revisions
326 if( !$maxtimestamp ) {
327 $wgOut->addWikiMsg('mergehistory-fail');
330 # Get the latest timestamp of the source
331 $lasttimestamp = $dbw->selectField( array('page','revision'),
333 array('page_id' => $this->mTargetID
, 'page_latest = rev_id' ),
335 # $this->mTimestamp must be older than $maxtimestamp
336 if( $this->mTimestamp
>= $maxtimestamp ) {
337 $wgOut->addWikiMsg('mergehistory-fail');
340 # Update the revisions
341 if( $this->mTimestamp
) {
342 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
343 $TimestampLimit = wfTimestamp(TS_MW
,$this->mTimestamp
);
345 $timewhere = "rev_timestamp <= {$maxtimestamp}";
346 $TimestampLimit = wfTimestamp(TS_MW
,$lasttimestamp);
349 $dbw->update( 'revision',
350 array( 'rev_page' => $this->mDestID
),
351 array( 'rev_page' => $this->mTargetID
,
355 $count = $dbw->affectedRows();
356 # Make the source page a redirect if no revisions are left
357 $haveRevisions = $dbw->selectField( 'revision',
359 array( 'rev_page' => $this->mTargetID
),
361 array( 'FOR UPDATE' ) );
362 if( !$haveRevisions ) {
363 if( $this->mComment
) {
364 $comment = wfMsgForContent( 'mergehistory-comment', $targetTitle->getPrefixedText(),
365 $destTitle->getPrefixedText(), $this->mComment
);
367 $comment = wfMsgForContent( 'mergehistory-autocomment', $targetTitle->getPrefixedText(),
368 $destTitle->getPrefixedText() );
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 );
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',
385 'pl_from' => $this->mDestID
,
386 'pl_namespace' => $destTitle->getNamespace(),
387 'pl_title' => $destTitle->getDBkey() ),
390 $targetTitle->invalidateCache(); // update histories
392 $destTitle->invalidateCache(); // update histories
393 # Check if this did anything
395 $wgOut->addWikiMsg('mergehistory-fail');
399 $log = new LogPage( 'merge' );
400 $log->addEntry( 'merge', $targetTitle, $this->mComment
,
401 array($destTitle->getPrefixedText(),$TimestampLimit) );
403 $wgOut->addHTML( wfMsgExt( 'mergehistory-success', array('parseinline'),
404 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count ) );
406 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
412 class MergeHistoryPager
extends ReverseChronologicalPager
{
413 public $mForm, $mConds;
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();
421 $dbr = wfGetDB( DB_SLAVE
);
422 $maxtimestamp = $dbr->selectField( 'revision', 'MIN(rev_timestamp)',
423 array('rev_page' => $dest->getArticleID() ),
425 $this->maxTimestamp
= $maxtimestamp;
427 parent
::__construct();
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
) );
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;
447 $rev_id = $row->rev_id
;
451 $this->mResult
->seek( 0 );
453 wfProfileOut( __METHOD__
);
457 function formatRow( $row ) {
459 return $this->mForm
->formatRevisionRow( $row );
462 function getQueryInfo() {
463 $conds = $this->mConds
;
464 $conds['rev_page'] = $this->articleID
;
465 $conds[] = 'page_id = rev_page';
466 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
468 'tables' => array('revision','page'),
469 'fields' => array( 'rev_minor_edit', 'rev_timestamp', 'rev_user', 'rev_user_text', 'rev_comment',
470 'rev_id', 'rev_page', 'rev_parent_id', 'rev_text_id', 'rev_len', 'rev_deleted' ),
475 function getIndexField() {
476 return 'rev_timestamp';