Source for file indexer.json.php

Documentation is available at indexer.json.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_finder
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. // Register dependent classes.
  13. JLoader::register('FinderIndexer'JPATH_COMPONENT_ADMINISTRATOR '/helpers/indexer/indexer.php');
  14.  
  15. /**
  16.  * Indexer controller class for Finder.
  17.  *
  18.  * @package     Joomla.Administrator
  19.  * @subpackage  com_finder
  20.  * @since       2.5
  21.  */
  22. {
  23.     /**
  24.      * Method to start the indexer.
  25.      *
  26.      * @return  void 
  27.      *
  28.      * @since   2.5
  29.      */
  30.     public function start()
  31.     {
  32.         static $log;
  33.  
  34.         $params JComponentHelper::getParams('com_finder');
  35.  
  36.         if ($params->get('enable_logging''0'))
  37.         {
  38.             if ($log == null)
  39.             {
  40.                 $options['format''{DATE}\t{TIME}\t{LEVEL}\t{CODE}\t{MESSAGE}';
  41.                 $options['text_file''indexer.php';
  42.                 $log JLog::addLogger($options);
  43.             }
  44.         }
  45.  
  46.         // Log the start
  47.         JLog::add('Starting the indexer'JLog::INFO);
  48.  
  49.         // We don't want this form to be cached.
  50.         header('Pragma: no-cache');
  51.         header('Cache-Control: no-cache');
  52.         header('Expires: -1');
  53.  
  54.         // Check for a valid token. If invalid, send a 403 with the error message.
  55.         JSession::checkToken('request'or $this->sendResponse(new Exception(JText::_('JINVALID_TOKEN')403));
  56.  
  57.         // Put in a buffer to silence noise.
  58.         ob_start();
  59.  
  60.         // Reset the indexer state.
  61.         FinderIndexer::resetState();
  62.  
  63.         // Import the finder plugins.
  64.         JPluginHelper::importPlugin('finder');
  65.  
  66.         // Add the indexer language to JS
  67.         JText::script('COM_FINDER_AN_ERROR_HAS_OCCURRED');
  68.         JText::script('COM_FINDER_NO_ERROR_RETURNED');
  69.  
  70.         // Start the indexer.
  71.         try
  72.         {
  73.             // Trigger the onStartIndex event.
  74.             JEventDispatcher::getInstance()->trigger('onStartIndex');
  75.  
  76.             // Get the indexer state.
  77.             $state FinderIndexer::getState();
  78.             $state->start 1;
  79.  
  80.             // Send the response.
  81.             $this->sendResponse($state);
  82.         }
  83.         // Catch an exception and return the response.
  84.         catch (Exception $e)
  85.         {
  86.             $this->sendResponse($e);
  87.         }
  88.     }
  89.  
  90.     /**
  91.      * Method to run the next batch of content through the indexer.
  92.      *
  93.      * @return  void 
  94.      *
  95.      * @since   2.5
  96.      */
  97.     public function batch()
  98.     {
  99.         static $log;
  100.  
  101.         $params JComponentHelper::getParams('com_finder');
  102.  
  103.         if ($params->get('enable_logging''0'))
  104.         {
  105.             if ($log == null)
  106.             {
  107.                 $options['format''{DATE}\t{TIME}\t{LEVEL}\t{CODE}\t{MESSAGE}';
  108.                 $options['text_file''indexer.php';
  109.                 $log JLog::addLogger($options);
  110.             }
  111.         }
  112.  
  113.         // Log the start
  114.         JLog::add('Starting the indexer batch process'JLog::INFO);
  115.  
  116.         // We don't want this form to be cached.
  117.         header('Pragma: no-cache');
  118.         header('Cache-Control: no-cache');
  119.         header('Expires: -1');
  120.  
  121.         // Check for a valid token. If invalid, send a 403 with the error message.
  122.         JSession::checkToken('request'or $this->sendResponse(new Exception(JText::_('JINVALID_TOKEN')403));
  123.  
  124.         // Put in a buffer to silence noise.
  125.         ob_start();
  126.  
  127.         // Remove the script time limit.
  128.         @set_time_limit(0);
  129.  
  130.         // Get the indexer state.
  131.         $state FinderIndexer::getState();
  132.  
  133.         // Reset the batch offset.
  134.         $state->batchOffset 0;
  135.  
  136.         // Update the indexer state.
  137.         FinderIndexer::setState($state);
  138.  
  139.         // Import the finder plugins.
  140.         JPluginHelper::importPlugin('finder');
  141.  
  142.         /*
  143.          * We are going to swap out the raw document object with an HTML document
  144.          * in order to work around some plugins that don't do proper environment
  145.          * checks before trying to use HTML document functions.
  146.          */
  147.         $raw clone(JFactory::getDocument());
  148.         $lang JFactory::getLanguage();
  149.  
  150.         // Get the document properties.
  151.         $attributes array (
  152.             'charset'    => 'utf-8',
  153.             'lineend'    => 'unix',
  154.             'tab'        => '  ',
  155.             'language'    => $lang->getTag(),
  156.             'direction'    => $lang->isRTL('rtl' 'ltr'
  157.         );
  158.  
  159.         // Get the HTML document.
  160.         $html JDocument::getInstance('html'$attributes);
  161.         $doc JFactory::getDocument();
  162.  
  163.         // Swap the documents.
  164.         $doc $html;
  165.  
  166.         // Get the admin application.
  167.         $admin clone(JFactory::getApplication());
  168.  
  169.         // Get the site app.
  170.         $site JApplication::getInstance('site');
  171.  
  172.         // Swap the app.
  173.         $app JFactory::getApplication();
  174.         $app $site;
  175.  
  176.         // Start the indexer.
  177.         try
  178.         {
  179.             // Trigger the onBeforeIndex event.
  180.             JEventDispatcher::getInstance()->trigger('onBeforeIndex');
  181.  
  182.             // Trigger the onBuildIndex event.
  183.             JEventDispatcher::getInstance()->trigger('onBuildIndex');
  184.  
  185.             // Get the indexer state.
  186.             $state FinderIndexer::getState();
  187.             $state->start 0;
  188.             $state->complete 0;
  189.  
  190.             // Swap the documents back.
  191.             $doc $raw;
  192.  
  193.             // Swap the applications back.
  194.             $app $admin;
  195.  
  196.             // Send the response.
  197.             $this->sendResponse($state);
  198.         }
  199.         // Catch an exception and return the response.
  200.         catch (Exception $e)
  201.         {
  202.             // Swap the documents back.
  203.             $doc $raw;
  204.  
  205.             // Send the response.
  206.             $this->sendResponse($e);
  207.         }
  208.     }
  209.  
  210.     /**
  211.      * Method to optimize the index and perform any necessary cleanup.
  212.      *
  213.      * @return  void 
  214.      *
  215.      * @since   2.5
  216.      */
  217.     public function optimize()
  218.     {
  219.         // We don't want this form to be cached.
  220.         header('Pragma: no-cache');
  221.         header('Cache-Control: no-cache');
  222.         header('Expires: -1');
  223.  
  224.         // Check for a valid token. If invalid, send a 403 with the error message.
  225.         JSession::checkToken('request'or $this->sendResponse(new Exception(JText::_('JINVALID_TOKEN')403));
  226.  
  227.         // Put in a buffer to silence noise.
  228.         ob_start();
  229.  
  230.         // Import the finder plugins.
  231.         JPluginHelper::importPlugin('finder');
  232.  
  233.         try
  234.         {
  235.             // Optimize the index
  236.             FinderIndexer::getInstance()->optimize();
  237.  
  238.             // Get the indexer state.
  239.             $state FinderIndexer::getState();
  240.             $state->start 0;
  241.             $state->complete 1;
  242.  
  243.             // Send the response.
  244.             $this->sendResponse($state);
  245.         }
  246.         // Catch an exception and return the response.
  247.         catch (Exception $e)
  248.         {
  249.             $this->sendResponse($e);
  250.         }
  251.     }
  252.  
  253.     /**
  254.      * Method to handle a send a JSON response. The body parameter
  255.      * can be a Exception object for when an error has occurred or
  256.      * a JObject for a good response.
  257.      *
  258.      * @param   mixed  $data  JObject on success, Exception on error. [optional]
  259.      *
  260.      * @return  void 
  261.      *
  262.      * @since   2.5
  263.      */
  264.     public static function sendResponse($data null)
  265.     {
  266.         static $log;
  267.  
  268.         $params JComponentHelper::getParams('com_finder');
  269.  
  270.         if ($params->get('enable_logging''0'))
  271.         {
  272.             if ($log == null)
  273.             {
  274.                 $options['format''{DATE}\t{TIME}\t{LEVEL}\t{CODE}\t{MESSAGE}';
  275.                 $options['text_file''indexer.php';
  276.                 $log JLog::addLogger($options);
  277.             }
  278.         }
  279.  
  280.         // Send the assigned error code if we are catching an exception.
  281.         if ($data instanceof Exception)
  282.         {
  283.             $app JFactory::getApplication();
  284.             JLog::add($data->getMessage()JLog::ERROR);
  285.             $app->setHeader('status'$data->getCode());
  286.             $app->sendHeaders();
  287.         }
  288.  
  289.         // Create the response object.
  290.         $response new FinderIndexerResponse($data);
  291.  
  292.         // Add the buffer.
  293.         $response->buffer JDEBUG ob_get_contents(ob_end_clean();
  294.  
  295.         // Send the JSON response.
  296.         echo json_encode($response);
  297.  
  298.         // Close the application.
  299.         JFactory::getApplication()->close();
  300.     }
  301. }
  302.  
  303. /**
  304.  * Finder Indexer JSON Response Class
  305.  *
  306.  * @package     Joomla.Administrator
  307.  * @subpackage  com_finder
  308.  * @since       2.5
  309.  */
  310. {
  311.     /**
  312.      * Class Constructor
  313.      *
  314.      * @param   mixed  $state  The processing state for the indexer
  315.      *
  316.      * @since   2.5
  317.      */
  318.     public function __construct($state)
  319.     {
  320.         static $log;
  321.  
  322.         $params JComponentHelper::getParams('com_finder');
  323.  
  324.         if ($params->get('enable_logging''0'))
  325.         {
  326.             if ($log == null)
  327.             {
  328.                 $options['format''{DATE}\t{TIME}\t{LEVEL}\t{CODE}\t{MESSAGE}';
  329.                 $options['text_file''indexer.php';
  330.                 $log JLog::addLogger($options);
  331.             }
  332.         }
  333.  
  334.         // The old token is invalid so send a new one.
  335.         $this->token JFactory::getSession()->getFormToken();
  336.  
  337.         // Check if we are dealing with an error.
  338.         if ($state instanceof Exception)
  339.         {
  340.             // Log the error
  341.             JLog::add($state->getMessage()JLog::ERROR);
  342.  
  343.             // Prepare the error response.
  344.             $this->error true;
  345.             $this->header JText::_('COM_FINDER_INDEXER_HEADER_ERROR');
  346.             $this->message $state->getMessage();
  347.         }
  348.         else
  349.         {
  350.             // Prepare the response data.
  351.             $this->batchSize = (int) $state->batchSize;
  352.             $this->batchOffset = (int) $state->batchOffset;
  353.             $this->totalItems = (int) $state->totalItems;
  354.  
  355.             $this->startTime $state->startTime;
  356.             $this->endTime JFactory::getDate()->toSQL();
  357.  
  358.             $this->start !empty($state->start? (int) $state->start 0;
  359.             $this->complete !empty($state->complete? (int) $state->complete 0;
  360.  
  361.             // Set the appropriate messages.
  362.             if ($this->totalItems <= && $this->complete)
  363.             {
  364.                 $this->header JText::_('COM_FINDER_INDEXER_HEADER_COMPLETE');
  365.                 $this->message JText::_('COM_FINDER_INDEXER_MESSAGE_COMPLETE');
  366.             }
  367.             elseif ($this->totalItems <= 0)
  368.             {
  369.                 $this->header JText::_('COM_FINDER_INDEXER_HEADER_OPTIMIZE');
  370.                 $this->message JText::_('COM_FINDER_INDEXER_MESSAGE_OPTIMIZE');
  371.             }
  372.             else
  373.             {
  374.                 $this->header JText::_('COM_FINDER_INDEXER_HEADER_RUNNING');
  375.                 $this->message JText::_('COM_FINDER_INDEXER_MESSAGE_RUNNING');
  376.             }
  377.         }
  378.     }
  379. }
  380.  
  381. // Register the error handler.
  382. JError::setErrorHandling(E_ALL'callback'array('FinderControllerIndexer''sendResponse'));

Documentation generated on Tue, 19 Nov 2013 15:05:29 +0100 by phpDocumentor 1.4.3