84e215edec2baceb23bb51e94efae993a1a31618
[lhc/web/wiklou.git] / includes / SpecialExport.php
1 <?php
2 # Copyright (C) 2003 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 function wfSpecialExport( $page = "" ) {
21 global $wgOut, $wgLang;
22
23 if( $_REQUEST['action'] == 'submit') {
24 $page = $_REQUEST['pages'];
25 $curonly = isset($_REQUEST['curonly']) ? true : false;
26 } else {
27 $curonly = true;
28 }
29
30 if( $page != "" ) {
31 header( "Content-type: application/xml; charset=utf-8" );
32 $pages = explode( "\n", $page );
33 $xml = pages2xml( $pages, $curonly );
34 echo $xml;
35 wfAbruptExit();
36 }
37
38 $wgOut->addWikiText( wfMsg( "exporttext" ) );
39 $titleObj = Title::makeTitle( NS_SPECIAL, "Export" );
40 $action = $titleObj->escapeLocalURL();
41 $wgOut->addHTML( "
42 <form method='post' action=\"$action\">
43 <input type='hidden' name='action' value='submit' />
44 <textarea name='pages' cols='40' rows='10'></textarea><br />
45 <label><input type='checkbox' name='curonly' value='true' checked />
46 " . wfMsg( "exportcuronly" ) . "</label><br />
47 <input type='submit' />
48 </form>
49 " );
50 }
51
52 function pages2xml( $pages, $curonly = false ) {
53 global $wgLanguageCode, $wgInputEncoding, $wgLang;
54 $xml = "<" . "?xml version=\"1.0\" encoding=\"UTF-8\" ?" . ">\n" .
55 "<mediawiki version=\"0.1\" xml:lang=\"$wgLanguageCode\">\n";
56 foreach( $pages as $page ) {
57 $xml .= page2xml( $page, $curonly );
58 }
59 $xml .= "</mediawiki>\n";
60 if($wgInputEncoding != "utf-8")
61 $xml = $wgLang->iconv( $wgInputEncoding, "utf-8", $xml );
62 return $xml;
63 }
64
65 function page2xml( $page, $curonly, $full = false ) {
66 global $wgInputCharset, $wgLang;
67 $title = Title::NewFromText( $page );
68 if( !$title ) return "";
69 $t = wfStrencode( $title->getDBKey() );
70 $ns = $title->getNamespace();
71 $sql = "SELECT cur_id as id,cur_timestamp as timestamp,cur_user as user,cur_user_text as user_text," .
72 "cur_restrictions as restrictions,cur_comment as comment,cur_text as text FROM cur " .
73 "WHERE cur_namespace=$ns AND cur_title='$t'";
74 $res = wfQuery( $sql, DB_READ );
75 if( $s = wfFetchObject( $res ) ) {
76 $tl = htmlspecialchars( $title->getPrefixedText() );
77 $xml = " <page>\n";
78 $xml .= " <title>$tl</title>\n";
79 if( $full ) {
80 $xml .= " <id>$s->id</id>\n";
81 }
82 if( $s->restrictions ) {
83 $xml .= " <restrictions>$s->restrictions</restrictions>\n";
84 }
85 if( !$curonly ) {
86 $sql = "SELECT old_id as id,old_timestamp as timestamp, old_user as user, old_user_text as user_text," .
87 "old_comment as comment, old_text as text, old_flags as flags FROM old " .
88 "WHERE old_namespace=$ns AND old_title='$t' ORDER BY old_timestamp";
89 $res = wfQuery( $sql, DB_READ );
90
91 while( $s2 = wfFetchObject( $res ) ) {
92 $xml .= revision2xml( $s2, $full, false );
93 }
94 }
95 $xml .= revision2xml( $s, $full, true );
96 $xml .= " </page>\n";
97 return $xml;
98 } else {
99 return "";
100 }
101 }
102
103 function revision2xml( $s, $full, $cur ) {
104 $ts = wfTimestamp2ISO8601( $s->timestamp );
105 $xml = " <revision>\n";
106 if($full && !$cur)
107 $xml .= " <id>$s->id</id>\n";
108 $xml .= " <timestamp>$ts</timestamp>\n";
109 if($s->user) {
110 $u = "<username>" . htmlspecialchars( $s->user_text ) . "</username>";
111 if($full)
112 $u .= "<id>$s->user</id>";
113 } else {
114 $u = "<ip>" . htmlspecialchars( $s->user_text ) . "</ip>";
115 }
116 $xml .= " <contributor>$u</contributor>\n";
117 if($s->minor) {
118 $xml .= " <minor/>\n";
119 }
120 if($s->comment != "") {
121 $c = htmlspecialchars( $s->comment );
122 $xml .= " <comment>$c</comment>\n";
123 }
124 $t = htmlspecialchars( Article::getRevisionText( $s, "" ) );
125 $xml .= " <text>$t</text>\n";
126 $xml .= " </revision>\n";
127 return $xml;
128 }
129
130 function wfTimestamp2ISO8601( $ts ) {
131 #2003-08-05T18:30:02Z
132 return preg_replace( '/^(....)(..)(..)(..)(..)(..)$/', '$1-$2-$3T$4:$5:$6Z', $ts );
133 }
134
135 ?>