5 * @subpackage SpecialPage
8 require_once( 'Revision.php' );
13 function wfSpecialUndelete( $par ) {
16 $form = new UndeleteForm( $wgRequest, $par );
23 * @subpackage SpecialPage
28 function PageArchive( &$title ) {
29 if( is_null( $title ) ) {
30 wfDebugDieBacktrace( 'Archiver() given a null title.');
32 $this->title
=& $title;
36 * List all deleted pages recorded in the archive table. Returns result
37 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
38 * namespace/title. Can be called staticaly.
40 * @return ResultWrapper
42 /* static */ function &listAllPages() {
43 $dbr =& wfGetDB( DB_SLAVE
);
44 $archive = $dbr->tableName( 'archive' );
46 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM $archive " .
47 "GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
49 return $dbr->resultObject( $dbr->query( $sql, 'PageArchive::listAllPages' ) );
53 * List the revisions of the given page. Returns result wrapper with
54 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
56 * @return ResultWrapper
58 function &listRevisions() {
59 $dbr =& wfGetDB( DB_SLAVE
);
60 return $dbr->resultObject( $dbr->select( 'archive',
61 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment' ),
62 array( 'ar_namespace' => $this->title
->getNamespace(),
63 'ar_title' => $this->title
->getDBkey() ),
64 'PageArchive::listRevisions',
65 array( 'ORDER BY' => 'ar_timestamp DESC' ) ) );
69 * Fetch (and decompress if necessary) the stored text for the deleted
70 * revision of the page with the given timestamp.
74 function getRevisionText( $timestamp ) {
75 $dbr =& wfGetDB( DB_SLAVE
);
76 $row = $dbr->selectRow( 'archive',
77 array( 'ar_text', 'ar_flags' ),
78 array( 'ar_namespace' => $this->title
->getNamespace(),
79 'ar_title' => $this->title
->getDbkey(),
80 'ar_timestamp' => $dbr->timestamp( $timestamp ) ) );
81 return Revision
::getRevisionText( $row, "ar_" );
85 * Fetch (and decompress if necessary) the stored text of the most
86 * recently edited deleted revision of the page.
88 * If there are no archived revisions for the page, returns NULL.
92 function getLastRevisionText() {
93 $dbr =& wfGetDB( DB_SLAVE
);
94 $row = $dbr->selectRow( 'archive',
95 array( 'ar_text', 'ar_flags' ),
96 array( 'ar_namespace' => $this->title
->getNamespace(),
97 'ar_title' => $this->title
->getDBkey() ),
98 'PageArchive::getLastRevisionText',
99 'ORDER BY ar_timestamp DESC LIMIT 1' );
101 return Revision
::getRevisionText( $row, "ar_" );
108 * Quick check if any archived revisions are present for the page.
111 function isDeleted() {
112 $dbr =& wfGetDB( DB_SLAVE
);
113 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
114 array( 'ar_namespace' => $this->title
->getNamespace(),
115 'ar_title' => $this->title
->getDBkey() ) );
120 * This is the meaty bit -- restores archived revisions of the given page
121 * to the cur/old tables. If the page currently exists, all revisions will
122 * be stuffed into old, otherwise the most recent will go into cur.
123 * The deletion log will be updated with an undeletion notice.
125 * Returns true on success.
127 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
130 function undelete( $timestamps ) {
131 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
132 global $wgUseSquid, $wgInternalServer, $wgLinkCache;
134 $fname = "doUndeleteArticle";
135 $restoreAll = empty( $timestamps );
136 $restoreRevisions = count( $timestamps );
138 $dbw =& wfGetDB( DB_MASTER
);
139 extract( $dbw->tableNames( 'cur', 'archive', 'old' ) );
140 $namespace = $this->title
->getNamespace();
141 $ttl = $this->title
->getDBkey();
142 $t = $dbw->strencode( $ttl );
144 # Move article and history from the "archive" table
145 $sql = "SELECT COUNT(*) AS count FROM $cur WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
147 if( $wgDBtype != 'PostgreSQL' ) { # HACKHACKHACKHACK
148 $sql .= ' FOR UPDATE';
150 $res = $dbw->query( $sql, $fname );
151 $row = $dbw->fetchObject( $res );
152 $now = wfTimestampNow();
154 if( $row->count
== 0) {
155 # Have to create new article...
156 $sql = "SELECT ar_text,ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit,ar_flags FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' ";
158 $max = $dbw->addQuotes( $dbw->timestamp( array_shift( $timestamps ) ) );
159 $sql .= "AND ar_timestamp={$max} ";
161 $sql .= "ORDER BY ar_timestamp DESC LIMIT 1 FOR UPDATE";
162 $res = $dbw->query( $sql, $fname );
163 $s = $dbw->fetchObject( $res );
165 $max = $s->ar_timestamp
;
167 $text = Revision
::getRevisionText( $s, "ar_" );
169 $redirect = MagicWord
::get( MAG_REDIRECT
);
170 $redir = $redirect->matchStart( $text ) ?
1 : 0;
173 $dbw->insert( 'cur', array(
174 'cur_id' => $dbw->nextSequenceValue( 'cur_cur_id_seq' ),
175 'cur_namespace' => $namespace,
178 'cur_comment' => $s->ar_comment
,
179 'cur_user' => $s->ar_user
,
180 'cur_timestamp' => $s->ar_timestamp
,
181 'cur_minor_edit' => $s->ar_minor_edit
,
182 'cur_user_text' => $s->ar_user_text
,
183 'cur_is_redirect' => $redir,
184 'cur_random' => $rand,
185 'cur_touched' => $dbw->timestamp( $now ),
186 'inverse_timestamp' => wfInvertTimestamp( wfTimestamp( TS_MW
, $s->ar_timestamp
) ),
189 $newid = $dbw->insertId();
191 $oldones = "AND ar_timestamp<" . $dbw->addQuotes( $dbw->timestamp( $max ) );
194 # If already exists, put history entirely into old table
198 # But to make the history list show up right, we need to touch it.
199 $sql = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
200 $dbw->query( $sql, $fname );
202 # FIXME: Sometimes restored entries will be _newer_ than the current version.
208 foreach( $timestamps as $ts ) {
209 array_push( $oldts, $dbw->addQuotes( $dbw->timestamp( $ts ) ) );
211 $oldts = join( ",", $oldts );
212 $oldones = "AND ar_timestamp IN ( {$oldts} )";
214 $sql = "INSERT INTO $old (old_namespace,old_title,old_text," .
215 "old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp,old_minor_edit," .
216 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," .
217 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,ar_flags " .
218 "FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}";
219 if( $restoreAll ||
!empty( $oldts ) ) {
220 $dbw->query( $sql, $fname );
223 # Finally, clean up the link tables
225 $wgLinkCache = new LinkCache();
227 $wgLinkCache->forUpdate( true );
228 # Create a dummy OutputPage to update the outgoing links
229 $dummyOut = new OutputPage();
230 $dummyOut->addWikiText( $text );
232 $u = new LinksUpdate( $newid, $this->title
->getPrefixedDBkey() );
233 array_push( $wgDeferredUpdateList, $u );
235 Article
::onArticleCreate( $this->title
);
237 #TODO: SearchUpdate, etc.
240 # Now that it's safely stored, take it out of the archive
241 $sql = "DELETE FROM $archive WHERE ar_namespace={$namespace} AND " .
244 $sql .= " AND ar_timestamp IN ( {$oldts}";
246 if( !empty( $oldts ) ) $sql .= ",";
251 $dbw->query( $sql, $fname );
255 $log = new LogPage( 'delete' );
259 $reason = wfMsg( 'undeletedrevisions', $restoreRevisions );
261 $log->addEntry( 'restore', $this->title
, $reason );
270 * @subpackage SpecialPage
273 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
274 var $mTargetTimestamp;
276 function UndeleteForm( &$request, $par = "" ) {
277 $this->mAction
= $request->getText( 'action' );
278 $this->mTarget
= $request->getText( 'target' );
279 $this->mTimestamp
= $request->getText( 'timestamp' );
280 $this->mRestore
= $request->getCheck( 'restore' ) && $request->wasPosted();
282 $this->mTarget
= $par;
284 if ( $this->mTarget
!== "" ) {
285 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
287 $this->mTargetObj
= NULL;
289 if( $this->mRestore
) {
290 $timestamps = array();
291 foreach( $_REQUEST as $key => $val ) {
292 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
293 array_push( $timestamps, $matches[1] );
296 rsort( $timestamps );
297 $this->mTargetTimestamp
= $timestamps;
302 if( is_null( $this->mTargetObj
) ) {
303 return $this->showList();
305 if( $this->mTimestamp
!== "" ) {
306 return $this->showRevision( $this->mTimestamp
);
308 if( $this->mRestore
&& $this->mAction
== "submit" ) {
309 return $this->undelete();
311 return $this->showHistory();
314 /* private */ function showList() {
315 global $wgLang, $wgContLang, $wgUser, $wgOut;
316 $fname = "UndeleteForm::showList";
318 # List undeletable articles
319 $result = PageArchive
::listAllPages();
321 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
322 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
324 $sk = $wgUser->getSkin();
325 $undelete =& Title
::makeTitle( NS_SPECIAL
, 'Undelete' );
326 $wgOut->addHTML( "<ul>\n" );
327 while( $row = $result->fetchObject() ) {
328 $n = ($row->ar_namespace ?
329 ($wgContLang->getNsText( $row->ar_namespace
) . ":") : "").
331 $link = $sk->makeKnownLinkObj( $undelete,
332 htmlspecialchars( $n ), "target=" . urlencode( $n ) );
333 $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
334 $wgLang->formatNum( $row->count
) ) );
335 $wgOut->addHTML( "<li>$link $revisions</li>\n" );
338 $wgOut->addHTML( "</ul>\n" );
343 /* private */ function showRevision( $timestamp ) {
344 global $wgLang, $wgUser, $wgOut;
345 $fname = "UndeleteForm::showRevision";
347 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
349 $archive =& new PageArchive( $this->mTargetObj
);
350 $text = $archive->getRevisionText( $timestamp );
352 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
353 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
354 $wgLang->date( $timestamp ) ) . ")\n<hr />\n" . $text );
357 /* private */ function showHistory() {
358 global $wgLang, $wgUser, $wgOut;
360 $sk = $wgUser->getSkin();
361 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
363 $archive = new PageArchive( $this->mTargetObj
);
364 $text = $archive->getLastRevisionText();
365 if( is_null( $text ) ) {
366 $wgOut->addWikiText( wfMsg( "nohistory" ) );
369 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n----\n" . $text );
371 # List all stored revisions
372 $revisions = $archive->listRevisions();
374 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Undelete" );
375 $action = $titleObj->escapeLocalURL( "action=submit" );
376 $encTarget = htmlspecialchars( $this->mTarget
);
377 $button = htmlspecialchars( wfMsg("undeletebtn") );
380 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
381 <input type=\"hidden\" name=\"target\" value=\"{$encTarget}\" />
382 <input type=\"submit\" name=\"restore\" value=\"{$button}\" />
385 # Show relevant lines from the deletion log:
386 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
387 require_once( 'SpecialLog.php' );
388 $logViewer =& new LogViewer(
391 array( 'page' => $this->mTargetObj
->getPrefixedText(),
392 'type' => 'delete' ) ) ) );
393 $logViewer->showList( $wgOut );
395 # The page's stored (deleted) history:
396 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
397 $wgOut->addHTML("<ul>");
398 $target = urlencode( $this->mTarget
);
399 while( $row = $revisions->fetchObject() ) {
400 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
401 $checkBox = "<input type=\"checkbox\" name=\"ts$ts\" value=\"1\" />";
402 $pageLink = $sk->makeKnownLinkObj( $titleObj,
403 $wgLang->timeanddate( $row->ar_timestamp
, true ),
404 "target=$target×tamp=$ts" );
405 $userLink = htmlspecialchars( $row->ar_user_text
);
406 if( $row->ar_user
) {
407 $userLink = $sk->makeKnownLinkObj(
408 Title
::makeTitle( NS_USER
, $row->ar_user_text
),
411 $comment = $sk->formatComment( $row->ar_comment
);
412 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink (<i>$comment</i>)</li>\n" );
416 $wgOut->addHTML("</ul>\n</form>");
421 function undelete() {
423 if( !is_null( $this->mTargetObj
) ) {
424 $archive = new PageArchive( $this->mTargetObj
);
425 if( $archive->undelete( $this->mTargetTimestamp
) ) {
426 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget
) );
430 $wgOut->fatalError( wfMsg( "cannotundelete" ) );