Skip to content
PoStreamReader.php 17.7 KiB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
<?php

/**
 * @file
 * Definition of Drupal\Component\Gettext\PoStreamReader.
 */

namespace Drupal\Component\Gettext;

use Drupal\Component\Gettext\PoReaderInterface;
use Drupal\Component\Gettext\PoStreamInterface;
use Drupal\Component\Gettext\PoHeader;

/**
 * Implements Gettext PO stream reader.
 *
 * The PO file format parsing is implemented according to the documentation at
 * http://www.gnu.org/software/gettext/manual/gettext.html#PO-Files
 */
class PoStreamReader implements PoStreamInterface, PoReaderInterface {

  /**
   * Source line number of the stream being parsed.
   *
   * @var int
   */
  private $_line_number = 0;

  /**
   * Parser context for the stream reader state machine.
   *
   * Possible contexts are:
   *  - 'COMMENT' (#)
   *  - 'MSGID' (msgid)
   *  - 'MSGID_PLURAL' (msgid_plural)
   *  - 'MSGCTXT' (msgctxt)
   *  - 'MSGSTR' (msgstr or msgstr[])
   *  - 'MSGSTR_ARR' (msgstr_arg)
   *
   * @var string
   */
  private $_context = 'COMMENT';

  /**
   * Current entry being read. Incomplete.
   *
   * @var array
   */
  private $_current_item = array();

  /**
   * Current plural index for plural translations.
   *
   * @var int
   */
  private $_current_plural_index = 0;

  /**
   * URI of the PO stream that is being read.
   *
   * @var string
   */
  private $_uri = '';

  /**
   * Language code for the PO stream being read.
   *
   * @var string
   */
  private $_langcode = NULL;

  /**
   * Size of the current PO stream.
   *
   * @var int
   */
  private $_size;

  /**
   * File handle of the current PO stream.
   *
   * @var resource
   */
  private $_fd;

  /**
   * The PO stream header.
   *
   * @var Drupal\Component\Gettext\PoHeader
   */
  private $_header;

  /**
   * Object wrapper for the last read source/translation pair.
   *
   * @var Drupal\Component\Gettext\PoItem
   */
  private $_last_item;

  /**
   * Indicator of whether the stream reading is finished.
   *
   * @var boolean
   */
  private $_finished;

  /**
   * Array of translated error strings recorded on reading this stream so far.
   *
   * @var array
   */
  private $_errors;

  /**
   * Implements Drupal\Component\Gettext\PoMetadataInterface::getLangcode().
   */
  public function getLangcode() {
    return $this->_langcode;
  }

  /**
   * Implements Drupal\Component\Gettext\PoMetadataInterface::setLangcode().
   */
  public function setLangcode($langcode) {
    $this->_langcode = $langcode;
  }

  /**
   * Implements Drupal\Component\Gettext\PoMetadataInterface::getHeader().
   */
  public function getHeader() {
    return $this->_header;
  }

  /**
   * Implements Drupal\Component\Gettext\PoMetadataInterface::setHeader().
   *
   * Not applicable to stream reading and therefore not implemented.
   */
  public function setHeader(PoHeader $header) {
  }

  /**
   * Implements Drupal\Component\Gettext\PoStreamInterface::getURI().
   */
  public function getURI() {
    return $this->_uri;
  }

  /**
   * Implements Drupal\Component\Gettext\PoStreamInterface::setURI().
   */
  public function setURI($uri) {
    $this->_uri = $uri;
  }

  /**
   * Implements Drupal\Component\Gettext\PoStreamInterface::open().
   *
   * Opens the stream and reads the header. The stream is ready for reading
   * items after.
   *
   * @throws Exception
   *   If the URI is not yet set.
   */
  public function open() {
    if (!empty($this->_uri)) {
      $this->_fd = fopen($this->_uri, 'rb');
      $this->_size = ftell($this->_fd);
      $this->readHeader();
    }
    else {
      throw new Exception('Cannot open stream without URI set.');
    }
  }

  /**
   * Implements Drupal\Component\Gettext\PoStreamInterface::close().
   *
   * @throws Exception
   *   If the stream is not open.
   */
  public function close() {
    if ($this->_fd) {
      fclose($this->_fd);
    }
    else {
      throw new Exception('Cannot close stream that is not open.');
    }
  }

