*Cleanup log and timestamps for merge stuff
[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 # Get timestamp into DB format
269 $this->mTimestamp = $this->mTimestamp ? $dbw->timestamp($this->mTimestamp) : '';
270
271 $maxtimestamp = $dbw->selectField( 'revision', 'MIN(rev_timestamp)',
272 array('rev_page' => $this->mDestID ),
273 __METHOD__ );
274 # Destination page must exist with revisions
275 if( !$maxtimestamp ) {
276 $wgOut->addWikiText( wfMsg('mergehistory-fail') );
277 return false;
278 }
279 # Leave the latest version no matter what
280 $lasttime = $dbw->selectField( array('page','revision'),
281 'rev_timestamp',
282 array('page_id' => $this->mTargetID, 'page_latest = rev_id' ),
283 __METHOD__ );
284 # Take the most restrictive of the twain
285 $maxtimestamp = ($lasttime < $maxtimestamp) ? $lasttime : $maxtimestamp;
286 // $this->mTimestamp must be less than $maxtimestamp
287 if( $this->mTimestamp >= $maxtimestamp ) {
288 $wgOut->addHtml( wfMsg('mergehistory-fail') );
289 return false;
290 }
291 # Update the revisions
292 if( $this->mTimestamp ) {
293 $timewhere = "rev_timestamp <= {$this->mTimestamp}";
294 $TimestampLimit = wfTimestamp(TS_MW,$this->mTimestamp);
295 } else {
296 $timewhere = "rev_timestamp < {$maxtimestamp}";
297 $TimestampLimit = wfTimestamp(TS_MW,$maxtimestamp);
298 }
299
300 $dbw->update( 'revision',
301 array( 'rev_page' => $this->mDestID ),
302 array( 'rev_page' => $this->mTargetID,
303 $timewhere ),
304 __METHOD__ );
305 # Check if this did anything
306 if( !$count = $dbw->affectedRows() ) {
307 $wgOut->addHtml( wfMsg('mergehistory-fail') );
308 return false;
309 }
310 # Update our logs
311 $log = new LogPage( 'merge' );
312 $log->addEntry( 'merge', $targetTitle, $this->mComment,
313 array($destTitle->getPrefixedText(),$TimestampLimit) );
314
315 $wgOut->addHtml( wfMsgExt( 'mergehistory-success', array('parseinline'),
316 $targetTitle->getPrefixedText(), $destTitle->getPrefixedText(), $count ) );
317
318 wfRunHooks( 'ArticleMergeComplete', array( $targetTitle, $destTitle ) );
319
320 return true;
321 }
322 }
323
324 class MergeHistoryPager extends ReverseChronologicalPager {
325 public $mForm, $mConds;
326
327 function __construct( $form, $conds = array(), $title, $title2 ) {
328 $this->mForm = $form;
329 $this->mConds = $conds;
330 $this->title = $title;
331 $this->articleID = $title->getArticleID();
332
333 $dbr = wfGetDB( DB_SLAVE );
334 $maxtimestamp = $dbr->selectField( 'revision', 'MIN(rev_timestamp)',
335 array('rev_page' => $title2->getArticleID() ),
336 __METHOD__ );
337 $this->maxTimestamp = $maxtimestamp;
338
339 parent::__construct();
340 }
341
342 function getStartBody() {
343 wfProfileIn( __METHOD__ );
344 # Do a link batch query
345 $this->mResult->seek( 0 );
346 $batch = new LinkBatch();
347 # Give some pointers to make (last) links
348 $this->mForm->prevId = array();
349 while( $row = $this->mResult->fetchObject() ) {
350 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->rev_user_text ) );
351 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->rev_user_text ) );
352
353 $rev_id = isset($rev_id) ? $rev_id : $row->rev_id;
354 if( $rev_id > $row->rev_id )
355 $this->mForm->prevId[$rev_id] = $row->rev_id;
356 else if( $rev_id < $row->rev_id )
357 $this->mForm->prevId[$row->rev_id] = $rev_id;
358
359 $rev_id = $row->rev_id;
360 }
361
362 $batch->execute();
363 $this->mResult->seek( 0 );
364
365 wfProfileOut( __METHOD__ );
366 return '';
367 }
368
369 function formatRow( $row ) {
370 $block = new Block;
371 return $this->mForm->formatRevisionRow( $row );
372 }
373
374 function getQueryInfo() {
375 $conds = $this->mConds;
376 $conds['rev_page'] = $this->articleID;
377 $conds[] = "rev_timestamp < {$this->maxTimestamp}";
378 # Skip the latest one, as that could cause problems
379 if( $page = $this->title->getLatestRevID() )
380 $conds[] = "rev_id != {$page}";
381
382 return array(
383 'tables' => array('revision'),
384 'fields' => array( 'rev_minor_edit', 'rev_timestamp', 'rev_user', 'rev_user_text', 'rev_comment',
385 'rev_id', 'rev_page', 'rev_text_id', 'rev_len', 'rev_deleted' ),
386 'conds' => $conds
387 );
388 }
389
390 function getIndexField() {
391 return 'rev_timestamp';
392 }
393 }