summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/scripts/poimport.php
blob: df32a2fbc85a9a1ac6de23dd79bbbec818053899 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
/**
 * Imports gettext files exported from Special:Translate back.
 *
 * @author Niklas Laxström
 * @author Siebrand Mazeland
 * @copyright Copyright © 2007-2013 Niklas Laxström, Siebrand Mazeland
 * @license GPL-2.0+
 * @file
 */

// Standard boilerplate to define $IP
if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
	$IP = getenv( 'MW_INSTALL_PATH' );
} else {
	$dir = __DIR__;
	$IP = "$dir/../../..";
}
require_once "$IP/maintenance/Maintenance.php";

class PoImport extends Maintenance {
	public function __construct() {
		parent::__construct();
		$this->mDescription = 'Po file importer (does not make changes unless specified).';
		$this->addOption(
			'file',
			'Gettext file to import (Translate specific formatting)',
			true, /*required*/
			true /*has arg*/
		);
		$this->addOption(
			'user',
			'User who makes edits to wiki',
			true, /*required*/
			true /*has arg*/
		);
		$this->addOption(
			'really',
			'(optional) Actually make changes',
			false, /*required*/
			false /*has arg*/
		);
	}

	public function execute() {
		// Parse the po file.
		$p = new PoImporter( $this->getOption( 'file' ) );
		$p->setProgressCallback( array( $this, 'myOutput' ) );
		list( $changes, $group ) = $p->parse();

		if ( !count( $changes ) ) {
			$this->output( "No changes to import\n" );
			exit( 0 );
		}

		// Import changes to wiki.
		$w = new WikiWriter(
			$changes,
			$group,
			$this->getOption( 'user' ),
			!$this->hasOption( 'really' )
		);

		$w->setProgressCallback( array( $this, 'myOutput' ) );
		$w->execute();
	}

	/**
	 * Public alternative for protected Maintenance::output() as we need to get
	 * messages from the ChangeSyncer class to the commandline.
	 * @param string $text The text to show to the user
	 * @param string $channel Unique identifier for the channel.
	 * @param bool $error Whether this is an error message
	 */
	public function myOutput( $text, $channel = null, $error = false ) {
		if ( $error ) {
			$this->error( $text, $channel );
		} else {
			$this->output( $text, $channel );
		}
	}
}

/**
 * Parses a po file that has been exported from Mediawiki. Other files are not
 * supported.
 */
class PoImporter {
	/** @var callable Function to report progress updates */
	protected $progressCallback;

	/**
	 * Path to file to parse.
	 * @var bool|string
	 */
	private $file = false;

	/**
	 * @param string $file File to import
	 */
	public function __construct( $file ) {
		$this->file = $file;
	}

	public function setProgressCallback( $callback ) {
		$this->progressCallback = $callback;
	}

	/// @see Maintenance::output for param docs
	protected function reportProgress( $text, $channel = null, $severity = 'status' ) {
		if ( is_callable( $this->progressCallback ) ) {
			$useErrorOutput = $severity === 'error';
			call_user_func( $this->progressCallback, $text, $channel, $useErrorOutput );
		}
	}

	/**
	 * Loads translations for comparison.
	 *
	 * @param string $id Id of MessageGroup.
	 * @param string $code Language code.
	 * @return MessageCollection
	 */
	protected function initMessages( $id, $code ) {
		$group = MessageGroups::getGroup( $id );

		$messages = $group->initCollection( $code );
		$messages->loadTranslations();

		return $messages;
	}

