3 function wfSpecialUndelete( $par )
7 $form = new UndeleteForm( $wgRequest, $par );
12 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
14 function UndeleteForm( &$request, $par = "" ) {
15 $this->mAction
= $request->getText( 'action' );
16 $this->mTarget
= $request->getText( 'target' );
17 $this->mTimestamp
= $request->getText( 'timestamp' );
18 $this->mRestore
= $request->getCheck( 'restore' );
20 $this->mTarget
= $par;
22 if ( $this->mTarget
!== "" ) {
23 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
25 $this->mTargetObj
= NULL;
30 if( !is_null( $this->mTargetObj
) ) {
31 $title = $this->mTargetObj
->mDbkeyform
;
32 $namespace = $this->mTargetObj
->mNamespace
;
33 if( $this->mTimestamp
!== "" ) {
34 return $this->showRevision( $namespace, $title, $this->mTimestamp
);
36 if( $this->mRestore
and $this->mAction
== "submit" ) {
37 return $this->undelete( $namespace, $title );
39 return $this->showHistory( $namespace, $title );
41 return $this->showList();
45 /* private */ function showList() {
46 global $wgLang, $wgUser, $wgOut;
47 $fname = "UndeleteForm::showList";
49 # List undeletable articles
50 $dbr =& wfGetDB( DB_SLAVE
);
51 $archive = $dbr->tableName( 'archive' );
53 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM $archive " .
54 "GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
56 $res = $dbr->query( $sql, $fname );
58 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
59 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
61 $special = $wgLang->getNsText( Namespace::getSpecial() );
62 $sk = $wgUser->getSkin();
63 $wgOut->addHTML( "<ul>\n" );
64 while ($row = $dbr->fetchObject( $res )) {
65 $n = ($row->ar_namespace ?
66 ($wgLang->getNsText( $row->ar_namespace
) . ":") : "").
69 $wgOut->addHTML( "<li>" .
70 $sk->makeKnownLink( $wgLang->specialPage( "Undelete" ),
71 $n, "target=" . urlencode($n) ) . " " .
72 wfMsg( "undeleterevisions", $row->count
) );
74 $wgOut->addHTML( "</ul>\n" );
79 /* private */ function showRevision( $namespace, $title, $timestamp ) {
80 global $wgLang, $wgUser, $wgOut;
81 $fname = "UndeleteForm::showRevision";
83 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
85 $dbr =& wfGetDB( DB_SLAVE
);
86 $archive = $dbr->tableName( 'archive' );
87 $sql = "SELECT ar_text,ar_flags FROM $archive ".
88 "WHERE ar_namespace={$namespace} AND ar_title='" .
89 $dbr->strencode( $title ) . "' AND ar_timestamp='" . $dbr->strencode( $timestamp ) ."'";
90 $ret = $dbr->query( $sql, $fname );
91 $row = $dbr->fetchObject( $ret );
93 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
94 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision", $wgLang->date($timestamp, true) )
95 . ")\n<hr>\n" . Article
::getRevisionText( $row, "ar_" ) );
100 /* private */ function showHistory( $namespace, $title ) {
101 global $wgLang, $wgUser, $wgOut;
103 $sk = $wgUser->getSkin();
104 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
105 $dbr =& wfGetDB( DB_SLAVE
);
106 $archive = $dbr->tableName( 'archive' );
108 # Get text of first revision
109 $sql = "SELECT ar_text FROM $archive WHERE ar_namespace={$namespace} AND ar_title='" .
110 $dbr->strencode( $title ) . "' ORDER BY ar_timestamp DESC LIMIT 1";
111 $ret = $dbr->query( $sql );
113 if( $dbr->numRows( $ret ) == 0 ) {
114 $wgOut->addWikiText( wfMsg( "nohistory" ) );
117 $row = $dbr->fetchObject( $ret );
118 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n<hr>\n" . $row->ar_text
);
120 # Get remaining revisions
121 $sql = "SELECT ar_minor_edit,ar_timestamp,ar_user,ar_user_text,ar_comment
122 FROM archive WHERE ar_namespace={$namespace} AND ar_title='" . $dbr->strencode( $title ) .
123 "' ORDER BY ar_timestamp DESC";
124 $ret = $dbr->query( $sql );
126 $row = $dbr->fetchObject( $ret );
128 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Undelete" );
129 $action = $titleObj->escapeLocalURL( "action=submit" );
130 $encTarget = htmlspecialchars( $this->mTarget
);
133 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
134 <input type=hidden name=\"target\" value=\"{$encTarget}\">
135 <input type=submit name=\"restore\" value=\"".wfMsg("undeletebtn")."\">
138 $log = $dbr->selectField( "cur", "cur_text",
139 array( 'cur_namespace' => NS_WIKIPEDIA
, 'cur_title' => wfMsg("dellogpage") ) );
140 if(preg_match("/^(.*".
141 preg_quote( ($namespace ?
($wgLang->getNsText($namespace) . ":") : "")
142 . str_replace("_", " ", $title), "/" ).".*)$/m", $log, $m)) {
143 $wgOut->addWikiText( $m[1] );
146 $special = $wgLang->getNsText( Namespace::getSpecial() );
147 $wgOut->addHTML("<ul>");
148 while( $row = $dbr->fetchObject( $ret ) ) {
149 $wgOut->addHTML( "<li>" .
150 $sk->makeKnownLink( $wgLang->specialPage( "Undelete" ),
151 $wgLang->timeanddate( $row->ar_timestamp
, true ),
152 "target=" . urlencode($this->mTarget
) . "×tamp={$row->ar_timestamp}" ) . " " .
153 ". . " . htmlspecialchars( $row->ar_user_text
) .
154 " <i>(" . htmlspecialchars($row->ar_comment
) . "</i>)\n");
157 $wgOut->addHTML("</ul>");
162 /* private */ function undelete( $namespace, $title )
164 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
165 global $wgUseSquid, $wgInternalServer, $wgLinkCache;
167 $fname = "doUndeleteArticle";
169 if ( "" == $title ) {
170 $wgOut->fatalError( wfMsg( "cannotundelete" ) );
173 $dbw =& wfGetDB( DB_MASTER
);
174 extract( $dbw->tableNames( 'cur', 'archive' ) );
175 $t = $dbw->strencode($title);
177 # Move article and history from the "archive" table
178 $sql = "SELECT COUNT(*) AS count FROM $cur WHERE cur_namespace={$namespace} AND cur_title='{$t}' FOR UPDATE";
179 $res = $dbw->query( $sql, $fname );
180 $row = $dbw->fetchObject( $res );
181 $now = wfTimestampNow();
183 if( $row->count
== 0) {
184 # Have to create new article...
185 $sql = "SELECT ar_text,ar_timestamp,ar_flags FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' " .
186 "ORDER BY ar_timestamp DESC LIMIT 1 FOR UPDATE";
187 $res = $dbw->query( $sql, $fname );
188 $s = $dbw->fetchObject( $res );
189 $max = $s->ar_timestamp
;
190 $redirect = MagicWord
::get( MAG_REDIRECT
);
191 $redir = $redirect->matchStart( $s->ar_text
) ?
1 : 0;
193 $seqVal = addQuotes( $dbw->nextSequenceValue( 'cur_cur_id_seq' ) );
195 $sql = "INSERT INTO $cur (cur_id,cur_namespace,cur_title,cur_text," .
196 "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp,cur_minor_edit,cur_is_redirect,cur_random,cur_touched)" .
197 "SELECT $seqVal,ar_namespace,ar_title,ar_text,ar_comment," .
198 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,{$redir},RAND(),'{$now}' FROM $archive " .
199 "WHERE ar_namespace={$namespace} AND ar_title='{$t}' AND ar_timestamp={$max}";
200 $dbw->query( $sql, $fname );
201 $newid = $dbw->insertId();
202 $oldones = "AND ar_timestamp<{$max}";
204 # If already exists, put history entirely into old table
208 # But to make the history list show up right, we need to touch it.
209 $sql = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
210 $dbw->query( $sql, $fname );
212 # FIXME: Sometimes restored entries will be _newer_ than the current version.
216 $sql = "INSERT INTO $old (old_namespace,old_title,old_text," .
217 "old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp,old_minor_edit," .
218 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," .
219 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,ar_flags " .
220 "FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}";
221 $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();
231 $text = $dbw->selectField( 'cur', 'cur_text',
232 array( 'cur_id' => $newid, 'cur_namespace' => $namespace ),
235 $dummyOut->addWikiText( $text );
237 $u = new LinksUpdate( $newid, $this->mTargetObj
->getPrefixedDBkey() );
238 array_push( $wgDeferredUpdateList, $u );
240 Article
::onArticleCreate( $this->mTargetObj
);
242 #TODO: SearchUpdate, etc.
245 # Now that it's safely stored, take it out of the archive
246 $sql = "DELETE FROM $archive WHERE ar_namespace={$namespace} AND " .
248 $dbw->query( $sql, $fname );
252 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
253 $log->addEntry( wfMsg( "undeletedarticle", $this->mTarget
), "" );
255 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget
) );