  /**
   * Implements Drupal\Component\Gettext\PoReaderInterface::readItem().
   */
  public function readItem() {
    // Clear out the last item.
    $this->_last_item = NULL;

    // Read until finished with the stream or a complete item was identified.
    while (!$this->_finished && is_null($this->_last_item)) {
      $this->readLine();
    }

    return $this->_last_item;
  }

  /**
   * Read the header from the PO stream.
   *
   * The header is a special case PoItem, using the empty string as source and
   * key-value pairs as translation. We just reuse the item reader logic to
   * read the header.
   */
  private function readHeader() {
    $item = $this->readItem();
    $header = new PoHeader;
    $header->setFromString(trim($item->getTranslation()));
    $this->_header = $header;
  }

  /**
   * Reads a line from the PO stream and stores data internally.
   *
   * Expands $this->_current_item based on new data for the current item. If
   * this line ends the current item, it is saved with setItemFromArray() with
   * data from $this->_current_item.
   *
   * An internal state machine is maintained in this reader using $this->_context
   * as the reading state. PO items are inbetween COMMENT states (when items have
   * at least one line or comment inbetween them or indicated by MSGSTR or
   * MSGSTR_ARR followed immediately by an MSGID or MSGCTXT (when items closely
   * follow each other).
   *
   * @return
   *   FALSE if an error was logged, NULL otherwise. The errors are considered
   *   non-blocking, so reading can continue, while the errors are collected
   *   for later presentation.
   */
  private function readLine() {
    // Read a line and set the stream finished indicator if it was not
    // possible anymore.
    $line = fgets($this->_fd);
    $this->_finished = ($line === FALSE);

    if (!$this->_finished) {

      if ($this->_line_number == 0) {
        // The first line might come with a UTF-8 BOM, which should be removed.
        $line = str_replace("\xEF\xBB\xBF", '', $line);
        // Current plurality for 'msgstr[]'.
        $this->_current_plural_index = 0;
      }

      // Track the line number for error reporting.
      $this->_line_number++;

      // Initialize common values for error logging.
      $log_vars = array(
        '%uri' => $this->getURI(),
        '%line' => $this->_line_number,
      );
      $t = get_t();

      // Trim away the linefeed. \\n might appear at the end of the string if
      // another line continuing the same string follows. We can remove that.
      $line = trim(strtr($line, array("\\\n" => "")));

      if (!strncmp('#', $line, 1)) {
        // Lines starting with '#' are comments.

        if ($this->_context == 'COMMENT') {
          // Already in comment context, add to current comment.
          $this->_current_item['#'][] = substr($line, 1);
        }
        elseif (($this->_context == 'MSGSTR') || ($this->_context == 'MSGSTR_ARR')) {
          // We are currently in string context, save current item.
          $this->setItemFromArray($this->_current_item);

          // Start a new entry for the comment.
          $this->_current_item = array();
          $this->_current_item['#'][] = substr($line, 1);

          $this->_context = 'COMMENT';
          return;
        }
        else {
          // A comment following any other context is a syntax error.
          $this->_errors[] = $t('The translation stream %uri contains an error: "msgstr" was expected but not found on line %line.', $log_vars);
          return FALSE;
        }
        return;
      }
      elseif (!strncmp('msgid_plural', $line, 12)) {
        // A plural form for the current source string.

        if ($this->_context != 'MSGID') {
          // A plural form can only be added to an msgid directly.
          $this->_errors[] = $t('The translation stream %uri contains an error: "msgid_plural" was expected but not found on line %line.', $log_vars);
          return FALSE;
        }

        // Remove 'msgid_plural' and trim away whitespace.
        $line = trim(substr($line, 12));

        // Only the plural source string is left, parse it.
        $quoted = $this->parseQuoted($line);
        if ($quoted === FALSE) {
          // The plural form must be wrapped in quotes.
          $this->_errors[] = $t('The translation stream %uri contains a syntax error on line %line.', $log_vars);
          return FALSE;
        }

        // Append the plural source to the current entry.
        if (is_string($this->_current_item['msgid'])) {
          // The first value was stored as string. Now we know the context is
          // plural, it is converted to array.
          $this->_current_item['msgid'] = array($this->_current_item['msgid']);
        }
        $this->_current_item['msgid'][] = $quoted;

        $this->_context = 'MSGID_PLURAL';
        return;
      }
      elseif (!strncmp('msgid', $line, 5)) {
        // Starting a new message.

        if (($this->_context == 'MSGSTR') || ($this->_context == 'MSGSTR_ARR')) {
          // We are currently in string context, save current item.
          $this->setItemFromArray($this->_current_item);

          // Start a new context for the msgid.
          $this->_current_item = array();
        }
        elseif ($this->_context == 'MSGID') {
          // We are currently already in the context, meaning we passed an id with no data.
          $this->_errors[] = $t('The translation stream %uri contains an error: "msgid" is unexpected on line %line.', $log_vars);
          return FALSE;
        }

        // Remove 'msgid' and trim away whitespace.
        $line = trim(substr($line, 5));

        // Only the message id string is left, parse it.
        $quoted = $this->parseQuoted($line);
        if ($quoted === FALSE) {
          // The message id must be wrapped in quotes.
          $this->_errors[] = $t('The translation stream %uri contains an error: invalid format for "msgid" on line %line.', $log_vars, $log_vars);
          return FALSE;
        }

        $this->_current_item['msgid'] = $quoted;
        $this->_context = 'MSGID';
        return;
      }
      elseif (!strncmp('msgctxt', $line, 7)) {
        // Starting a new context.

        if (($this->_context == 'MSGSTR') || ($this->_context == 'MSGSTR_ARR')) {
          // We are currently in string context, save current item.
          $this->setItemFromArray($this->_current_item);
          $this->_current_item = array();
        }
        elseif (!empty($this->_current_item['msgctxt'])) {
          // A context cannot apply to another context.
          $this->_errors[] = $t('The translation stream %uri contains an error: "msgctxt" is unexpected on line %line.', $log_vars);
          return FALSE;
        }

        // Remove 'msgctxt' and trim away whitespaces.
        $line = trim(substr($line, 7));

        // Only the msgctxt string is left, parse it.
        $quoted = $this->parseQuoted($line);
        if ($quoted === FALSE) {
          // The context string must be quoted.
          $this->_errors[] = $t('The translation stream %uri contains an error: invalid format for "msgctxt" on line %line.', $log_vars);
          return FALSE;
        }

        $this->_current_item['msgctxt'] = $quoted;

        $this->_context = 'MSGCTXT';
        return;
      }
      elseif (!strncmp('msgstr[', $line, 7)) {
        // A message string for a specific plurality.

        if (($this->_context != 'MSGID') &&
            ($this->_context != 'MSGCTXT') &&
            ($this->_context != 'MSGID_PLURAL') &&
            ($this->_context != 'MSGSTR_ARR')) {
          // Plural message strings must come after msgid, msgxtxt,
          // msgid_plural, or other msgstr[] entries.
          $this->_errors[] = $t('The translation stream %uri contains an error: "msgstr[]" is unexpected on line %line.', $log_vars);
          return FALSE;
        }

        // Ensure the plurality is terminated.
        if (strpos($line, ']') === FALSE) {
          $this->_errors[] = $t('The translation stream %uri contains an error: invalid format for "msgstr[]" on line %line.', $log_vars);
          return FALSE;
        }

        // Extract the plurality.
        $frombracket = strstr($line, '[');
        $this->_current_plural_index = substr($frombracket, 1, strpos($frombracket, ']') - 1);

        // Skip to the next whitespace and trim away any further whitespace,
        // bringing $line to the message text only.
        $line = trim(strstr($line, " "));

        $quoted = $this->parseQuoted($line);
        if ($quoted === FALSE) {
          // The string must be quoted.
          $this->_errors[] = $t('The translation stream %uri contains an error: invalid format for "msgstr[]" on line %line.', $log_vars);
          return FALSE;
        }
        if (!isset($this->_current_item['msgstr']) || !is_array($this->_current_item['msgstr'])) {
          $this->_current_item['msgstr'] = array();
        }

        $this->_current_item['msgstr'][$this->_current_plural_index] = $quoted;

        $this->_context = 'MSGSTR_ARR';
        return;
      }
      elseif (!strncmp("msgstr", $line, 6)) {
        // A string pair for an msgidid (with optional context).

        if (($this->_context != 'MSGID') && ($this->_context != 'MSGCTXT')) {
          // Strings are only valid within an id or context scope.
          $this->_errors[] = $t('The translation stream %uri contains an error: "msgstr" is unexpected on line %line.', $log_vars);
          return FALSE;
        }

        // Remove 'msgstr' and trim away away whitespaces.
        $line = trim(substr($line, 6));

        // Only the msgstr string is left, parse it.
        $quoted = $this->parseQuoted($line);
        if ($quoted === FALSE) {
          // The string must be quoted.
          $this->_errors[] = $t('The translation stream %uri contains an error: invalid format for "msgstr" on line %line.', $log_vars);
          return FALSE;
        }

        $this->_current_item['msgstr'] = $quoted;

        $this->_context = 'MSGSTR';
        return;
      }
      elseif ($line != '') {
        // Anything that is not a token may be a continuation of a previous token.

        $quoted = $this->parseQuoted($line);
        if ($quoted === FALSE) {
          // This string must be quoted.
          $this->_errors[] = $t('The translation stream %uri contains an error: string continuation expected on line %line.', $log_vars);
          return FALSE;
        }

        // Append the string to the current item.
        if (($this->_context == 'MSGID') || ($this->_context == 'MSGID_PLURAL')) {
          if (is_array($this->_current_item['msgid'])) {
            // Add string to last array element for plural sources.
            $last_index = count($this->_current_item['msgid']) - 1;
            $this->_current_item['msgid'][$last_index] .= $quoted;
          }
          else {
            // Singular source, just append the string.
            $this->_current_item['msgid'] .= $quoted;
          }
        }
        elseif ($this->_context == 'MSGCTXT') {
          // Multiline context name.
          $this->_current_item['msgctxt'] .= $quoted;
        }
        elseif ($this->_context == 'MSGSTR') {
          // Multiline translation string.
          $this->_current_item['msgstr'] .= $quoted;
        }
        elseif ($this->_context == 'MSGSTR_ARR') {
          // Multiline plural translation string.
          $this->_current_item['msgstr'][$this->_current_plural_index] .= $quoted;
        }
        else {
          // No valid context to append to.
          $this->_errors[] = $t('The translation stream %uri contains an error: unexpected string on line %line.', $log_vars);
          return FALSE;
        }
        return;
      }
    }

    // Empty line read or EOF of PO stream, close out the last entry.
    if (($this->_context == 'MSGSTR') || ($this->_context == 'MSGSTR_ARR')) {
      $this->setItemFromArray($this->_current_item);
      $this->_current_item = array();
    }
    elseif ($this->_context != 'COMMENT') {
      $this->_errors[] = $t('The translation stream %uri ended unexpectedly at line %line.', $log_vars);
      return FALSE;
    }
  }

