2 # Copyright (C) 2003 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
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.
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.
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
23 * @subpackage SpecialPage
29 function wfSpecialExport( $page = '' ) {
30 global $wgOut, $wgLang, $wgRequest;
32 if( $wgRequest->getVal( 'action' ) == 'submit') {
33 $page = $wgRequest->getText( 'pages' );
34 $curonly = $wgRequest->getCheck( 'curonly' );
36 # Pre-check the 'current version only' box in the UI
42 header( "Content-type: application/xml; charset=utf-8" );
43 $pages = explode( "\n", $page );
44 $xml = pages2xml( $pages, $curonly );
49 $wgOut->addWikiText( wfMsg( "exporttext" ) );
50 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Export" );
51 $action = $titleObj->escapeLocalURL();
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' />
63 function pages2xml( $pages, $curonly = false ) {
65 wfProfileIn( $fname );
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 );
73 $xml .= "</mediawiki>\n";
74 if($wgInputEncoding != "utf-8")
75 $xml = $wgContLang->iconv( $wgInputEncoding, "utf-8", $xml );
77 wfProfileOut( $fname );
81 function page2xml( $page, $curonly, $full = false ) {
84 wfProfileIn( $fname );
86 $title = Title
::NewFromText( $page );
88 wfProfileOut( $fname );
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 );
97 $tl = xmlsafe( $title->getPrefixedText() );
99 $xml .= " <title>$tl</title>\n";
101 $xml .= " <id>$s->id</id>\n";
103 if( $s->restrictions
) {
104 $xml .= " <restrictions>$s->restrictions</restrictions>\n";
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' )
113 while( $s2 = $dbr->fetchObject( $res ) ) {
114 $xml .= revision2xml( $s2, $full, false );
117 $xml .= revision2xml( $s, $full, true );
118 $xml .= " </page>\n";
119 wfProfileOut( $fname );
122 wfProfileOut( $fname );
127 function revision2xml( $s, $full, $cur ) {
128 $fname = 'revision2xml';
129 wfProfileIn( $fname );
131 $ts = wfTimestamp2ISO8601( $s->timestamp
);
132 $xml = " <revision>\n";
134 $xml .= " <id>$s->id</id>\n";
135 $xml .= " <timestamp>$ts</timestamp>\n";
137 $u = "<username>" . xmlsafe( $s->user_text
) . "</username>";
139 $u .= "<id>$s->user</id>";
141 $u = "<ip>" . xmlsafe( $s->user_text
) . "</ip>";
143 $xml .= " <contributor>$u</contributor>\n";
144 if( !empty( $s->minor
) ) {
145 $xml .= " <minor/>\n";
147 if($s->comment
!= "") {
148 $c = xmlsafe( $s->comment
);
149 $xml .= " <comment>$c</comment>\n";
151 $t = xmlsafe( Article
::getRevisionText( $s, "" ) );
152 $xml .= " <text>$t</text>\n";
153 $xml .= " </revision>\n";
154 wfProfileOut( $fname );
158 function wfTimestamp2ISO8601( $ts ) {
159 #2003-08-05T18:30:02Z
160 return preg_replace( '/^(....)(..)(..)(..)(..)(..)$/', '$1-$2-$3T$4:$5:$6Z', $ts );
163 function xmlsafe( $string ) {
165 wfProfileIn( $fname );
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.
175 * We know the UTF-8 is valid since we converted outselves.
176 * Just check for forbidden controls...
178 $string = preg_replace( '/[\x00-\x08\x0b-\x1f]/', '', $string );
180 $string = UtfNormal
::cleanUp( $string );
183 $string = htmlspecialchars( $string );
184 wfProfileOut( $fname );