12use MediaWiki\MediaWikiServices;
15if ( getenv(
'MW_INSTALL_PATH' ) !==
false ) {
16 $IP = getenv(
'MW_INSTALL_PATH' );
19 $IP =
"$dir/../../..";
21require_once
"$IP/maintenance/Maintenance.php";
24 public function __construct() {
25 parent::__construct();
26 $this->addDescription(
'Po file importer (does not make changes unless specified).' );
29 'Gettext file to import (Translate specific formatting)',
35 'User who makes edits to wiki',
41 '(optional) Actually make changes',
45 $this->requireExtension(
'Translate' );
48 public function execute() {
50 $p =
new PoImporter( $this->getOption(
'file' ) );
51 $p->setProgressCallback( [ $this,
'myOutput' ] );
52 list( $changes, $group ) = $p->parse();
54 if ( !count( $changes ) ) {
55 $this->output(
"No changes to import\n" );
63 $this->getOption(
'user' ),
64 !$this->hasOption(
'really' )
67 $w->setProgressCallback( [ $this,
'myOutput' ] );
78 public function myOutput( $text, $channel =
null, $error =
false ) {
80 $this->error( $text );
82 $this->output( $text, $channel );
93 private $progressCallback;
105 public function setProgressCallback( $callback ) {
106 $this->progressCallback = $callback;
110 protected function reportProgress( $text, $channel =
null, $severity =
'status' ) {
111 if ( is_callable( $this->progressCallback ) ) {
112 $useErrorOutput = $severity ===
'error';
113 call_user_func( $this->progressCallback, $text, $channel, $useErrorOutput );
125 $group = MessageGroups::getGroup( $id );
127 $messages = $group->initCollection( $code );
128 $messages->loadTranslations();
138 $data = file_get_contents( $this->file );
139 $data = str_replace(
"\r\n",
"\n", $data );
142 if ( preg_match(
'/X-Language-Code:\s+(.*)\\\n/', $data, $matches ) ) {
146 $this->
reportProgress(
'Unable to determine language code',
'code',
'error' );
151 if ( preg_match(
'/X-Message-Group:\s+(.*)\\\n/', $data, $matches ) ) {
152 $groupId = $matches[1];
153 $this->
reportProgress(
"Detected message group as $groupId",
'group' );
155 $this->
reportProgress(
'Unable to determine message group',
'group',
'error' );
164 $poformat =
'".*"\n?(^".*"$\n?)*';
165 $quotePattern =
'/(^"|"$\n?)/m';
167 $sections = preg_split(
'/\n{2,}/', $data );
169 foreach ( $sections as $section ) {
171 if ( preg_match(
"/^msgctxt\s($poformat)/mx", $section, $matches ) ) {
173 $key = preg_replace( $quotePattern,
'', $matches[1] );
176 if ( !isset( $contents[$key] ) ) {
183 if ( preg_match(
"/^msgstr\s($poformat)/mx", $section, $matches ) ) {
185 $translation = preg_replace( $quotePattern,
'', $matches[1] );
187 $translation = stripcslashes( $translation );
193 if ( preg_match(
'/^#, fuzzy$/m', $section ) ) {
194 $translation = TRANSLATE_FUZZY . $translation;
197 $oldtranslation = (string)$contents[$key]->translation();
199 if ( $translation !== $oldtranslation ) {
200 if ( $translation ===
'' ) {
201 $this->
reportProgress(
"Skipping empty translation in the po file for $key!\n" );
203 if ( $oldtranslation ===
'' ) {
206 $this->
reportProgress(
"Translation of $key differs:\n$translation\n" );
208 $changes[
"$key/$code"] = $translation;
213 return [ $changes, $groupId ];
222 private $progressCallback;
238 public function __construct( array $changes, $groupId, $user, $dryrun =
true ) {
239 $this->changes = $changes;
240 $this->group = MessageGroups::getGroup( $groupId );
241 $this->user = User::newFromName( $user );
242 $this->dryrun = $dryrun;
245 public function setProgressCallback( $callback ) {
246 $this->progressCallback = $callback;
251 if ( is_callable( $this->progressCallback ) ) {
252 $useErrorOutput = $severity ===
'error';
253 call_user_func( $this->progressCallback, $text, $channel, $useErrorOutput );
261 if ( !$this->group ) {
262 $this->
reportProgress(
'Given group does not exist.',
'groupId',
'error' );
267 if ( !$this->user->idForName() ) {
268 $this->
reportProgress(
'Given user does not exist.',
'user',
'error' );
273 $count = count( $this->changes );
274 $this->
reportProgress(
"Going to update $count pages.",
'pagecount' );
276 $ns = $this->group->getNamespace();
278 foreach ( $this->changes as $title => $text ) {
279 $this->updateMessage( $ns, $title, $text );
289 private function updateMessage( $namespace, $page, $text ) {
290 $title = Title::makeTitleSafe( $namespace, $page );
292 if ( !$title instanceof Title ) {
297 $this->
reportProgress(
"Updating {$title->getPrefixedText()}... ", $title );
299 if ( $this->dryrun ) {
305 $page = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $title );
306 $content = ContentHandler::makeContent( $text, $title );
307 $status = $page->doUserEditContent(
310 'Updating translation from gettext import'
313 if ( $status ===
true || ( is_object( $status ) && $status->isOK() ) ) {
321$maintClass = Poimport::class;
322require_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')