14use MediaWiki\MediaWikiServices;
17if ( getenv(
'MW_INSTALL_PATH' ) !==
false ) {
18 $IP = getenv(
'MW_INSTALL_PATH' );
21 $IP =
"$dir/../../..";
23require_once
"$IP/maintenance/Maintenance.php";
26 public function __construct() {
27 parent::__construct();
28 $this->addDescription(
'Po file importer (does not make changes unless specified).' );
31 'Gettext file to import (Translate specific formatting)',
37 'User who makes edits to wiki',
43 '(optional) Actually make changes',
47 $this->requireExtension(
'Translate' );
50 public function execute() {
52 $p =
new PoImporter( $this->getOption(
'file' ) );
53 $p->setProgressCallback( [ $this,
'myOutput' ] );
54 list( $changes, $group ) = $p->parse();
56 if ( !count( $changes ) ) {
57 $this->output(
"No changes to import\n" );
65 $this->getOption(
'user' ),
66 !$this->hasOption(
'really' )
69 $w->setProgressCallback( [ $this,
'myOutput' ] );
80 public function myOutput( $text, $channel =
null, $error =
false ) {
82 $this->error( $text );
84 $this->output( $text, $channel );
95 private $progressCallback;
107 public function setProgressCallback( $callback ) {
108 $this->progressCallback = $callback;
112 protected function reportProgress( $text, $channel =
null, $severity =
'status' ) {
113 if ( is_callable( $this->progressCallback ) ) {
114 $useErrorOutput = $severity ===
'error';
115 call_user_func( $this->progressCallback, $text, $channel, $useErrorOutput );
127 $group = MessageGroups::getGroup( $id );
129 $messages = $group->initCollection( $code );
130 $messages->loadTranslations();
140 $data = file_get_contents( $this->file );
141 $data = TextContent::normalizeLineEndings( $data );
144 if ( preg_match(
'/X-Language-Code:\s+(.*)\\\n/', $data, $matches ) ) {
148 $this->
reportProgress(
'Unable to determine language code',
'code',
'error' );
153 if ( preg_match(
'/X-Message-Group:\s+(.*)\\\n/', $data, $matches ) ) {
154 $groupId = $matches[1];
155 $this->
reportProgress(
"Detected message group as $groupId",
'group' );
157 $this->
reportProgress(
'Unable to determine message group',
'group',
'error' );
166 $poformat =
'".*"\n?(^".*"$\n?)*';
167 $quotePattern =
'/(^"|"$\n?)/m';
169 $sections = preg_split(
'/\n{2,}/', $data );
171 foreach ( $sections as $section ) {
173 if ( preg_match(
"/^msgctxt\s($poformat)/mx", $section, $matches ) ) {
175 $key = preg_replace( $quotePattern,
'', $matches[1] );
178 if ( !isset( $contents[$key] ) ) {
185 if ( preg_match(
"/^msgstr\s($poformat)/mx", $section, $matches ) ) {
187 $translation = preg_replace( $quotePattern,
'', $matches[1] );
189 $translation = stripcslashes( $translation );
195 if ( preg_match(
'/^#, fuzzy$/m', $section ) ) {
196 $translation = TRANSLATE_FUZZY . $translation;
199 $oldtranslation = (string)$contents[$key]->translation();
201 if ( $translation !== $oldtranslation ) {
202 if ( $translation ===
'' ) {
203 $this->
reportProgress(
"Skipping empty translation in the po file for $key!\n" );
205 if ( $oldtranslation ===
'' ) {
208 $this->
reportProgress(
"Translation of $key differs:\n$translation\n" );
210 $changes[
"$key/$code"] = $translation;
215 return [ $changes, $groupId ];
224 private $progressCallback;
240 public function __construct( array $changes, $groupId, $user, $dryrun =
true ) {
241 $this->changes = $changes;
242 $this->group = MessageGroups::getGroup( $groupId );
243 $this->user = User::newFromName( $user );
244 $this->dryrun = $dryrun;
247 public function setProgressCallback( $callback ) {
248 $this->progressCallback = $callback;
253 if ( is_callable( $this->progressCallback ) ) {
254 $useErrorOutput = $severity ===
'error';
255 call_user_func( $this->progressCallback, $text, $channel, $useErrorOutput );
263 if ( !$this->group ) {
264 $this->
reportProgress(
'Given group does not exist.',
'groupId',
'error' );
269 if ( !$this->user->idForName() ) {
270 $this->
reportProgress(
'Given user does not exist.',
'user',
'error' );
275 $count = count( $this->changes );
276 $this->
reportProgress(
"Going to update $count pages.",
'pagecount' );
278 $ns = $this->group->getNamespace();
280 foreach ( $this->changes as $title => $text ) {
281 $this->updateMessage( $ns, $title, $text );
291 private function updateMessage( $namespace, $page, $text ) {
292 $title = Title::makeTitleSafe( $namespace, $page );
294 if ( !$title instanceof Title ) {
299 $this->
reportProgress(
"Updating {$title->getPrefixedText()}... ", $title );
301 if ( $this->dryrun ) {
307 $page = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $title );
308 $content = ContentHandler::makeContent( $text, $title );
309 $status = $page->doUserEditContent(
312 'Updating translation from gettext import'
315 if ( $status->isOK() ) {
323$maintClass = Poimport::class;
324require_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')