Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

SpecialUndelete.php

Go to the documentation of this file.
00001 <?php 00002 00003 function wfSpecialUndelete( $par ) 00004 { 00005 global $wgRequest; 00006 00007 $form = new UndeleteForm( $wgRequest, $par ); 00008 $form->execute(); 00009 } 00010 00011 class UndeleteForm { 00012 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj; 00013 00014 function UndeleteForm( &$request, $par = "" ) { 00015 $this->mAction = $request->getText( 'action' ); 00016 $this->mTarget = $request->getText( 'target' ); 00017 $this->mTimestamp = $request->getText( 'timestamp' ); 00018 $this->mRestore = $request->getCheck( 'restore' ); 00019 if( $par != "" ) { 00020 $this->mTarget = $par; 00021 } 00022 if ( $this->mTarget !== "" ) { 00023 $this->mTargetObj = Title::newFromURL( $this->mTarget ); 00024 } else { 00025 $this->mTargetObj = NULL; 00026 } 00027 } 00028 00029 function execute() { 00030 if( !is_null( $this->mTargetObj ) ) { 00031 $title = $this->mTargetObj->mDbkeyform; 00032 $namespace = $this->mTargetObj->mNamespace; 00033 if( $this->mTimestamp !== "" ) { 00034 return $this->showRevision( $namespace, $title, $this->mTimestamp ); 00035 } 00036 if( $this->mRestore and $this->mAction == "submit" ) { 00037 return $this->undelete( $namespace, $title ); 00038 } 00039 return $this->showHistory( $namespace, $title ); 00040 } else { 00041 return $this->showList(); 00042 } 00043 } 00044 00045 /* private */ function showList() { 00046 global $wgLang, $wgUser, $wgOut; 00047 $fname = "UndeleteForm::showList"; 00048 00049 # List undeletable articles 00050 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM archive " . 00051 "GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title"; 00052 $res = wfQuery( $sql, DB_READ, $fname ); 00053 00054 $wgOut->setPagetitle( wfMsg( "undeletepage" ) ); 00055 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) ); 00056 00057 $special = $wgLang->getNsText( Namespace::getSpecial() ); 00058 $sk = $wgUser->getSkin(); 00059 $wgOut->addHTML( "<ul>\n" ); 00060 while ($row = wfFetchObject( $res )) { 00061 $n = ($row->ar_namespace ? 00062 ($wgLang->getNsText( $row->ar_namespace ) . ":") : ""). 00063 $row->ar_title; 00064 00065 $wgOut->addHTML( "<li>" . 00066 $sk->makeKnownLink( $wgLang->specialPage( "Undelete" ), 00067 $n, "target=" . urlencode($n) ) . " " . 00068 wfMsg( "undeleterevisions", $row->count ) ); 00069 } 00070 $wgOut->addHTML( "</ul>\n" ); 00071 00072 return 0; 00073 } 00074 00075 /* private */ function showRevision( $namespace, $title, $timestamp ) { 00076 global $wgLang, $wgUser, $wgOut; 00077 $fname = "UndeleteForm::showRevision"; 00078 00079 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0; 00080 00081 $sql = "SELECT ar_text,ar_flags FROM archive ". 00082 "WHERE ar_namespace={$namespace} AND ar_title='" . 00083 wfStrencode( $title ) . "' AND ar_timestamp='" . wfStrencode( $timestamp ) ."'"; 00084 $ret = wfQuery( $sql, DB_READ, $fname ); 00085 $row = wfFetchObject( $ret ); 00086 00087 $wgOut->setPagetitle( wfMsg( "undeletepage" ) ); 00088 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision", $wgLang->date($timestamp, true) ) 00089 . ")\n<hr>\n" . Article::getRevisionText( $row, "ar_" ) ); 00090 00091 return 0; 00092 } 00093 00094 /* private */ function showHistory( $namespace, $title ) { 00095 global $wgLang, $wgUser, $wgOut; 00096 00097 $sk = $wgUser->getSkin(); 00098 $wgOut->setPagetitle( wfMsg( "undeletepage" ) ); 00099 00100 # Get text of first revision 00101 $sql = "SELECT ar_text FROM archive WHERE ar_namespace={$namespace} AND ar_title='" . 00102 wfStrencode( $title ) . "' ORDER BY ar_timestamp DESC LIMIT 1"; 00103 $ret = wfQuery( $sql, DB_READ ); 00104 00105 if( wfNumRows( $ret ) == 0 ) { 00106 $wgOut->addWikiText( wfMsg( "nohistory" ) ); 00107 return 0; 00108 } 00109 $row = wfFetchObject( $ret ); 00110 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n<hr>\n" . $row->ar_text ); 00111 00112 # Get remaining revisions 00113 $sql = "SELECT ar_minor_edit,ar_timestamp,ar_user,ar_user_text,ar_comment 00114 FROM archive WHERE ar_namespace={$namespace} AND ar_title='" . wfStrencode( $title ) . 00115 "' ORDER BY ar_timestamp DESC"; 00116 $ret = wfQuery( $sql, DB_READ ); 00117 # Ditch first row 00118 $row = wfFetchObject( $ret ); 00119 00120 $titleObj = Title::makeTitle( NS_SPECIAL, "Undelete" ); 00121 $action = $titleObj->escapeLocalURL( "action=submit" ); 00122 $encTarget = htmlspecialchars( $this->mTarget ); 00123 00124 $wgOut->addHTML("<p> 00125 <form id=\"undelete\" method=\"post\" action=\"{$action}\"> 00126 <input type=hidden name=\"target\" value=\"{$encTarget}\"> 00127 <input type=submit name=\"restore\" value=\"".wfMsg("undeletebtn")."\"> 00128 </form>"); 00129 00130 $log = wfGetSQL("cur", "cur_text", "cur_namespace=4 AND cur_title='". 00131 wfStrencode( wfMsg("dellogpage") ) . "'" ); 00132 if(preg_match("/^(.*". 00133 preg_quote( ($namespace ? ($wgLang->getNsText($namespace) . ":") : "") 00134 . str_replace("_", " ", $title), "/" ).".*)$/m", $log, $m)) { 00135 $wgOut->addWikiText( $m[1] ); 00136 } 00137 00138 $special = $wgLang->getNsText( Namespace::getSpecial() ); 00139 $wgOut->addHTML("<ul>"); 00140 while( $row = wfFetchObject( $ret ) ) { 00141 $wgOut->addHTML( "<li>" . 00142 $sk->makeKnownLink( $wgLang->specialPage( "Undelete" ), 00143 $wgLang->timeanddate( $row->ar_timestamp, true ), 00144 "target=" . urlencode($this->mTarget) . "&timestamp={$row->ar_timestamp}" ) . " " . 00145 ". . " . htmlspecialchars( $row->ar_user_text ) . 00146 " <i>(" . htmlspecialchars($row->ar_comment) . "</i>)\n"); 00147 00148 } 00149 $wgOut->addHTML("</ul>"); 00150 00151 return 0; 00152 } 00153 00154 /* private */ function undelete( $namespace, $title ) 00155 { 00156 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList; 00157 global $wgUseSquid, $wgInternalServer; 00158 00159 $fname = "doUndeleteArticle"; 00160 00161 if ( "" == $title ) { 00162 $wgOut->fatalError( wfMsg( "cannotundelete" ) ); 00163 return; 00164 } 00165 $t = wfStrencode($title); 00166 00167 # Move article and history from the "archive" table 00168 $sql = "SELECT COUNT(*) AS count FROM cur WHERE cur_namespace={$namespace} AND cur_title='{$t}'"; 00169 $res = wfQuery( $sql, DB_READ ); 00170 $row = wfFetchObject( $res ); 00171 $now = wfTimestampNow(); 00172 00173 if( $row->count == 0) { 00174 # Have to create new article... 00175 $sql = "SELECT ar_text,ar_timestamp,ar_flags FROM archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' ORDER BY ar_timestamp DESC LIMIT 1"; 00176 $res = wfQuery( $sql, DB_READ, $fname ); 00177 $s = wfFetchObject( $res ); 00178 $max = $s->ar_timestamp; 00179 $redirect = MagicWord::get( MAG_REDIRECT ); 00180 $redir = $redirect->matchStart( $s->ar_text ) ? 1 : 0; 00181 00182 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," . 00183 "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp,cur_minor_edit,cur_is_redirect,cur_random,cur_touched)" . 00184 "SELECT ar_namespace,ar_title,ar_text,ar_comment," . 00185 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,{$redir},RAND(),'{$now}' FROM archive " . 00186 "WHERE ar_namespace={$namespace} AND ar_title='{$t}' AND ar_timestamp={$max}"; 00187 wfQuery( $sql, DB_WRITE, $fname ); 00188 $newid = wfInsertId(); 00189 $oldones = "AND ar_timestamp<{$max}"; 00190 } else { 00191 # If already exists, put history entirely into old table 00192 $oldones = ""; 00193 $newid = 0; 00194 00195 # But to make the history list show up right, we need to touch it. 00196 $sql = "UPDATE cur SET cur_touched='{$now}' WHERE cur_namespace={$namespace} AND cur_title='{$t}'"; 00197 wfQuery( $sql, DB_WRITE, $fname ); 00198 00199 # FIXME: Sometimes restored entries will be _newer_ than the current version. 00200 # We should merge. 00201 } 00202 00203 $sql = "INSERT INTO old (old_namespace,old_title,old_text," . 00204 "old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp,old_minor_edit," . 00205 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," . 00206 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,ar_flags " . 00207 "FROM archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}"; 00208 wfQuery( $sql, DB_WRITE, $fname ); 00209 00210 # Finally, clean up the link tables 00211 if( $newid ) { 00212 # Create a dummy OutputPage to update the outgoing links 00213 # This works at the moment due to good luck. It may stop working in the 00214 # future. Damn globals. 00215 $dummyOut = new OutputPage(); 00216 $res = wfQuery( "SELECT cur_text FROM cur WHERE cur_id={$newid} " . 00217 "AND cur_namespace={$namespace}", DB_READ, $fname ); 00218 $row = wfFetchObject( $res ); 00219 $text = $row->cur_text; 00220 $dummyOut->addWikiText( $text ); 00221 wfFreeResult( $res ); 00222 00223 $u = new LinksUpdate( $newid, $this->mTargetObj->getPrefixedDBkey() ); 00224 array_push( $wgDeferredUpdateList, $u ); 00225 00226 Article::onArticleCreate( $this->mTargetObj ); 00227 00228 #TODO: SearchUpdate, etc. 00229 } 00230 00231 # Now that it's safely stored, take it out of the archive 00232 $sql = "DELETE FROM archive WHERE ar_namespace={$namespace} AND " . 00233 "ar_title='{$t}'"; 00234 wfQuery( $sql, DB_WRITE, $fname ); 00235 00236 00237 # Touch the log? 00238 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) ); 00239 $log->addEntry( wfMsg( "undeletedarticle", $this->mTarget ), "" ); 00240 00241 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget ) ); 00242 return 0; 00243 } 00244 } 00245 ?>

Generated on Tue Jun 29 23:40:07 2004 for Mediawiki by doxygen 1.3.7