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