PK!)Cd'd'FetchCommand.phpnu[getInstalledCore() as $tag ){ if( 'en_US' === $tag ){ continue; } $locale = Loco_Locale::parse($tag); if( $locale->isValid() ){ $locales[] = $locale; } } if( ! $locales ){ throw new Loco_error_Exception('No installed languages, try with --locale='); } } foreach( $projects as $project ){ $type = strtolower( $project->getBundle()->getType() ); $domain = $project->getDomain()->getName(); $info = $project->getBundle()->getHeaderInfo(); $version = $info->Version; // Currently only supporting WordPress community translation sources. $args = [ 'version' => $version ]; if( 'core' !== $type ){ $type.= 's'; if( $project->getSlug() !== $domain ){ WP_CLI::warning( sprintf('Skipping %s, only single text domain %s supported',$project->getId(),$type)); continue; } $args['slug'] = $domain; } WP_CLI::log( sprintf('Looking up %s v%s..',$project,$version) ); Loco_cli_Utils::debug('Querying WordPress translations API for %s => %s..',$type,json_encode($args) ); $result = $wp->apiGet($type,$args); // pre-index installable language packs $packages = []; foreach( $result['translations'] as $data ){ $packages[$data['language']] = $data['package']; } // Translations API does not error when GlotPress project doesn't exist, it just returns empty. if( ! $packages ){ Loco_cli_Utils::debug('No installable language packs available for %s. Checking if a GlotPress project exists..',$project); // Ping GlotPress project page. This is the only way we can know if an incomplete project exists $response = wp_remote_head( sprintf('https://translate.wordpress.org/projects/wp-%s/%s/',$type,$args['slug']) ); $status = wp_remote_retrieve_response_code($response); if( 404 === $status ){ WP_CLI::warning( sprintf("Skipping %s: 404 from translate.wordpress.org. Probably no GlotPress project.",$project) ); continue; } else if( 200 !== $status ){ WP_CLI::warning( sprintf("Status %u from translate.wordpress.org. Skipping %s.",$status,$project) ); } Loco_cli_Utils::debug('> Ok, looks like GlotPress project exists; probably no locales above the threshold for a package build'); } // Save path is under "system" location because we are installing from GlotPress $dir = new Loco_fs_Directory( 'core' === $type ? '.' : $type ); $dir->normalize( loco_constant('WP_LANG_DIR') ); foreach( $locales as $locale ){ $tag = (string) $locale; if( 'en_US' == $tag ){ WP_CLI::warning('There are no translations in en_US. It is the source locale.'); continue; } // Map WP locale codes to GlotPress teams. They differ, naturally. $team = $locale->lang; if( $locale->region ){ $team.= '-'.strtolower($locale->region); } $gp = Loco_data_CompiledData::get('gp'); if( array_key_exists($team,$gp['aliases']) ){ $team = $gp['aliases'][$team]; } // variant code (e.g. formal) is a sub-entity and not part of team language id $variant = $locale->variant; if( ! $variant ){ $variant = 'default'; } if( 'core' === $type ){ // core projects are per-version. "dev" being upcoming. Then e.g. 5.6.x for stable if( $opts['trunk'] || preg_match('/^\\d.\\d-(?:rc|dev|beta)/i',$version) ){ $slug = 'dev'; } else { list($major,$minor) = explode('.',$version,3); $slug = sprintf('%u.%u.x',$major,$minor); } // Core projects are sub projects. plugins and themes don't have this $map = [ 'default.' => '', 'default.admin' => '/admin', 'default.admin-network' => '/admin/network', 'continents-cities' => '/cc', ]; $slug .= $map[ $project->getId() ]; $url = 'https://translate.wordpress.org/projects/wp/'.$slug.'/'.$team.'/'.$variant.'/export-translations/?format=po'; } else { $slug = $domain; // plugins are either "stable" or "dev"; themes don't appear to have stability/version slug ?? if( 'plugins' === $type ) { $slug .= $opts['trunk'] ? '/dev' : '/stable'; } $url = 'https://translate.wordpress.org/projects/wp-'.$type.'/'.$slug.'/'.$team.'/' . $variant . '/export-translations/?format=po'; } // Note that this export URL is not a documented API and may change without notice // TODO We could pass If-Modified-Since with current PO file header, BUT that could not know if existing file is purged or not. Make configurable? WP_CLI::log( sprintf('Fetching PO from %s..',$url)); $response = wp_remote_get($url); $status = wp_remote_retrieve_response_code($response); if( 200 !== $status ){ WP_CLI::warning( sprintf('Status %u from translate.wordpress.org; skipping "%s". Probably no translation team',$status,$tag) ); continue; } Loco_cli_Utils::debug('OK, last modified %s', wp_remote_retrieve_header($response,'last-modified') ); /*/ TODO fallback to installable package if( $packages && ! array_key_exists($tag,$packages) ){ WP_ClI::warning( sprintf('%s is not installable in `%s` (probably not complete enough)',$project,$tag) ); }*/ // Parse PO data to check it's valid, and also because we're going to compile it. $pobody = wp_remote_retrieve_body($response); $podata = Loco_gettext_Data::fromSource($pobody); $response = null; // keep translations if file already exists in this location. $pofile = $project->initLocaleFile($dir,$locale); $info = new Loco_mvc_FileParams( [], $pofile ); Loco_cli_Utils::debug('Saving %s..', $info->relpath ); $compiler = new Loco_gettext_Compiler($pofile); if( $pofile->exists() ){ $info = new Loco_mvc_FileParams( [], $pofile ); Loco_cli_Utils::debug('PO already exists at %s (%s), merging..',$info->relpath,$info->size); $original = Loco_gettext_Data::load($pofile); $matcher = new Loco_gettext_Matcher($project); $matcher->loadRefs($podata,true); // downloaded file is in memory can be replaced with merged version $podata = clone $original; $podata->clear(); $stats = $matcher->merge($original,$podata); $original = null; if( ! $stats['add'] && ! $stats['del'] && ! $stats['fuz'] && ! $stats['trn'] ){ WP_CLI::log( sprintf('%s unchanged in "%s". Skipping %s', $project,$locale,$info->relpath) ); continue; } // Overwrite merged PO, which will back up first if configured Loco_cli_Utils::debug('OK: %u added, %u dropped, %u fuzzy', count($stats['add']), count($stats['del']), count($stats['fuz']) ); $podata->localize($locale); $compiler->writePo($podata); } // Copy PO directly to disk as per remote source else { $compiler->writeFile($pofile,$pobody); $podata->inheritHeader( Loco_gettext_Data::dummy()->localize($locale)->getHeaders() ); } // Compile new MO and JSON files.. Loco_cli_Utils::debug('Compiling %s.{mo,json}',$pofile->filename() ); $compiler->writeMo($podata); $compiler->writeJson($project,$podata); $pofile->clearStat(); WP_CLI::success( sprintf('Fetched %s for "%s": %s PO at %s', $project,$locale,$info->size,$info->relpath) ); Loco_error_AdminNotices::get()->flush(); // clean up memory and ready for next file unset($podata,$pobody); $done++; } } if( 0 === $done ){ WP_CLI::success('Completed OK, but no files were installed'); } } } PK!&&ExtractCommand.phpnu[getId(), '.' ); WP_CLI::log( sprintf('Extracting "%s" (%s)',$project->getName(),$id) ); // POT file may or may not exist currently $potfile = $project->getPot(); if( ! $potfile ){ WP_CLI::warning('Skipping undefined POT'); continue; } if( $potfile->locked() ){ WP_CLI::warning('Skipping unwritable POT'); Loco_cli_Utils::tabulateFiles( $potfile->getParent(), $potfile ); continue; } // Do extraction and grab only given domain's strings $ext = new Loco_gettext_Extraction( $project->getBundle() ); $domain = $project->getDomain()->getName(); $data = $ext->addProject($project)->includeMeta()->getTemplate( $domain ); Loco_cli_Utils::debug('Extracted %u strings', count($data) ); $list = $ext->getSkipped(); if( $list ){ $current = Loco_data_Settings::get()->max_php_size; $suggest = ceil( $ext->getMaxPhpSize() / 1024 ); WP_CLI::warning(sprintf('%u source files skipped over %s. Consider running with --maxsize=%uK',count($list),$current,$suggest) ); foreach( $list as $file ) { $f = new Loco_mvc_FileParams([],$file); Loco_cli_Utils::debug('%s (%s)', $f->relpath, $f->size ); } } // if POT exists check if update is necessary. $data->sort(); if( $potfile->exists() && ! $force ){ try { Loco_cli_Utils::debug('Checking if sources have changed since '.date('c',$potfile->modified()) ); $prev = Loco_gettext_Data::fromSource( $potfile->getContents() ); if( $prev->equal($data) ){ WP_CLI::log('No update required for '.$potfile->basename() ); continue; } } catch( Loco_error_ParseException $e ){ Loco_cli_Utils::debug( $e->getMessage().' in '.$potfile->basename() ); } } if( $noop ){ WP_CLI::success( sprintf('**DRY RUN** would update %s', $potfile->basename() ) ); continue; } // additional headers to set in new POT file $head = $data->getHeaders(); $head['Project-Id-Version'] = $project->getName(); $head['X-Domain'] = $domain; // write POT file to disk returning byte length Loco_cli_Utils::debug('Writing POT file...'); $bytes = $potfile->putContents( $data->msgcat() ); Loco_cli_Utils::debug('%u bytes written to %s',$bytes, $potfile->getRelativePath($content_dir) ); WP_CLI::success( sprintf('Updated %s', $potfile->basename() ) ); $updated++; } // sync summary if( 0 === $updated ){ WP_CLI::log('Nothing updated'); } else { WP_CLI::success( sprintf('%u POT files written',$updated) ); } } }PK!Ϸ Commands.phpnu[] * : Restrict to a type of bundle (plugins|themes|core); a single bundle (e.g. plugins:); or a Text Domain * * [--locale=] * : Restrict to one or more locales. Separate multiple codes with commas. * * [--fuzziness=] * : Override plugin settings for fuzzy matching tolerance (0-100). * * [--noop] * : Specify dry run. Makes no changes on disk. * * [--force] * : Update even when nothing has changed. Useful for recompiling MO/JSON. * * ## EXAMPLES * * wp loco sync plugins * * @param string[] $args * @param string[] $opts */ public function sync( $args, $opts ){ if( array_key_exists('fuzziness',$opts) ){ Loco_data_Settings::get()->fuzziness = (int) $opts['fuzziness']; } try { Loco_cli_SyncCommand::run ( Loco_cli_Utils::collectProjects( isset($args[0]) ? $args[0] : '' ), Loco_cli_Utils::collectLocales( isset($opts['locale']) ? $opts['locale'] : '' ), Loco_cli_Utils::bool($opts,'noop'), Loco_cli_Utils::bool($opts,'force') ); } catch( Loco_error_Exception $e ){ WP_CLI::error( $e->getMessage() ); } } /** * Extract available source strings * * ## OPTIONS * * [] * : Restrict to a type of bundle (plugins|themes|core); a single bundle (e.g. plugins:); or a Text Domain * * [--maxsize=] * : Override plugin settings for maximum PHP file size * * [--noop] * : Specify dry run. Makes no changes on disk. * * [--force] * : Update even when nothing has changed. Useful for updating meta properties. * * ## EXAMPLES * * wp loco extract core --maxsize=400K * * @param string[] $args * @param string[] $opts */ public function extract( $args, $opts ){ try { if( array_key_exists('maxsize',$opts) ){ Loco_data_Settings::get()->max_php_size = $opts['maxsize']; } Loco_cli_ExtractCommand::run ( Loco_cli_Utils::collectProjects( isset($args[0]) ? $args[0] : '' ), Loco_cli_Utils::bool($opts,'noop'), Loco_cli_Utils::bool($opts,'force') ); } catch( Loco_error_Exception $e ){ WP_CLI::error( $e->getMessage() ); } } /** * EXPERIMENTAL. Attempts to install translation source files from an external repository. * Use this to replace *installed* PO files if they are missing or have been purged of script translations. * * ## OPTIONS * * [] * : Restrict to a type of bundle (plugins|themes|core); a single bundle (e.g. plugins:); or a Text Domain * * [--locale=] * : Restrict to one or more locales. Separate multiple codes with commas. * * [--trunk] * : Install strings for upcoming dev version as opposed to latest stable * * ## EXAMPLES * * wp loco fetch loco-translate --locale=en_GB * * @param string[] $args * @param string[] $opts */ public function fetch( $args, $opts ){ try { Loco_cli_FetchCommand::run ( Loco_cli_Utils::collectProjects( isset($args[0]) ? $args[0] : '' ), Loco_cli_Utils::collectLocales( isset($opts['locale']) ? $opts['locale'] : '' ), [ 'trunk' => Loco_cli_Utils::bool($opts,'trunk') ] ); } catch( Loco_error_Exception $e ){ WP_CLI::error( $e->getMessage() ); } } } PK! Utils.phpnu[getDomain()->getName() !== $domain ){ continue; } if( $slug && $project->getSlug() !== $slug ){ continue; } $projects[] = $project; } } if( ! $projects ){ throw new Loco_error_Exception('No translation sets found'); } return $projects; } /** * Collect locales from one or more language tags * @param string zero or more language tags * @return Loco_Locale[] */ public static function collectLocales( $tags ){ $locales = []; if( '' !== $tags ){ $api = new Loco_api_WordPressTranslations; foreach( preg_split('/[\\s,;]+/i',$tags,-1,PREG_SPLIT_NO_EMPTY) as $tag ){ $locale = Loco_Locale::parse($tag); if( ! $locale->isValid() ){ throw new Loco_error_Exception('Invalid locale: '.json_encode($tag) ); } // TODO could expand language-only tags to known WordPress locales e.g. fr -> fr_FR $locales[ (string) $locale ] = $locale; $locale->ensureName($api); } // empty locales means ALL locales, so refuse to return ALL when filter was non-empty if( 0 === count($locales) ){ throw new Loco_error_Exception('No valid locales in: '.json_encode($tags) ); } } return $locales; } /** * Simple space-padded table * @param string[][] data rows to print */ public static function tabulate( array $t ){ $w = []; foreach( $t as $y => $row ){ foreach( $row as $x => $value ){ $width = mb_strlen($value,'UTF-8'); $w[$x] = isset($w[$x]) ? max($w[$x],$width) : $width; } } foreach( $t as $y => $row ){ $line = []; foreach( $w as $x => $width ){ $value = isset($row[$x]) ? $row[$x] : ''; $value = str_pad($value,$width,' ',STR_PAD_RIGHT); $line[] = $value; } self::debug( implode(' ',$line) ); } } /** * Prints file listing to stdout */ public static function tabulateFiles(){ $t = []; /* @var Loco_fs_File $file */ foreach( func_get_args() as $file ){ if( $file instanceof Loco_fs_File && $file->exists() ){ $f = new Loco_mvc_FileParams([],$file); $t[] = [ $f->owner, $f->group, $f->smode, $f->relpath ]; } } self::tabulate($t); } /** * WP_CLI debug logger */ public static function debug(){ $args = func_get_args(); $message = array_shift($args); if( $args ){ $message = vsprintf($message,$args); } WP_CLI::debug( $message,'loco' ); } /** * Parse boolean command line option. Absence is equal to false * @param string[] * @param string * @return bool */ public static function bool( array $opts, $key ){ $value = isset($opts[$key]) ? $opts[$key] : false; if( ! is_bool($value) ){ $value = $value && 'false' !== $value & 'no' !== $value; } return $value; } } PK!G¹!!SyncCommand.phpnu[getId(), '.' ); $base_dir = $project->getBundle()->getDirectoryPath(); WP_CLI::log( sprintf('Syncing "%s" (%s)',$project->getName(),$id) ); // Check if project has POT, which will be used as default template unless PO overrides $pot = null; $potfile = $project->getPot(); if( $potfile && $potfile->exists() ){ Loco_cli_Utils::debug('Parsing template: %s',$potfile->getRelativePath($content_dir)); try { $pot = Loco_gettext_Data::fromSource( $potfile->getContents() ); } catch( Loco_error_ParseException $e ){ WP_CLI::error( $e->getMessage().' in '.$potfile->getRelativePath($content_dir), false ); $potfile = null; } } /* @var Loco_fs_LocaleFile $pofile */ $pofiles = $project->findLocaleFiles('po'); foreach( $pofiles as $pofile ){ $locale = $pofile->getLocale(); $tag = (string) $locale; if( $locales && ! array_key_exists($tag,$locales) ){ continue; } // Preempt write errors and print useful file mode info $mofile = $pofile->cloneExtension('mo'); if( ! $pofile->writable() || $mofile->locked() ){ WP_CLI::warning('Skipping unwritable: '.self::fname($pofile) ); Loco_cli_Utils::tabulateFiles( $pofile->getParent(), $pofile, $mofile ); continue; } // Parsing candidate PO file (definitions) Loco_cli_Utils::debug('Parsing PO: %s',$pofile->getRelativePath($content_dir)); try { $def = Loco_gettext_Data::fromSource( $pofile->getContents() ); } catch( Loco_error_ParseException $e ){ WP_CLI::error( $e->getMessage().' in '.$pofile->getRelativePath($content_dir), false ); continue; } // Check if PO defines alternative template (reference) $ref = $pot; $head = $def->getHeaders(); $opts = new Loco_gettext_SyncOptions($head); $translate = $opts->mergeMsgstr(); if( $opts->hasTemplate() ){ $ref = null; $potfile = $opts->getTemplate(); $potfile->normalize( $base_dir ); if( $potfile->exists() ){ try { Loco_cli_Utils::debug('> Parsing alternative template: %s',$potfile->getRelativePath($content_dir) ); $ref = Loco_gettext_Data::fromSource( $potfile->getContents() ); } catch( Loco_error_ParseException $e ){ WP_CLI::error( $e->getMessage().' in '.$potfile->getRelativePath($content_dir), false ); } } else { Loco_cli_Utils::debug('Template not found (%s)', $potfile->basename() ); } } if( ! $ref ){ WP_CLI::warning( sprintf('Skipping %s; no valid translation template',$pofile->getRelativePath($content_dir) ) ); continue; } // Perform merge if we have a reference file Loco_cli_Utils::debug('Merging %s <- %s', $pofile->basename(), $potfile->basename() ); $matcher = new Loco_gettext_Matcher($project); $matcher->loadRefs($ref,$translate ); // Merge jsons if configured and available if( $opts->mergeJson() ){ $siblings = new Loco_fs_Siblings( $potfile->cloneBasename( $pofile->basename() ) ); $jsons = $siblings->getJsons( $project->getDomain()->getName() ); $njson = $matcher->loadJsons($jsons); Loco_cli_Utils::debug('> merged %u json files', $njson ); } // Get fuzzy matching tolerance from plugin settings, can be set temporarily in command line $fuzziness = Loco_data_Settings::get()->fuzziness; $matcher->setFuzziness( (string) $fuzziness ); // update matches sources, deferring unmatched for deferred fuzzy match $po = clone $def; $po->clear(); $nvalid = count( $matcher->mergeValid($def,$po) ); $nfuzzy = count( $matcher->mergeFuzzy($po) ); $nadded = count( $matcher->mergeAdded($po) ); $ndropped = count( $matcher->redundant() ); // TODO Support --previous to keep old strings, or at least comment them out as #| msgid..... if( $nfuzzy || $nadded || $ndropped ){ Loco_cli_Utils::debug('> unchanged:%u added:%u fuzzy:%u dropped:%u', $nvalid, $nadded, $nfuzzy, $ndropped ); } else { Loco_cli_Utils::debug('> %u identical sources',$nvalid); } // File is synced, but may be identical $po->sort(); if( ! $force && $po->equal($def) ){ WP_CLI::log( sprintf('No update required for %s', self::fname($pofile) ) ); continue; } if( $noop ){ WP_CLI::success( sprintf('**DRY RUN** would update %s', self::fname($pofile) ) ); continue; } try { $locale->ensureName($wp_locales); $po->localize($locale); $compiler = new Loco_gettext_Compiler($pofile); $bytes = $compiler->writePo($po); Loco_cli_Utils::debug('+ %u bytes written to %s',$bytes, $pofile->basename()); $updated++; // compile MO $bytes = $compiler->writeMo($po); if( $bytes ){ Loco_cli_Utils::debug('+ %u bytes written to %s',$bytes, $mofile->basename()); $compiled++; } // Done PO/MO pair, now generate JSON fragments as applicable $jsons = $compiler->writeJson($project,$po); foreach( $jsons as $file ){ $compiled++; $param = new Loco_mvc_FileParams([],$file); Loco_cli_Utils::debug('+ %u bytes written to %s',$param->size,$param->name); } // Done compile of this set Loco_error_AdminNotices::get()->flush(); WP_CLI::success( sprintf('Updated %s', self::fname($pofile) ) ); } catch( Loco_error_WriteException $e ){ WP_CLI::error( $e->getMessage(), false ); } } } // sync summary if( 0 === $updated ){ WP_CLI::log('Nothing updated'); } else { WP_CLI::success( sprintf('%u PO files synced, %u files compiled',$updated,$compiled) ); } } /** * Debug file name showing directory location * @param Loco_fs_File * @return string */ private static function fname( Loco_fs_File $file ){ $dir = new Loco_fs_LocaleDirectory( $file->dirname() ); return $file->filename().' ('.$dir->getTypeLabel( $dir->getTypeId() ).')'; } }PK!)Cd'd'FetchCommand.phpnu[PK!&&'ExtractCommand.phpnu[PK!Ϸ  7Commands.phpnu[PK! FUtils.phpnu[PK!G¹!!K[SyncCommand.phpnu[PKC}