halt = null === $halt ? array( \WP_CLI::class, 'halt' ) : $halt; } /** * Show connection, plan, languages and translation memory stats. * * @subcommand status */ public function status() { $conn = Settings::connection(); $config = Settings::effective(); $rows = array( array( 'key' => 'connected', 'value' => Settings::is_connected() ? 'yes' : 'no' ), array( 'key' => 'cloud', 'value' => Settings::cloud_url() ), array( 'key' => 'site', 'value' => ( $conn['site_name'] ?? '' ) . ' (' . ( $conn['site_id'] ?? '-' ) . ')' ), array( 'key' => 'plan', 'value' => $conn['plan']['name'] ?? '-' ), array( 'key' => 'default language', 'value' => $config['default_language'] ), array( 'key' => 'languages', 'value' => implode( ', ', $config['languages'] ) ?: '-' ), array( 'key' => 'url mode', 'value' => $config['url_mode'] ), array( 'key' => 'usage', 'value' => isset( $conn['usage']['chars_translated'] ) ? number_format( (int) $conn['usage']['chars_translated'] ) . ' / ' . number_format( (int) $conn['usage']['chars_quota'] ) . ' chars' : '-' ), array( 'key' => 'config version', 'value' => (string) Settings::config_version() ), array( 'key' => 'last error', 'value' => $conn['last_error'] ?? '' ), ); foreach ( Translation_Memory::counts() as $lang => $n ) { $rows[] = array( 'key' => 'strings ' . $lang, 'value' => (string) $n ); } \WP_CLI\Utils\format_items( 'table', $rows, array( 'key', 'value' ) ); } /** * Connect this site with an API key from the Langferry dashboard. * * ## OPTIONS * * * : Site API key (starts with lf_). * * [--cloud=] * : Langferry Cloud URL (defaults to the configured one). */ public function connect( $args, $assoc ) { $key = trim( $args[0] ); if ( 0 !== strpos( $key, 'lf_' ) ) { \WP_CLI::error( 'That does not look like a Langferry API key.' ); } $changes = array( 'api_key' => $key ); if ( ! empty( $assoc['cloud'] ) ) { $changes['cloud_url'] = esc_url_raw( untrailingslashit( $assoc['cloud'] ) ); if ( ! Settings::is_allowed_cloud_url( $changes['cloud_url'] ) ) { \WP_CLI::error( 'The Langferry Cloud URL must use HTTPS.' ); } } Settings::update_connection( $changes ); $result = Cloud::sync(); if ( is_wp_error( $result ) ) { Settings::clear_connection(); \WP_CLI::error( 'Could not connect: ' . $result->get_error_message() ); } $this->status(); \WP_CLI::success( 'Connected.' ); } /** * Pull the latest config, plan and usage from the cloud. */ public function sync() { delete_transient( 'langferry_quota_exceeded' ); Translator::clear_plan_pauses(); delete_transient( 'langferry_cloud_backoff' ); $result = Cloud::sync(); if ( is_wp_error( $result ) ) { \WP_CLI::error( $result->get_error_message() ); } \WP_CLI::success( 'Synced (config version ' . Settings::config_version() . ').' ); } /** * Disconnect from Langferry Cloud (revokes this site's API key). */ public function disconnect() { Cloud::request( 'DELETE', '/site/connection' ); Settings::clear_connection(); \WP_CLI::success( 'Disconnected.' ); } /** * Pre-translate the site in the order the background worker follows. * * Main pages first, language by language, then the pages people asked * for, then the sitemap. Each page is asked for from the site itself as * pre-translation, one at a time, so its own render translates it within * your plan's pace. A page that can't be finished is skipped for the rest * of the run and tried again on the next one; after 3 tries in a row it * is set aside for 7 days. A page that takes longer than 20 seconds gets * up to 20 more to finish before the next one is asked for. A page people * asked for that isn't there any more (404, 410 or a redirect) is taken * off the queue. A language Langferry Cloud paused for a few minutes is * passed over for the rest of the run, and the others go on. * * Exits 0 when nothing is left or --limit is reached, and 1 when it * stopped early: paced by your plan, Langferry Cloud unavailable (or * only paused languages left), the server unable to generate random * numbers, or the site not answering its own requests 3 times in a row. * * ## OPTIONS * * [--lang=] * : Only this language. * * [--limit=] * : At most this many pages (a page in one language counts once). * --- * default: 500 * --- */ public function translate( $args, $assoc ) { if ( ! Settings::is_active() ) { $this->fail( 'Connect the site and choose languages first.' ); return; } $lang = isset( $assoc['lang'] ) ? sanitize_text_field( (string) $assoc['lang'] ) : ''; if ( '' !== $lang && ! in_array( $lang, Settings::effective()['languages'], true ) ) { $this->fail( sprintf( '%s is not one of this site\'s languages.', $lang ) ); return; } $skipped = 0; $signin = 0; $result = Queue::pretranslate( max( 1, (int) ( $assoc['limit'] ?? 500 ) ), $lang, static function ( array $pair, array $step ) use ( &$skipped, &$signin ) { if ( 'signin' === $step['outcome'] ) { ++$signin; // Set aside at once, not after 3 tries (ruling R55). } elseif ( in_array( $step['outcome'], Queue::FAILED, true ) ) { ++$skipped; } \WP_CLI::log( sprintf( '%s %s: %s', $pair['lang'], $step['url'], self::outcome( $step ) ) ); } ); $done = self::pages( $result['done'] ); $left = $skipped ? sprintf( ' %s couldn\'t be finished (see above): they are tried again on the next run, and set aside for 7 days after 3 tries in a row.', self::pages( $skipped ) ) : ''; if ( $signin ) { $left .= sprintf( ' %s %s someone signed in, so %s set aside for 7 days.', self::pages( $signin ), 1 === $signin ? 'needs' : 'need', 1 === $signin ? 'it was' : 'they were' ); } switch ( $result['stopped'] ) { case 'locked': $this->fail( 'Pre-translation is already running in the background. Try again in a minute.' ); return; case 'paced': \WP_CLI::warning( "Your plan's pace says not yet: stopped after $done finished. The background worker carries on as the pace allows, or run this again later.$left" ); break; case 'cloud': if ( ! empty( $result['languages_paused'] ) ) { // Only the languages Langferry Cloud paused were left (ruling R63). \WP_CLI::warning( "Langferry Cloud has paused the remaining languages for now. Stopped after $done finished.$left" ); } else { \WP_CLI::warning( "Langferry Cloud can't translate right now (unreachable, allowance used up, or the key refused): stopped after $done finished. See `wp langferry status`.$left" ); } break; case 'system': \WP_CLI::warning( "This server can't generate random numbers, so pre-translation can't run.$left" ); break; case 'site': \WP_CLI::warning( "The site didn't answer its own requests 3 times in a row (no answer, 401, 403, 429 or a server error): stopped after $done finished. Check that the site can reach itself (Site Health), or ask your host.$left" ); break; case 'pairs': \WP_CLI::success( "$done finished. More are waiting: run this again, or raise --limit.$left" ); return; default: \WP_CLI::success( "$done finished. Nothing else is left to translate in this run.$left" ); return; } ( $this->halt )( 1 ); // Stopped early: after the summary, for scripts. } /** * Prints the error and ends the command with exit code 1, through $halt. */ private function fail( string $message ): void { \WP_CLI::error( $message, false ); ( $this->halt )( 1 ); } private static function pages( int $n ): string { return 1 === $n ? '1 page' : "$n pages"; } /** * What a pre-translation request did, for the log. * * @param array{outcome: string, status: int, requested: bool, timeout?: bool} $step */ private static function outcome( array $step ): string { if ( ! empty( $step['timeout'] ) && in_array( $step['outcome'], array( 'error', 'refused' ), true ) ) { return 'error' === $step['outcome'] ? 'timed out, and still not finished 20 seconds later' : 'timed out; rendered, but nothing more was translated'; } switch ( $step['outcome'] ) { case 'done': return 'finished'; case 'progress': return 'more translated, not finished yet'; case 'paced': return 'paced'; case 'cloud': return 'Langferry Cloud can\'t translate right now'; case 'language': return 'Langferry Cloud paused this language for a few minutes; the others go on'; case 'cache': return 'answered from a page cache, not rendered'; case 'unreachable': return 'no answer'; case 'signin': return 'HTTP ' . $step['status'] . ' to a sign-in page: visitors can\'t read it, set aside for 7 days'; case 'gone': return 'HTTP ' . $step['status'] . ': no page here any more, taken off the queue'; case 'error': return 'HTTP ' . $step['status']; default: return $step['requested'] ? 'rendered, but nothing more was translated' : 'not an address of this site to ask for'; } } /** * Delete machine translations so they are re-translated on next view. * * ## OPTIONS * * [--lang=] * : Only this language. * * [--include-manual] * : Also delete human edits. * * [--yes] * : Skip confirmation. */ public function purge( $args, $assoc ) { $lang = $assoc['lang'] ?? null; \WP_CLI::confirm( 'Delete ' . ( $lang ? $lang : 'all' ) . ' machine translations?', $assoc ); $n = Translation_Memory::purge( $lang, ! empty( $assoc['include-manual'] ) ); Slugs::purge( $lang ); \WP_CLI::success( "Deleted {$n} strings." ); } }