Index: trunk/extensions/DataTransfer/specials/DT_ImportCSV.php |
— | — | @@ -0,0 +1,152 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Lets the user import a CSV file to turn into wiki pages |
| 5 | + * |
| 6 | + * @author Yaron Koren |
| 7 | + */ |
| 8 | + |
| 9 | +if (!defined('MEDIAWIKI')) die(); |
| 10 | + |
| 11 | +class DTPage { |
| 12 | + var $mName; |
| 13 | + var $mTemplates; |
| 14 | + var $mFreeText; |
| 15 | + |
| 16 | + public function DTPage() { |
| 17 | + $this->mTemplates = array(); |
| 18 | + } |
| 19 | + |
| 20 | + function setName($name) { |
| 21 | + $this->mName = $name; |
| 22 | + } |
| 23 | + |
| 24 | + function getName() { |
| 25 | + return $this->mName; |
| 26 | + } |
| 27 | + |
| 28 | + function addTemplateField($template_name, $field_name, $value) { |
| 29 | + if (! array_key_exists($template_name, $this->mTemplates)) { |
| 30 | + $this->mTemplates[$template_name] = array(); |
| 31 | + } |
| 32 | + $this->mTemplates[$template_name][$field_name] = $value; |
| 33 | + } |
| 34 | + |
| 35 | + function setFreeText($free_text) { |
| 36 | + $this->mFreeText = $free_text; |
| 37 | + } |
| 38 | + |
| 39 | + function createText() { |
| 40 | + $text = ""; |
| 41 | + foreach ($this->mTemplates as $template_name => $fields) { |
| 42 | + $text .= '{{' . $template_name . "\n"; |
| 43 | + foreach ($fields as $field_name => $val) { |
| 44 | + $text .= "|$field_name=$val\n"; |
| 45 | + } |
| 46 | + $text .= '}}' . "\n"; |
| 47 | + } |
| 48 | + $text .= $this->mFreeText; |
| 49 | + return $text; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +class DTImportCSV extends SpecialPage { |
| 54 | + |
| 55 | + /** |
| 56 | + * Constructor |
| 57 | + */ |
| 58 | + public function DTImportCSV() { |
| 59 | + global $wgLanguageCode; |
| 60 | + SpecialPage::SpecialPage('ImportCSV'); |
| 61 | + dtfInitContentLanguage($wgLanguageCode); |
| 62 | + wfLoadExtensionMessages('DataTransfer'); |
| 63 | + } |
| 64 | + |
| 65 | + function execute($query) { |
| 66 | + global $wgUser, $wgOut, $wgRequest; |
| 67 | + $this->setHeaders(); |
| 68 | + |
| 69 | + if ( ! $wgUser->isAllowed('importxml') ) { |
| 70 | + global $wgOut; |
| 71 | + $wgOut->permissionRequired('importxml'); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + if ($wgRequest->getCheck('import_file')) { |
| 76 | + $text = "<p>" . wfMsg('dt_import_importing') . "</p>\n"; |
| 77 | + $source = ImportStreamSource::newFromUpload( "csv_file" ); |
| 78 | + $pages = array(); |
| 79 | + $error_msg = self::getCSVData($source->mHandle, $pages); |
| 80 | + if (! is_null($error_msg)) |
| 81 | + $text .= $error_msg; |
| 82 | + else |
| 83 | + $text .= self::modifyPages($pages); |
| 84 | + } else { |
| 85 | + $select_file_label = wfMsg('dt_import_selectfile', 'CSV'); |
| 86 | + $import_button = wfMsg('import-interwiki-submit'); |
| 87 | + $text =<<<END |
| 88 | + <p>$select_file_label</p> |
| 89 | + <form enctype="multipart/form-data" action="" method="post"> |
| 90 | + <p><input type="file" name="csv_file" size="25" /></p> |
| 91 | + <p><input type="Submit" name="import_file" value="$import_button"></p> |
| 92 | + </form> |
| 93 | + |
| 94 | +END; |
| 95 | + } |
| 96 | + |
| 97 | + $wgOut->addHTML($text); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + static function getCSVData($csv_file, &$pages) { |
| 102 | + $table = array(); |
| 103 | + while ($line = fgetcsv($csv_file)) { |
| 104 | + array_push($table, $line); |
| 105 | + } |
| 106 | + fclose($csv_file); |
| 107 | + // check header line to make sure every term is in the |
| 108 | + // correct format |
| 109 | + $title_label = wfMsgForContent('dt_xml_title'); |
| 110 | + $free_text_label = wfMsgForContent('dt_xml_freetext'); |
| 111 | + foreach ($table[0] as $i => $header_val) { |
| 112 | + if ($header_val !== $title_label && $header_val !== $free_text_label && |
| 113 | + ! preg_match('/^[^\[\]]+\[[^\[\]]+]$/', $header_val)) { |
| 114 | + $error_msg = wfMsg('dt_importcsv_badheader', $i, $header_val, $title_label, $free_text_label); |
| 115 | + return $error_msg; |
| 116 | + } |
| 117 | + } |
| 118 | + foreach ($table as $i => $line) { |
| 119 | + if ($i == 0) continue; |
| 120 | + $page = new DTPage(); |
| 121 | + foreach ($line as $j => $val) { |
| 122 | + if ($val == '') continue; |
| 123 | + if ($table[0][$j] == $title_label) { |
| 124 | + $page->setName($val); |
| 125 | + } elseif ($table[0][$j] == $free_text_label) { |
| 126 | + $page->setFreeText($val); |
| 127 | + } else { |
| 128 | + list($template_name, $field_name) = explode('[', str_replace(']', '', $table[0][$j])); |
| 129 | + $page->addTemplateField($template_name, $field_name, $val); |
| 130 | + } |
| 131 | + } |
| 132 | + $pages[] = $page; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + function modifyPages($pages) { |
| 137 | + $text = ""; |
| 138 | + $jobs = array(); |
| 139 | + $job_params = array(); |
| 140 | + global $wgUser; |
| 141 | + $job_params['user_id'] = $wgUser->getId(); |
| 142 | + $job_params['edit_summary'] = wfMsgForContent('dt_import_editsummary', 'CSV'); |
| 143 | + foreach ($pages as $page) { |
| 144 | + $title = Title::newFromText($page->getName()); |
| 145 | + $job_params['text'] = $page->createText(); |
| 146 | + $jobs[] = new DTImportJob( $title, $job_params ); |
| 147 | + } |
| 148 | + Job::batchInsert( $jobs ); |
| 149 | + $text .= wfMsg('dt_import_success', count($jobs), 'CSV'); |
| 150 | + return $text; |
| 151 | + } |
| 152 | + |
| 153 | +} |