Replace the random boolean parameters on Title::getURL() with a set of
[lhc/web/wiklou.git] / includes / SpecialUndelete.php
1 <?php
2
3 function wfSpecialUndelete( $par )
4 {
5 global $wgLang, $wgUser, $wgOut, $action, $target, $timestamp, $restore;
6
7 if( $par != "" ) $target = $par;
8 if( isset($target ) ) {
9 $t = Title::newFromURL( $target );
10 $title = $t->mDbkeyform;
11 $namespace = $t->mNamespace;
12 if( isset( $timestamp ) ) {
13 return doUndeleteShowRevision( $namespace, $title, $timestamp );
14 }
15 if( isset( $action ) and isset( $restore) and $action == "submit" ) {
16 return doUndeleteArticle( $namespace, $title );
17 }
18 return doUndeleteShowHistory( $namespace, $title );
19 }
20
21 # List undeletable articles
22 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM archive GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
23 $res = wfQuery( $sql, DB_READ );
24
25 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
26 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
27
28 $special = $wgLang->getNsText( Namespace::getSpecial() );
29 $sk = $wgUser->getSkin();
30 $wgOut->addHTML( "<ul>\n" );
31 while ($row = wfFetchObject( $res )) {
32 $n = ($row->ar_namespace ?
33 ($wgLang->getNsText( $row->ar_namespace ) . ":") : "").
34 $row->ar_title;
35
36 $wgOut->addHTML( "<li>" .
37 $sk->makeKnownLink( $wgLang->specialPage( "Undelete" ),
38 $n, "target=" . urlencode($n) ) . " " .
39 wfMsg( "undeleterevisions", $row->count ) );
40 }
41 $wgOut->addHTML( "</ul>\n" );
42
43 return $ret;
44 }
45
46 /* private */ function doUndeleteShowRevision( $namespace, $title, $timestamp ) {
47 global $wgLang, $wgUser, $wgOut, $action, $target, $timestamp, $restore;
48
49 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
50
51 $sql = "SELECT ar_text,ar_flags FROM archive WHERE ar_namespace={$namespace} AND ar_title=\"{$title}\" AND ar_timestamp={$timestamp}";
52 $ret = wfQuery( $sql, DB_READ );
53 $row = wfFetchObject( $ret );
54
55 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
56 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision", $wgLang->date($timestamp, true) )
57 . ")\n<hr>\n" . Article::getRevisionText( $row, "ar_" ) );
58
59 return 0;
60 }
61
62 /* private */ function doUndeleteShowHistory( $namespace, $title ) {
63 global $wgLang, $wgUser, $wgOut, $action, $target, $timestamp, $restore;
64
65 $sk = $wgUser->getSkin();
66 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
67
68 $sql = "SELECT ar_minor_edit,ar_timestamp,ar_user,ar_user_text,ar_comment
69 FROM archive WHERE ar_namespace={$namespace} AND ar_title=\"{$title}\"
70 ORDER BY ar_timestamp DESC";
71 $ret = wfQuery( $sql, DB_READ );
72
73 if( wfNumRows( $ret ) == 0 ) {
74 $wgOut->addWikiText( wfMsg( "nohistory" ) );
75 return 0;
76 }
77
78 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n<hr>\n" . $row->ar_text );
79
80 $titleObj = Title::makeTitle( NS_SPECIAL, "Undelete" );
81 $action = $titleObj->escapeLocalURL( "action=submit" );
82 $wgOut->addHTML("<p>
83 <form id=\"undelete\" method=\"post\" action=\"{$action}\">
84 <input type=hidden name=\"target\" value=\"{$target}\">
85 <input type=submit name=\"restore\" value=\"".wfMsg("undeletebtn")."\">
86 </form>");
87
88 $log = wfGetSQL("cur", "cur_text", "cur_namespace=4 AND cur_title=\"".wfMsg("dellogpage")."\"" );
89 if(preg_match("/^(.*".
90 preg_quote( ($namespace ? ($wgLang->getNsText($namespace) . ":") : "")
91 . str_replace("_", " ", $title), "/" ).".*)$/m", $log, $m)) {
92 $wgOut->addWikiText( $m[1] );
93 }
94
95 $special = $wgLang->getNsText( Namespace::getSpecial() );
96 $wgOut->addHTML("<ul>");
97 while( $row = wfFetchObject( $ret ) ) {
98 $wgOut->addHTML( "<li>" .
99 $sk->makeKnownLink( $wgLang->specialPage( "Undelete" ),
100 $wgLang->timeanddate( $row->ar_timestamp, true ),
101 "target=" . urlencode($target) . "&timestamp={$row->ar_timestamp}" ) . " " .
102 ". . {$row->ar_user_text}" .
103 " <i>(" . htmlspecialchars($row->ar_comment) . "</i>)\n");
104
105 }
106 $wgOut->addHTML("</ul>");
107
108 return 0;
109 }
110
111 /* private */ function doUndeleteArticle( $namespace, $title )
112 {
113 global $wgUser, $wgOut, $wgLang, $target, $wgDeferredUpdateList;
114 global $wgUseSquid, $wgInternalServer;
115
116 $fname = "doUndeleteArticle";
117
118 if ( "" == $title ) {
119 $wgOut->fatalError( wfMsg( "cannotundelete" ) );
120 return;
121 }
122 $t = addslashes($title);
123
124 # Move article and history from the "archive" table
125 $sql = "SELECT COUNT(*) AS count FROM cur WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
126 $res = wfQuery( $sql, DB_READ );
127 $row = wfFetchObject( $res );
128 $now = wfTimestampNow();
129
130 if( $row->count == 0) {
131 # Have to create new article...
132 $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";
133 $res = wfQuery( $sql, DB_READ, $fname );
134 $s = wfFetchObject( $res );
135 $max = $s->ar_timestamp;
136 $redirect = MagicWord::get( MAG_REDIRECT );
137 $redir = $redirect->matchStart( $s->ar_text ) ? 1 : 0;
138
139 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
140 "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp,cur_minor_edit,cur_is_redirect,cur_random,cur_touched)" .
141 "SELECT ar_namespace,ar_title,ar_text,ar_comment," .
142 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,{$redir},RAND(),'{$now}' FROM archive " .
143 "WHERE ar_namespace={$namespace} AND ar_title='{$t}' AND ar_timestamp={$max}";
144 wfQuery( $sql, DB_WRITE, $fname );
145 $newid = wfInsertId();
146 $oldones = "AND ar_timestamp<{$max}";
147 } else {
148 # If already exists, put history entirely into old table
149 $oldones = "";
150 $newid = 0;
151
152 # But to make the history list show up right, we need to touch it.
153 $sql = "UPDATE cur SET cur_touched='{$now}' WHERE cur_namespace={$namespace} AND cur_title='{$t}'";
154 wfQuery( $sql, DB_WRITE, $fname );
155
156 # FIXME: Sometimes restored entries will be _newer_ than the current version.
157 # We should merge.
158 }
159
160 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
161 "old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp,old_minor_edit," .
162 "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," .
163 "ar_user,ar_user_text,ar_timestamp,99999999999999-ar_timestamp,ar_minor_edit,ar_flags " .
164 "FROM archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}";
165 wfQuery( $sql, DB_WRITE, $fname );
166
167 # Finally, clean up the link tables
168 if( $newid ) {
169 # Create a dummy OutputPage to update the outgoing links
170 # This works at the moment due to good luck. It may stop working in the
171 # future. Damn globals.
172 $dummyOut = new OutputPage();
173 $to = Title::newFromDBKey( $target );
174 $res = wfQuery( "SELECT cur_text FROM cur WHERE cur_id={$newid} " .
175 "AND cur_namespace={$namespace}", DB_READ, $fname );
176 $row = wfFetchObject( $res );
177 $text = $row->cur_text;
178 $dummyOut->addWikiText( $text );
179 wfFreeResult( $res );
180
181 $u = new LinksUpdate( $newid, $to->getPrefixedDBkey() );
182 array_push( $wgDeferredUpdateList, $u );
183
184 Article::onArticleCreate( $to );
185
186 # Squid purging
187 if ( $wgUseSquid ) {
188 /* this needs to be done after LinksUpdate */
189 $u = new SquidUpdate($to);
190 array_push( $wgDeferredUpdateList, $u );
191 }
192
193 #TODO: SearchUpdate, etc.
194 }
195
196 # Now that it's safely stored, take it out of the archive
197 $sql = "DELETE FROM archive WHERE ar_namespace={$namespace} AND " .
198 "ar_title='{$t}'";
199 wfQuery( $sql, DB_WRITE, $fname );
200
201
202 # Touch the log?
203 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
204 $log->addEntry( wfMsg( "undeletedarticle", $target ), "" );
205
206 $wgOut->addWikiText( wfMsg( "undeletedtext", $target ) );
207 return 0;
208 }
209 ?>