Plugin Directory


Ignore:
Timestamp:
10/13/2026 02:57:34 AM (2 weeks ago)
Author:
avakodeforge
Message:

Release 1.2.10 (trunk + tag; see readme changelog)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • langforge/trunk/includes/class-cli.php

    r3736621 r3742801  
    406406        if ($result['success']) {
    407407            WP_CLI::success("Imported {$result['imported']} translations.");
     408            self::print_import_notes($importer, $result);
    408409        } else {
    409410            WP_CLI::error($result['error'] ?? 'Import failed.');
     411        }
     412    }
     413
     414    /**
     415 * The rows the importer did not take as the file wrote them (`notes` of its answer), one line each, as the screen of
     416 * the importer lists them: the title of the page, the sentence of what happened to the row, and the link to edit the
     417 * page. Only what the add-on sent is printed: an add-on that cannot make the sentences (describe_notes) gets the
     418 * page, the row and the kind it named, and nothing of ours.
     419 *
     420 * @param object $importer The importer that answered.
     421 * @param array $result Its answer.
     422 */
     423    private static function print_import_notes($importer, array $result) {
     424        $notes = isset($result['notes']) && is_array($result['notes']) ? $result['notes'] : [];
     425        if (!$notes) {
     426            return;
     427        }
     428        WP_CLI::log('Some rows of the file need your attention:');
     429
     430        if (method_exists($importer, 'describe_notes')) {
     431            foreach ((array) $importer->describe_notes($notes) as $note) {
     432                $title   = isset($note['title']) ? (string) $note['title'] : '';
     433                $message = isset($note['message']) && (string) $note['message'] !== '' ? (string) $note['message'] : (isset($note['row']) ? (string) $note['row'] : '');
     434                $line    = $title . ': ' . $message;
     435                if (!empty($note['edit_url'])) {
     436                    $line .= ' (' . $note['edit_url'] . ')';
     437                }
     438                WP_CLI::warning($line);
     439            }
     440            return;
     441        }
     442
     443        foreach ($notes as $note) {
     444            if (!is_array($note)) {
     445                continue;
     446            }
     447            $about = (int) (!empty($note['translation']) ? $note['translation'] : ($note['post'] ?? 0));
     448            $row   = isset($note['row']) ? (string) $note['row'] : '';
     449            $kind  = isset($note['kind']) ? (string) $note['kind'] : '';
     450            WP_CLI::warning('Post ' . $about . ($row !== '' ? ', row ' . $row : '') . ': ' . $kind);
     451        }
     452    }
     453
     454    /**
     455 * Show, or finish, the binary key of the string translation table.
     456 *
     457 * Lang Forge tells "Size" from "size" and "Resume" from "Résumé" in the table of its strings. It changes the key of
     458 * the table when an admin page is opened, except on a site whose table has more than 500,000 rows: a web request
     459 * must not hold such a table for minutes, so that site stays on the old key until this command is run.
     460 *
     461 * Without --yes it only shows the number of rows, the key and the answer the admin pages left. With --yes it
     462 * rebuilds the key now, whatever the number of rows (the time it takes depends on the server), under the same lock
     463 * a web request takes, and says "already" when the key is binary.
     464 *
     465 * ## OPTIONS
     466 *
     467 * [--yes]
     468 * : Rebuild the key now.
     469 *
     470 * ## EXAMPLES
     471 * wp langforge strings-key
     472 * wp langforge strings-key --yes
     473 *
     474 * @subcommand strings-key
     475 */
     476    public function strings_key($args, $assoc_args) {
     477        $yes = isset($assoc_args['yes']) && filter_var($assoc_args['yes'], FILTER_VALIDATE_BOOLEAN);
     478        $overview = LANGFORGE_Schema::strings_key_overview();
     479        if (!$overview['table']) {
     480            WP_CLI::error('There is no strings table on this site yet: activate Lang Forge once and run this command again.');
     481        }
     482
     483        $state = $overview['state'];
     484        $answer = isset($state['status']) ? (string) $state['status'] : 'none yet';
     485        if (!empty($state['error'])) {
     486            $answer .= ' (' . $state['error'] . ')';
     487        }
     488        $key = $overview['binary'] === true ? 'binary' : ($overview['binary'] === false ? 'not binary, "Size" and "size" are one string' : 'unknown, the columns could not be read');
     489        WP_CLI::log('Strings table: ' . $overview['rows'] . ' rows.');
     490        WP_CLI::log('Key: ' . $key . '.');
     491        WP_CLI::log('Last answer of the admin pages: ' . $answer . '.');
     492
     493        if (!$yes) {
     494            if ($overview['binary'] !== true) {
     495                WP_CLI::log('To rebuild the key now, run: wp langforge strings-key --yes');
     496            }
     497            return;
     498        }
     499
     500        $result = LANGFORGE_Schema::migrate_strings_key(true);
     501        if ($result === 'migrated') {
     502            WP_CLI::success('The key is rebuilt: the names and domains of the strings now compare byte for byte; ' . $overview['rows'] . ' rows kept.');
     503        } elseif ($result === 'already') {
     504            WP_CLI::success('Already done: the key is binary, nothing was changed.');
     505        } elseif ($result === 'busy') {
     506            WP_CLI::error('Another process is rebuilding the table now (its lock is held; a lock nobody let go of is taken over after 15 minutes). Nothing was changed; run the command again later.');
     507        } elseif ($result === 'no_table') {
     508            WP_CLI::error('There is no strings table on this site yet: activate Lang Forge once and run this command again.');
     509        } else {
     510            $after = LANGFORGE_Schema::strings_key_overview();
     511            $why = !empty($after['state']['error']) ? (string) $after['state']['error'] : 'the server refused the change';
     512            WP_CLI::error('The key was not changed: ' . rtrim($why, '. ') . '. The table is as it was; the site works as before.');
    410513        }
    411514    }
Note: See TracChangeset for help on using the changeset viewer.