*Add a special page to merge non-overlapping page histories to fix copy-paste moves...
[lhc/web/wiklou.git] / includes / SpecialMergeHistory.php
1 <?php
2
3 /**
4 * Special page allowing users with the appropriate permissions to
5 * merge article histories, with some restrictions
6 *
7 * @addtogroup 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 * @addtogroup 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
36 $this->mTargetID = intval( $request->getVal( 'targetID' ) );
37 $this->mDestID = intval( $request->getVal( 'destID' ) );
38 $this->mTimestamp = $request->getVal( 'mergepoint' );
39 $this->mComment = $request->getText( 'wpComment' );
40
41 $this->mMerge = $request->wasPosted() && $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
42 // target page
43 if( $this->mTarget !== "" ) {
44 $this->mTargetObj = Title::newFromURL( $this->mTarget );
45 } else {
46 $this->mTargetObj = NULL;
47 }
48 # Destination
49 if( $this->mDest !== "" ) {
50 $this->mDestObj = Title::newFromURL( $this->mDest );
51 } else {
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( is_object($this->mTargetObj) && is_object($this->mDestObj) ) {
79 return $this->showHistory();
80 }
81
82 return $this->showMergeForm();
83 }
84
85 function showMergeForm() {
86 global $wgOut, $wgScript;
87
88 $wgOut->addWikiText( wfMsg( 'mergehistory-header' ) );
89
90 $wgOut->addHtml(
91 Xml::openElement( 'form', array(
92 'method' => 'get',
93 'action' => $wgScript ) ) .
94 '<fieldset>' .
95 Xml::element( 'legend', array(),
96 wfMsg( 'mergehistory-box' ) ) .
97 Xml::hidden( 'title',
98 SpecialPage::getTitleFor( 'Mergehistory' )->getPrefixedDbKey() ) .
99 Xml::openElement( 'table' ) .
100 "<tr>
101 <td>".Xml::Label( wfMsg( 'mergehistory-from' ), 'target' )."</td>
102 <td>".Xml::input( 'target', 30, $this->mTarget, array('id'=>'target') )."</td>
103 </tr><tr>
104 <td>".Xml::Label( wfMsg( 'mergehistory-into' ), 'dest' )."</td>
105 <td>".Xml::input( 'dest', 30, $this->mDest, array('id'=>'dest') )."</td>
106 </tr><tr><td>" .
107 Xml::submitButton( wfMsg( 'mergehistory-go' ) ) .
108 "</td></tr>" .
109 Xml::closeElement( 'table' ) .
110 '</fieldset>' .
111 '</form>' );
112 }
113
114 private function showHistory() {
115 global $wgLang, $wgContLang, $wgUser, $wgOut;
116
117 $this->sk = $wgUser->getSkin();
118
119 $wgOut->setPagetitle( wfMsg( "mergehistory" ) );
120
121 $this->showMergeForm();
122
123 # List all stored revisions
124 $revisions = new MergeHistoryPager( $this, array(), $this->mTargetObj, $this->mDestObj );
125 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
126
127 $titleObj = SpecialPage::getTitleFor( "Mergehistory" );
128 $action = $titleObj->getLocalURL( "action=submit" );
129 # Start the form here
130 $top = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'merge' ) );
131 $wgOut->addHtml( $top );
132
133 if( $haveRevisions ) {
134 # Format the user-visible controls (comment field, submission button)
135 # in a nice little table
136 $align = $wgContLang->isRtl() ? 'left' : 'right';
137 $table =
138 Xml::openElement( 'fieldset' ) .
139 Xml::openElement( 'table' ) .
140 "<tr>
141 <td colspan='2'>" .
142 wfMsgExt( 'mergehistory-merge', array('parseinline'),
143 $this->mTargetObj->getPrefixedText(), $this->mDestObj->getPrefixedText() ) .
144 "</td>
145 </tr>
146 <tr>
147 <td align='$align'>" .
148 Xml::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
149 "</td>
150 <td>" .
151 Xml::input( 'wpComment', 50, $this->mComment ) .
152 "</td>
153 </tr>
154 <tr>
155 <td>&nbsp;</td>
156 <td>" .
157 Xml::submitButton( wfMsg( 'mergehistory-submit' ), array( 'name' => 'merge', 'id' => 'mw-merge-submit' ) ) .
158 "</td>
159 </tr>" .
160 Xml::closeElement( 'table' ) .
161 Xml::closeElement( 'fieldset' );
162
163 $wgOut->addHtml( $table );
164 }
165
166 $wgOut->addHTML( "<h2 id=\"mw-mergehistory\">" . wfMsgHtml( "mergehistory-list" ) . "</h2>\n" );
167
168 if( $haveRevisions ) {
169 $wgOut->addHTML( $revisions->getNavigationBar() );
170 $wgOut->addHTML( "<ul>" );
171 $wgOut->addHTML( $revisions->getBody() );
172 $wgOut->addHTML( "</ul>" );
173 $wgOut->addHTML( $revisions->getNavigationBar() );
174 } else {
175 $wgOut->addWikiText( wfMsg( "mergehistory-empty" ) );
176 }
177
178 # Show relevant lines from the deletion log:
179 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'merge' ) ) . "</h2>\n" );
180 $logViewer = new LogViewer(
181 new LogReader(
182 new FauxRequest(
183 array( 'page' => $this->mTargetObj->getPrefixedText(),
184 'type' => 'merge' ) ) ) );
185 $logViewer->showList( $wgOut );
186
187 # Slip in the hidden controls here
188 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
189 # Such would happen if a page was renamed after the form loaded, but before submit
190 $misc = Xml::hidden( 'targetID', $this->mTargetObj->getArticleID() );
191 $misc .= Xml::hidden( 'destID', $this->mDestObj->getArticleID() );
192 $misc .= Xml::hidden( 'target', $this->mTarget );
193 $misc .= Xml::hidden( 'dest', $this->mDest );
194 $misc .= Xml::hidden( 'wpEditToken', $wgUser->editToken() );
195 $misc .= Xml::closeElement( 'form' );
196 $wgOut->addHtml( $misc );
197
198 return true;
199 }
200
201 function formatRevisionRow( $row ) {
202 global $wgUser, $wgLang;
203
204 $rev = new Revision( $row );
205
206 $stxt = '';
207 $last = $this->message['last'];
208
209 $ts = wfTimestamp( TS_MW, $row->rev_timestamp );
210 $checkBox = wfRadio( "mergepoint", $ts, false );
211
212 $pageLink = $this->sk->makeKnownLinkObj( $rev->getTitle(),
213 $wgLang->timeanddate( $ts ), 'oldid=' . $rev->getID() );
214 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
215 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
216 }
217
218 # Last link
219 if( !$rev->userCan( Revision::DELETED_TEXT ) )
220 $last = $this->message['last'];
221 else if( isset($this->prevId[$row->rev_id]) )
222 $last = $this->sk->makeKnownLinkObj( $rev->getTitle(), $this->message['last'],
223 "&diff=" . $row->rev_id . "&oldid=" . $this->prevId[$row->rev_id] );
224
225 $userLink = $this->sk->revUserTools( $rev );
226
227 if(!is_null($size = $row->rev_len)) {
228 if($size == 0)
229 $stxt = wfMsgHtml('historyempty');
230 else
231 $stxt = wfMsgHtml('historysize', $wgLang->formatNum( $size ) );
232 }
233 $comment = $this->sk->revComment( $rev );
234
235 return "<li>$checkBox ($last) $pageLink . . $userLink $stxt $comment</li>";
236 }
237
238 /**
239 * Fetch revision text link if it's available to all users
240 * @return string
241 */
242 function getPageLink( $row, $titleObj, $ts, $target ) {
243 global $wgLang;
244
245 if( !$this->userCan($row, Revision::DELETED_TEXT) ) {
246 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
247 } else {
248 $link = $this->sk->makeKnownLinkObj( $titleObj,
249 $wgLang->timeanddate( $ts, true ), "target=$target&timestamp=$ts" );
250 if( $this->isDeleted($row, Revision::DELETED_TEXT) )
251 $link = '<span class="history-deleted">' . $link . '</span>';
252 return $link;
253 }
254 }
255
256 function merge() {
257 global $wgOut, $wgUser;
258 # Get the titles directly from the IDs, in case the target page params
259 # were spoofed. The queries are done based on the IDs, so it's best to
260 # keep it consistent...
261 $targetTitle = Title::newFromID( $this->mTargetID );
262 $destTitle = Title::newFromID( $this->mDestID );
263 if( is_null($targetTitle) || is_null($destTitle) )
264 return false; // validate these
265 # Verify that this timestamp is valid
266 # Must be older than the destination page
267 $dbw = wfGetDB( DB_MASTER );
268 $maxtimestamp = $dbw->selectField( 'revision', 'MIN(rev_timestamp)',
269 array('rev_page' => $this->mDestID ),
270 __METHOD__ );
271 # Destination page must exist with revisions
272 if( !$maxtimestamp ) {
273 $wgOut->addWikiText( wfMsg('mergehistory-fail') );
274 return false;
275 }
276 # Leave the latest version no matter what
277 $lasttime = $dbw->selectField( array('page','revision'),
278 'rev_timestamp',
279 array('page_id' => $this->mTargetID, 'page_latest = rev_id' ),
280 __METHOD__ );
281 # Take the most restrictive of the twain
282 $maxtimestamp = ($lasttime < $maxtimestamp) ? $lasttime : $maxtimestamp;
283 if( $this->mTimestamp >= $maxtimestamp ) {
284 $wgOut->addHtml( wfMsg('mergehistory-fail') );
285 return false;
286 }
287 # Update the revisions
288 if( $this->mTimestamp )
289 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
290 else
291 $timewhere = '1 = 1';
292
293 $dbw->update( 'revision',
294 array( 'rev_page' => $this->mDestID ),
295 array( 'rev_page' => $this->mTargetID,
296 "rev_timestamp < {$maxtimestamp}",
297 $timewhere ),
298 __METHOD__ );
299 # Check if this did anything
300 $count = $dbw->affectedRows();
301 if( !$count ) {
302 $wgOut->addHtml( wfMsg('mergehistory-fail') );
303 return false;
304 }
305 # Update our logs
306 $log = new LogPage( 'merge' );
307 $log->addEntry( 'merge', $targetTitle, $this->mComment,
308 array($destTitle->getPrefixedText(),$this->mTimestamp) );
309
310 $wgOut->addHtml( wfMsgExt( 'mergehistory-success', array('parseinline'),
311 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count ) );
312
313 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
314
315 return true;
316 }
317 }
318
319 class MergeHistoryPager extends ReverseChronologicalPager {
320 public $mForm, $mConds;
321
322 function __construct( $form, $conds = array(), $title, $title2 ) {
323 $this->mForm = $form;
324 $this->mConds = $conds;
325 $this->title = $title;
326 $this->articleID = $title->getArticleID();
327
328 $dbr = wfGetDB( DB_SLAVE );
329 $maxtimestamp = $dbr->selectField( 'revision', 'MIN(rev_timestamp)',
330 array('rev_page' => $title2->getArticleID() ),
331 __METHOD__ );
332 $this->maxTimestamp = $maxtimestamp;
333
334 parent::__construct();
335 }
336
337 function getStartBody() {
338 wfProfileIn( __METHOD__ );
339 # Do a link batch query
340 $this->mResult->seek( 0 );
341 $batch = new LinkBatch();
342 # Give some pointers to make (last) links
343 $this->mForm->prevId = array();
344 while( $row = $this->mResult->fetchObject() ) {
345 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->rev_user_text ) );
346 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->rev_user_text ) );
347
348 $rev_id = isset($rev_id) ? $rev_id : $row->rev_id;
349 if( $rev_id > $row->rev_id )
350 $this->mForm->prevId[$rev_id] = $row->rev_id;
351 else if( $rev_id < $row->rev_id )
352 $this->mForm->prevId[$row->rev_id] = $rev_id;
353
354 $rev_id = $row->rev_id;
355 }
356
357 $batch->execute();
358 $this->mResult->seek( 0 );
359
360 wfProfileOut( __METHOD__ );
361 return '';
362 }
363
364 function formatRow( $row ) {
365 $block = new Block;
366 return $this->mForm->formatRevisionRow( $row );
367 }
368
369 function getQueryInfo() {
370 $conds = $this->mConds;
371 $conds['rev_page'] = $this->articleID;
372 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
373 # Skip the latest one, as that could cause problems
374 if( $page = $this->title->getLatestRevID() )
375 $conds[] = "rev_id != {$page}";
376
377 return array(
378 'tables' => array('revision'),
379 'fields' => array( 'rev_minor_edit', 'rev_timestamp', 'rev_user', 'rev_user_text', 'rev_comment',
380 'rev_id', 'rev_page', 'rev_text_id', 'rev_len', 'rev_deleted' ),
381 'conds' => $conds
382 );
383 }
384
385 function getIndexField() {
386 return 'rev_timestamp';
387 }
388 }