[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_finder/src/Controller/ -> IndexerController.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_finder
   6   *
   7   * @copyright   (C) 2011 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Finder\Administrator\Controller;
  12  
  13  use Joomla\CMS\Component\ComponentHelper;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\Log\Log;
  17  use Joomla\CMS\MVC\Controller\BaseController;
  18  use Joomla\CMS\Plugin\PluginHelper;
  19  use Joomla\CMS\Session\Session;
  20  use Joomla\Component\Finder\Administrator\Indexer\Indexer;
  21  use Joomla\Component\Finder\Administrator\Response\Response;
  22  
  23  // phpcs:disable PSR1.Files.SideEffects
  24  \defined('_JEXEC') or die;
  25  // phpcs:enable PSR1.Files.SideEffects
  26  
  27  /**
  28   * Indexer controller class for Finder.
  29   *
  30   * @since  2.5
  31   */
  32  class IndexerController extends BaseController
  33  {
  34      /**
  35       * Method to start the indexer.
  36       *
  37       * @return  void
  38       *
  39       * @since   2.5
  40       */
  41      public function start()
  42      {
  43          // Check for a valid token. If invalid, send a 403 with the error message.
  44          if (!Session::checkToken('request')) {
  45              static::sendResponse(new \Exception(Text::_('JINVALID_TOKEN_NOTICE'), 403));
  46  
  47              return;
  48          }
  49  
  50          $params = ComponentHelper::getParams('com_finder');
  51  
  52          if ($params->get('enable_logging', '0')) {
  53              $options['format'] = '{DATE}\t{TIME}\t{LEVEL}\t{CODE}\t{MESSAGE}';
  54              $options['text_file'] = 'indexer.php';
  55              Log::addLogger($options);
  56          }
  57  
  58          // Log the start
  59          try {
  60              Log::add('Starting the indexer', Log::INFO);
  61          } catch (\RuntimeException $exception) {
  62              // Informational log only
  63          }
  64  
  65          // We don't want this form to be cached.
  66          $this->app->allowCache(false);
  67  
  68          // Put in a buffer to silence noise.
  69          ob_start();
  70  
  71          // Reset the indexer state.
  72          Indexer::resetState();
  73  
  74          // Import the finder plugins.
  75          PluginHelper::importPlugin('finder');
  76  
  77          // Add the indexer language to \JS
  78          Text::script('COM_FINDER_AN_ERROR_HAS_OCCURRED');
  79          Text::script('COM_FINDER_NO_ERROR_RETURNED');
  80  
  81          // Start the indexer.
  82          try {
  83              // Trigger the onStartIndex event.
  84              $this->app->triggerEvent('onStartIndex');
  85  
  86              // Get the indexer state.
  87              $state = Indexer::getState();
  88              $state->start = 1;
  89  
  90              // Send the response.
  91              static::sendResponse($state);
  92          } catch (\Exception $e) {
  93              // Catch an exception and return the response.
  94              static::sendResponse($e);
  95          }
  96      }
  97  
  98      /**
  99       * Method to run the next batch of content through the indexer.
 100       *
 101       * @return  void
 102       *
 103       * @since   2.5
 104       */
 105      public function batch()
 106      {
 107          // Check for a valid token. If invalid, send a 403 with the error message.
 108          if (!Session::checkToken('request')) {
 109              static::sendResponse(new \Exception(Text::_('JINVALID_TOKEN_NOTICE'), 403));
 110  
 111              return;
 112          }
 113  
 114          $params = ComponentHelper::getParams('com_finder');
 115  
 116          if ($params->get('enable_logging', '0')) {
 117              $options['format'] = '{DATE}\t{TIME}\t{LEVEL}\t{CODE}\t{MESSAGE}';
 118              $options['text_file'] = 'indexer.php';
 119              Log::addLogger($options);
 120          }
 121  
 122          // Log the start
 123          try {
 124              Log::add('Starting the indexer batch process', Log::INFO);
 125          } catch (\RuntimeException $exception) {
 126              // Informational log only
 127          }
 128  
 129          // We don't want this form to be cached.
 130          $this->app->allowCache(false);
 131  
 132          // Put in a buffer to silence noise.
 133          ob_start();
 134  
 135          // Remove the script time limit.
 136          @set_time_limit(0);
 137  
 138          // Get the indexer state.
 139          $state = Indexer::getState();
 140  
 141          // Reset the batch offset.
 142          $state->batchOffset = 0;
 143  
 144          // Update the indexer state.
 145          Indexer::setState($state);
 146  
 147          // Import the finder plugins.
 148          PluginHelper::importPlugin('finder');
 149  
 150          /*
 151           * We are going to swap out the raw document object with an HTML document
 152           * in order to work around some plugins that don't do proper environment
 153           * checks before trying to use HTML document functions.
 154           */
 155          $lang = Factory::getLanguage();
 156  
 157          // Get the document properties.
 158          $attributes = array (
 159              'charset'   => 'utf-8',
 160              'lineend'   => 'unix',
 161              'tab'       => '  ',
 162              'language'  => $lang->getTag(),
 163              'direction' => $lang->isRtl() ? 'rtl' : 'ltr'
 164          );
 165  
 166          // Start the indexer.
 167          try {
 168              // Trigger the onBeforeIndex event.
 169              $this->app->triggerEvent('onBeforeIndex');
 170  
 171              // Trigger the onBuildIndex event.
 172              $this->app->triggerEvent('onBuildIndex');
 173  
 174              // Get the indexer state.
 175              $state = Indexer::getState();
 176              $state->start = 0;
 177              $state->complete = 0;
 178  
 179              // Log batch completion and memory high-water mark.
 180              try {
 181                  Log::add('Batch completed, peak memory usage: ' . number_format(memory_get_peak_usage(true)) . ' bytes', Log::INFO);
 182              } catch (\RuntimeException $exception) {
 183                  // Informational log only
 184              }
 185  
 186              // Send the response.
 187              static::sendResponse($state);
 188          } catch (\Exception $e) {
 189              // Catch an exception and return the response.
 190              // Send the response.
 191              static::sendResponse($e);
 192          }
 193      }
 194  
 195      /**
 196       * Method to optimize the index and perform any necessary cleanup.
 197       *
 198       * @return  void
 199       *
 200       * @since   2.5
 201       */
 202      public function optimize()
 203      {
 204          // Check for a valid token. If invalid, send a 403 with the error message.
 205          if (!Session::checkToken('request')) {
 206              static::sendResponse(new \Exception(Text::_('JINVALID_TOKEN_NOTICE'), 403));
 207  
 208              return;
 209          }
 210  
 211          // We don't want this form to be cached.
 212          $this->app->allowCache(false);
 213  
 214          // Put in a buffer to silence noise.
 215          ob_start();
 216  
 217          // Import the finder plugins.
 218          PluginHelper::importPlugin('finder');
 219  
 220          try {
 221              // Optimize the index
 222              $indexer = new Indexer();
 223              $indexer->optimize();
 224  
 225              // Get the indexer state.
 226              $state = Indexer::getState();
 227              $state->start = 0;
 228              $state->complete = 1;
 229  
 230              // Send the response.
 231              static::sendResponse($state);
 232          } catch (\Exception $e) {
 233              // Catch an exception and return the response.
 234              static::sendResponse($e);
 235          }
 236      }
 237  
 238      /**
 239       * Method to handle a send a \JSON response. The body parameter
 240       * can be an \Exception object for when an error has occurred or
 241       * a CMSObject for a good response.
 242       *
 243       * @param   \Joomla\CMS\Object\CMSObject|\Exception  $data  CMSObject on success, \Exception on error. [optional]
 244       *
 245       * @return  void
 246       *
 247       * @since   2.5
 248       */
 249      public static function sendResponse($data = null)
 250      {
 251          $app = Factory::getApplication();
 252  
 253          $params = ComponentHelper::getParams('com_finder');
 254  
 255          if ($params->get('enable_logging', '0')) {
 256              $options['format'] = '{DATE}\t{TIME}\t{LEVEL}\t{CODE}\t{MESSAGE}';
 257              $options['text_file'] = 'indexer.php';
 258              Log::addLogger($options);
 259          }
 260  
 261          // Send the assigned error code if we are catching an exception.
 262          if ($data instanceof \Exception) {
 263              try {
 264                  Log::add($data->getMessage(), Log::ERROR);
 265              } catch (\RuntimeException $exception) {
 266                  // Informational log only
 267              }
 268  
 269              $app->setHeader('status', $data->getCode());
 270          }
 271  
 272          // Create the response object.
 273          $response = new Response($data);
 274  
 275          if (\JDEBUG) {
 276              // Add the buffer and memory usage
 277              $response->buffer = ob_get_contents();
 278              $response->memory = memory_get_usage(true);
 279          }
 280  
 281          // Send the JSON response.
 282          echo json_encode($response);
 283      }
 284  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer