24c8dff2bbd8b4e25280bfadfb664b25348211e6
[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 */
23
24 /**
25 *
26 */
27 function wfSpecialExport( $page = '' ) {
28 global $wgOut, $wgLang, $wgRequest;
29
30 if( $wgRequest->getVal( 'action' ) == 'submit') {
31 $page = $wgRequest->getText( 'pages' );
32 $curonly = $wgRequest->getCheck( 'curonly' );
33 } else {
34 # Pre-check the 'current version only' box in the UI
35 $curonly = true;
36 }
37
38 if( $page != '' ) {
39 header( "Content-type: application/xml; charset=utf-8" );
40 $pages = explode( "\n", $page );
41 $xml = pages2xml( $pages, $curonly );
42 echo $xml;
43 wfAbruptExit();
44 }
45
46 $wgOut->addWikiText( wfMsg( "exporttext" ) );
47 $titleObj = Title::makeTitle( NS_SPECIAL, "Export" );
48 $action = $titleObj->escapeLocalURL();
49 $wgOut->addHTML( "
50 <form method='post' action=\"$action\">
51 <input type='hidden' name='action' value='submit' />
52 <textarea name='pages' cols='40' rows='10'></textarea><br />
53 <label><input type='checkbox' name='curonly' value='true' checked='checked' />
54 " . wfMsg( "exportcuronly" ) . "</label><br />
55 <input type='submit' />
56 </form>
57 " );
58 }
59
60 function pages2xml( $pages, $curonly = false ) {
61 global $wgLanguageCode, $wgInputEncoding, $wgLang;
62 $xml = "<" . "?xml version=\"1.0\" encoding=\"UTF-8\" ?" . ">\n" .
63 "<mediawiki version=\"0.1\" xml:lang=\"$wgLanguageCode\">\n";
64 foreach( $pages as $page ) {
65 $xml .= page2xml( $page, $curonly );
66 }
67 $xml .= "</mediawiki>\n";
68 if($wgInputEncoding != "utf-8")
69 $xml = $wgLang->iconv( $wgInputEncoding, "utf-8", $xml );
70 return $xml;
71 }
72
73 function page2xml( $page, $curonly, $full = false ) {
74 global $wgLang;
75 $fname = 'page2xml';
76
77 $title = Title::NewFromText( $page );
78 if( !$title ) return "";
79
80 $dbr =& wfGetDB( DB_SLAVE );
81 $s = $dbr->selectRow( 'cur', array( 'cur_id as id','cur_timestamp as timestamp','cur_user as user',
82 'cur_user_text as user_text', 'cur_restrictions as restrictions','cur_comment as comment',
83 'cur_text as text' ), $title->curCond(), $fname );
84 if( $s !== false ) {
85 $tl = htmlspecialchars( $title->getPrefixedText() );
86 $xml = " <page>\n";
87 $xml .= " <title>$tl</title>\n";
88 if( $full ) {
89 $xml .= " <id>$s->id</id>\n";
90 }
91 if( $s->restrictions ) {
92 $xml .= " <restrictions>$s->restrictions</restrictions>\n";
93 }
94 if( !$curonly ) {
95 $res = $dbr->select( 'old', array( 'old_id as id','old_timestamp as timestamp',
96 'old_user as user', 'old_user_text as user_text', 'old_comment as comment',
97 'old_text as text', 'old_flags as flags' ), $title->curCond(),
98 array( 'ORDER BY' => 'old_timestamp' ), $fname
99 );
100
101 while( $s2 = $dbr->fetchObject( $res ) ) {
102 $xml .= revision2xml( $s2, $full, false );
103 }
104 }
105 $xml .= revision2xml( $s, $full, true );
106 $xml .= " </page>\n";
107 return $xml;
108 } else {
109 return "";
110 }
111 }
112
113 function revision2xml( $s, $full, $cur ) {
114 $ts = wfTimestamp2ISO8601( $s->timestamp );
115 $xml = " <revision>\n";
116 if($full && !$cur)
117 $xml .= " <id>$s->id</id>\n";
118 $xml .= " <timestamp>$ts</timestamp>\n";
119 if($s->user) {
120 $u = "<username>" . htmlspecialchars( $s->user_text ) . "</username>";
121 if($full)
122 $u .= "<id>$s->user</id>";
123 } else {
124 $u = "<ip>" . htmlspecialchars( $s->user_text ) . "</ip>";
125 }
126 $xml .= " <contributor>$u</contributor>\n";
127 if( !empty( $s->minor ) ) {
128 $xml .= " <minor/>\n";
129 }
130 if($s->comment != "") {
131 $c = htmlspecialchars( $s->comment );
132 $xml .= " <comment>$c</comment>\n";
133 }
134 $t = htmlspecialchars( Article::getRevisionText( $s, "" ) );
135 $xml .= " <text>$t</text>\n";
136 $xml .= " </revision>\n";
137 return $xml;
138 }
139
140 function wfTimestamp2ISO8601( $ts ) {
141 #2003-08-05T18:30:02Z
142 return preg_replace( '/^(....)(..)(..)(..)(..)(..)$/', '$1-$2-$3T$4:$5:$6Z', $ts );
143 }
144
145 ?>