Fix typos that keep undelete from working with new db code
[lhc/web/wiklou.git] / includes / SpecialUndelete.php
1 <?php
2
3 function wfSpecialUndelete( $par )
4 {
5 global $wgRequest;
6
7 $form = new UndeleteForm( $wgRequest, $par );
8 $form->execute();
9 }
10
11 class UndeleteForm {
12 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
13
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' );
19 if( $par != "" ) {
20 $this->mTarget = $par;
21 }
22 if ( $this->mTarget !== "" ) {
23 $this->mTargetObj = Title::newFromURL( $this->mTarget );
24 } else {
25 $this->mTargetObj = NULL;
26 }
27 }
28
29 function execute() {
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 );
35 }
36 if( $this->mRestore and $this->mAction == "submit" ) {
37 return $this->undelete( $namespace, $title );
38 }
39 return $this->showHistory( $namespace, $title );
40 } else {
41 return $this->showList();
42 }
43 }
44
45 /* private */ function showList() {
46 global $wgLang, $wgUser, $wgOut;
47 $fname = "UndeleteForm::showList";
48
49 # List undeletable articles
50 $dbr =& wfGetDB( DB_SLAVE );
51 $archive = $dbr->tableName( 'archive' );
52
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";
55
56 $res = $dbr->query( $sql, $fname );
57
58 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
59 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
60
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 ) . ":") : "").
67 $row->ar_title;
68
69 $wgOut->addHTML( "<li>" .
70 $sk->makeKnownLink( $wgLang->specialPage( "Undelete" ),
71 $n, "target=" . urlencode($n) ) . " " .
72 wfMsg( "undeleterevisions", $row->count ) );
73 }
74 $wgOut->addHTML( "</ul>\n" );
75
76 return 0;
77 }
78
79 /* private */ function showRevision( $namespace, $title, $timestamp ) {
80 global $wgLang, $wgUser, $wgOut;
81 $fname = "UndeleteForm::showRevision";
82
83 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
84
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 );
92
93 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
94 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision", $wgLang->date($timestamp, true) )
95 . ")\n<hr>\n" . Article::getRevisionText( $row, "ar_" ) );
96
97 return 0;
98 }
99
100 /* private */ function showHistory( $namespace, $title ) {
101 global $wgLang, $wgUser, $wgOut;
102
103 $sk = $wgUser->getSkin();
104 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
105 $dbr =& wfGetDB( DB_SLAVE );
106 $archive = $dbr->tableName( 'archive' );
107
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 );
112
113 if( $dbr->numRows( $ret ) == 0 ) {
114 $wgOut->addWikiText( wfMsg( "nohistory" ) );
115 return 0;
116 }
117 $row = $dbr->fetchObject( $ret );
118 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n<hr>\n" . $row->ar_text );
119
120 # List all stored 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 );
125
126 $titleObj = Title::makeTitle( NS_SPECIAL, "Undelete" );
127 $action = $titleObj->escapeLocalURL( "action=submit" );
128 $encTarget = htmlspecialchars( $this->mTarget );
129
130 $wgOut->addHTML("<p>
131 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
132 <input type=hidden name=\"target\" value=\"{$encTarget}\">
133 <input type=submit name=\"restore\" value=\"".wfMsg("undeletebtn")."\">
134 </form>");
135
136 $log = $dbr->selectField( "cur", "cur_text",
137 array( 'cur_namespace' => NS_WIKIPEDIA, 'cur_title' => wfMsg("dellogpage") ) );
138 if(preg_match("/^(.*".
139 preg_quote( ($namespace ? ($wgLang->getNsText($namespace) . ":") : "")
140 . str_replace("_", " ", $title), "/" ).".*)$/m", $log, $m)) {
141 $wgOut->addWikiText( $m[1] );
142 }
143
144 $special = $wgLang->getNsText( Namespace::getSpecial() );
145 $wgOut->addHTML("<ul>");
146 while( $row = $dbr->fetchObject( $ret ) ) {
147 $wgOut->addHTML( "<li>" .
148 $sk->makeKnownLink( $wgLang->specialPage( "Undelete" ),
149 $wgLang->timeanddate( $row->ar_timestamp, true ),
150 "target=" . urlencode($this->mTarget) . "&timestamp={$row->ar_timestamp}" ) . " " .
151 ". . " . htmlspecialchars( $row->ar_user_text ) .
152 " <i>(" . htmlspecialchars($row->ar_comment) . "</i>)\n");
153
154 }
155 $wgOut->addHTML("</ul>");
156
157 return 0;
158 }
159
160 /* private */ function undelete( $namespace, $title )
161 {
162 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
163 global $wgUseSquid, $wgInternalServer, $wgLinkCache;
164
165 $fname = "doUndeleteArticle";
166
167 if ( "" == $title ) {
168 $wgOut->fatalError( wfMsg( "cannotundelete" ) );
169 return;
170 }
171 $dbw =& wfGetDB( DB_MASTER );
172 extract( $dbw->tableNames( 'cur', 'archive', 'old' ) );
173 $t = $dbw->strencode($title);
174
175 # Move article and history from the "archive" table
176 $sql = "SELECT COUNT(*) AS count FROM $cur WHERE cur_namespace={$namespace} AND cur_title='{$t}' FOR UPDATE";
177 $res = $dbw->query( $sql, $fname );
178 $row = $dbw->fetchObject( $res );
179 $now = wfTimestampNow();
180
181 if( $row->count == 0) {
182 # Have to create new article...
183 $sql = "SELECT ar_text,ar_timestamp,ar_flags FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' " .
184 "ORDER BY ar_timestamp DESC LIMIT 1 FOR UPDATE";
185 $res = $dbw->query( $sql, $fname );
186 $s = $dbw->fetchObject( $res );
187 $max = $s->ar_timestamp;
188 $redirect = MagicWord::get( MAG_REDIRECT );
189 $redir = $redirect->matchStart( $s->ar_text ) ? 1 : 0;
190
191 $seqVal = $dbw->addQuotes( $dbw->nextSequenceValue( 'cur_cur_id_seq' ) );
192
193 $sql = "INSERT INTO $cur (cur_id,cur_namespace,cur_title,cur_text," .
194 "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp,cur_minor_edit,cur_is_redirect,cur_random,cur_touched)" .
195 "SELECT $seqVal,ar_namespace,ar_title,ar_text,ar_comment," .
196 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,{$redir},RAND(),'{$now}' FROM $archive " .
197 "WHERE ar_namespace={$namespace} AND ar_title='{$t}' AND ar_timestamp={$max}";
198 $dbw->query( $sql, $fname );
199 $newid = $dbw->insertId();
200 $oldones = "AND ar_timestamp<{$max}";
201 } else {
202 # If already exists, put history entirely into old table
203 $oldones = "";
204 $newid = 0;
205
206 # But to make the history list show up right, we need to touch it.
207 $sql = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
208 $dbw->query( $sql, $fname );
209
210 # FIXME: Sometimes restored entries will be _newer_ than the current version.
211 # We should merge.
212 }
213
214 $sql = "INSERT INTO $old (old_namespace,old_title,old_text," .
215 "old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp,old_minor_edit," .
216 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," .
217 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,ar_flags " .
218 "FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}";
219 $dbw->query( $sql, $fname );
220
221 # Finally, clean up the link tables
222 if( $newid ) {
223 $wgLinkCache = new LinkCache();
224 # Select for update
225 $wgLinkCache->forUpdate( true );
226 # Create a dummy OutputPage to update the outgoing links
227 $dummyOut = new OutputPage();
228 # Get the text
229 $text = $dbw->selectField( 'cur', 'cur_text',
230 array( 'cur_id' => $newid, 'cur_namespace' => $namespace ),
231 $fname, 'FOR UPDATE'
232 );
233 $dummyOut->addWikiText( $text );
234
235 $u = new LinksUpdate( $newid, $this->mTargetObj->getPrefixedDBkey() );
236 array_push( $wgDeferredUpdateList, $u );
237
238 Article::onArticleCreate( $this->mTargetObj );
239
240 #TODO: SearchUpdate, etc.
241 }
242
243 # Now that it's safely stored, take it out of the archive
244 $sql = "DELETE FROM $archive WHERE ar_namespace={$namespace} AND " .
245 "ar_title='{$t}'";
246 $dbw->query( $sql, $fname );
247
248
249 # Touch the log?
250 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
251 $log->addEntry( wfMsg( "undeletedarticle", $this->mTarget ), "" );
252
253 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget ) );
254 return 0;
255 }
256 }
257 ?>