[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/phpbb/console/command/reparser/ -> reparse.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\console\command\reparser;
  15  
  16  use phpbb\exception\runtime_exception;
  17  use Symfony\Component\Console\Input\InputInterface;
  18  use Symfony\Component\Console\Input\InputArgument;
  19  use Symfony\Component\Console\Input\InputOption;
  20  use Symfony\Component\Console\Output\OutputInterface;
  21  use Symfony\Component\Console\Style\SymfonyStyle;
  22  
  23  class reparse extends \phpbb\console\command\command
  24  {
  25      /**
  26      * @var InputInterface
  27      */
  28      protected $input;
  29  
  30      /**
  31      * @var SymfonyStyle
  32      */
  33      protected $io;
  34  
  35      /**
  36      * @var OutputInterface
  37      */
  38      protected $output;
  39  
  40      /**
  41       * @var \phpbb\lock\db
  42       */
  43      protected $reparse_lock;
  44  
  45      /**
  46       * @var \phpbb\textreparser\manager
  47       */
  48      protected $reparser_manager;
  49  
  50      /**
  51      * @var \phpbb\di\service_collection
  52      */
  53      protected $reparsers;
  54  
  55      /**
  56      * @var array The reparser's last $current ID as values
  57      */
  58      protected $resume_data;
  59  
  60      /**
  61      * Constructor
  62      *
  63      * @param \phpbb\user $user
  64      * @param \phpbb\lock\db $reparse_lock
  65      * @param \phpbb\textreparser\manager $reparser_manager
  66      * @param \phpbb\di\service_collection $reparsers
  67      */
  68  	public function __construct(\phpbb\user $user, \phpbb\lock\db $reparse_lock, \phpbb\textreparser\manager $reparser_manager, \phpbb\di\service_collection $reparsers)
  69      {
  70          require_once  __DIR__ . '/../../../../includes/functions_content.php';
  71  
  72          $this->reparse_lock = $reparse_lock;
  73          $this->reparser_manager = $reparser_manager;
  74          $this->reparsers = $reparsers;
  75          parent::__construct($user);
  76      }
  77  
  78      /**
  79      * Sets the command name and description
  80      *
  81      * @return null
  82      */
  83  	protected function configure()
  84      {
  85          $this
  86              ->setName('reparser:reparse')
  87              ->setDescription($this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE'))
  88              ->addArgument('reparser-name', InputArgument::OPTIONAL, $this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_ARG_1'))
  89              ->addOption(
  90                  'dry-run',
  91                  null,
  92                  InputOption::VALUE_NONE,
  93                  $this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_DRY_RUN')
  94              )
  95              ->addOption(
  96                  'force-bbcode-reparsing',
  97                  null,
  98                  InputOption::VALUE_NONE,
  99                  $this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_FORCE_BBCODE')
 100              )
 101              ->addOption(
 102                  'resume',
 103                  null,
 104                  InputOption::VALUE_NONE,
 105                  $this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RESUME')
 106              )
 107              ->addOption(
 108                  'range-min',
 109                  null,
 110                  InputOption::VALUE_REQUIRED,
 111                  $this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RANGE_MIN'),
 112                  1
 113              )
 114              ->addOption(
 115                  'range-max',
 116                  null,
 117                  InputOption::VALUE_REQUIRED,
 118                  $this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RANGE_MAX')
 119              )
 120              ->addOption(
 121                  'range-size',
 122                  null,
 123                  InputOption::VALUE_REQUIRED,
 124                  $this->user->lang('CLI_DESCRIPTION_REPARSER_REPARSE_OPT_RANGE_SIZE'),
 125                  100
 126              );
 127          ;
 128      }
 129  
 130      /**
 131      * Executes the command reparser:reparse
 132      *
 133      * @param InputInterface $input
 134      * @param OutputInterface $output
 135      * @return integer
 136      */
 137  	protected function execute(InputInterface $input, OutputInterface $output)
 138      {
 139          $this->input = $input;
 140          $this->output = $output;
 141          $this->io = new SymfonyStyle($input, $output);
 142  
 143          if (!$this->reparse_lock->acquire())
 144          {
 145              throw new runtime_exception('REPARSE_LOCK_ERROR', array(), null, 1);
 146          }
 147  
 148          $name = $input->getArgument('reparser-name');
 149          if ($name)
 150          {
 151              $name = $this->reparser_manager->find_reparser($name);
 152              $this->reparse($name);
 153          }
 154          else
 155          {
 156              foreach ($this->reparsers as $name => $service)
 157              {
 158                  $this->reparse($name);
 159              }
 160          }
 161  
 162          $this->io->success($this->user->lang('CLI_REPARSER_REPARSE_SUCCESS'));
 163  
 164          $this->reparse_lock->release();
 165  
 166          return 0;
 167      }
 168  
 169      /**
 170      * Get an option value, adjusted for given reparser
 171      *
 172      * Will use the last saved value if --resume is set and the option was not specified
 173      * on the command line
 174      *
 175      * @param  string  $option_name   Option name
 176      * @return integer
 177      */
 178  	protected function get_option($option_name)
 179      {
 180          // Return the option from the resume_data if applicable
 181          if ($this->input->getOption('resume') && isset($this->resume_data[$option_name]) && !$this->input->hasParameterOption('--' . $option_name))
 182          {
 183              return $this->resume_data[$option_name];
 184          }
 185  
 186          return $this->input->getOption($option_name);
 187      }
 188  
 189      /**
 190      * Reparse all text handled by given reparser within given range
 191      *
 192      * @param string $name Reparser service name
 193      */
 194  	protected function reparse($name)
 195      {
 196          $reparser = $this->reparsers[$name];
 197          $this->resume_data = $this->reparser_manager->get_resume_data($name);
 198          if ($this->input->getOption('dry-run'))
 199          {
 200              $reparser->disable_save();
 201          }
 202          else
 203          {
 204              $reparser->enable_save();
 205          }
 206  
 207          // Start at range-max if specified or at the highest ID otherwise
 208          $max  = $this->get_option('range-max');
 209          $min  = $this->get_option('range-min');
 210          $size = $this->get_option('range-size');
 211  
 212          // range-max has no default value, it must be computed for each reparser
 213          if ($max === null)
 214          {
 215              $max = $reparser->get_max_id();
 216          }
 217  
 218          if ($max < $min)
 219          {
 220              return;
 221          }
 222  
 223          $this->io->section($this->user->lang('CLI_REPARSER_REPARSE_REPARSING', $reparser->get_name(), $min, $max));
 224  
 225          $progress = $this->create_progress_bar($max, $this->io, $this->output, true);
 226          $progress->setMessage($this->user->lang('CLI_REPARSER_REPARSE_REPARSING_START', $reparser->get_name()));
 227          $progress->start();
 228  
 229          // Start from $max and decrement $current by $size until we reach $min
 230          $current = $max;
 231  
 232          $force_bbcode_reparsing = (bool) $this->get_option('force-bbcode-reparsing');
 233          while ($current >= $min)
 234          {
 235              $start = max($min, $current + 1 - $size);
 236              $end   = max($min, $current);
 237  
 238              $progress->setMessage($this->user->lang('CLI_REPARSER_REPARSE_REPARSING', $reparser->get_name(), $start, $end));
 239              $reparser->reparse_range($start, $end, $force_bbcode_reparsing);
 240  
 241              $current = $start - 1;
 242              $progress->setProgress($max + 1 - $start);
 243  
 244              $this->reparser_manager->update_resume_data($name, $min, $current, $size, !$this->input->getOption('dry-run'));
 245          }
 246          $progress->finish();
 247  
 248          $this->io->newLine(2);
 249      }
 250  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1