14use MediaWiki\MediaWikiServices;
15use MediaWiki\Title\Title;
18if ( getenv(
'MW_INSTALL_PATH' ) !==
false ) {
19 $IP = getenv(
'MW_INSTALL_PATH' );
22 $IP =
"$dir/../../..";
24require_once
"$IP/maintenance/Maintenance.php";
27 public function __construct() {
28 parent::__construct();
29 $this->addDescription(
'Po file importer (does not make changes unless specified).' );
32 'Gettext file to import (Translate specific formatting)',
38 'User who makes edits to wiki',
44 '(optional) Actually make changes',
48 $this->requireExtension(
'Translate' );
51 public function execute() {
53 $p =
new PoImporter( $this->getOption(
'file' ) );
54 $p->setProgressCallback( [ $this,
'myOutput' ] );
55 [ $changes, $group ] = $p->parse();
57 if ( !count( $changes ) ) {
58 $this->output(
"No changes to import\n" );
66 $this->getOption(
'user' ),
67 !$this->hasOption(
'really' )
70 $w->setProgressCallback( [ $this,
'myOutput' ] );
81 public function myOutput( $text, $channel =
null, $error =
false ) {
83 $this->error( $text );
85 $this->output( $text, $channel );
96 private $progressCallback;
108 public function setProgressCallback( $callback ) {
109 $this->progressCallback = $callback;
113 protected function reportProgress( $text, $channel =
null, $severity =
'status' ) {
114 if ( is_callable( $this->progressCallback ) ) {
115 $useErrorOutput = $severity ===
'error';
116 call_user_func( $this->progressCallback, $text, $channel, $useErrorOutput );
128 $group = MessageGroups::getGroup( $id );
130 $messages = $group->initCollection( $code );
131 $messages->loadTranslations();
141 $data = file_get_contents( $this->file );
142 $data = TextContent::normalizeLineEndings( $data );
145 if ( preg_match(
'/X-Language-Code:\s+(.*)\\\n/', $data, $matches ) ) {
149 $this->
reportProgress(
'Unable to determine language code',
'code',
'error' );
154 if ( preg_match(
'/X-Message-Group:\s+(.*)\\\n/', $data, $matches ) ) {
155 $groupId = $matches[1];
156 $this->
reportProgress(
"Detected message group as $groupId",
'group' );
158 $this->
reportProgress(
'Unable to determine message group',
'group',
'error' );
167 $poformat =
'".*"\n?(^".*"$\n?)*';
168 $quotePattern =
'/(^"|"$\n?)/m';
170 $sections = preg_split(
'/\n{2,}/', $data );
172 foreach ( $sections as $section ) {
174 if ( preg_match(
"/^msgctxt\s($poformat)/mx", $section, $matches ) ) {
176 $key = preg_replace( $quotePattern,
'', $matches[1] );
179 if ( !isset( $contents[$key] ) ) {
186 if ( preg_match(
"/^msgstr\s($poformat)/mx", $section, $matches ) ) {
188 $translation = preg_replace( $quotePattern,
'', $matches[1] );
190 $translation = stripcslashes( $translation );
196 if ( preg_match(
'/^#, fuzzy$/m', $section ) ) {
197 $translation = TRANSLATE_FUZZY . $translation;
200 $oldtranslation = (string)$contents[$key]->translation();
202 if ( $translation !== $oldtranslation ) {
203 if ( $translation ===
'' ) {
204 $this->
reportProgress(
"Skipping empty translation in the po file for $key!\n" );
206 if ( $oldtranslation ===
'' ) {
209 $this->
reportProgress(
"Translation of $key differs:\n$translation\n" );
211 $changes[
"$key/$code"] = $translation;
216 return [ $changes, $groupId ];
225 private $progressCallback;
241 public function __construct( array $changes, $groupId, $user, $dryrun =
true ) {
242 $this->changes = $changes;
243 $this->group = MessageGroups::getGroup( $groupId );
244 $this->user = User::newFromName( $user );
245 $this->dryrun = $dryrun;
248 public function setProgressCallback( $callback ) {
249 $this->progressCallback = $callback;
254 if ( is_callable( $this->progressCallback ) ) {
255 $useErrorOutput = $severity ===
'error';
256 call_user_func( $this->progressCallback, $text, $channel, $useErrorOutput );
264 if ( !$this->group ) {
265 $this->
reportProgress(
'Given group does not exist.',
'groupId',
'error' );
270 if ( !$this->user->idForName() ) {
271 $this->
reportProgress(
'Given user does not exist.',
'user',
'error' );
276 $count = count( $this->changes );
277 $this->
reportProgress(
"Going to update $count pages.",
'pagecount' );
279 $ns = $this->group->getNamespace();
281 foreach ( $this->changes as $title => $text ) {
282 $this->updateMessage( $ns, $title, $text );
292 private function updateMessage( $namespace, $page, $text ) {
293 $title = Title::makeTitleSafe( $namespace, $page );
295 if ( !$title instanceof Title ) {
300 $this->
reportProgress(
"Updating {$title->getPrefixedText()}... ", $title );
302 if ( $this->dryrun ) {
308 $page = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $title );
309 $content = ContentHandler::makeContent( $text, $title );
310 $status = $page->doUserEditContent(
313 'Updating translation from gettext import'
316 if ( $status->isOK() ) {
324$maintClass = Poimport::class;
325require_once RUN_MAINTENANCE_IF_MAIN;
Parses a po file that has been exported from MediaWiki.
parse()
Parses relevant stuff from the po file.
reportProgress( $text, $channel=null, $severity='status')
initMessages( $id, $code)
Loads translations for comparison.
myOutput( $text, $channel=null, $error=false)
Public alternative for protected Maintenance::output() as we need to get messages from the ChangeSync...
Import changes to wiki as given user.
execute()
Updates pages on by one.
__construct(array $changes, $groupId, $user, $dryrun=true)
reportProgress( $text, $channel, $severity='status')