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