9 function wfSpecialUndelete( $par ) {
12 $form = new UndeleteForm( $wgRequest, $par );
22 function PageArchive( &$title ) {
23 if( is_null( $title ) ) {
24 wfDebugDieBacktrace( 'Archiver() given a null title.');
26 $this->title
=& $title;
29 /* static */ function &listAllPages() {
30 $dbr =& wfGetDB( DB_SLAVE
);
31 $archive = $dbr->tableName( 'archive' );
33 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM $archive " .
34 "GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
36 return $dbr->resultObject( $dbr->query( $sql, 'PageArchive::listAllPages' ) );
39 function &listRevisions() {
40 $dbr =& wfGetDB( DB_SLAVE
);
41 $sql = "SELECT ar_minor_edit,ar_timestamp,ar_user,ar_user_text,ar_comment
42 FROM archive WHERE ar_namespace=" . $this->title
->getNamespace() .
43 " AND ar_title='" . $dbr->strencode( $this->title
->getDBkey() ) .
44 "' ORDER BY ar_timestamp DESC";
45 return $dbr->resultObject( $dbr->query( $sql ) );
48 function getRevisionText( $timestamp ) {
49 $dbr =& wfGetDB( DB_SLAVE
);
50 $row = $dbr->selectRow( 'archive',
51 array( 'ar_text', 'ar_flags' ),
52 array( 'ar_namespace' => $this->title
->getNamespace(),
53 'ar_title' => $this->title
->getDbkey(),
54 'ar_timestamp' => $timestamp ) );
55 return Article
::getRevisionText( $row, "ar_" );
58 function getLastRevisionText() {
59 $dbr =& wfGetDB( DB_SLAVE
);
60 $archive = $dbr->tableName( 'archive' );
62 # Get text of first revision
63 $sql = "SELECT ar_text,ar_flags FROM $archive WHERE ar_namespace=" . $this->title
->getNamespace() . " AND ar_title='" .
64 $dbr->strencode( $this->title
->getDBkey() ) . "' ORDER BY ar_timestamp DESC LIMIT 1";
65 $ret = $dbr->query( $sql );
66 $row = $dbr->fetchObject( $ret );
67 $dbr->freeResult( $ret );
69 return Article
::getRevisionText( $row, "ar_" );
75 function isDeleted() {
76 $dbr =& wfGetDB( DB_SLAVE
);
77 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
78 array( 'ar_namespace' => $this->title
->getNamespace(),
79 'ar_title' => $this->title
->getDBkey() ) );
84 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
85 global $wgUseSquid, $wgInternalServer, $wgLinkCache;
87 $fname = "doUndeleteArticle";
89 $dbw =& wfGetDB( DB_MASTER
);
90 extract( $dbw->tableNames( 'cur', 'archive', 'old' ) );
91 $namespace = $this->title
->getNamespace();
92 $t = $dbw->strencode( $this->title
->getDBkey() );
94 # Move article and history from the "archive" table
95 $sql = "SELECT COUNT(*) AS count FROM $cur WHERE cur_namespace={$namespace} AND cur_title='{$t}' FOR UPDATE";
96 $res = $dbw->query( $sql, $fname );
97 $row = $dbw->fetchObject( $res );
98 $now = wfTimestampNow();
100 if( $row->count
== 0) {
101 # Have to create new article...
102 $sql = "SELECT ar_text,ar_timestamp,ar_flags FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' " .
103 "ORDER BY ar_timestamp DESC LIMIT 1 FOR UPDATE";
104 $res = $dbw->query( $sql, $fname );
105 $s = $dbw->fetchObject( $res );
106 $max = $s->ar_timestamp
;
107 $redirect = MagicWord
::get( MAG_REDIRECT
);
108 $redir = $redirect->matchStart( $s->ar_text
) ?
1 : 0;
110 $seqVal = $dbw->addQuotes( $dbw->nextSequenceValue( 'cur_cur_id_seq' ) );
112 $sql = "INSERT INTO $cur (cur_id,cur_namespace,cur_title,cur_text," .
113 "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp,cur_minor_edit,cur_is_redirect,cur_random,cur_touched)" .
114 "SELECT $seqVal,ar_namespace,ar_title,ar_text,ar_comment," .
115 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,{$redir},RAND(),'{$now}' FROM $archive " .
116 "WHERE ar_namespace={$namespace} AND ar_title='{$t}' AND ar_timestamp={$max}";
117 $dbw->query( $sql, $fname );
118 $newid = $dbw->insertId();
119 $oldones = "AND ar_timestamp<{$max}";
121 # If already exists, put history entirely into old table
125 # But to make the history list show up right, we need to touch it.
126 $sql = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
127 $dbw->query( $sql, $fname );
129 # FIXME: Sometimes restored entries will be _newer_ than the current version.
133 $sql = "INSERT INTO $old (old_namespace,old_title,old_text," .
134 "old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp,old_minor_edit," .
135 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," .
136 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,ar_flags " .
137 "FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}";
138 $dbw->query( $sql, $fname );
140 # Finally, clean up the link tables
142 $wgLinkCache = new LinkCache();
144 $wgLinkCache->forUpdate( true );
145 # Create a dummy OutputPage to update the outgoing links
146 $dummyOut = new OutputPage();
148 $text = $dbw->selectField( 'cur', 'cur_text',
149 array( 'cur_id' => $newid, 'cur_namespace' => $namespace ),
152 $dummyOut->addWikiText( $text );
154 $u = new LinksUpdate( $newid, $this->title
->getPrefixedDBkey() );
155 array_push( $wgDeferredUpdateList, $u );
157 Article
::onArticleCreate( $this->title
);
159 #TODO: SearchUpdate, etc.
162 # Now that it's safely stored, take it out of the archive
163 $sql = "DELETE FROM $archive WHERE ar_namespace={$namespace} AND " .
165 $dbw->query( $sql, $fname );
169 $log = new LogPage( 'delete' );
170 $log->addEntry( 'restore', $this->title
, "" );
180 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
182 function UndeleteForm( &$request, $par = "" ) {
183 $this->mAction
= $request->getText( 'action' );
184 $this->mTarget
= $request->getText( 'target' );
185 $this->mTimestamp
= $request->getText( 'timestamp' );
186 $this->mRestore
= $request->getCheck( 'restore' ) && $request->wasPosted();
188 $this->mTarget
= $par;
190 if ( $this->mTarget
!== "" ) {
191 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
193 $this->mTargetObj
= NULL;
198 if( is_null( $this->mTargetObj
) ) {
199 return $this->showList();
201 if( $this->mTimestamp
!== "" ) {
202 return $this->showRevision( $this->mTimestamp
);
204 if( $this->mRestore
&& $this->mAction
== "submit" ) {
205 return $this->undelete();
207 return $this->showHistory();
210 /* private */ function showList() {
211 global $wgLang, $wgUser, $wgOut;
212 $fname = "UndeleteForm::showList";
214 # List undeletable articles
215 $result = PageArchive
::listAllPages();
217 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
218 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
220 $sk = $wgUser->getSkin();
221 $undelete =& Title
::makeTitle( NS_SPECIAL
, 'Undelete' );
222 $wgOut->addHTML( "<ul>\n" );
223 while( $row = $result->fetchObject() ) {
224 $n = ($row->ar_namespace ?
225 ($wgLang->getNsText( $row->ar_namespace
) . ":") : "").
227 $link = $sk->makeKnownLinkObj( $undelete,
228 htmlspecialchars( $n ), "target=" . urlencode( $n ) );
229 $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
230 $wgLang->formatNum( $row->count
) ) );
231 $wgOut->addHTML( "<li>$link $revisions</li>\n" );
234 $wgOut->addHTML( "</ul>\n" );
239 /* private */ function showRevision( $timestamp ) {
240 global $wgLang, $wgUser, $wgOut;
241 $fname = "UndeleteForm::showRevision";
243 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
245 $archive =& new PageArchive( $this->mTargetObj
);
246 $text = $archive->getRevisionText( $timestamp );
248 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
249 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
250 $wgLang->date( $timestamp ) ) . ")\n<hr />\n" . $text );
253 /* private */ function showHistory() {
254 global $wgLang, $wgUser, $wgOut;
256 $sk = $wgUser->getSkin();
257 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
259 $archive = new PageArchive( $this->mTargetObj
);
260 $text = $archive->getLastRevisionText();
261 if( is_null( $text ) ) {
262 $wgOut->addWikiText( wfMsg( "nohistory" ) );
265 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n----\n" . $text );
267 # List all stored revisions
268 $revisions = $archive->listRevisions();
270 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Undelete" );
271 $action = $titleObj->escapeLocalURL( "action=submit" );
272 $encTarget = htmlspecialchars( $this->mTarget
);
273 $button = htmlspecialchars( wfMsg("undeletebtn") );
276 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
277 <input type=\"hidden\" name=\"target\" value=\"{$encTarget}\" />
278 <input type=\"submit\" name=\"restore\" value=\"{$button}\" />
281 # Show relevant lines from the deletion log:
282 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
283 require_once( 'SpecialLog.php' );
284 $logViewer =& new LogViewer(
287 array( 'page', $this->mTargetObj
->getPrefixedText() ) ) ) );
288 $logViewer->showList( $wgOut );
290 # The page's stored (deleted) history:
291 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
292 $wgOut->addHTML("<ul>");
293 $target = urlencode( $this->mTarget
);
294 while( $row = $revisions->fetchObject() ) {
295 $pageLink = $sk->makeKnownLinkObj( $titleObj,
296 $wgLang->timeanddate( $row->ar_timestamp
, true ),
297 "target=$target×tamp={$row->ar_timestamp}" );
298 $userLink = htmlspecialchars( $row->ar_user_text
);
299 if( $row->ar_user
) {
300 $userLink = $sk->makeKnownLinkObj(
301 Title
::makeTitle( NS_USER
, $row->ar_user_text
),
304 $comment = $sk->formatComment( $row->ar_comment
);
305 $wgOut->addHTML( "<li>$pageLink . . $userLink (<i>$comment</i>)</li>\n" );
309 $wgOut->addHTML("</ul>");
314 function undelete() {
316 if( !is_null( $this->mTargetObj
) ) {
317 $archive = new PageArchive( $this->mTargetObj
);
318 if( $archive->undelete() ) {
319 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget
) );
323 $wgOut->fatalError( wfMsg( "cannotundelete" ) );