5 * @subpackage SpecialPage
11 function wfSpecialUndelete( $par ) {
14 $form = new UndeleteForm( $wgRequest, $par );
21 * @subpackage SpecialPage
26 function PageArchive( &$title ) {
27 if( is_null( $title ) ) {
28 wfDebugDieBacktrace( 'Archiver() given a null title.');
30 $this->title
=& $title;
34 * List all deleted pages recorded in the archive table. Returns result
35 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
36 * namespace/title. Can be called staticaly.
38 * @return ResultWrapper
40 /* static */ function &listAllPages() {
41 $dbr =& wfGetDB( DB_SLAVE
);
42 $archive = $dbr->tableName( 'archive' );
44 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM $archive " .
45 "GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
47 return $dbr->resultObject( $dbr->query( $sql, 'PageArchive::listAllPages' ) );
51 * List the revisions of the given page. Returns result wrapper with
52 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
54 * @return ResultWrapper
56 function &listRevisions() {
57 $dbr =& wfGetDB( DB_SLAVE
);
58 return $dbr->resultObject( $dbr->select( 'archive',
59 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment' ),
60 array( 'ar_namespace' => $this->title
->getNamespace(),
61 'ar_title' => $this->title
->getDBkey() ),
62 'PageArchive::listRevisions',
63 array( 'ORDER BY' => 'ar_timestamp DESC' ) ) );
67 * Fetch (and decompress if necessary) the stored text for the deleted
68 * revision of the page with the given timestamp.
72 function getRevisionText( $timestamp ) {
73 $dbr =& wfGetDB( DB_SLAVE
);
74 $row = $dbr->selectRow( 'archive',
75 array( 'ar_text', 'ar_flags' ),
76 array( 'ar_namespace' => $this->title
->getNamespace(),
77 'ar_title' => $this->title
->getDbkey(),
78 'ar_timestamp' => $dbr->timestamp( $timestamp ) ) );
79 return Article
::getRevisionText( $row, "ar_" );
83 * Fetch (and decompress if necessary) the stored text of the most
84 * recently edited deleted revision of the page.
86 * If there are no archived revisions for the page, returns NULL.
90 function getLastRevisionText() {
91 $dbr =& wfGetDB( DB_SLAVE
);
92 $row = $dbr->selectRow( 'archive',
93 array( 'ar_text', 'ar_flags' ),
94 array( 'ar_namespace' => $this->title
->getNamespace(),
95 'ar_title' => $this->title
->getDBkey() ),
96 'PageArchive::getLastRevisionText',
97 'ORDER BY ar_timestamp DESC LIMIT 1' );
99 return Article
::getRevisionText( $row, "ar_" );
106 * Quick check if any archived revisions are present for the page.
109 function isDeleted() {
110 $dbr =& wfGetDB( DB_SLAVE
);
111 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
112 array( 'ar_namespace' => $this->title
->getNamespace(),
113 'ar_title' => $this->title
->getDBkey() ) );
118 * This is the meaty bit -- restores archived revisions of the given page
119 * to the cur/old tables. If the page currently exists, all revisions will
120 * be stuffed into old, otherwise the most recent will go into cur.
121 * The deletion log will be updated with an undeletion notice.
123 * Returns true on success.
125 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
128 function undelete( $timestamps ) {
129 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
130 global $wgUseSquid, $wgInternalServer, $wgLinkCache;
132 $fname = "doUndeleteArticle";
133 $restoreAll = empty( $timestamps );
134 $restoreRevisions = count( $timestamps );
136 $dbw =& wfGetDB( DB_MASTER
);
137 extract( $dbw->tableNames( 'cur', 'archive', 'old' ) );
138 $namespace = $this->title
->getNamespace();
139 $ttl = $this->title
->getDBkey();
140 $t = $dbw->strencode( $ttl );
142 # Move article and history from the "archive" table
143 $sql = "SELECT COUNT(*) AS count FROM $cur WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
145 if( $wgDBtype != 'PostgreSQL' ) { # HACKHACKHACKHACK
146 $sql .= ' FOR UPDATE';
148 $res = $dbw->query( $sql, $fname );
149 $row = $dbw->fetchObject( $res );
150 $now = wfTimestampNow();
152 if( $row->count
== 0) {
153 # Have to create new article...
154 $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}' ";
156 $max = $dbw->addQuotes( $dbw->timestamp( array_shift( $timestamps ) ) );
157 $sql .= "AND ar_timestamp={$max} ";
159 $sql .= "ORDER BY ar_timestamp DESC LIMIT 1 FOR UPDATE";
160 $res = $dbw->query( $sql, $fname );
161 $s = $dbw->fetchObject( $res );
163 $max = $s->ar_timestamp
;
165 $text = Article
::getRevisionText( $s, "ar_" );
167 $redirect = MagicWord
::get( MAG_REDIRECT
);
168 $redir = $redirect->matchStart( $text ) ?
1 : 0;
170 $rand = number_format( mt_rand() / mt_getrandmax(), 12, '.', '' );
171 $dbw->insertArray( 'cur', array(
172 'cur_id' => $dbw->nextSequenceValue( 'cur_cur_id_seq' ),
173 'cur_namespace' => $namespace,
176 'cur_comment' => $s->ar_comment
,
177 'cur_user' => $s->ar_user
,
178 'cur_timestamp' => $s->ar_timestamp
,
179 'cur_minor_edit' => $s->ar_minor_edit
,
180 'cur_user_text' => $s->ar_user_text
,
181 'cur_is_redirect' => $redir,
182 'cur_random' => $rand,
183 'cur_touched' => $dbw->timestamp( $now ),
184 'inverse_timestamp' => wfInvertTimestamp( wfTimestamp( TS_MW
, $s->ar_timestamp
) ),
187 $newid = $dbw->insertId();
189 $oldones = "AND ar_timestamp<" . $dbw->addQuotes( $dbw->timestamp( $max ) );
192 # If already exists, put history entirely into old table
196 # But to make the history list show up right, we need to touch it.
197 $sql = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
198 $dbw->query( $sql, $fname );
200 # FIXME: Sometimes restored entries will be _newer_ than the current version.
206 foreach( $timestamps as $ts ) {
207 array_push( $oldts, $dbw->addQuotes( $dbw->timestamp( $ts ) ) );
209 $oldts = join( ",", $oldts );
210 $oldones = "AND ar_timestamp IN ( {$oldts} )";
212 $sql = "INSERT INTO $old (old_namespace,old_title,old_text," .
213 "old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp,old_minor_edit," .
214 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," .
215 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,ar_flags " .
216 "FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}";
217 if( $restoreAll ||
!empty( $oldts ) ) {
218 $dbw->query( $sql, $fname );
221 # Finally, clean up the link tables
223 $wgLinkCache = new LinkCache();
225 $wgLinkCache->forUpdate( true );
226 # Create a dummy OutputPage to update the outgoing links
227 $dummyOut = new OutputPage();
228 $dummyOut->addWikiText( $text );
230 $u = new LinksUpdate( $newid, $this->title
->getPrefixedDBkey() );
231 array_push( $wgDeferredUpdateList, $u );
233 Article
::onArticleCreate( $this->title
);
235 #TODO: SearchUpdate, etc.
238 # Now that it's safely stored, take it out of the archive
239 $sql = "DELETE FROM $archive WHERE ar_namespace={$namespace} AND " .
242 $sql .= " AND ar_timestamp IN ( {$oldts}";
244 if( !empty( $oldts ) ) $sql .= ",";
249 $dbw->query( $sql, $fname );
253 $log = new LogPage( 'delete' );
257 $reason = wfMsg( 'undeletedrevisions', $restoreRevisions );
259 $log->addEntry( 'restore', $this->title
, $reason );
268 * @subpackage SpecialPage
271 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
272 var $mTargetTimestamp;
274 function UndeleteForm( &$request, $par = "" ) {
275 $this->mAction
= $request->getText( 'action' );
276 $this->mTarget
= $request->getText( 'target' );
277 $this->mTimestamp
= $request->getText( 'timestamp' );
278 $this->mRestore
= $request->getCheck( 'restore' ) && $request->wasPosted();
280 $this->mTarget
= $par;
282 if ( $this->mTarget
!== "" ) {
283 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
285 $this->mTargetObj
= NULL;
287 if( $this->mRestore
) {
288 $timestamps = array();
289 foreach( $_REQUEST as $key => $val ) {
290 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
291 array_push( $timestamps, $matches[1] );
294 rsort( $timestamps );
295 $this->mTargetTimestamp
= $timestamps;
300 if( is_null( $this->mTargetObj
) ) {
301 return $this->showList();
303 if( $this->mTimestamp
!== "" ) {
304 return $this->showRevision( $this->mTimestamp
);
306 if( $this->mRestore
&& $this->mAction
== "submit" ) {
307 return $this->undelete();
309 return $this->showHistory();
312 /* private */ function showList() {
313 global $wgLang, $wgContLang, $wgUser, $wgOut;
314 $fname = "UndeleteForm::showList";
316 # List undeletable articles
317 $result = PageArchive
::listAllPages();
319 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
320 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
322 $sk = $wgUser->getSkin();
323 $undelete =& Title
::makeTitle( NS_SPECIAL
, 'Undelete' );
324 $wgOut->addHTML( "<ul>\n" );
325 while( $row = $result->fetchObject() ) {
326 $n = ($row->ar_namespace ?
327 ($wgContLang->getNsText( $row->ar_namespace
) . ":") : "").
329 $link = $sk->makeKnownLinkObj( $undelete,
330 htmlspecialchars( $n ), "target=" . urlencode( $n ) );
331 $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
332 $wgLang->formatNum( $row->count
) ) );
333 $wgOut->addHTML( "<li>$link $revisions</li>\n" );
336 $wgOut->addHTML( "</ul>\n" );
341 /* private */ function showRevision( $timestamp ) {
342 global $wgLang, $wgUser, $wgOut;
343 $fname = "UndeleteForm::showRevision";
345 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
347 $archive =& new PageArchive( $this->mTargetObj
);
348 $text = $archive->getRevisionText( $timestamp );
350 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
351 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
352 $wgLang->date( $timestamp ) ) . ")\n<hr />\n" . $text );
355 /* private */ function showHistory() {
356 global $wgLang, $wgUser, $wgOut;
358 $sk = $wgUser->getSkin();
359 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
361 $archive = new PageArchive( $this->mTargetObj
);
362 $text = $archive->getLastRevisionText();
363 if( is_null( $text ) ) {
364 $wgOut->addWikiText( wfMsg( "nohistory" ) );
367 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n----\n" . $text );
369 # List all stored revisions
370 $revisions = $archive->listRevisions();
372 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Undelete" );
373 $action = $titleObj->escapeLocalURL( "action=submit" );
374 $encTarget = htmlspecialchars( $this->mTarget
);
375 $button = htmlspecialchars( wfMsg("undeletebtn") );
378 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
379 <input type=\"hidden\" name=\"target\" value=\"{$encTarget}\" />
380 <input type=\"submit\" name=\"restore\" value=\"{$button}\" />
383 # Show relevant lines from the deletion log:
384 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
385 require_once( 'SpecialLog.php' );
386 $logViewer =& new LogViewer(
389 array( 'page' => $this->mTargetObj
->getPrefixedText(),
390 'type' => 'delete' ) ) ) );
391 $logViewer->showList( $wgOut );
393 # The page's stored (deleted) history:
394 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
395 $wgOut->addHTML("<ul>");
396 $target = urlencode( $this->mTarget
);
397 while( $row = $revisions->fetchObject() ) {
398 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
399 $checkBox = "<input type=\"checkbox\" name=\"ts$ts\" value=\"1\" />";
400 $pageLink = $sk->makeKnownLinkObj( $titleObj,
401 $wgLang->timeanddate( $row->ar_timestamp
, true ),
402 "target=$target×tamp=$ts" );
403 $userLink = htmlspecialchars( $row->ar_user_text
);
404 if( $row->ar_user
) {
405 $userLink = $sk->makeKnownLinkObj(
406 Title
::makeTitle( NS_USER
, $row->ar_user_text
),
409 $comment = $sk->formatComment( $row->ar_comment
);
410 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink (<i>$comment</i>)</li>\n" );
414 $wgOut->addHTML("</ul>\n</form>");
419 function undelete() {
421 if( !is_null( $this->mTargetObj
) ) {
422 $archive = new PageArchive( $this->mTargetObj
);
423 if( $archive->undelete( $this->mTargetTimestamp
) ) {
424 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget
) );
428 $wgOut->fatalError( wfMsg( "cannotundelete" ) );