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