4 * Special page allowing users with the appropriate permissions to view
5 * and restore deleted content
8 * @subpackage Special pages
14 function wfSpecialUndelete( $par ) {
17 $form = new UndeleteForm( $wgRequest, $par );
24 * @subpackage SpecialPage
29 function PageArchive( &$title ) {
30 if( is_null( $title ) ) {
31 throw new MWException( 'Archiver() given a null title.');
33 $this->title
=& $title;
37 * List all deleted pages recorded in the archive table. Returns result
38 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
39 * namespace/title. Can be called staticaly.
41 * @return ResultWrapper
43 /* static */ function listAllPages() {
44 $dbr =& wfGetDB( DB_SLAVE
);
45 $archive = $dbr->tableName( 'archive' );
47 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM $archive " .
48 "GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
50 return $dbr->resultObject( $dbr->query( $sql, 'PageArchive::listAllPages' ) );
54 * List the revisions of the given page. Returns result wrapper with
55 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
57 * @return ResultWrapper
59 function listRevisions() {
60 $dbr =& wfGetDB( DB_SLAVE
);
61 $res = $dbr->select( 'archive',
62 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment' ),
63 array( 'ar_namespace' => $this->title
->getNamespace(),
64 'ar_title' => $this->title
->getDBkey() ),
65 'PageArchive::listRevisions',
66 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
67 $ret = $dbr->resultObject( $res );
72 * Fetch (and decompress if necessary) the stored text for the deleted
73 * revision of the page with the given timestamp.
77 function getRevisionText( $timestamp ) {
78 $fname = 'PageArchive::getRevisionText';
79 $dbr =& wfGetDB( DB_SLAVE
);
80 $row = $dbr->selectRow( 'archive',
81 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
82 array( 'ar_namespace' => $this->title
->getNamespace(),
83 'ar_title' => $this->title
->getDbkey(),
84 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
86 return $this->getTextFromRow( $row );
90 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
92 function getTextFromRow( $row ) {
93 $fname = 'PageArchive::getTextFromRow';
95 if( is_null( $row->ar_text_id
) ) {
96 // An old row from MediaWiki 1.4 or previous.
97 // Text is embedded in this row in classic compression format.
98 return Revision
::getRevisionText( $row, "ar_" );
100 // New-style: keyed to the text storage backend.
101 $dbr =& wfGetDB( DB_SLAVE
);
102 $text = $dbr->selectRow( 'text',
103 array( 'old_text', 'old_flags' ),
104 array( 'old_id' => $row->ar_text_id
),
106 return Revision
::getRevisionText( $text );
112 * Fetch (and decompress if necessary) the stored text of the most
113 * recently edited deleted revision of the page.
115 * If there are no archived revisions for the page, returns NULL.
119 function getLastRevisionText() {
120 $dbr =& wfGetDB( DB_SLAVE
);
121 $row = $dbr->selectRow( 'archive',
122 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
123 array( 'ar_namespace' => $this->title
->getNamespace(),
124 'ar_title' => $this->title
->getDBkey() ),
125 'PageArchive::getLastRevisionText',
126 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
128 return $this->getTextFromRow( $row );
135 * Quick check if any archived revisions are present for the page.
138 function isDeleted() {
139 $dbr =& wfGetDB( DB_SLAVE
);
140 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
141 array( 'ar_namespace' => $this->title
->getNamespace(),
142 'ar_title' => $this->title
->getDBkey() ) );
147 * This is the meaty bit -- restores archived revisions of the given page
148 * to the cur/old tables. If the page currently exists, all revisions will
149 * be stuffed into old, otherwise the most recent will go into cur.
150 * The deletion log will be updated with an undeletion notice.
152 * Returns true on success.
154 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
157 function undelete( $timestamps, $comment = '' ) {
158 global $wgParser, $wgDBtype;
160 $fname = "doUndeleteArticle";
161 $restoreAll = empty( $timestamps );
162 $restoreRevisions = count( $timestamps );
164 $dbw =& wfGetDB( DB_MASTER
);
165 extract( $dbw->tableNames( 'page', 'archive' ) );
167 # Does this page already exist? We'll have to update it...
168 $article = new Article( $this->title
);
169 $options = ( $wgDBtype == 'PostgreSQL' )
170 ?
'' // pg doesn't support this?
172 $page = $dbw->selectRow( 'page',
173 array( 'page_id', 'page_latest' ),
174 array( 'page_namespace' => $this->title
->getNamespace(),
175 'page_title' => $this->title
->getDBkey() ),
179 # Page already exists. Import the history, and if necessary
180 # we'll update the latest revision field in the record.
182 $pageId = $page->page_id
;
183 $previousRevId = $page->page_latest
;
185 # Have to create a new article...
186 $newid = $article->insertOn( $dbw );
192 $oldones = '1 = 1'; # All revisions...
194 $oldts = implode( ',',
195 array_map( array( &$dbw, 'addQuotes' ),
196 array_map( array( &$dbw, 'timestamp' ),
199 $oldones = "ar_timestamp IN ( {$oldts} )";
203 * Restore each revision...
205 $result = $dbw->select( 'archive',
217 'ar_namespace' => $this->title
->getNamespace(),
218 'ar_title' => $this->title
->getDBkey(),
222 'ORDER BY' => 'ar_timestamp' )
225 $newRevId = $previousRevId;
227 while( $row = $dbw->fetchObject( $result ) ) {
228 if( $row->ar_text_id
) {
229 // Revision was deleted in 1.5+; text is in
230 // the regular text table, use the reference.
231 // Specify null here so the so the text is
232 // dereferenced for page length info if needed.
235 // Revision was deleted in 1.4 or earlier.
236 // Text is squashed into the archive row, and
237 // a new text table entry will be created for it.
238 $revText = Revision
::getRevisionText( $row, 'ar_' );
240 $revision = new Revision( array(
242 'id' => $row->ar_rev_id
,
244 'comment' => $row->ar_comment
,
245 'user' => $row->ar_user
,
246 'user_text' => $row->ar_user_text
,
247 'timestamp' => $row->ar_timestamp
,
248 'minor_edit' => $row->ar_minor_edit
,
249 'text_id' => $row->ar_text_id
,
251 $newRevId = $revision->insertOn( $dbw );
255 # FIXME: Update latest if newer as well...
257 # FIXME: update article count if changed...
258 $article->updateRevisionOn( $dbw, $revision, $previousRevId );
260 # Finally, clean up the link tables
261 $options = new ParserOptions
;
262 $parserOutput = $wgParser->parse( $revision->getText(), $this->title
, $options,
263 true, true, $newRevId );
264 $u = new LinksUpdate( $this->title
, $parserOutput );
267 #TODO: SearchUpdate, etc.
271 Article
::onArticleCreate( $this->title
);
273 Article
::onArticleEdit( $this->title
);
276 # Something went terribly wrong!
279 # Now that it's safely stored, take it out of the archive
280 $dbw->delete( 'archive',
282 'ar_namespace' => $this->title
->getNamespace(),
283 'ar_title' => $this->title
->getDBkey(),
288 $log = new LogPage( 'delete' );
292 $reason = wfMsgForContent( 'undeletedrevisions', $restoreRevisions );
293 if( trim( $comment ) != '' )
294 $reason .= ": {$comment}";
296 $log->addEntry( 'restore', $this->title
, $reason );
305 * @subpackage SpecialPage
308 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
309 var $mTargetTimestamp, $mAllowed, $mComment;
311 function UndeleteForm( &$request, $par = "" ) {
313 $this->mAction
= $request->getText( 'action' );
314 $this->mTarget
= $request->getText( 'target' );
315 $this->mTimestamp
= $request->getText( 'timestamp' );
317 $posted = $request->wasPosted() &&
318 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
319 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
320 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
321 $this->mComment
= $request->getText( 'wpComment' );
324 $this->mTarget
= $par;
326 if ( $wgUser->isAllowed( 'delete' ) && !$wgUser->isBlocked() ) {
327 $this->mAllowed
= true;
329 $this->mAllowed
= false;
330 $this->mTimestamp
= '';
331 $this->mRestore
= false;
333 if ( $this->mTarget
!== "" ) {
334 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
336 $this->mTargetObj
= NULL;
338 if( $this->mRestore
) {
339 $timestamps = array();
340 foreach( $_REQUEST as $key => $val ) {
341 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
342 array_push( $timestamps, $matches[1] );
345 rsort( $timestamps );
346 $this->mTargetTimestamp
= $timestamps;
352 if( is_null( $this->mTargetObj
) ) {
353 return $this->showList();
355 if( $this->mTimestamp
!== '' ) {
356 return $this->showRevision( $this->mTimestamp
);
358 if( $this->mRestore
&& $this->mAction
== "submit" ) {
359 return $this->undelete();
361 return $this->showHistory();
364 /* private */ function showList() {
365 global $wgLang, $wgContLang, $wgUser, $wgOut;
366 $fname = "UndeleteForm::showList";
368 # List undeletable articles
369 $result = PageArchive
::listAllPages();
371 if ( $this->mAllowed
) {
372 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
374 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
376 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
378 $sk = $wgUser->getSkin();
379 $undelete =& Title
::makeTitle( NS_SPECIAL
, 'Undelete' );
380 $wgOut->addHTML( "<ul>\n" );
381 while( $row = $result->fetchObject() ) {
382 $n = ($row->ar_namespace ?
383 ($wgContLang->getNsText( $row->ar_namespace
) . ":") : "").
385 $link = $sk->makeKnownLinkObj( $undelete,
386 htmlspecialchars( $n ), "target=" . urlencode( $n ) );
387 $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
388 $wgLang->formatNum( $row->count
) ) );
389 $wgOut->addHTML( "<li>$link ($revisions)</li>\n" );
392 $wgOut->addHTML( "</ul>\n" );
397 /* private */ function showRevision( $timestamp ) {
398 global $wgLang, $wgUser, $wgOut;
399 $fname = "UndeleteForm::showRevision";
401 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
403 $archive =& new PageArchive( $this->mTargetObj
);
404 $text = $archive->getRevisionText( $timestamp );
406 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
407 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
408 $wgLang->date( $timestamp ) ) . ")\n" );
410 if( $this->mPreview
) {
411 $wgOut->addHtml( "<hr />\n" );
412 $wgOut->addWikiText( $text );
415 $self = Title
::makeTitle( NS_SPECIAL
, "Undelete" );
418 wfElement( 'textarea', array(
420 'cols' => intval( $wgUser->getOption( 'cols' ) ),
421 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
423 wfOpenElement( 'div' ) .
424 wfOpenElement( 'form', array(
426 'action' => $self->getLocalURL( "action=submit" ) ) ) .
427 wfElement( 'input', array(
430 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
431 wfElement( 'input', array(
433 'name' => 'timestamp',
434 'value' => $timestamp ) ) .
435 wfElement( 'input', array(
437 'name' => 'wpEditToken',
438 'value' => $wgUser->editToken() ) ) .
439 wfElement( 'input', array(
443 wfElement( 'input', array(
445 'value' => wfMsg( 'showpreview' ) ) ) .
446 wfCloseElement( 'form' ) .
447 wfCloseElement( 'div' ) );
450 /* private */ function showHistory() {
451 global $wgLang, $wgUser, $wgOut;
453 $sk = $wgUser->getSkin();
454 if ( $this->mAllowed
) {
455 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
457 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
460 $archive = new PageArchive( $this->mTargetObj
);
461 $text = $archive->getLastRevisionText();
462 if( is_null( $text ) ) {
463 $wgOut->addWikiText( wfMsg( "nohistory" ) );
466 if ( $this->mAllowed
) {
467 $wgOut->addWikiText( wfMsg( "undeletehistory" ) );
469 $wgOut->addWikiText( wfMsg( "undeletehistorynoadmin" ) );
472 # List all stored revisions
473 $revisions = $archive->listRevisions();
475 # Batch existence check on user and talk pages
476 if( $revisions->numRows() > 0 ) {
477 $batch = new LinkBatch();
478 while( $row = $revisions->fetchObject() ) {
479 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
480 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
483 $revisions->seek( 0 );
486 if ( $this->mAllowed
) {
487 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Undelete" );
488 $action = $titleObj->getLocalURL( "action=submit" );
489 # Start the form here
490 $top = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
491 $wgOut->addHtml( $top );
494 # Show relevant lines from the deletion log:
495 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
496 require_once( 'SpecialLog.php' );
497 $logViewer =& new LogViewer(
500 array( 'page' => $this->mTargetObj
->getPrefixedText(),
501 'type' => 'delete' ) ) ) );
502 $logViewer->showList( $wgOut );
504 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
506 if( $this->mAllowed
) {
507 # Format the user-visible controls (comment field, submission button)
508 # in a nice little table
509 $table = '<fieldset><table><tr>';
510 $table .= '<td colspan="2">' . wfMsgWikiHtml( 'undeleteextrahelp' ) . '</td></tr><tr>';
511 $table .= '<td align="right"><strong>' . wfMsgHtml( 'undeletecomment' ) . '</strong></td>';
512 $table .= '<td>' . wfInput( 'wpComment', 50, $this->mComment
) . '</td>';
513 $table .= '</tr><tr><td> </td><td>';
514 $table .= wfSubmitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore' ) );
515 $table .= wfElement( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ) ) );
516 $table .= '</td></tr></table></fieldset>';
517 $wgOut->addHtml( $table );
520 # The page's stored (deleted) history:
521 $wgOut->addHTML("<ul>");
522 $target = urlencode( $this->mTarget
);
523 while( $row = $revisions->fetchObject() ) {
524 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
525 if ( $this->mAllowed
) {
526 $checkBox = "<input type=\"checkbox\" name=\"ts$ts\" value=\"1\" />";
527 $pageLink = $sk->makeKnownLinkObj( $titleObj,
528 $wgLang->timeanddate( $ts, true ),
529 "target=$target×tamp=$ts" );
532 $pageLink = $wgLang->timeanddate( $ts, true );
534 $userLink = $sk->userLink( $row->ar_user
, $row->ar_user_text
);
535 $comment = $sk->commentBlock( $row->ar_comment
);
536 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $comment</li>\n" );
540 $wgOut->addHTML("</ul>");
542 if ( $this->mAllowed
) {
543 # Slip in the hidden controls here
544 $misc = wfHidden( 'target', $this->mTarget
);
545 $misc .= wfHidden( 'wpEditToken', $wgUser->editToken() );
546 $wgOut->addHtml( $misc . '</form>' );
552 function undelete() {
553 global $wgOut, $wgUser;
554 if( !is_null( $this->mTargetObj
) ) {
555 $archive = new PageArchive( $this->mTargetObj
);
556 if( $archive->undelete( $this->mTargetTimestamp
, $this->mComment
) ) {
557 $skin =& $wgUser->getSkin();
558 $link = $skin->makeKnownLinkObj( $this->mTargetObj
);
559 $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
561 if (NS_IMAGE
== $this->mTargetObj
->getNamespace()) {
562 /* refresh image metadata cache */
563 new Image( $this->mTargetObj
);
569 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );