3 function wfSpecialImport( $page = "" ) {
4 global $wgOut, $wgLang;
6 if( $_REQUEST['action'] == 'submit') {
7 $importer = WikiImporter
::fromUpload();
8 if( empty($importer) ) {
9 $wgOut->addHTML( wfMsg( "importnofile" ) );
11 if( $importer->doImport() ) {
13 $wgOut->addHTML( "<p>" . wfMsg( "importsuccess" ) . "</p>" );
15 $wgOut->addHTML( "<p>" . wfMsg( "importfailed",
16 htmlspecialchars( $importer->getError() ) ) . "</p>" );
21 $wgOut->addWikiText( "<p>" . wfMsg( "importtext" ) . "</p>" );
22 $action = wfLocalUrlE( $wgLang->SpecialPage( "Import" ) );
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" ) . "'/>
32 function wfISO86012Timestamp( $ts ) {
34 # Warning: overly simplistic!
35 return preg_replace( '/^(....)-(..)-(..)T(..):(..):(..)$/', '$1$2$3$4$5$6', $ts );
39 var $mSource, $mError;
41 function WikiImporter() {
42 $this->mSource
= NULL;
43 $this->mError
= XML_ERROR_NONE
;
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 );
59 if( !$this->mSource
) return false;
63 $wgOut->addHTML( "<pre>" . htmlspecialchars($this->mSource
) . "</pre>" );
65 $parser = xml_parser_create( "UTF-8" );
67 /* case folding violates XML standard, turn it off */
68 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
70 xml_set_object( $parser, &$this );
71 xml_set_element_handler( $parser, "in_start", "" );
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 );
79 xml_parser_free( $parser );
84 return xml_error_string( $this->mError
);
87 function throwXmlError( $err ) {
88 #echo htmlspecialchars("\n** $err **\n");
90 $this->debug( "FAILURE: $err" );
93 function debug( $data ) {
96 $wgOut->addHTML( htmlspecialchars( $data ) . "<br>\n" );
99 /* xml parser callbacks */
100 function donothing( $parser, $x, $y="" ) {
101 #$this->debug( "donothing" );
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>" );
109 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
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>" );
117 xml_set_element_handler( $parser, "in_page", "out_page" );
119 function out_mediawiki( $parser, $name ) {
120 $this->debug( "out_mediawiki $name" );
121 if( $name != "mediawiki" ) {
122 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
124 xml_set_element_handler( $parser, "donothing", "donothing" );
127 function in_page( $parser, $name, $attribs ) {
128 $this->debug( "in_page $name" );
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" );
139 xml_set_element_handler( $parser, "in_revision", "out_revision" );
142 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
145 function out_page( $parser, $name ) {
146 $this->debug( "out_page $name" );
147 if( $name != "page" ) {
148 return $this->throwXMLerror( "Expected </page>, got </$name>" );
150 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
154 function in_nothing( $parser, $name, $attribs ) {
155 $this->debug( "in_nothing $name" );
156 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
158 function char_append( $parser, $data ) {
159 $this->debug( "char_append '$data'" );
160 $x = $this->appendfield
;
161 $this->pagedata
->$x .= $data;
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>" );
168 xml_set_element_handler( $parser, "in_$this->parenttag", "out_$this->parenttag" );
169 xml_set_character_data_handler( $parser, "donothing" );
172 function in_revision( $parser, $name, $attribs ) {
173 $this->debug( "in_revision $name" );
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" );
185 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
188 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
191 function out_revision( $parser, $name ) {
192 $this->debug( "out_revision $name" );
193 if( $name != "revision" ) {
194 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
196 xml_set_element_handler( $parser, "in_page", "out_page" );
199 function in_contributor( $parser, $name, $attribs ) {
200 $this->debug( "in_contributor $name" );
207 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
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>" );
215 xml_set_element_handler( $parser, "in_revision", "out_revision" );