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;
33 /* static */ function &listAllPages() {
34 $dbr =& wfGetDB( DB_SLAVE
);
35 $archive = $dbr->tableName( 'archive' );
37 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM $archive " .
38 "GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
40 return $dbr->resultObject( $dbr->query( $sql, 'PageArchive::listAllPages' ) );
43 function &listRevisions() {
44 $dbr =& wfGetDB( DB_SLAVE
);
45 $sql = "SELECT ar_minor_edit,ar_timestamp,ar_user,ar_user_text,ar_comment
46 FROM archive WHERE ar_namespace=" . $this->title
->getNamespace() .
47 " AND ar_title='" . $dbr->strencode( $this->title
->getDBkey() ) .
48 "' ORDER BY ar_timestamp DESC";
49 return $dbr->resultObject( $dbr->query( $sql ) );
52 function getRevisionText( $timestamp ) {
53 $dbr =& wfGetDB( DB_SLAVE
);
54 $row = $dbr->selectRow( 'archive',
55 array( 'ar_text', 'ar_flags' ),
56 array( 'ar_namespace' => $this->title
->getNamespace(),
57 'ar_title' => $this->title
->getDbkey(),
58 'ar_timestamp' => $timestamp ) );
59 return Article
::getRevisionText( $row, "ar_" );
62 function getLastRevisionText() {
63 $dbr =& wfGetDB( DB_SLAVE
);
64 $archive = $dbr->tableName( 'archive' );
66 # Get text of first revision
67 $sql = "SELECT ar_text,ar_flags FROM $archive WHERE ar_namespace=" . $this->title
->getNamespace() . " AND ar_title='" .
68 $dbr->strencode( $this->title
->getDBkey() ) . "' ORDER BY ar_timestamp DESC LIMIT 1";
69 $ret = $dbr->query( $sql );
70 $row = $dbr->fetchObject( $ret );
71 $dbr->freeResult( $ret );
73 return Article
::getRevisionText( $row, "ar_" );
79 function isDeleted() {
80 $dbr =& wfGetDB( DB_SLAVE
);
81 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
82 array( 'ar_namespace' => $this->title
->getNamespace(),
83 'ar_title' => $this->title
->getDBkey() ) );
88 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
89 global $wgUseSquid, $wgInternalServer, $wgLinkCache;
91 $fname = "doUndeleteArticle";
93 $dbw =& wfGetDB( DB_MASTER
);
94 extract( $dbw->tableNames( 'cur', 'archive', 'old' ) );
95 $namespace = $this->title
->getNamespace();
96 $t = $dbw->strencode( $this->title
->getDBkey() );
98 # Move article and history from the "archive" table
99 $sql = "SELECT COUNT(*) AS count FROM $cur WHERE cur_namespace={$namespace} AND cur_title='{$t}' FOR UPDATE";
100 $res = $dbw->query( $sql, $fname );
101 $row = $dbw->fetchObject( $res );
102 $now = wfTimestampNow();
104 if( $row->count
== 0) {
105 # Have to create new article...
106 $sql = "SELECT ar_text,ar_timestamp,ar_flags FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' " .
107 "ORDER BY ar_timestamp DESC LIMIT 1 FOR UPDATE";
108 $res = $dbw->query( $sql, $fname );
109 $s = $dbw->fetchObject( $res );
110 $max = $s->ar_timestamp
;
111 $redirect = MagicWord
::get( MAG_REDIRECT
);
112 $redir = $redirect->matchStart( $s->ar_text
) ?
1 : 0;
114 $seqVal = $dbw->addQuotes( $dbw->nextSequenceValue( 'cur_cur_id_seq' ) );
116 $sql = "INSERT INTO $cur (cur_id,cur_namespace,cur_title,cur_text," .
117 "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp,cur_minor_edit,cur_is_redirect,cur_random,cur_touched)" .
118 "SELECT $seqVal,ar_namespace,ar_title,ar_text,ar_comment," .
119 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,{$redir},RAND(),'{$now}' FROM $archive " .
120 "WHERE ar_namespace={$namespace} AND ar_title='{$t}' AND ar_timestamp={$max}";
121 $dbw->query( $sql, $fname );
122 $newid = $dbw->insertId();
123 $oldones = "AND ar_timestamp<{$max}";
125 # If already exists, put history entirely into old table
129 # But to make the history list show up right, we need to touch it.
130 $sql = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
131 $dbw->query( $sql, $fname );
133 # FIXME: Sometimes restored entries will be _newer_ than the current version.
137 $sql = "INSERT INTO $old (old_namespace,old_title,old_text," .
138 "old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp,old_minor_edit," .
139 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," .
140 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,ar_flags " .
141 "FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}";
142 $dbw->query( $sql, $fname );
144 # Finally, clean up the link tables
146 $wgLinkCache = new LinkCache();
148 $wgLinkCache->forUpdate( true );
149 # Create a dummy OutputPage to update the outgoing links
150 $dummyOut = new OutputPage();
152 $text = $dbw->selectField( 'cur', 'cur_text',
153 array( 'cur_id' => $newid, 'cur_namespace' => $namespace ),
156 $dummyOut->addWikiText( $text );
158 $u = new LinksUpdate( $newid, $this->title
->getPrefixedDBkey() );
159 array_push( $wgDeferredUpdateList, $u );
161 Article
::onArticleCreate( $this->title
);
163 #TODO: SearchUpdate, etc.
166 # Now that it's safely stored, take it out of the archive
167 $sql = "DELETE FROM $archive WHERE ar_namespace={$namespace} AND " .
169 $dbw->query( $sql, $fname );
173 $log = new LogPage( 'delete' );
174 $log->addEntry( 'restore', $this->title
, "" );
183 * @subpackage SpecialPage
186 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
188 function UndeleteForm( &$request, $par = "" ) {
189 $this->mAction
= $request->getText( 'action' );
190 $this->mTarget
= $request->getText( 'target' );
191 $this->mTimestamp
= $request->getText( 'timestamp' );
192 $this->mRestore
= $request->getCheck( 'restore' ) && $request->wasPosted();
194 $this->mTarget
= $par;
196 if ( $this->mTarget
!== "" ) {
197 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
199 $this->mTargetObj
= NULL;
204 if( is_null( $this->mTargetObj
) ) {
205 return $this->showList();
207 if( $this->mTimestamp
!== "" ) {
208 return $this->showRevision( $this->mTimestamp
);
210 if( $this->mRestore
&& $this->mAction
== "submit" ) {
211 return $this->undelete();
213 return $this->showHistory();
216 /* private */ function showList() {
217 global $wgLang, $wgContLang, $wgUser, $wgOut;
218 $fname = "UndeleteForm::showList";
220 # List undeletable articles
221 $result = PageArchive
::listAllPages();
223 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
224 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
226 $sk = $wgUser->getSkin();
227 $undelete =& Title
::makeTitle( NS_SPECIAL
, 'Undelete' );
228 $wgOut->addHTML( "<ul>\n" );
229 while( $row = $result->fetchObject() ) {
230 $n = ($row->ar_namespace ?
231 ($wgContLang->getNsText( $row->ar_namespace
) . ":") : "").
233 $link = $sk->makeKnownLinkObj( $undelete,
234 htmlspecialchars( $n ), "target=" . urlencode( $n ) );
235 $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
236 $wgLang->formatNum( $row->count
) ) );
237 $wgOut->addHTML( "<li>$link $revisions</li>\n" );
240 $wgOut->addHTML( "</ul>\n" );
245 /* private */ function showRevision( $timestamp ) {
246 global $wgLang, $wgUser, $wgOut;
247 $fname = "UndeleteForm::showRevision";
249 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
251 $archive =& new PageArchive( $this->mTargetObj
);
252 $text = $archive->getRevisionText( $timestamp );
254 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
255 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
256 $wgLang->date( $timestamp ) ) . ")\n<hr />\n" . $text );
259 /* private */ function showHistory() {
260 global $wgLang, $wgUser, $wgOut;
262 $sk = $wgUser->getSkin();
263 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
265 $archive = new PageArchive( $this->mTargetObj
);
266 $text = $archive->getLastRevisionText();
267 if( is_null( $text ) ) {
268 $wgOut->addWikiText( wfMsg( "nohistory" ) );
271 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n----\n" . $text );
273 # List all stored revisions
274 $revisions = $archive->listRevisions();
276 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Undelete" );
277 $action = $titleObj->escapeLocalURL( "action=submit" );
278 $encTarget = htmlspecialchars( $this->mTarget
);
279 $button = htmlspecialchars( wfMsg("undeletebtn") );
282 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
283 <input type=\"hidden\" name=\"target\" value=\"{$encTarget}\" />
284 <input type=\"submit\" name=\"restore\" value=\"{$button}\" />
287 # Show relevant lines from the deletion log:
288 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
289 require_once( 'SpecialLog.php' );
290 $logViewer =& new LogViewer(
293 array( 'page' => $this->mTargetObj
->getPrefixedText(),
294 'type' => 'delete' ) ) ) );
295 $logViewer->showList( $wgOut );
297 # The page's stored (deleted) history:
298 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
299 $wgOut->addHTML("<ul>");
300 $target = urlencode( $this->mTarget
);
301 while( $row = $revisions->fetchObject() ) {
302 $pageLink = $sk->makeKnownLinkObj( $titleObj,
303 $wgLang->timeanddate( $row->ar_timestamp
, true ),
304 "target=$target×tamp={$row->ar_timestamp}" );
305 $userLink = htmlspecialchars( $row->ar_user_text
);
306 if( $row->ar_user
) {
307 $userLink = $sk->makeKnownLinkObj(
308 Title
::makeTitle( NS_USER
, $row->ar_user_text
),
311 $comment = $sk->formatComment( $row->ar_comment
);
312 $wgOut->addHTML( "<li>$pageLink . . $userLink (<i>$comment</i>)</li>\n" );
316 $wgOut->addHTML("</ul>");
321 function undelete() {
323 if( !is_null( $this->mTargetObj
) ) {
324 $archive = new PageArchive( $this->mTargetObj
);
325 if( $archive->undelete() ) {
326 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget
) );
330 $wgOut->fatalError( wfMsg( "cannotundelete" ) );