eff9c25deccd801f7886c30bb1f6ac816466ca83
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
32 function PageArchive( &$title ) {
33 if( is_null( $title ) ) {
34 wfDebugDieBacktrace( 'Archiver() given a null title.');
36 $this->title
=& $title;
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.
44 * @return ResultWrapper
46 /* static */ function listAllPages() {
47 $dbr =& wfGetDB( DB_SLAVE
);
48 $archive = $dbr->tableName( 'archive' );
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";
53 return $dbr->resultObject( $dbr->query( $sql, 'PageArchive::listAllPages' ) );
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.
60 * @return ResultWrapper
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 );
75 * Fetch (and decompress if necessary) the stored text for the deleted
76 * revision of the page with the given timestamp.
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 ) ),
89 return $this->getTextFromRow( $row );
93 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
95 function getTextFromRow( $row ) {
96 $fname = 'PageArchive::getTextFromRow';
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_" );
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
),
109 return Revision
::getRevisionText( $text );
115 * Fetch (and decompress if necessary) the stored text of the most
116 * recently edited deleted revision of the page.
118 * If there are no archived revisions for the page, returns NULL.
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' ) );
131 return $this->getTextFromRow( $row );
138 * Quick check if any archived revisions are present for the page.
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() ) );
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.
155 * Returns true on success.
157 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
160 function undelete( $timestamps, $comment = '' ) {
161 global $wgParser, $wgDBtype;
163 $fname = "doUndeleteArticle";
164 $restoreAll = empty( $timestamps );
165 $restoreRevisions = count( $timestamps );
167 $dbw =& wfGetDB( DB_MASTER
);
168 extract( $dbw->tableNames( 'page', 'archive' ) );
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?
175 $page = $dbw->selectRow( 'page',
176 array( 'page_id', 'page_latest' ),
177 array( 'page_namespace' => $this->title
->getNamespace(),
178 'page_title' => $this->title
->getDBkey() ),
182 # Page already exists. Import the history, and if necessary
183 # we'll update the latest revision field in the record.
185 $pageId = $page->page_id
;
186 $previousRevId = $page->page_latest
;
188 # Have to create a new article...
189 $newid = $article->insertOn( $dbw );
195 $oldones = '1 = 1'; # All revisions...
197 $oldts = implode( ',',
198 array_map( array( &$dbw, 'addQuotes' ),
199 array_map( array( &$dbw, 'timestamp' ),
202 $oldones = "ar_timestamp IN ( {$oldts} )";
206 * Restore each revision...
208 $result = $dbw->select( 'archive',
220 'ar_namespace' => $this->title
->getNamespace(),
221 'ar_title' => $this->title
->getDBkey(),
225 'ORDER BY' => 'ar_timestamp' )
228 $newRevId = $previousRevId;
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.
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_' );
243 $revision = new Revision( array(
245 'id' => $row->ar_rev_id
,
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
,
254 $newRevId = $revision->insertOn( $dbw );
258 # FIXME: Update latest if newer as well...
260 # FIXME: update article count if changed...
261 $article->updateRevisionOn( $dbw, $revision, $previousRevId );
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 );
270 #TODO: SearchUpdate, etc.
274 Article
::onArticleCreate( $this->title
);
276 Article
::onArticleEdit( $this->title
);
279 # Something went terribly wrong!
282 # Now that it's safely stored, take it out of the archive
283 $dbw->delete( 'archive',
285 'ar_namespace' => $this->title
->getNamespace(),
286 'ar_title' => $this->title
->getDBkey(),
291 $log = new LogPage( 'delete' );
295 $reason = wfMsgForContent( 'undeletedrevisions', $restoreRevisions );
296 if( trim( $comment ) != '' )
297 $reason .= ": {$comment}";
299 $log->addEntry( 'restore', $this->title
, $reason );
308 * @subpackage SpecialPage
311 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
312 var $mTargetTimestamp, $mAllowed, $mComment;
314 function UndeleteForm( &$request, $par = "" ) {
316 $this->mAction
= $request->getText( 'action' );
317 $this->mTarget
= $request->getText( 'target' );
318 $this->mTimestamp
= $request->getText( 'timestamp' );
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' );
327 $this->mTarget
= $par;
329 if ( $wgUser->isAllowed( 'delete' ) && !$wgUser->isBlocked() ) {
330 $this->mAllowed
= true;
332 $this->mAllowed
= false;
333 $this->mTimestamp
= '';
334 $this->mRestore
= false;
336 if ( $this->mTarget
!== "" ) {
337 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
339 $this->mTargetObj
= NULL;
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] );
348 rsort( $timestamps );
349 $this->mTargetTimestamp
= $timestamps;
355 if( is_null( $this->mTargetObj
) ) {
356 return $this->showList();
358 if( $this->mTimestamp
!== '' ) {
359 return $this->showRevision( $this->mTimestamp
);
361 if( $this->mRestore
&& $this->mAction
== "submit" ) {
362 return $this->undelete();
364 return $this->showHistory();
367 /* private */ function showList() {
368 global $wgLang, $wgContLang, $wgUser, $wgOut;
369 $fname = "UndeleteForm::showList";
371 # List undeletable articles
372 $result = PageArchive
::listAllPages();
374 if ( $this->mAllowed
) {
375 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
377 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
379 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
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
) . ":") : "").
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" );
395 $wgOut->addHTML( "</ul>\n" );
400 /* private */ function showRevision( $timestamp ) {
401 global $wgLang, $wgUser, $wgOut;
402 $fname = "UndeleteForm::showRevision";
404 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
406 $archive =& new PageArchive( $this->mTargetObj
);
407 $text = $archive->getRevisionText( $timestamp );
409 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
410 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
411 $wgLang->date( $timestamp ) ) . ")\n" );
413 if( $this->mPreview
) {
414 $wgOut->addHtml( "<hr />\n" );
415 $wgOut->addWikiText( $text );
418 $self = Title
::makeTitle( NS_SPECIAL
, "Undelete" );
421 wfElement( 'textarea', array(
423 'cols' => intval( $wgUser->getOption( 'cols' ) ),
424 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
426 wfOpenElement( 'div' ) .
427 wfOpenElement( 'form', array(
429 'action' => $self->getLocalURL( "action=submit" ) ) ) .
430 wfElement( 'input', array(
433 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
434 wfElement( 'input', array(
436 'name' => 'timestamp',
437 'value' => $timestamp ) ) .
438 wfElement( 'input', array(
440 'name' => 'wpEditToken',
441 'value' => $wgUser->editToken() ) ) .
442 wfElement( 'input', array(
446 wfElement( 'input', array(
448 'value' => wfMsg( 'showpreview' ) ) ) .
449 wfCloseElement( 'form' ) .
450 wfCloseElement( 'div' ) );
453 /* private */ function showHistory() {
454 global $wgLang, $wgUser, $wgOut;
456 $sk = $wgUser->getSkin();
457 if ( $this->mAllowed
) {
458 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
460 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
463 $archive = new PageArchive( $this->mTargetObj
);
464 $text = $archive->getLastRevisionText();
465 if( is_null( $text ) ) {
466 $wgOut->addWikiText( wfMsg( "nohistory" ) );
469 if ( $this->mAllowed
) {
470 $wgOut->addWikiText( wfMsg( "undeletehistory" ) );
472 $wgOut->addWikiText( wfMsg( "undeletehistorynoadmin" ) );
475 # List all stored revisions
476 $revisions = $archive->listRevisions();
478 # Batch existence check on user and talk pages
479 if( $revisions->numRows() > 0 ) {
480 $batch = new LinkBatch();
481 while( $row = $revisions->fetchObject() ) {
482 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
483 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
486 $revisions->seek( 0 );
489 if ( $this->mAllowed
) {
490 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Undelete" );
491 $action = $titleObj->getLocalURL( "action=submit" );
492 # Start the form here
493 $top = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
494 $wgOut->addHtml( $top );
497 # Show relevant lines from the deletion log:
498 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
499 require_once( 'SpecialLog.php' );
500 $logViewer =& new LogViewer(
503 array( 'page' => $this->mTargetObj
->getPrefixedText(),
504 'type' => 'delete' ) ) ) );
505 $logViewer->showList( $wgOut );
507 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
509 if( $this->mAllowed
) {
510 # Format the user-visible controls (comment field, submission button)
511 # in a nice little table
512 $table = '<fieldset><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 = $sk->userLink( $row->ar_user
, $row->ar_user_text
);
538 $comment = $sk->commentBlock( $row->ar_comment
);
539 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $comment</li>\n" );
543 $wgOut->addHTML("</ul>");
545 if ( $this->mAllowed
) {
546 # Slip in the hidden controls here
547 $misc = wfHidden( 'target', $this->mTarget
);
548 $misc .= wfHidden( 'wpEditToken', $wgUser->editToken() );
549 $wgOut->addHtml( $misc . '</form>' );
555 function undelete() {
556 global $wgOut, $wgUser;
557 if( !is_null( $this->mTargetObj
) ) {
558 $archive = new PageArchive( $this->mTargetObj
);
559 if( $archive->undelete( $this->mTargetTimestamp
, $this->mComment
) ) {
560 $skin =& $wgUser->getSkin();
561 $link = $skin->makeKnownLinkObj( $this->mTargetObj
);
562 $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
564 if (NS_IMAGE
== $this->mTargetObj
->getNamespace()) {
565 /* refresh image metadata cache */
566 new Image( $this->mTargetObj
);
572 $wgOut->fatalError( wfMsg( "cannotundelete" ) );