4 * Special page allowing users with the appropriate permissions to view
5 * and restore deleted content
8 * @subpackage Special pages
12 require_once( 'Revision.php' );
17 function wfSpecialUndelete( $par ) {
20 $form = new UndeleteForm( $wgRequest, $par );
27 * @subpackage SpecialPage
33 function PageArchive( &$title ) {
34 if( is_null( $title ) ) {
35 wfDebugDieBacktrace( 'Archiver() given a null title.');
37 $this->title
=& $title;
41 * List all deleted pages recorded in the archive table. Returns result
42 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
43 * namespace/title. Can be called staticaly.
45 * @return ResultWrapper
47 /* static */ function listAllPages() {
48 $dbr =& wfGetDB( DB_SLAVE
);
49 $archive = $dbr->tableName( 'archive' );
51 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM $archive " .
52 "GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
54 return $dbr->resultObject( $dbr->query( $sql, 'PageArchive::listAllPages' ) );
58 * List the revisions of the given page. Returns result wrapper with
59 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
61 * @return ResultWrapper
63 function listRevisions() {
64 $dbr =& wfGetDB( DB_SLAVE
);
65 $res = $dbr->select( 'archive',
66 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment' ),
67 array( 'ar_namespace' => $this->title
->getNamespace(),
68 'ar_title' => $this->title
->getDBkey() ),
69 'PageArchive::listRevisions',
70 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
71 $ret = $dbr->resultObject( $res );
76 * Fetch (and decompress if necessary) the stored text for the deleted
77 * revision of the page with the given timestamp.
81 function getRevisionText( $timestamp ) {
82 $fname = 'PageArchive::getRevisionText';
83 $dbr =& wfGetDB( DB_SLAVE
);
84 $row = $dbr->selectRow( 'archive',
85 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
86 array( 'ar_namespace' => $this->title
->getNamespace(),
87 'ar_title' => $this->title
->getDbkey(),
88 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
90 return $this->getTextFromRow( $row );
94 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
96 function getTextFromRow( $row ) {
97 $fname = 'PageArchive::getTextFromRow';
99 if( is_null( $row->ar_text_id
) ) {
100 // An old row from MediaWiki 1.4 or previous.
101 // Text is embedded in this row in classic compression format.
102 return Revision
::getRevisionText( $row, "ar_" );
104 // New-style: keyed to the text storage backend.
105 $dbr =& wfGetDB( DB_SLAVE
);
106 $text = $dbr->selectRow( 'text',
107 array( 'old_text', 'old_flags' ),
108 array( 'old_id' => $row->ar_text_id
),
110 return Revision
::getRevisionText( $text );
116 * Fetch (and decompress if necessary) the stored text of the most
117 * recently edited deleted revision of the page.
119 * If there are no archived revisions for the page, returns NULL.
123 function getLastRevisionText() {
124 $dbr =& wfGetDB( DB_SLAVE
);
125 $row = $dbr->selectRow( 'archive',
126 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
127 array( 'ar_namespace' => $this->title
->getNamespace(),
128 'ar_title' => $this->title
->getDBkey() ),
129 'PageArchive::getLastRevisionText',
130 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
132 return $this->getTextFromRow( $row );
139 * Quick check if any archived revisions are present for the page.
142 function isDeleted() {
143 $dbr =& wfGetDB( DB_SLAVE
);
144 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
145 array( 'ar_namespace' => $this->title
->getNamespace(),
146 'ar_title' => $this->title
->getDBkey() ) );
151 * This is the meaty bit -- restores archived revisions of the given page
152 * to the cur/old tables. If the page currently exists, all revisions will
153 * be stuffed into old, otherwise the most recent will go into cur.
154 * The deletion log will be updated with an undeletion notice.
156 * Returns true on success.
158 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
161 function undelete( $timestamps, $comment = '' ) {
162 global $wgParser, $wgDBtype;
164 $fname = "doUndeleteArticle";
165 $restoreAll = empty( $timestamps );
166 $restoreRevisions = count( $timestamps );
168 $dbw =& wfGetDB( DB_MASTER
);
169 extract( $dbw->tableNames( 'page', 'archive' ) );
171 # Does this page already exist? We'll have to update it...
172 $article = new Article( $this->title
);
173 $options = ( $wgDBtype == 'PostgreSQL' )
174 ?
'' // pg doesn't support this?
176 $page = $dbw->selectRow( 'page',
177 array( 'page_id', 'page_latest' ),
178 array( 'page_namespace' => $this->title
->getNamespace(),
179 'page_title' => $this->title
->getDBkey() ),
183 # Page already exists. Import the history, and if necessary
184 # we'll update the latest revision field in the record.
186 $pageId = $page->page_id
;
187 $previousRevId = $page->page_latest
;
189 # Have to create a new article...
190 $newid = $article->insertOn( $dbw );
196 $oldones = '1 = 1'; # All revisions...
198 $oldts = implode( ',',
199 array_map( array( &$dbw, 'addQuotes' ),
200 array_map( array( &$dbw, 'timestamp' ),
203 $oldones = "ar_timestamp IN ( {$oldts} )";
207 * Restore each revision...
209 $result = $dbw->select( 'archive',
221 'ar_namespace' => $this->title
->getNamespace(),
222 'ar_title' => $this->title
->getDBkey(),
226 'ORDER BY' => 'ar_timestamp' )
229 $newRevId = $previousRevId;
231 while( $row = $dbw->fetchObject( $result ) ) {
232 if( $row->ar_text_id
) {
233 // Revision was deleted in 1.5+; text is in
234 // the regular text table, use the reference.
235 // Specify null here so the so the text is
236 // dereferenced for page length info if needed.
239 // Revision was deleted in 1.4 or earlier.
240 // Text is squashed into the archive row, and
241 // a new text table entry will be created for it.
242 $revText = Revision
::getRevisionText( $row, 'ar_' );
244 $revision = new Revision( array(
246 'id' => $row->ar_rev_id
,
248 'comment' => $row->ar_comment
,
249 'user' => $row->ar_user
,
250 'user_text' => $row->ar_user_text
,
251 'timestamp' => $row->ar_timestamp
,
252 'minor_edit' => $row->ar_minor_edit
,
253 'text_id' => $row->ar_text_id
,
255 $newRevId = $revision->insertOn( $dbw );
259 # FIXME: Update latest if newer as well...
261 # FIXME: update article count if changed...
262 $article->updateRevisionOn( $dbw, $revision, $previousRevId );
264 # Finally, clean up the link tables
265 $options = new ParserOptions
;
266 $parserOutput = $wgParser->parse( $revision->getText(), $this->title
, $options,
267 true, true, $newRevId );
268 $u = new LinksUpdate( $this->title
, $parserOutput );
271 #TODO: SearchUpdate, etc.
275 Article
::onArticleCreate( $this->title
);
277 Article
::onArticleEdit( $this->title
);
280 # Something went terribly wrong!
283 # Now that it's safely stored, take it out of the archive
284 $dbw->delete( 'archive',
286 'ar_namespace' => $this->title
->getNamespace(),
287 'ar_title' => $this->title
->getDBkey(),
292 $log = new LogPage( 'delete' );
296 $reason = wfMsgForContent( 'undeletedrevisions', $restoreRevisions );
297 if( trim( $comment ) != '' )
298 $reason .= ": {$comment}";
300 $log->addEntry( 'restore', $this->title
, $reason );
309 * @subpackage SpecialPage
322 function UndeleteForm( &$request, $par = "" ) {
324 $this->mAction
= $request->getText( 'action' );
325 $this->mTarget
= $request->getText( 'target' );
326 $this->mTimestamp
= $request->getText( 'timestamp' );
328 $posted = $request->wasPosted() &&
329 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
330 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
331 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
332 $this->mComment
= $request->getText( 'wpComment' );
335 $this->mTarget
= $par;
337 if ( $wgUser->isAllowed( 'delete' ) && !$wgUser->isBlocked() ) {
338 $this->mAllowed
= true;
340 $this->mAllowed
= false;
341 $this->mTimestamp
= '';
342 $this->mRestore
= false;
344 if ( $this->mTarget
!== "" ) {
345 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
347 $this->mTargetObj
= NULL;
349 if( $this->mRestore
) {
350 $timestamps = array();
351 foreach( $_REQUEST as $key => $val ) {
352 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
353 array_push( $timestamps, $matches[1] );
356 rsort( $timestamps );
357 $this->mTargetTimestamp
= $timestamps;
363 if( is_null( $this->mTargetObj
) ) {
364 return $this->showList();
366 if( $this->mTimestamp
!== '' ) {
367 return $this->showRevision( $this->mTimestamp
);
369 if( $this->mRestore
&& $this->mAction
== "submit" ) {
370 return $this->undelete();
372 return $this->showHistory();
375 /* private */ function showList() {
376 global $wgLang, $wgContLang, $wgUser, $wgOut;
377 $fname = "UndeleteForm::showList";
379 # List undeletable articles
380 $result = PageArchive
::listAllPages();
382 if ( $this->mAllowed
) {
383 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
385 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
387 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
389 $sk = $wgUser->getSkin();
390 $undelete =& Title
::makeTitle( NS_SPECIAL
, 'Undelete' );
391 $wgOut->addHTML( "<ul>\n" );
392 while( $row = $result->fetchObject() ) {
393 $n = ($row->ar_namespace ?
394 ($wgContLang->getNsText( $row->ar_namespace
) . ":") : "").
396 $link = $sk->makeKnownLinkObj( $undelete,
397 htmlspecialchars( $n ), "target=" . urlencode( $n ) );
398 $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
399 $wgLang->formatNum( $row->count
) ) );
400 $wgOut->addHTML( "<li>$link $revisions</li>\n" );
403 $wgOut->addHTML( "</ul>\n" );
408 /* private */ function showRevision( $timestamp ) {
409 global $wgLang, $wgUser, $wgOut;
410 $fname = "UndeleteForm::showRevision";
412 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
414 $archive =& new PageArchive( $this->mTargetObj
);
415 $text = $archive->getRevisionText( $timestamp );
417 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
418 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
419 $wgLang->date( $timestamp ) ) . ")\n" );
421 if( $this->mPreview
) {
422 $wgOut->addHtml( "<hr />\n" );
423 $wgOut->addWikiText( $text );
426 $self = Title
::makeTitle( NS_SPECIAL
, "Undelete" );
429 wfElement( 'textarea', array(
431 'cols' => intval( $wgUser->getOption( 'cols' ) ),
432 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
434 wfOpenElement( 'div' ) .
435 wfOpenElement( 'form', array(
437 'action' => $self->getLocalURL( "action=submit" ) ) ) .
438 wfElement( 'input', array(
441 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
442 wfElement( 'input', array(
444 'name' => 'timestamp',
445 'value' => $timestamp ) ) .
446 wfElement( 'input', array(
448 'name' => 'wpEditToken',
449 'value' => $wgUser->editToken() ) ) .
450 wfElement( 'input', array(
454 wfElement( 'input', array(
456 'value' => wfMsg( 'showpreview' ) ) ) .
457 wfCloseElement( 'form' ) .
458 wfCloseElement( 'div' ) );
461 /* private */ function showHistory() {
462 global $wgLang, $wgUser, $wgOut;
464 $sk = $wgUser->getSkin();
465 if ( $this->mAllowed
) {
466 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
468 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
471 $archive = new PageArchive( $this->mTargetObj
);
472 $text = $archive->getLastRevisionText();
473 if( is_null( $text ) ) {
474 $wgOut->addWikiText( wfMsg( "nohistory" ) );
477 if ( $this->mAllowed
) {
478 $wgOut->addWikiText( wfMsg( "undeletehistory" ) );
480 $wgOut->addWikiText( wfMsg( "undeletehistorynoadmin" ) );
483 # List all stored revisions
484 $revisions = $archive->listRevisions();
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 ) );
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 # Brief explanation of how it all works
508 $wgOut->addHtml( '<fieldset>' );
509 #$wgOut->addWikiText( wfMsg( 'undeleteextrahelp' ) );
510 # Format the user-visible controls (comment field, submission button)
511 # in a nice little table
512 $table = '<table><tr>';
513 $table .= '<td colspan="2">' . wfMsgWikiHtml( 'undeleteextrahelp' ) . '</td></tr><tr>';
514 $table .= '<td align="right"><strong>' . wfMsgHtml( 'undeletecomment' ) . '</strong></td>';
515 $table .= '<td>' . wfInput( 'wpComment', 50, $this->mComment
) . '</td>';
516 $table .= '</tr><tr><td> </td><td>';
517 $table .= wfSubmitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore' ) );
518 $table .= wfElement( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ) ) );
519 $table .= '</td></tr></table></fieldset>';
520 $wgOut->addHtml( $table );
523 # The page's stored (deleted) history:
524 $wgOut->addHTML("<ul>");
525 $target = urlencode( $this->mTarget
);
526 while( $row = $revisions->fetchObject() ) {
527 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
528 if ( $this->mAllowed
) {
529 $checkBox = "<input type=\"checkbox\" name=\"ts$ts\" value=\"1\" />";
530 $pageLink = $sk->makeKnownLinkObj( $titleObj,
531 $wgLang->timeanddate( $ts, true ),
532 "target=$target×tamp=$ts" );
535 $pageLink = $wgLang->timeanddate( $ts, true );
537 $userLink = htmlspecialchars( $row->ar_user_text
);
538 if( $row->ar_user
) {
539 $userLink = $sk->makeKnownLinkObj(
540 Title
::makeTitle( NS_USER
, $row->ar_user_text
),
543 $userLink = $sk->makeKnownLinkObj(
544 Title
::makeTitle( NS_SPECIAL
, 'Contributions' ),
545 $userLink, 'target=' . $row->ar_user_text
);
547 $comment = $sk->commentBlock( $row->ar_comment
);
548 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $comment</li>\n" );
552 $wgOut->addHTML("</ul>");
554 if ( $this->mAllowed
) {
555 # Slip in the hidden controls here
556 $misc = wfHidden( 'target', $this->mTarget
);
557 $misc .= wfHidden( 'wpEditToken', $wgUser->editToken() );
558 $wgOut->addHtml( $misc . '</form>' );
564 function undelete() {
566 if( !is_null( $this->mTargetObj
) ) {
567 $archive = new PageArchive( $this->mTargetObj
);
568 if( $archive->undelete( $this->mTargetTimestamp
, $this->mComment
) ) {
569 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget
) );
571 if (NS_IMAGE
== $this->mTargetObj
->getNamespace()) {
572 /* refresh image metadata cache */
573 new Image( $this->mTargetObj
);
579 $wgOut->fatalError( wfMsg( "cannotundelete" ) );