URL-escape the main page link on the logo
[mediawiki.git] / includes / SpecialUndelete.php
blob0c19aae64d6dd92d9074bebe6f4927d116fd5915
1 <?
3 function wfSpecialUndelete( $par )
5 global $wgLang, $wgUser, $wgOut, $action, $target, $timestamp, $restore;
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 );
15 if( isset( $action ) and isset( $restore) and $action == "submit" ) {
16 return doUndeleteArticle( $namespace, $title );
18 return doUndeleteShowHistory( $namespace, $title );
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 );
25 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
26 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
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;
36 $wgOut->addHTML( "<li>" .
37 $sk->makeKnownLink( $wgLang->specialPage( "Undelete" ),
38 $n, "target=" . urlencode($n) ) . " " .
39 str_replace( '$1', $row->count, wfMsg( "undeleterevisions" )) );
41 $wgOut->addHTML( "</ul>\n" );
43 return $ret;
46 /* private */ function doUndeleteShowRevision( $namespace, $title, $timestamp ) {
47 global $wgLang, $wgUser, $wgOut, $action, $target, $timestamp, $restore;
49 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
51 $sql = "SELECT ar_text FROM archive WHERE ar_namespace={$namespace} AND ar_title=\"{$title}\" AND ar_timestamp={$timestamp}";
52 $ret = wfQuery( $sql );
53 $row = wfFetchObject( $ret );
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 );
60 return 0;
63 /* private */ function doUndeleteShowHistory( $namespace, $title ) {
64 global $wgLang, $wgUser, $wgOut, $action, $target, $timestamp, $restore;
66 $sk = $wgUser->getSkin();
67 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
68 $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n<hr>\n" . $row->ar_text );
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>");
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] );
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 );
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");
100 $wgOut->addHTML("</ul>");
102 return 0;
105 /* private */ function doUndeleteArticle( $namespace, $title )
107 global $wgUser, $wgOut, $wgLang, $target, $wgDeferredUpdateList;
109 $fname = "doUndeleteArticle";
111 if ( "" == $title ) {
112 $wgOut->fatalError( wfMsg( "cannotundelete" ) );
113 return;
115 $t = addslashes($title);
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 );
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, $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;
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, $fname );
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}", $fname );
155 $row = wfFetchObject( $res );
156 $text = $row->cur_text;
157 $dummyOut->addWikiText( $text );
158 wfFreeResult( $res );
160 $u = new LinksUpdate( $newid, $to->getPrefixedDBkey() );
161 array_push( $wgDeferredUpdateList, $u );
163 #TODO: SearchUpdate, etc.
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, $fname );
172 # Touch the log?
174 $wgOut->addWikiText(str_replace("$1", $target, wfMsg("undeletedtext")));
175 return 0;