mTemplates = array(); } function setName( $name ) { $this->mName = $name; } function getName() { return $this->mName; } function addTemplateField( $template_name, $field_name, $value ) { if ( ! array_key_exists( $template_name, $this->mTemplates ) ) { $this->mTemplates[$template_name] = array(); } $this->mTemplates[$template_name][$field_name] = $value; } function setFreeText( $free_text ) { $this->mFreeText = $free_text; } function createText() { $text = ""; foreach ( $this->mTemplates as $template_name => $fields ) { $text .= '{{' . $template_name . "\n"; foreach ( $fields as $field_name => $val ) { $text .= "|$field_name=$val\n"; } $text .= '}}' . "\n"; } $text .= $this->mFreeText; return $text; } } class DTImportCSV extends SpecialPage { /** * Constructor */ public function DTImportCSV() { global $wgLanguageCode; parent::__construct( 'ImportCSV' ); DTUtils::loadMessages(); } function execute( $query ) { global $wgUser, $wgOut, $wgRequest; $this->setHeaders(); if ( ! $wgUser->isAllowed( 'datatransferimport' ) ) { global $wgOut; $wgOut->permissionRequired( 'datatransferimport' ); return; } if ( $wgRequest->getCheck( 'import_file' ) ) { $text = DTUtils::printImportingMessage(); $uploadResult = ImportStreamSource::newFromUpload( "file_name" ); // handling changed in MW 1.17 $uploadError = null; if ( $uploadResult instanceof Status ) { if ( $uploadResult->isOK() ) { $source = $uploadResult->value; } else { $uploadError = $wgOut->parse( $uploadResult->getWikiText() ); } } elseif ( $uploadResult instanceof WikiErrorMsg ) { $uploadError = $uploadResult->getMessage(); } else { $source = $uploadResult; } if ( !is_null( $uploadError ) ) { $text .= $uploadError; $wgOut->addHTML( $text ); return; } $encoding = $wgRequest->getVal( 'encoding' ); $pages = array(); $error_msg = self::getCSVData( $source->mHandle, $encoding, $pages ); if ( ! is_null( $error_msg ) ) { $text .= $error_msg; $wgOut->addHTML( $text ); return; } $importSummary = $wgRequest->getVal( 'import_summary' ); $forPagesThatExist = $wgRequest->getVal( 'pagesThatExist' ); $text .= self::modifyPages( $pages, $importSummary, $forPagesThatExist ); } else { $formText = DTUtils::printFileSelector( 'CSV' ); $utf8OptionText = "\t" . Xml::element( 'option', array( 'selected' => 'selected', 'value' => 'utf8' ), 'UTF-8' ) . "\n"; $utf16OptionText = "\t" . Xml::element( 'option', array( 'value' => 'utf16' ), 'UTF-16' ) . "\n"; $encodingSelectText = Xml::tags( 'select', array( 'name' => 'encoding' ), "\n" . $utf8OptionText . $utf16OptionText. "\t" ) . "\n\t"; $formText .= "\t" . Xml::tags( 'p', null, wfMsg( 'dt_import_encodingtype', 'CSV' ) . " " . $encodingSelectText ) . "\n"; $formText .= "\t" . '
' . wfMsg( 'img-auth-badtitle', $page->getName() ) . "
\n"; continue; } $jobParams['text'] = $page->createText(); $jobs[] = new DTImportJob( $title, $jobParams ); } Job::batchInsert( $jobs ); $text .= wfMsgExt( 'dt_import_success', array( 'parse' ), $wgLang->formatNum( count( $jobs ) ), 'CSV' ); return $text; } }