Skip bad titles
[lhc/web/wiklou.git] / includes / SpecialExport.php
1 <?
2
3 function wfSpecialExport( $page = "" ) {
4 global $wgOut, $wgLang;
5
6 if( $_REQUEST['action'] == 'submit') {
7 $page = $_REQUEST['pages'];
8 $curonly = isset($_REQUEST['curonly']) ? true : false;
9 } else {
10 $curonly = true;
11 }
12
13 if( $page != "" ) {
14 header( "Content-type: application/xml; charset=utf-8" );
15 $pages = explode( "\n", $page );
16 $xml = pages2xml( $pages, $curonly );
17 echo $xml;
18 exit();
19 }
20
21 $wgOut->addWikiText( wfMsg( "exporttext" ) );
22 $action = wfLocalUrlE( $wgLang->SpecialPage( "Export" ) );
23 $wgOut->addHTML( "
24 <form method='post' action=\"$action\">
25 <input type='hidden' name='action' value='submit' />
26 <textarea name='pages' cols='40' rows='10'></textarea><br />
27 <label><input type='checkbox' name='curonly' value='true' checked />
28 " . wfMsg( "exportcuronly" ) . "</label><br />
29 <input type='submit' />
30 </form>
31 " );
32 }
33
34 function pages2xml( $pages, $curonly = false ) {
35 global $wgLanguageCode, $wgInputEncoding, $wgLang;
36 $xml = "<mediawiki version=\"0.1\" xml:ns=\"$wgLanguageCode\">\n";
37 foreach( $pages as $page ) {
38 $xml .= page2xml( $page, $curonly );
39 }
40 $xml .= "</mediawiki>\n";
41 if($wgInputEncoding != "utf-8")
42 $xml = $wgLang->iconv( $wgInputEncoding, "utf-8", $xml );
43 return $xml;
44 }
45
46 function page2xml( $page, $curonly, $full = false ) {
47 global $wgInputCharset, $wgLang;
48 $title = Title::NewFromText( $page );
49 if( !$title ) return "";
50 $t = wfStrencode( $title->getDBKey() );
51 $ns = $title->getNamespace();
52 $sql = "SELECT cur_id as id,cur_timestamp as timestamp,cur_user as user,cur_user_text as user_text," .
53 "cur_restrictions as restrictions,cur_comment as comment,cur_text as text FROM cur " .
54 "WHERE cur_namespace=$ns AND cur_title='$t'";
55 $res = wfQuery( $sql, DB_READ );
56 if( $s = wfFetchObject( $res ) ) {
57 $tl = htmlspecialchars( $title->getPrefixedText() );
58 $xml = " <page>\n";
59 $xml .= " <title>$tl</title>\n";
60 if( $full ) {
61 $xml .= " <id>$s->id</id>\n";
62 }
63 if( $s->restrictions ) {
64 $xml .= " <restrictions>$s->restrictions</restrictions>\n";
65 }
66 if( !$curonly ) {
67 $sql = "SELECT old_id as id,old_timestamp as timestamp, old_user as user, old_user_text as user_text," .
68 "old_comment as comment, old_text as text FROM old " .
69 "WHERE old_namespace=$ns AND old_title='$t' ORDER BY old_timestamp";
70 $res = wfQuery( $sql, DB_READ );
71
72 while( $s = wfFetchObject( $res ) ) {
73 $xml .= revision2xml( $s, $full, false );
74 }
75 }
76 $xml .= revision2xml( $s, $full, true );
77 $xml .= " </page>\n";
78 return $xml;
79 } else {
80 return "";
81 }
82 }
83
84 function revision2xml( $s, $full, $cur ) {
85 $ts = wfTimestamp2ISO8601( $s->timestamp );
86 $xml = " <revision>\n";
87 if($full && !$cur)
88 $xml .= " <id>$s->id</id>\n";
89 $xml .= " <timestamp>$ts</timestamp>\n";
90 if($s->user) {
91 $u = "<username>" . htmlspecialchars( $s->user_text ) . "</username>";
92 if($full)
93 $u .= "<id>$s->user</id>";
94 } else {
95 $u = "<ip>" . htmlspecialchars( $s->user_text ) . "</ip>";
96 }
97 $xml .= " <contributor>$u</contributor>\n";
98 if($s->minor) {
99 $xml .= " <minor/>\n";
100 }
101 if($s->comment != "") {
102 $c = htmlspecialchars( $s->comment );
103 $xml .= " <comment>$c</comment>\n";
104 }
105 $t = htmlspecialchars( $s->text );
106 $xml .= " <text>$t</text>\n";
107 $xml .= " </revision>\n";
108 return $xml;
109 }
110
111 function wfTimestamp2ISO8601( $ts ) {
112 #2003-08-05T18:30:02Z
113 return preg_replace( '/^(....)(..)(..)(..)(..)(..)$/', '$1-$2-$3T$4:$5:$6Z', $ts );
114 }
115
116 ?>