didn't mean to commit that comment
[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 *
21 * @package MediaWiki
22 * @subpackage SpecialPage
23 */
24
25 /** */
26 require_once( 'Revision.php' );
27
28 /**
29 *
30 */
31 function wfSpecialExport( $page = '' ) {
32 global $wgOut, $wgLang, $wgRequest;
33
34 if( $wgRequest->getVal( 'action' ) == 'submit') {
35 $page = $wgRequest->getText( 'pages' );
36 $curonly = $wgRequest->getCheck( 'curonly' );
37 } else {
38 # Pre-check the 'current version only' box in the UI
39 $curonly = true;
40 }
41
42 if( $page != '' ) {
43 $wgOut->disable();
44 header( "Content-type: application/xml; charset=utf-8" );
45 $pages = explode( "\n", $page );
46 $xml = pages2xml( $pages, $curonly );
47 echo $xml;
48 return;
49 }
50
51 $wgOut->addWikiText( wfMsg( "exporttext" ) );
52 $titleObj = Title::makeTitle( NS_SPECIAL, "Export" );
53 $action = $titleObj->escapeLocalURL();
54 $wgOut->addHTML( "
55 <form method='post' action=\"$action\">
56 <input type='hidden' name='action' value='submit' />
57 <textarea name='pages' cols='40' rows='10'></textarea><br />
58 <label><input type='checkbox' name='curonly' value='true' checked='checked' />
59 " . wfMsg( "exportcuronly" ) . "</label><br />
60 <input type='submit' />
61 </form>
62 " );
63 }
64
65 function pages2xml( $pages, $curonly = false ) {
66 $fname = 'pages2xml';
67 wfProfileIn( $fname );
68
69 global $wgContLanguageCode, $wgInputEncoding, $wgContLang;
70 $xml = "<" . "?xml version=\"1.0\" encoding=\"UTF-8\" ?" . ">\n" .
71 "<mediawiki version=\"0.1\" xml:lang=\"$wgContLanguageCode\">\n";
72 foreach( $pages as $page ) {
73 $xml .= page2xml( $page, $curonly );
74 }
75 $xml .= "</mediawiki>\n";
76 if($wgInputEncoding != "utf-8")
77 $xml = $wgContLang->iconv( $wgInputEncoding, "utf-8", $xml );
78
79 wfProfileOut( $fname );
80 return $xml;
81 }
82
83 function page2xml( $page, $curonly, $full = false ) {
84 global $wgLang;
85 $fname = 'page2xml';
86 wfProfileIn( $fname );
87
88 $title = Title::NewFromText( $page );
89 if( !$title ) {
90 wfProfileOut( $fname );
91 return "";
92 }
93
94 $dbr =& wfGetDB( DB_SLAVE );
95 $s = $dbr->selectRow( 'page',
96 array( 'page_id', 'page_restrictions' ),
97 array( 'page_namespace' => $title->getNamespace(),
98 'page_title' => $title->getDbkey() ) );
99 if( $s ) {
100 $tl = xmlsafe( $title->getPrefixedText() );
101 $xml = " <page>\n";
102 $xml .= " <title>$tl</title>\n";
103
104 if( $full ) {
105 $xml .= " <id>$s->page_id</id>\n";
106 }
107 if( $s->page_restrictions ) {
108 $xml .= " <restrictions>" . xmlsafe( $s->page_restrictions ) . "</restrictions>\n";
109 }
110
111 if( $curonly ) {
112 $res = Revision::fetchRevision( $title );
113 } else {
114 $res = Revision::fetchAllRevisions( $title );
115 }
116 if( $res ) {
117 while( $s = $res->fetchObject() ) {
118 $rev = new Revision( $s );
119 $xml .= revision2xml( $rev, $full, false );
120 }
121 $res->free();
122 }
123
124 $xml .= " </page>\n";
125 wfProfileOut( $fname );
126 return $xml;
127 } else {
128 wfProfileOut( $fname );
129 return "";
130 }
131 }
132
133 /**
134 * @return string
135 * @param Revision $rev
136 * @param bool $full
137 * @access private
138 */
139 function revision2xml( $rev, $full ) {
140 $fname = 'revision2xml';
141 wfProfileIn( $fname );
142
143 $xml = " <revision>\n";
144 if( $full )
145 $xml .= " <id>" . $rev->getId() . "</id>\n";
146
147 $ts = wfTimestamp2ISO8601( $rev->getTimestamp() );
148 $xml .= " <timestamp>$ts</timestamp>\n";
149
150 if( $rev->getUser() ) {
151 $u = "<username>" . xmlsafe( $rev->getUserText() ) . "</username>";
152 if( $full )
153 $u .= "<id>" . $rev->getUser() . "</id>";
154 } else {
155 $u = "<ip>" . xmlsafe( $rev->getUserText() ) . "</ip>";
156 }
157 $xml .= " <contributor>$u</contributor>\n";
158
159 if( $rev->isMinor() ) {
160 $xml .= " <minor/>\n";
161 }
162 if($rev->getComment() != "") {
163 $c = xmlsafe( $rev->getComment() );
164 $xml .= " <comment>$c</comment>\n";
165 }
166
167 $t = xmlsafe( $rev->getText() );
168
169 $xml .= " <text>$t</text>\n";
170 $xml .= " </revision>\n";
171 wfProfileOut( $fname );
172 return $xml;
173 }
174
175 function wfTimestamp2ISO8601( $ts ) {
176 #2003-08-05T18:30:02Z
177 return preg_replace( '/^(....)(..)(..)(..)(..)(..)$/', '$1-$2-$3T$4:$5:$6Z', $ts );
178 }
179
180 function xmlsafe( $string ) {
181 $fname = 'xmlsafe';
182 wfProfileIn( $fname );
183
184 /**
185 * The page may contain old data which has not been properly normalized.
186 * Invalid UTF-8 sequences or forbidden control characters will make our
187 * XML output invalid, so be sure to strip them out.
188 */
189 global $wgUseLatin1;
190 if( $wgUseLatin1 ) {
191 /**
192 * We know the UTF-8 is valid since we converted outselves.
193 * Just check for forbidden controls...
194 */
195 $string = preg_replace( '/[\x00-\x08\x0b-\x1f]/', '', $string );
196 } else {
197 $string = UtfNormal::cleanUp( $string );
198 }
199
200 $string = htmlspecialchars( $string );
201 wfProfileOut( $fname );
202 return $string;
203 }
204 ?>