New global config setting $wgMaxTocLevel: Maximum indent level of toc.
[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 # 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 );
125 # Ditch first row
126 $row = $dbr->fetchObject( $ret );
127
128 $titleObj = Title::makeTitle( NS_SPECIAL, "Undelete" );
129 $action = $titleObj->escapeLocalURL( "action=submit" );
130 $encTarget = htmlspecialchars( $this->mTarget );
131
132 $wgOut->addHTML("<p>
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")."\">
136 </form>");
137
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] );
144 }
145
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) . "&timestamp={$row->ar_timestamp}" ) . " " .
153 ". . " . htmlspecialchars( $row->ar_user_text ) .
154 " <i>(" . htmlspecialchars($row->ar_comment) . "</i>)\n");
155
156 }
157 $wgOut->addHTML("</ul>");
158
159 return 0;
160 }
161
162 /* private */ function undelete( $namespace, $title )
163 {
164 global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList;
165 global $wgUseSquid, $wgInternalServer, $wgLinkCache;
166
167 $fname = "doUndeleteArticle";
168
169 if ( "" == $title ) {
170 $wgOut->fatalError( wfMsg( "cannotundelete" ) );
171 return;
172 }
173 $dbw =& wfGetDB( DB_MASTER );
174 extract( $dbw->tableNames( 'cur', 'archive' ) );
175 $t = $dbw->strencode($title);
176
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();
182
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;
192
193 $seqVal = addQuotes( $dbw->nextSequenceValue( 'cur_cur_id_seq' ) );
194
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}";
203 } else {
204 # If already exists, put history entirely into old table
205 $oldones = "";
206 $newid = 0;
207
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 );
211
212 # FIXME: Sometimes restored entries will be _newer_ than the current version.
213 # We should merge.
214 }
215
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 );
222
223 # Finally, clean up the link tables
224 if( $newid ) {
225 $wgLinkCache = new LinkCache();
226 # Select for update
227 $wgLinkCache->forUpdate( true );
228 # Create a dummy OutputPage to update the outgoing links
229 $dummyOut = new OutputPage();
230 # Get the text
231 $text = $dbw->selectField( 'cur', 'cur_text',
232 array( 'cur_id' => $newid, 'cur_namespace' => $namespace ),
233 $fname, 'FOR UPDATE'
234 );
235 $dummyOut->addWikiText( $text );
236
237 $u = new LinksUpdate( $newid, $this->mTargetObj->getPrefixedDBkey() );
238 array_push( $wgDeferredUpdateList, $u );
239
240 Article::onArticleCreate( $this->mTargetObj );
241
242 #TODO: SearchUpdate, etc.
243 }
244
245 # Now that it's safely stored, take it out of the archive
246 $sql = "DELETE FROM $archive WHERE ar_namespace={$namespace} AND " .
247 "ar_title='{$t}'";
248 $dbw->query( $sql, $fname );
249
250
251 # Touch the log?
252 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
253 $log->addEntry( wfMsg( "undeletedarticle", $this->mTarget ), "" );
254
255 $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget ) );
256 return 0;
257 }
258 }
259 ?>