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