When showing the "blah has been undeleted" page, make sure it's a blue link
[lhc/web/wiklou.git] / includes / SpecialUndelete.php
1 <?php
2
3 /**
4 * Special page allowing users with the appropriate permissions to view
5 * and restore deleted content
6 *
7 * @package MediaWiki
8 * @subpackage Special pages
9 */
10
11 /** */
12 require_once( 'Revision.php' );
13
14 /**
15 *
16 */
17 function wfSpecialUndelete( $par ) {
18 global $wgRequest;
19
20 $form = new UndeleteForm( $wgRequest, $par );
21 $form->execute();
22 }
23
24 /**
25 *
26 * @package MediaWiki
27 * @subpackage SpecialPage
28 */
29 class PageArchive {
30 var $title;
31
32 function PageArchive( &$title ) {
33 if( is_null( $title ) ) {
34 wfDebugDieBacktrace( 'Archiver() given a null title.');
35 }
36 $this->title =& $title;
37 }
38
39 /**
40 * List all deleted pages recorded in the archive table. Returns result
41 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
42 * namespace/title. Can be called staticaly.
43 *
44 * @return ResultWrapper
45 */
46 /* static */ function listAllPages() {
47 $dbr =& wfGetDB( DB_SLAVE );
48 $archive = $dbr->tableName( 'archive' );
49
50 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM $archive " .
51 "GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
52
53 return $dbr->resultObject( $dbr->query( $sql, 'PageArchive::listAllPages' ) );
54 }
55
56 /**
57 * List the revisions of the given page. Returns result wrapper with
58 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
59 *
60 * @return ResultWrapper
61 */
62 function listRevisions() {
63 $dbr =& wfGetDB( DB_SLAVE );
64 $res = $dbr->select( 'archive',
65 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment' ),
66 array( 'ar_namespace' => $this->title->getNamespace(),
67 'ar_title' => $this->title->getDBkey() ),
68 'PageArchive::listRevisions',
69 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
70 $ret = $dbr->resultObject( $res );
71 return $ret;
72 }
73
74 /**
75 * Fetch (and decompress if necessary) the stored text for the deleted
76 * revision of the page with the given timestamp.
77 *
78 * @return string
79 */
80 function getRevisionText( $timestamp ) {
81 $fname = 'PageArchive::getRevisionText';
82 $dbr =& wfGetDB( DB_SLAVE );
83 $row = $dbr->selectRow( 'archive',
84 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
85 array( 'ar_namespace' => $this->title->getNamespace(),
86 'ar_title' => $this->title->getDbkey(),
87 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
88 $fname );
89 return $this->getTextFromRow( $row );
90 }
91
92 /**
93 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
94 */
95 function getTextFromRow( $row ) {
96 $fname = 'PageArchive::getTextFromRow';
97
98 if( is_null( $row->ar_text_id ) ) {
99 // An old row from MediaWiki 1.4 or previous.
100 // Text is embedded in this row in classic compression format.
101 return Revision::getRevisionText( $row, "ar_" );
102 } else {
103 // New-style: keyed to the text storage backend.
104 $dbr =& wfGetDB( DB_SLAVE );
105 $text = $dbr->selectRow( 'text',
106 array( 'old_text', 'old_flags' ),
107 array( 'old_id' => $row->ar_text_id ),
108 $fname );
109 return Revision::getRevisionText( $text );
110 }
111 }
112
113
114 /**
115 * Fetch (and decompress if necessary) the stored text of the most
116 * recently edited deleted revision of the page.
117 *
118 * If there are no archived revisions for the page, returns NULL.
119 *
120 * @return string
121 */
122 function getLastRevisionText() {
123 $dbr =& wfGetDB( DB_SLAVE );
124 $row = $dbr->selectRow( 'archive',
125 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
126 array( 'ar_namespace' => $this->title->getNamespace(),
127 'ar_title' => $this->title->getDBkey() ),
128 'PageArchive::getLastRevisionText',
129 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
130 if( $row ) {
131 return $this->getTextFromRow( $row );
132 } else {
133 return NULL;
134 }
135 }
136
137 /**
138 * Quick check if any archived revisions are present for the page.
139 * @return bool
140 */
141 function isDeleted() {
142 $dbr =& wfGetDB( DB_SLAVE );
143 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
144 array( 'ar_namespace' => $this->title->getNamespace(),
145 'ar_title' => $this->title->getDBkey() ) );
146 return ($n > 0);
147 }
148
149 /**
150 * This is the meaty bit -- restores archived revisions of the given page
151 * to the cur/old tables. If the page currently exists, all revisions will
152 * be stuffed into old, otherwise the most recent will go into cur.
153 * The deletion log will be updated with an undeletion notice.
154 *
155 * Returns true on success.
156 *
157 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
158 * @return bool
159 */
160 function undelete( $timestamps, $comment = '' ) {
161 global $wgParser, $wgDBtype;
162
163 $fname = "doUndeleteArticle";
164 $restoreAll = empty( $timestamps );
165 $restoreRevisions = count( $timestamps );
166
167 $dbw =& wfGetDB( DB_MASTER );
168 extract( $dbw->tableNames( 'page', 'archive' ) );
169
170 # Does this page already exist? We'll have to update it...
171 $article = new Article( $this->title );
172 $options = ( $wgDBtype == 'PostgreSQL' )
173 ? '' // pg doesn't support this?
174 : 'FOR UPDATE';
175 $page = $dbw->selectRow( 'page',
176 array( 'page_id', 'page_latest' ),
177 array( 'page_namespace' => $this->title->getNamespace(),
178 'page_title' => $this->title->getDBkey() ),
179 $fname,
180 $options );
181 if( $page ) {
182 # Page already exists. Import the history, and if necessary
183 # we'll update the latest revision field in the record.
184 $newid = 0;
185 $pageId = $page->page_id;
186 $previousRevId = $page->page_latest;
187 } else {
188 # Have to create a new article...
189 $newid = $article->insertOn( $dbw );
190 $pageId = $newid;
191 $previousRevId = 0;
192 }
193
194 if( $restoreAll ) {
195 $oldones = '1 = 1'; # All revisions...
196 } else {
197 $oldts = implode( ',',
198 array_map( array( &$dbw, 'addQuotes' ),
199 array_map( array( &$dbw, 'timestamp' ),
200 $timestamps ) ) );
201
202 $oldones = "ar_timestamp IN ( {$oldts} )";
203 }
204
205 /**
206 * Restore each revision...
207 */
208 $result = $dbw->select( 'archive',
209 /* fields */ array(
210 'ar_rev_id',
211 'ar_text',
212 'ar_comment',
213 'ar_user',
214 'ar_user_text',
215 'ar_timestamp',
216 'ar_minor_edit',
217 'ar_flags',
218 'ar_text_id' ),
219 /* WHERE */ array(
220 'ar_namespace' => $this->title->getNamespace(),
221 'ar_title' => $this->title->getDBkey(),
222 $oldones ),
223 $fname,
224 /* options */ array(
225 'ORDER BY' => 'ar_timestamp' )
226 );
227 $revision = null;
228 $newRevId = $previousRevId;
229
230 while( $row = $dbw->fetchObject( $result ) ) {
231 if( $row->ar_text_id ) {
232 // Revision was deleted in 1.5+; text is in
233 // the regular text table, use the reference.
234 // Specify null here so the so the text is
235 // dereferenced for page length info if needed.
236 $revText = null;
237 } else {
238 // Revision was deleted in 1.4 or earlier.
239 // Text is squashed into the archive row, and
240 // a new text table entry will be created for it.
241 $revText = Revision::getRevisionText( $row, 'ar_' );
242 }
243 $revision = new Revision( array(
244 'page' => $pageId,
245 'id' => $row->ar_rev_id,
246 'text' => $revText,
247 'comment' => $row->ar_comment,
248 'user' => $row->ar_user,
249 'user_text' => $row->ar_user_text,
250 'timestamp' => $row->ar_timestamp,
251 'minor_edit' => $row->ar_minor_edit,
252 'text_id' => $row->ar_text_id,
253 ) );
254 $newRevId = $revision->insertOn( $dbw );
255 }
256
257 if( $revision ) {
258 # FIXME: Update latest if newer as well...
259 if( $newid ) {
260 # FIXME: update article count if changed...
261 $article->updateRevisionOn( $dbw, $revision, $previousRevId );
262
263 # Finally, clean up the link tables
264 $options = new ParserOptions;
265 $parserOutput = $wgParser->parse( $revision->getText(), $this->title, $options,
266 true, true, $newRevId );
267 $u = new LinksUpdate( $this->title, $parserOutput );
268 $u->doUpdate();
269
270 #TODO: SearchUpdate, etc.
271 }
272
273 if( $newid ) {
274 Article::onArticleCreate( $this->title );
275 } else {
276 Article::onArticleEdit( $this->title );
277 }
278 } else {
279 # Something went terribly wrong!
280 }
281
282 # Now that it's safely stored, take it out of the archive
283 $dbw->delete( 'archive',
284 /* WHERE */ array(
285 'ar_namespace' => $this->title->getNamespace(),
286 'ar_title' => $this->title->getDBkey(),
287 $oldones ),
288 $fname );
289
290 # Touch the log!
291 $log = new LogPage( 'delete' );
292 if( $restoreAll ) {
293 $reason = $comment;
294 } else {
295 $reason = wfMsgForContent( 'undeletedrevisions', $restoreRevisions );
296 if( trim( $comment ) != '' )
297 $reason .= ": {$comment}";
298 }
299 $log->addEntry( 'restore', $this->title, $reason );
300
301 return true;
302 }
303 }
304
305 /**
306 *
307 * @package MediaWiki
308 * @subpackage SpecialPage
309 */
310 class UndeleteForm {
311 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
312 var $mTargetTimestamp, $mAllowed, $mComment;
313
314 function UndeleteForm( &$request, $par = "" ) {
315 global $wgUser;
316 $this->mAction = $request->getText( 'action' );
317 $this->mTarget = $request->getText( 'target' );
318 $this->mTimestamp = $request->getText( 'timestamp' );
319
320 $posted = $request->wasPosted() &&
321 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
322 $this->mRestore = $request->getCheck( 'restore' ) && $posted;
323 $this->mPreview = $request->getCheck( 'preview' ) && $posted;
324 $this->mComment = $request->getText( 'wpComment' );
325
326 if( $par != "" ) {
327 $this->mTarget = $par;
328 }
329 if ( $wgUser->isAllowed( 'delete' ) && !$wgUser->isBlocked() ) {
330 $this->mAllowed = true;
331 } else {
332 $this->mAllowed = false;
333 $this->mTimestamp = '';
334 $this->mRestore = false;
335 }
336 if ( $this->mTarget !== "" ) {
337 $this->mTargetObj = Title::newFromURL( $this->mTarget );
338 } else {
339 $this->mTargetObj = NULL;
340 }
341 if( $this->mRestore ) {
342 $timestamps = array();
343 foreach( $_REQUEST as $key => $val ) {
344 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
345 array_push( $timestamps, $matches[1] );
346 }
347 }
348 rsort( $timestamps );
349 $this->mTargetTimestamp = $timestamps;
350 }
351 }
352
353 function execute() {
354
355 if( is_null( $this->mTargetObj ) ) {
356 return $this->showList();
357 }
358 if( $this->mTimestamp !== '' ) {
359 return $this->showRevision( $this->mTimestamp );
360 }
361 if( $this->mRestore && $this->mAction == "submit" ) {
362 return $this->undelete();
363 }
364 return $this->showHistory();
365 }
366
367 /* private */ function showList() {
368 global $wgLang, $wgContLang, $wgUser, $wgOut;
369 $fname = "UndeleteForm::showList";
370
371 # List undeletable articles
372 $result = PageArchive::listAllPages();
373
374 if ( $this->mAllowed ) {
375 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
376 } else {
377 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
378 }
379 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
380
381 $sk = $wgUser->getSkin();
382 $undelete =& Title::makeTitle( NS_SPECIAL, 'Undelete' );
383 $wgOut->addHTML( "<ul>\n" );
384 while( $row = $result->fetchObject() ) {
385 $n = ($row->ar_namespace ?
386 ($wgContLang->getNsText( $row->ar_namespace ) . ":") : "").
387 $row->ar_title;
388 $link = $sk->makeKnownLinkObj( $undelete,
389 htmlspecialchars( $n ), "target=" . urlencode( $n ) );
390 $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
391 $wgLang->formatNum( $row->count ) ) );
392 $wgOut->addHTML( "<li>$link ($revisions)</li>\n" );
393 }
394 $result->free();
395 $wgOut->addHTML( "</ul>\n" );
396
397 return true;
398 }
399
400 /* private */ function showRevision( $timestamp ) {
401 global $wgLang, $wgUser, $wgOut;
402 $fname = "UndeleteForm::showRevision";
403
404 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
405
406 $archive =& new PageArchive( $this->mTargetObj );
407 $text = $archive->getRevisionText( $timestamp );
408
409 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
410 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
411 $wgLang->date( $timestamp ) ) . ")\n" );
412
413 if( $this->mPreview ) {
414 $wgOut->addHtml( "<hr />\n" );
415 $wgOut->addWikiText( $text );
416 }
417
418 $self = Title::makeTitle( NS_SPECIAL, "Undelete" );
419
420 $wgOut->addHtml(
421 wfElement( 'textarea', array(
422 'readonly' => true,
423 'cols' => intval( $wgUser->getOption( 'cols' ) ),
424 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
425 $text . "\n" ) .
426 wfOpenElement( 'div' ) .
427 wfOpenElement( 'form', array(
428 'method' => 'post',
429 'action' => $self->getLocalURL( "action=submit" ) ) ) .
430 wfElement( 'input', array(
431 'type' => 'hidden',
432 'name' => 'target',
433 'value' => $this->mTargetObj->getPrefixedDbKey() ) ) .
434 wfElement( 'input', array(
435 'type' => 'hidden',
436 'name' => 'timestamp',
437 'value' => $timestamp ) ) .
438 wfElement( 'input', array(
439 'type' => 'hidden',
440 'name' => 'wpEditToken',
441 'value' => $wgUser->editToken() ) ) .
442 wfElement( 'input', array(
443 'type' => 'hidden',
444 'name' => 'preview',
445 'value' => '1' ) ) .
446 wfElement( 'input', array(
447 'type' => 'submit',
448 'value' => wfMsg( 'showpreview' ) ) ) .
449 wfCloseElement( 'form' ) .
450 wfCloseElement( 'div' ) );
451 }
452
453 /* private */ function showHistory() {
454 global $wgLang, $wgUser, $wgOut;
455
456 $sk = $wgUser->getSkin();
457 if ( $this->mAllowed ) {
458 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
459 } else {
460 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
461 }
462
463 $archive = new PageArchive( $this->mTargetObj );
464 $text = $archive->getLastRevisionText();
465 if( is_null( $text ) ) {
466 $wgOut->addWikiText( wfMsg( "nohistory" ) );
467 return;
468 }
469 if ( $this->mAllowed ) {
470 $wgOut->addWikiText( wfMsg( "undeletehistory" ) );
471 } else {
472 $wgOut->addWikiText( wfMsg( "undeletehistorynoadmin" ) );
473 }
474
475 # List all stored revisions
476 $revisions = $archive->listRevisions();
477
478 if ( $this->mAllowed ) {
479 $titleObj = Title::makeTitle( NS_SPECIAL, "Undelete" );
480 $action = $titleObj->getLocalURL( "action=submit" );
481 # Start the form here
482 $top = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
483 $wgOut->addHtml( $top );
484 }
485
486 # Show relevant lines from the deletion log:
487 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
488 require_once( 'SpecialLog.php' );
489 $logViewer =& new LogViewer(
490 new LogReader(
491 new FauxRequest(
492 array( 'page' => $this->mTargetObj->getPrefixedText(),
493 'type' => 'delete' ) ) ) );
494 $logViewer->showList( $wgOut );
495
496 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
497
498 if( $this->mAllowed ) {
499 # Brief explanation of how it all works
500 $wgOut->addHtml( '<fieldset>' );
501 #$wgOut->addWikiText( wfMsg( 'undeleteextrahelp' ) );
502 # Format the user-visible controls (comment field, submission button)
503 # in a nice little table
504 $table = '<table><tr>';
505 $table .= '<td colspan="2">' . wfMsgWikiHtml( 'undeleteextrahelp' ) . '</td></tr><tr>';
506 $table .= '<td align="right"><strong>' . wfMsgHtml( 'undeletecomment' ) . '</strong></td>';
507 $table .= '<td>' . wfInput( 'wpComment', 50, $this->mComment ) . '</td>';
508 $table .= '</tr><tr><td>&nbsp;</td><td>';
509 $table .= wfSubmitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore' ) );
510 $table .= wfElement( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ) ) );
511 $table .= '</td></tr></table></fieldset>';
512 $wgOut->addHtml( $table );
513 }
514
515 # The page's stored (deleted) history:
516 $wgOut->addHTML("<ul>");
517 $target = urlencode( $this->mTarget );
518 while( $row = $revisions->fetchObject() ) {
519 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
520 if ( $this->mAllowed ) {
521 $checkBox = "<input type=\"checkbox\" name=\"ts$ts\" value=\"1\" />";
522 $pageLink = $sk->makeKnownLinkObj( $titleObj,
523 $wgLang->timeanddate( $ts, true ),
524 "target=$target&timestamp=$ts" );
525 } else {
526 $checkBox = '';
527 $pageLink = $wgLang->timeanddate( $ts, true );
528 }
529 $userLink = htmlspecialchars( $row->ar_user_text );
530 if( $row->ar_user ) {
531 $userLink = $sk->makeKnownLinkObj(
532 Title::makeTitle( NS_USER, $row->ar_user_text ),
533 $userLink );
534 } else {
535 $userLink = $sk->makeKnownLinkObj(
536 Title::makeTitle( NS_SPECIAL, 'Contributions' ),
537 $userLink, 'target=' . $row->ar_user_text );
538 }
539 $comment = $sk->commentBlock( $row->ar_comment );
540 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $comment</li>\n" );
541
542 }
543 $revisions->free();
544 $wgOut->addHTML("</ul>");
545
546 if ( $this->mAllowed ) {
547 # Slip in the hidden controls here
548 $misc = wfHidden( 'target', $this->mTarget );
549 $misc .= wfHidden( 'wpEditToken', $wgUser->editToken() );
550 $wgOut->addHtml( $misc . '</form>' );
551 }
552
553 return true;
554 }
555
556 function undelete() {
557 global $wgOut, $wgUser;
558 if( !is_null( $this->mTargetObj ) ) {
559 $archive = new PageArchive( $this->mTargetObj );
560 if( $archive->undelete( $this->mTargetTimestamp, $this->mComment ) ) {
561 $skin =& $wgUser->getSkin();
562 $link = $skin->makeKnownLinkObj( $this->mTargetObj );
563 $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
564
565 if (NS_IMAGE == $this->mTargetObj->getNamespace()) {
566 /* refresh image metadata cache */
567 new Image( $this->mTargetObj );
568 }
569
570 return true;
571 }
572 }
573 $wgOut->fatalError( wfMsg( "cannotundelete" ) );
574 return false;
575 }
576 }
577
578 ?>