	/**
	 * Parses relevant stuff from the po file.
	 * @return array|bool
	 */
	public function parse() {
		$data = file_get_contents( $this->file );
		$data = str_replace( "\r\n", "\n", $data );

		$matches = array();
		if ( preg_match( '/X-Language-Code:\s+(.*)\\\n/', $data, $matches ) ) {
			$code = $matches[1];
			$this->reportProgress( "Detected language as $code", 'code' );
		} else {
			$this->reportProgress( 'Unable to determine language code', 'code', 'error' );

			return false;
		}

		if ( preg_match( '/X-Message-Group:\s+(.*)\\\n/', $data, $matches ) ) {
			$groupId = $matches[1];
			$this->reportProgress( "Detected message group as $groupId", 'group' );
		} else {
			$this->reportProgress( 'Unable to determine message group', 'group', 'error' );

			return false;
		}

		$contents = $this->initMessages( $groupId, $code );

		echo "----\n";

		$poformat = '".*"\n?(^".*"$\n?)*';
		$quotePattern = '/(^"|"$\n?)/m';

		$sections = preg_split( '/\n{2,}/', $data );
		$changes = array();
		foreach ( $sections as $section ) {
			$matches = array();
			if ( preg_match( "/^msgctxt\s($poformat)/mx", $section, $matches ) ) {
				// Remove quoting
				$key = preg_replace( $quotePattern, '', $matches[1] );

				// Ignore unknown keys
				if ( !isset( $contents[$key] ) ) {
					continue;
				}
			} else {
				continue;
			}
			$matches = array();
			if ( preg_match( "/^msgstr\s($poformat)/mx", $section, $matches ) ) {
				// Remove quoting
				$translation = preg_replace( $quotePattern, '', $matches[1] );
				// Restore new lines and remove quoting
				$translation = stripcslashes( $translation );
			} else {
				continue;
			}

			// Fuzzy messages
			if ( preg_match( '/^#, fuzzy$/m', $section ) ) {
				$translation = TRANSLATE_FUZZY . $translation;
			}

			$oldtranslation = (string)$contents[$key]->translation();

			if ( $translation !== $oldtranslation ) {
				if ( $translation === '' ) {
					$this->reportProgress( "Skipping empty translation in the po file for $key!\n" );
				} else {
					if ( $oldtranslation === '' ) {
						$this->reportProgress( "New translation for $key\n" );
					} else {
						$this->reportProgress( "Translation of $key differs:\n$translation\n" );
					}
					$changes["$key/$code"] = $translation;
				}
			}
		}

		return array( $changes, $groupId );
	}
}

/**
 * Import changes to wiki as given user
 */
class WikiWriter {
	/** @var callable Function to report progress updates */
	protected $progressCallback;

	protected $user;

	private $changes = array();
	private $dryrun = true;
	private $group = null;

	/**
	 * @param array $changes Array of key/langcode => translation.
	 * @param string $groupId Group ID.
	 * @param string $user User who makes the edits in wiki.
	 * @param bool $dryrun Do not do anything that affects the database.
	 */
	public function __construct( $changes, $groupId, $user, $dryrun = true ) {
		$this->changes = $changes;
		$this->group = MessageGroups::getGroup( $groupId );
		$this->user = User::newFromName( $user );
		$this->dryrun = $dryrun;
	}

	public function setProgressCallback( $callback ) {
		$this->progressCallback = $callback;
	}

	/// @see Maintenance::output for param docs
	protected function reportProgress( $text, $channel, $severity = 'status' ) {
		if ( is_callable( $this->progressCallback ) ) {
			$useErrorOutput = $severity === 'error';
			call_user_func( $this->progressCallback, $text, $channel, $useErrorOutput );
		}
	}

	/**
	 * Updates pages on by one.
	 */
	public function execute() {
		if ( !$this->group ) {
			$this->reportProgress( 'Given group does not exist.', 'groupId', 'error' );

			return;
		}

		if ( !$this->user->idForName() ) {
			$this->reportProgress( 'Given user does not exist.', 'user', 'error' );

			return;
		}

		$count = count( $this->changes );
		$this->reportProgress( "Going to update $count pages.", 'pagecount' );

		$ns = $this->group->getNamespace();

		foreach ( $this->changes as $title => $text ) {
			$this->updateMessage( $ns, $title, $text );
		}
	}

	/**
	 * Actually adds the new translation.
	 * @param int $namespace
	 * @param string $page
	 * @param string $text
	 */
	private function updateMessage( $namespace, $page, $text ) {
		$title = Title::makeTitleSafe( $namespace, $page );

		if ( !$title instanceof Title ) {
			$this->reportProgress( 'INVALID TITLE!', $page, 'error' );

			return;
		}
		$this->reportProgress( "Updating {$title->getPrefixedText()}... ", $title );

		if ( $this->dryrun ) {
			$this->reportProgress( 'DRY RUN!', $title );

			return;
		}

		$page = WikiPage::factory( $title );
		$content = ContentHandler::makeContent( $text, $title );
		$status = $page->doEditContent(
			$content,
			'Updating translation from gettext import',
			0,
			false,
			$this->user
		);

		if ( $status === true || ( is_object( $status ) && $status->isOK() ) ) {
			$this->reportProgress( 'OK!', $title );
		} else {
			$this->reportProgress( 'Failed!', $title );
		}
	}
}

$maintClass = 'PoImport';
require_once RUN_MAINTENANCE_IF_MAIN;