5 * @subpackage SpecialPage
9 require_once( 'Revision.php' );
14 function wfSpecialUndelete( $par ) {
17 $form = new UndeleteForm( $wgRequest, $par );
24 * @subpackage SpecialPage
29 function PageArchive( &$title ) {
30 if( is_null( $title ) ) {
31 wfDebugDieBacktrace( '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 return $dbr->resultObject( $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' ) ) );
70 * Fetch (and decompress if necessary) the stored text for the deleted
71 * revision of the page with the given timestamp.
75 function getRevisionText( $timestamp ) {
76 $fname = 'PageArchive::getRevisionText';
77 $dbr =& wfGetDB( DB_SLAVE
);
78 $row = $dbr->selectRow( 'archive',
79 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
80 array( 'ar_namespace' => $this->title
->getNamespace(),
81 'ar_title' => $this->title
->getDbkey(),
82 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
84 if( is_null( $row->ar_text_id
) ) {
85 // An old row from MediaWiki 1.4 or previous.
86 // Text is embedded in this row in classic compression format.
87 return Revision
::getRevisionText( $row, "ar_" );
89 // New-style: keyed to the text storage backend.
90 $text = $dbr->selectRow( 'text',
91 array( 'old_text', 'old_flags' ),
92 array( 'old_id' => $row->ar_text_id
),
94 return Revision
::getRevisionText( $text );
99 * Fetch (and decompress if necessary) the stored text of the most
100 * recently edited deleted revision of the page.
102 * If there are no archived revisions for the page, returns NULL.
106 function getLastRevisionText() {
107 $dbr =& wfGetDB( DB_SLAVE
);
108 $row = $dbr->selectRow( 'archive',
109 array( 'ar_text', 'ar_flags' ),
110 array( 'ar_namespace' => $this->title
->getNamespace(),
111 'ar_title' => $this->title
->getDBkey() ),
112 'PageArchive::getLastRevisionText',
113 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
115 return Revision
::getRevisionText( $row, "ar_" );
122 * Quick check if any archived revisions are present for the page.
125 function isDeleted() {
126 $dbr =& wfGetDB( DB_SLAVE
);
127 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
128 array( 'ar_namespace' => $this->title
->getNamespace(),
129 'ar_title' => $this->title
->getDBkey() ) );
134 * This is the meaty bit -- restores archived revisions of the given page
135 * to the cur/old tables. If the page currently exists, all revisions will
136 * be stuffed into old, otherwise the most recent will go into cur.
137 * The deletion log will be updated with an undeletion notice.
139 * Returns true on success.
141 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
144 function undelete( $timestamps ) {
145 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
146 global $wgUseSquid, $wgInternalServer, $wgLinkCache;
149 $fname = "doUndeleteArticle";
150 $restoreAll = empty( $timestamps );
151 $restoreRevisions = count( $timestamps );
153 $dbw =& wfGetDB( DB_MASTER
);
154 extract( $dbw->tableNames( 'page', 'archive' ) );
156 # Does this page already exist? We'll have to update it...
157 $article = new Article( $this->title
);
158 $options = ( $wgDBtype == 'PostgreSQL' )
159 ?
'' // pg doesn't support this?
161 $page = $dbw->selectRow( 'page',
162 array( 'page_id', 'page_latest' ),
163 array( 'page_namespace' => $this->title
->getNamespace(),
164 'page_title' => $this->title
->getDBkey() ),
168 # Page already exists. Import the history, and if necessary
169 # we'll update the latest revision field in the record.
171 $pageId = $page->page_id
;
172 $previousRevId = $page->page_latest
;
173 $previousTimestamp = $page->rev_timestamp
;
175 # Have to create a new article...
176 $newid = $article->insertOn( $dbw );
179 $previousTimestamp = 0;
183 $oldones = '1'; # All revisions...
185 $oldts = implode( ',',
186 array_map( array( &$dbw, 'addQuotes' ),
187 array_map( array( &$dbw, 'timestamp' ),
190 $oldones = "ar_timestamp IN ( {$oldts} )";
194 * Restore each revision...
196 $result = $dbw->select( 'archive',
208 'ar_namespace' => $this->title
->getNamespace(),
209 'ar_title' => $this->title
->getDBkey(),
213 'ORDER BY' => 'ar_timestamp' )
216 while( $row = $dbw->fetchObject( $result ) ) {
217 $revision = new Revision( array(
219 'id' => $row->ar_rev_id
,
220 'text' => Revision
::getRevisionText( $row, 'ar_' ),
221 'comment' => $row->ar_comment
,
222 'user' => $row->ar_user
,
223 'user_text' => $row->ar_user_text
,
224 'timestamp' => $row->ar_timestamp
,
225 'minor_edit' => $row->ar_minor_edit
,
226 'text_id' => $row->ar_text_id
,
228 $revision->insertOn( $dbw );
232 # FIXME: Update latest if newer as well...
234 # FIXME: update article count if changed...
235 $article->updateRevisionOn( $dbw, $revision, $previousRevId );
237 # Finally, clean up the link tables
238 $wgLinkCache = new LinkCache();
240 $wgLinkCache->forUpdate( true );
242 # Create a dummy OutputPage to update the outgoing links
243 $dummyOut = new OutputPage();
244 $dummyOut->addWikiText( $revision->getText() );
246 $u = new LinksUpdate( $newid, $this->title
->getPrefixedDBkey() );
247 array_push( $wgDeferredUpdateList, $u );
249 #TODO: SearchUpdate, etc.
253 Article
::onArticleCreate( $this->title
);
255 Article
::onArticleEdit( $this->title
);
258 # Something went terribly worong!
261 # Now that it's safely stored, take it out of the archive
262 $dbw->delete( 'archive',
264 'ar_namespace' => $this->title
->getNamespace(),
265 'ar_title' => $this->title
->getDBkey(),
270 $log = new LogPage( 'delete' );
274 $reason = wfMsg( 'undeletedrevisions', $restoreRevisions );
276 $log->addEntry( 'restore', $this->title
, $reason );
285 * @subpackage SpecialPage
288 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
289 var $mTargetTimestamp;
291 function UndeleteForm( &$request, $par = "" ) {
293 $this->mAction
= $request->getText( 'action' );
294 $this->mTarget
= $request->getText( 'target' );
295 $this->mTimestamp
= $request->getText( 'timestamp' );
296 $this->mRestore
= $request->getCheck( 'restore' ) &&
297 $request->wasPosted() &&
298 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
300 $this->mTarget
= $par;
302 if ( $this->mTarget
!== "" ) {
303 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
305 $this->mTargetObj
= NULL;
307 if( $this->mRestore
) {
308 $timestamps = array();
309 foreach( $_REQUEST as $key => $val ) {
310 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
311 array_push( $timestamps, $matches[1] );
314 rsort( $timestamps );
315 $this->mTargetTimestamp
= $timestamps;
320 if( is_null( $this->mTargetObj
) ) {
321 return $this->showList();
323 if( $this->mTimestamp
!== "" ) {
324 return $this->showRevision( $this->mTimestamp
);
326 if( $this->mRestore
&& $this->mAction
== "submit" ) {
327 return $this->undelete();
329 return $this->showHistory();
332 /* private */ function showList() {
333 global $wgLang, $wgContLang, $wgUser, $wgOut;
334 $fname = "UndeleteForm::showList";
336 # List undeletable articles
337 $result = PageArchive
::listAllPages();
339 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
340 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
342 $sk = $wgUser->getSkin();
343 $undelete =& Title
::makeTitle( NS_SPECIAL
, 'Undelete' );
344 $wgOut->addHTML( "<ul>\n" );
345 while( $row = $result->fetchObject() ) {
346 $n = ($row->ar_namespace ?
347 ($wgContLang->getNsText( $row->ar_namespace
) . ":") : "").
349 $link = $sk->makeKnownLinkObj( $undelete,
350 htmlspecialchars( $n ), "target=" . urlencode( $n ) );
351 $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
352 $wgLang->formatNum( $row->count
) ) );
353 $wgOut->addHTML( "<li>$link $revisions</li>\n" );
356 $wgOut->addHTML( "</ul>\n" );
361 /* private */ function showRevision( $timestamp ) {
362 global $wgLang, $wgUser, $wgOut;
363 $fname = "UndeleteForm::showRevision";
365 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
367 $archive =& new PageArchive( $this->mTargetObj
);
368 $text = $archive->getRevisionText( $timestamp );
370 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
371 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
372 $wgLang->date( $timestamp ) ) . ")\n<hr />\n" . $text );
375 /* private */ function showHistory() {
376 global $wgLang, $wgUser, $wgOut;
378 $sk = $wgUser->getSkin();
379 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
381 $archive = new PageArchive( $this->mTargetObj
);
382 $text = $archive->getLastRevisionText();
383 if( is_null( $text ) ) {
384 $wgOut->addWikiText( wfMsg( "nohistory" ) );
387 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n----\n" . $text );
389 # List all stored revisions
390 $revisions = $archive->listRevisions();
392 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Undelete" );
393 $action = $titleObj->escapeLocalURL( "action=submit" );
394 $encTarget = htmlspecialchars( $this->mTarget
);
395 $button = htmlspecialchars( wfMsg("undeletebtn") );
396 $token = htmlspecialchars( $wgUser->editToken() );
399 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
400 <input type=\"hidden\" name=\"target\" value=\"{$encTarget}\" />
401 <input type=\"submit\" name=\"restore\" value=\"{$button}\" />
402 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
405 # Show relevant lines from the deletion log:
406 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
407 require_once( 'SpecialLog.php' );
408 $logViewer =& new LogViewer(
411 array( 'page' => $this->mTargetObj
->getPrefixedText(),
412 'type' => 'delete' ) ) ) );
413 $logViewer->showList( $wgOut );
415 # The page's stored (deleted) history:
416 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
417 $wgOut->addHTML("<ul>");
418 $target = urlencode( $this->mTarget
);
419 while( $row = $revisions->fetchObject() ) {
420 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
421 $checkBox = "<input type=\"checkbox\" name=\"ts$ts\" value=\"1\" />";
422 $pageLink = $sk->makeKnownLinkObj( $titleObj,
423 $wgLang->timeanddate( $row->ar_timestamp
, true ),
424 "target=$target×tamp=$ts" );
425 $userLink = htmlspecialchars( $row->ar_user_text
);
426 if( $row->ar_user
) {
427 $userLink = $sk->makeKnownLinkObj(
428 Title
::makeTitle( NS_USER
, $row->ar_user_text
),
431 $userLink = $sk->makeKnownLinkObj(
432 Title
::makeTitle( NS_SPECIAL
, 'Contributions' ),
433 $userLink, 'target=' . $row->ar_user_text
);
435 $comment = $sk->commentBlock( $row->ar_comment
);
436 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $comment</li>\n" );
440 $wgOut->addHTML("</ul>\n</form>");
445 function undelete() {
447 if( !is_null( $this->mTargetObj
) ) {
448 $archive = new PageArchive( $this->mTargetObj
);
449 if( $archive->undelete( $this->mTargetTimestamp
) ) {
450 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget
) );
452 if (NS_IMAGE
== $this->mTargetObj
->getNamespace()) {
453 /* refresh image metadata cache */
454 new Image( $this->mTargetObj
->getText(), true );
460 $wgOut->fatalError( wfMsg( "cannotundelete" ) );