summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/DataTransfer/specials/DT_ImportCSV.php
blob: 1a221424d5ca200db4960cf14310d26e4fa40872 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
 * Lets the user import a CSV file to turn into wiki pages
 *
 * @author Yaron Koren
 */

class DTImportCSV extends SpecialPage {

	/**
	 * Constructor
	 */
	public function __construct( $name='ImportCSV' ) {
		parent::__construct( $name );
	}

	public function doesWrites() {
		return true;
	}

	function execute( $query ) {
		$this->setHeaders();

		if ( ! $this->getUser()->isAllowed( 'datatransferimport' ) ) {
			throw new PermissionsError( 'datatransferimport' );
		}

		if ( $this->getRequest()->getCheck( 'import_file' ) ) {
			$text = $this->importFromUploadAndModifyPages();
		} else {
			$text = $this->printForm();
		}

		$this->getOutput()->addModuleStyles( 'ext.datatransfer');
		$this->getOutput()->addHTML( $text );
	}

	protected function importFromUploadAndModifyPages () {

		$text = DTUtils::printImportingMessage();
		$uploadResult = ImportStreamSource::newFromUpload( "file_name" );

		if ( !$uploadResult->isOK() ) {
			$uploadError = $this->getOutput()->parse( $uploadResult->getWikiText() );
			$text .= $uploadError;
			return $text;
		}

		$source = $uploadResult->value;

		$encoding = $this->getRequest()->getVal( 'encoding' );
		$pages = array();

		$error_msg = $this->importFromFile( $source->mHandle, $encoding, $pages );

		if ( ! is_null( $error_msg ) ) {
			$text .= $error_msg;
			return $text;
		}

		$importSummary = $this->getRequest()->getVal( 'import_summary' );
		$forPagesThatExist = $this->getRequest()->getVal( 'pagesThatExist' );

		$text .= self::modifyPages( $pages, $importSummary, $forPagesThatExist );

		return $text;
	}

	protected function printForm() {
		$formText = DTUtils::printFileSelector( $this->getFiletype() );
		$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, $this->msg( 'dt_import_encodingtype', 'CSV' )->text() . " " . $encodingSelectText ) . "\n";
		$formText .= "\t" . '<hr style="margin: 10px 0 10px 0" />' . "\n";
		$formText .= DTUtils::printExistingPagesHandling();
		$formText .= DTUtils::printImportSummaryInput( $this->getFiletype() );
		$formText .= DTUtils::printSubmitButton();
		$text = "\t" . Xml::tags( 'form',
				array(
					'enctype' => 'multipart/form-data',
					'action' => '',
					'method' => 'post'
				), $formText ) . "\n";
		return $text;
	}

	protected function importFromFile( $csv_file, $encoding, &$pages ) {
		if ( is_null( $csv_file ) ) {
			return wfMessage( 'emptyfile' )->text();
		}

		$table = array();
		if ( $encoding == 'utf16' ) {
			// Change encoding to UTF-8.
			// Starting with PHP 5.3 we could use str_getcsv(),
			// which would save the tempfile hassle.
			$tempfile = tmpfile();
			$csv_string = '';
			while ( !feof( $csv_file ) ) {
				$csv_string .= fgets( $csv_file, 65535 );
 			}
			fwrite( $tempfile, iconv( 'UTF-16', 'UTF-8', $csv_string ) );
			fseek( $tempfile, 0 );
			while ( $line = fgetcsv( $tempfile ) ) {
				array_push( $table, $line );
			}
			fclose( $tempfile );
		} else {
			while ( $line = fgetcsv( $csv_file ) ) {
				// Convert from UTF-8 to ASCII - htmlentities()
				// fails for UTF-8 if there are non-ASCII
				// characters.
				// $convertedLine = array();
				// foreach ( $line as $value ) {
				// 	$convertedLine[] = mb_convert_encoding( $value, 'UTF-8', 'ASCII' );
				// }
				array_push( $table, $line );
			}
		}
		fclose( $csv_file );

		// Get rid of the "byte order mark", if it's there - this is
		// a three-character string sometimes put at the beginning
		// of files to indicate its encoding.
		// Code copied from:
		// http://www.dotvoid.com/2010/04/detecting-utf-bom-byte-order-mark/
		$byteOrderMark = pack( "CCC", 0xef, 0xbb, 0xbf );
		if ( 0 == strncmp( $table[0][0], $byteOrderMark, 3 ) ) {
			$table[0][0] = substr( $table[0][0], 3 );
			// If there were quotation marks around this value,
			// they didn't get removed, so remove them now.
			$table[0][0] = trim( $table[0][0], '"' );
		}

		return $this->importFromArray( $table, $pages );

	}

	protected function importFromArray( $table, &$pages ) {
		// Check header line to make sure every term is in the
		// correct format.
		$titleLabels = array( wfMessage( 'dt_xml_title' )->inContentLanguage()->text() );
		$freeTextLabels = array( wfMessage( 'dt_xml_freetext' )->inContentLanguage()->text() );
		// Add the English-language values as well, if this isn't an
		// English-language wiki.
		if ( $this->getLanguage()->getCode() !== 'en' ) {
			$titleLabels[] = wfMessage( 'dt_xml_title' )->inLanguage( 'en' )->text();
			$freeTextLabels[] = wfMessage( 'dt_xml_freetext' )->inLanguage( 'en' )->text();
		}
		foreach ( $table[0] as $i => $headerVal ) {
			if ( !in_array( $headerVal, $titleLabels )
			&& !in_array( $headerVal, $freeTextLabels )
			&& $headerVal !== ''
			&& !preg_match( '/^[^\[\]]+\[[^\[\]]+]$/', $headerVal ) ) {
				$errorMsg = wfMessage( 'dt_importcsv_badheader', $i, $headerVal, $titleLabels[0], $freeTextLabels[0] )->text();
				return $errorMsg;
			}
		}
		foreach ( $table as $i => $line ) {
			if ( $i == 0 ) continue;
			$page = new DTPage();
			foreach ( $line as $j => $val ) {
				if ( $table[0][$j] === '' ) {
					continue;
				}
				if ( in_array( $table[0][$j], $titleLabels ) ) {
					$page->setName( $val );
				} elseif ( in_array( $table[0][$j], $freeTextLabels ) ) {
					$page->setFreeText( $val );
				} else {
					list( $templateName, $fieldName ) = explode( '[', str_replace( ']', '', $table[0][$j] ) );
					$page->addTemplateField( $templateName, $fieldName, $val );
				}
			}
			$pages[] = $page;
		}

		return null;
	}

	protected function modifyPages( $pages, $editSummary, $forPagesThatExist ) {
		$text = "";
		$jobs = array();
		$jobParams = array();
		$jobParams['user_id'] = $this->getUser()->getId();
		$jobParams['edit_summary'] = $editSummary;
		$jobParams['for_pages_that_exist'] = $forPagesThatExist;
		foreach ( $pages as $page ) {
			$title = Title::newFromText( $page->getName() );
			if ( is_null( $title ) ) {
				$text .= '<p>' . $this->msg( 'img-auth-badtitle', $page->getName() )->text() . "</p>\n";
				continue;
			}
			$jobParams['text'] = $page->createText();
			$jobs[] = new DTImportJob( $title, $jobParams );
		}
		JobQueueGroup::singleton()->push( $jobs );

		$text .= $this->msg( 'dt_import_success' )->numParams( count( $jobs ) )->params( $this->getFiletype() )->parseAsBlock();

		return $text;
	}

	protected function getFiletype() {
		return wfMessage( 'dt_filetype_csv' )->text();
	}
}