  /**
   * Store the parsed values as a PoItem object.
   */
  public function setItemFromArray($value) {
    $plural = FALSE;

    $comments = '';
    if (isset($value['#'])) {
      $comments = $this->shortenComments($value['#']);
    }

    if (is_array($value['msgstr'])) {
      // Sort plural variants by their form index.
      ksort($value['msgstr']);
      $plural = TRUE;
    }

    $item = new PoItem();
    $item->setContext(isset($value['msgctxt']) ? $value['msgctxt'] : '');
    $item->setSource($value['msgid']);
    $item->setTranslation($value['msgstr']);
    $item->setPlural($plural);
    $item->setComment($comments);
    $item->setLangcode($this->_langcode);

    $this->_last_item = $item;

    $this->_context = 'COMMENT';
  }

  /**
   * Parses a string in quotes.
   *
   * @param $string
   *   A string specified with enclosing quotes.
   *
   * @return
   *   The string parsed from inside the quotes.
   */
  function parseQuoted($string) {
    if (substr($string, 0, 1) != substr($string, -1, 1)) {
      // Start and end quotes must be the same.
      return FALSE;
    }
    $quote = substr($string, 0, 1);
    $string = substr($string, 1, -1);
    if ($quote == '"') {
      // Double quotes: strip slashes.
      return stripcslashes($string);
    }
    elseif ($quote == "'") {
      // Simple quote: return as-is.
      return $string;
    }
    else {
      // Unrecognized quote.
      return FALSE;
    }
  }

  /**
   * Generates a short, one-string version of the passed comment array.
   *
   * @param $comment
   *   An array of strings containing a comment.
   *
   * @return
   *   Short one-string version of the comment.
   */
  private function shortenComments($comment) {
    $comm = '';
    while (count($comment)) {
      $test = $comm . substr(array_shift($comment), 1) . ', ';
      if (strlen($comm) < 130) {
        $comm = $test;
      }
      else {
        break;
      }
    }
    return trim(substr($comm, 0, -2));
  }

}