Change behavior of toolbar in browsers not supporting document.selection:
[lhc/web/wiklou.git] / includes / SpecialImport.php
1 <?
2
3 function wfSpecialImport( $page = "" ) {
4 global $wgOut, $wgLang;
5
6 if( $_REQUEST['action'] == 'submit') {
7 $importer = WikiImporter::fromUpload();
8 if( empty($importer) ) {
9 $wgOut->addHTML( wfMsg( "importnofile" ) );
10 } else {
11 if( $importer->doImport() ) {
12 # Success!
13 $wgOut->addHTML( "<p>" . wfMsg( "importsuccess" ) . "</p>" );
14 } else {
15 $wgOut->addHTML( "<p>" . wfMsg( "importfailed",
16 htmlspecialchars( $importer->getError() ) ) . "</p>" );
17 }
18 }
19 }
20
21 $wgOut->addWikiText( "<p>" . wfMsg( "importtext" ) . "</p>" );
22 $action = wfLocalUrlE( $wgLang->SpecialPage( "Import" ) );
23 $wgOut->addHTML( "
24 <form enctype='multipart/form-data' method='post' action=\"$action\">
25 <input type='hidden' name='action' value='submit' />
26 <input type='file' name='xmlimport' value='' size='40' /><br />
27 <input type='submit' value='" . wfMsg( "uploadbtn" ) . "'/>
28 </form>
29 " );
30 }
31
32 function wfISO86012Timestamp( $ts ) {
33 #2003-08-05T18:30:02Z
34 # Warning: overly simplistic!
35 return preg_replace( '/^(....)-(..)-(..)T(..):(..):(..)$/', '$1$2$3$4$5$6', $ts );
36 }
37
38 class WikiImporter {
39 var $mSource, $mError;
40
41 function WikiImporter() {
42 $this->mSource = NULL;
43 $this->mError = XML_ERROR_NONE;
44 }
45
46 /* static */ function fromUpload() {
47 if( !empty( $_FILES['xmlimport'] ) ) {
48 $fname = $_FILES['xmlimport']['tmp_name'];
49 if( is_uploaded_file($fname ) ) {
50 $im = new WikiImporter;
51 $im->mSource = file_get_contents( $fname );
52 return $im;
53 }
54 }
55 return NULL;
56 }
57
58 function doImport() {
59 if( !$this->mSource ) return false;
60
61 /*FIXME*/
62 global $wgOut;
63 $wgOut->addHTML( "<pre>" . htmlspecialchars($this->mSource) . "</pre>" );
64
65 $parser = xml_parser_create( "UTF-8" );
66
67 /* case folding violates XML standard, turn it off */
68 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
69
70 xml_set_object( $parser, &$this );
71 xml_set_element_handler( $parser, "in_start", "" );
72
73 if( !xml_parse( $parser, $this->mSource, true ) ) {
74 # return error message
75 $this->mError = xml_get_error_code( $parser );
76 xml_parser_free( $parser );
77 return false;
78 }
79 xml_parser_free( $parser );
80 return true;
81 }
82
83 function getError() {
84 return xml_error_string( $this->mError );
85 }
86
87 function throwXmlError( $err ) {
88 #echo htmlspecialchars("\n** $err **\n");
89 #die();
90 $this->debug( "FAILURE: $err" );
91 }
92
93 function debug( $data ) {
94 global $wgOut;
95 if( true )
96 $wgOut->addHTML( htmlspecialchars( $data ) . "<br>\n" );
97 }
98
99 /* xml parser callbacks */
100 function donothing( $parser, $x, $y="" ) {
101 #$this->debug( "donothing" );
102 }
103
104 function in_start( $parser, $name, $attribs ) {
105 $this->debug( "in_start $name" );
106 if( $name != "mediawiki" ) {
107 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
108 }
109 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
110 }
111
112 function in_mediawiki( $parser, $name, $attribs ) {
113 $this->debug( "in_mediawiki $name" );
114 if( $name != "page" ) {
115 return $this->throwXMLerror( "Expected <page>, got <$name>" );
116 }
117 xml_set_element_handler( $parser, "in_page", "out_page" );
118 }
119 function out_mediawiki( $parser, $name ) {
120 $this->debug( "out_mediawiki $name" );
121 if( $name != "mediawiki" ) {
122 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
123 }
124 xml_set_element_handler( $parser, "donothing", "donothing" );
125 }
126
127 function in_page( $parser, $name, $attribs ) {
128 $this->debug( "in_page $name" );
129 switch( $name ) {
130 case "id":
131 case "title":
132 case "restrictions":
133 $this->appendfield = $name;
134 $this->parenttag = "page";
135 xml_set_element_handler( $parser, "in_nothing", "out_append" );
136 xml_set_character_data_handler( $parser, "char_append" );
137 break;
138 case "revision":
139 xml_set_element_handler( $parser, "in_revision", "out_revision" );
140 break;
141 default:
142 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
143 }
144 }
145 function out_page( $parser, $name ) {
146 $this->debug( "out_page $name" );
147 if( $name != "page" ) {
148 return $this->throwXMLerror( "Expected </page>, got </$name>" );
149 }
150 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
151 }
152
153
154 function in_nothing( $parser, $name, $attribs ) {
155 $this->debug( "in_nothing $name" );
156 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
157 }
158 function char_append( $parser, $data ) {
159 $this->debug( "char_append '$data'" );
160 $x = $this->appendfield;
161 $this->pagedata->$x .= $data;
162 }
163 function out_append( $parser, $name ) {
164 $this->debug( "out_append $name" );
165 if( $name != $this->appendfield ) {
166 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
167 }
168 xml_set_element_handler( $parser, "in_$this->parenttag", "out_$this->parenttag" );
169 xml_set_character_data_handler( $parser, "donothing" );
170 }
171
172 function in_revision( $parser, $name, $attribs ) {
173 $this->debug( "in_revision $name" );
174 switch( $name ) {
175 case "id":
176 case "timestamp":
177 case "comment":
178 case "text":
179 $this->parenttag = "revision";
180 $this->appendfield = $name;
181 xml_set_element_handler( $parser, "in_nothing", "out_append" );
182 xml_set_character_data_handler( $parser, "char_append" );
183 break;
184 case "contributor":
185 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
186 break;
187 default:
188 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
189 }
190 }
191 function out_revision( $parser, $name ) {
192 $this->debug( "out_revision $name" );
193 if( $name != "revision" ) {
194 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
195 }
196 xml_set_element_handler( $parser, "in_page", "out_page" );
197 }
198
199 function in_contributor( $parser, $name, $attribs ) {
200 $this->debug( "in_contributor $name" );
201 switch( $name ) {
202 case "username":
203 case "ip":
204
205 break;
206 default:
207 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
208 }
209 }
210 function out_contributor( $parser, $name, $attribs ) {
211 $this->debug( "out_contributor $name" );
212 if( $name != "contributor" ) {
213 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
214 }
215 xml_set_element_handler( $parser, "in_revision", "out_revision" );
216 }
217 }
218
219
220 ?>