Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/vendors/Textpattern/Textpack/Parser.php - 253 lines - 6934 bytes - Summary - Text - Print

Description: Textpack parser.

   1  <?php
   2  
   3  /*
   4   * Textpattern Content Management System
   5   * https://textpattern.com/
   6   *
   7   * Copyright (C) 2020 The Textpattern Development Team
   8   *
   9   * This file is part of Textpattern.
  10   *
  11   * Textpattern is free software; you can redistribute it and/or
  12   * modify it under the terms of the GNU General Public License
  13   * as published by the Free Software Foundation, version 2.
  14   *
  15   * Textpattern is distributed in the hope that it will be useful,
  16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18   * GNU General Public License for more details.
  19   *
  20   * You should have received a copy of the GNU General Public License
  21   * along with Textpattern. If not, see <https://www.gnu.org/licenses/>.
  22   */
  23  
  24  /**
  25   * Textpack parser.
  26   *
  27   * @since   4.6.0
  28   * @package Textpack
  29   */
  30  
  31  namespace Textpattern\Textpack;
  32  
  33  class Parser
  34  {
  35      /**
  36       * The default language.
  37       *
  38       * @var string
  39       */
  40  
  41      protected $language;
  42  
  43      /**
  44       * The default owner.
  45       *
  46       * @var string
  47       */
  48  
  49      protected $owner;
  50  
  51      /**
  52       * The list of strings in the pack, separated by language.
  53       *
  54       * @var array
  55       */
  56  
  57      protected $packs = array();
  58  
  59      /**
  60       * Constructor.
  61       */
  62  
  63      public function __construct()
  64      {
  65          $this->language = get_pref('language', TEXTPATTERN_DEFAULT_LANG);
  66          $this->owner = TEXTPATTERN_LANG_OWNER_SITE;
  67      }
  68  
  69      /**
  70       * Set the default language.
  71       *
  72       * @param string $language The language code
  73       */
  74  
  75      public function setLanguage($language)
  76      {
  77          $this->language = $language;
  78      }
  79  
  80      /**
  81       * Set the default owner.
  82       *
  83       * @param string $owner The default owner
  84       */
  85  
  86      public function setOwner($owner)
  87      {
  88          $this->owner = $owner;
  89      }
  90  
  91      /**
  92       * Convert a Textpack to an array.
  93       *
  94       * <code>
  95       * $textpack = \Textpattern\Textpack\Parser();
  96       * print_r(
  97       *     $textpack->parse("string => translation")
  98       * );
  99       * </code>
 100       *
 101       * @param  string       $textpack The Textpack
 102       * @param  string|array $group    Only return strings with the given event(s)
 103       * @return array An array of translations
 104       */
 105  
 106      public function parse($textpack, $group = null)
 107      {
 108          static $replacements = array(
 109              "\nnull" => "\n@null",
 110              "\nyes" => "\n@yes",
 111              "\nno" => "\n@no",
 112              "\ntrue" => "\n@true",
 113              "\nfalse" => "\n@false",
 114              "\non" => "\n@on",
 115              "\noff" => "\n@off",
 116              "\nnone" => "\n@none"
 117          );
 118  
 119          if ($group && !is_array($group)) {
 120              $group = do_list($group);
 121          } else {
 122              $group = (array)$group;
 123          }
 124  
 125          $out = array();
 126          $version = false;
 127          $lastmod = false;
 128          $event = false;
 129          $language = $this->language;
 130          $owner = $this->owner;
 131  
 132          // Are we dealing with the .ini file format?
 133          if (strpos($textpack, '=>') === false
 134              && $sections = parse_ini_string('[common]'.n.strtr($textpack, $replacements), true)) {
 135              if (!empty($sections['@common'])
 136                  && !empty($sections['@common']['lang_code'])
 137                  && $sections['@common']['lang_code'] !== TEXTPATTERN_DEFAULT_LANG
 138              ) {
 139                  $language = \Txp::get('\Textpattern\L10n\Locale')->validLocale($sections['@common']['lang_code']);
 140              }
 141  
 142              foreach ($sections as $event => $strings) {
 143                  $event = trim($event, ' @');
 144  
 145                  if (!empty($group) && !in_array($event, $group)) {
 146                      continue;
 147                  } else {
 148                      foreach (array_filter($strings) as $name => $data) {
 149                          $out[] = array(
 150                              'name'    => ltrim($name, ' @'),
 151                              'lang'    => $language,
 152                              'data'    => $data,
 153                              'event'   => $event,
 154                              'owner'   => $owner,
 155                              'version' => $version,
 156                              'lastmod' => $lastmod,
 157                          );
 158                      }
 159                  }
 160              }
 161  
 162              $this->packs[$language] = $out;
 163              return;
 164          }
 165  
 166          // Not .ini, must be dealing with a regular .txt/.textpack file format.
 167          $lines = explode(n, (string)$textpack);
 168  
 169          foreach ($lines as $line) {
 170              $line = trim($line);
 171  
 172              // A blank/comment line.
 173              if ($line === '' || preg_match('/^#[^@]/', $line, $m)) {
 174                  continue;
 175              }
 176  
 177              // Set version. The lastmod timestamp after the ';' in the regex
 178              // remains for reading legacy files, but is no longer used.
 179              if (preg_match('/^#@version\s+([^;\n]+);?([0-9]*)$/', $line, $m)) {
 180                  $version = $m[1];
 181                  continue;
 182              }
 183  
 184              // Set language.
 185              if (preg_match('/^#@language\s+(.+)$/', $line, $m)) {
 186                  $language = \Txp::get('\Textpattern\L10n\Locale')->validLocale($m[1]);
 187                  continue;
 188              }
 189  
 190              // Set owner.
 191              if (preg_match('/^#@owner\s+(.+)$/', $line, $m)) {
 192                  $owner = $m[1];
 193                  continue;
 194              }
 195  
 196              // Set event.
 197              if (preg_match('/^#@([a-zA-Z0-9_-]+)$/', $line, $m)) {
 198                  $event = $m[1];
 199                  continue;
 200              }
 201  
 202              // Translation.
 203              if (preg_match('/^([\w\-]+)\s*=>\s*(.+)$/', $line, $m)) {
 204                  if (!empty($m[1]) && !empty($m[2]) && (empty($group) || in_array($event, $group))) {
 205                      $langGiven = $language ? $language : $this->language;
 206                      $langList = do_list_unique($langGiven);
 207  
 208                      foreach ($langList as $langToStore) {
 209                          $this->packs[$langToStore][] = array(
 210                              'name'    => $m[1],
 211                              'lang'    => $langToStore,
 212                              'data'    => $m[2],
 213                              'event'   => $event,
 214                              'owner'   => $owner,
 215                              'version' => $version,
 216                              'lastmod' => $lastmod,
 217                          );
 218                      }
 219                  }
 220              }
 221          }
 222  
 223          return;
 224      }
 225  
 226      /**
 227       * Fetch the language strings extracted by the last-parsed Textpack.
 228       *
 229       * @return array
 230       */
 231  
 232      public function getStrings($lang_code)
 233      {
 234          $out = array();
 235  
 236          if (isset($this->packs[$lang_code])) {
 237              $out = $this->packs[$lang_code];
 238          }
 239  
 240          return $out;
 241      }
 242  
 243      /**
 244       * Fetch the list of languages used in the last-parsed Textpack.
 245       *
 246       * @return array
 247       */
 248  
 249      public function getLanguages()
 250      {
 251          return array_keys($this->packs);
 252      }
 253  }

title

Description

title

Description

title

Description

title

title

Body