Source for file finder_indexer.php

Documentation is available at finder_indexer.php

  1. <?php
  2. /**
  3.  * @package    Joomla.Cli
  4.  *
  5.  * @copyright  Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  6.  * @license    GNU General Public License version 2 or later; see LICENSE.txt
  7.  */
  8.  
  9. /**
  10.  * Finder CLI Bootstrap
  11.  *
  12.  * Run the framework bootstrap with a couple of mods based on the script's needs
  13.  */
  14.  
  15. // We are a valid entry point.
  16. const _JEXEC = 1;
  17.  
  18. // Load system defines
  19. if (file_exists(dirname(__DIR__) . '/defines.php'))
  20. {
  21.     require_once dirname(__DIR__) . '/defines.php';
  22. }
  23.  
  24. if (!defined('_JDEFINES'))
  25. {
  26.     define('JPATH_BASE', dirname(__DIR__));
  27.     require_once JPATH_BASE . '/includes/defines.php';
  28. }
  29.  
  30. // Get the framework.
  31. require_once JPATH_LIBRARIES . '/import.legacy.php';
  32.  
  33. // Bootstrap the CMS libraries.
  34. require_once JPATH_LIBRARIES . '/cms.php';
  35.  
  36. // Import the configuration.
  37. require_once JPATH_CONFIGURATION . '/configuration.php';
  38.  
  39. // System configuration.
  40. $config = new JConfig;
  41.  
  42. // Configure error reporting to maximum for CLI output.
  43. ini_set('display_errors', 1);
  44.  
  45. // Load Library language
  46. $lang = JFactory::getLanguage();
  47.  
  48. // Try the finder_cli file in the current language (without allowing the loading of the file in the default language)
  49. $lang->load('finder_cli', JPATH_SITE, null, false, false)
  50. // Fallback to the finder_cli file in the default language
  51. || $lang->load('finder_cli', JPATH_SITE, null, true);
  52.  
  53. /**
  54.  * A command line cron job to run the Finder indexer.
  55.  *
  56.  * @package     Joomla.CLI
  57.  * @subpackage  com_finder
  58.  * @since       2.5
  59.  */
  60. class FinderCli extends JApplicationCli
  61. {
  62.     /**
  63.      * Start time for the index process
  64.      *
  65.      * @var    string 
  66.      * @since  2.5
  67.      */
  68.     private $_time = null;
  69.  
  70.     /**
  71.      * Start time for each batch
  72.      *
  73.      * @var    string 
  74.      * @since  2.5
  75.      */
  76.     private $_qtime = null;
  77.  
  78.     /**
  79.      * Entry point for Finder CLI script
  80.      *
  81.      * @return  void 
  82.      *
  83.      * @since   2.5
  84.      */
  85.     public function doExecute()
  86.     {
  87.         // Print a blank line.
  88.         $this->out(JText::_('FINDER_CLI'));
  89.         $this->out('============================');
  90.         $this->out();
  91.  
  92.         $this->_index();
  93.  
  94.         // Print a blank line at the end.
  95.         $this->out();
  96.     }
  97.  
  98.     /**
  99.      * Run the indexer
  100.      *
  101.      * @return  void 
  102.      *
  103.      * @since   2.5
  104.      */
  105.     private function _index()
  106.     {
  107.         $this->_time = microtime(true);
  108.  
  109.         require_once JPATH_ADMINISTRATOR . '/components/com_finder/helpers/indexer/indexer.php';
  110.  
  111.         // Fool the system into thinking we are running as JSite with Finder as the active component
  112.         JFactory::getApplication('site');
  113.         $_SERVER['HTTP_HOST'] = 'domain.com';
  114.         define('JPATH_COMPONENT_ADMINISTRATOR', JPATH_ADMINISTRATOR . '/components/com_finder');
  115.  
  116.         // Disable caching.
  117.         $config = JFactory::getConfig();
  118.         $config->set('caching', 0);
  119.         $config->set('cache_handler', 'file');
  120.  
  121.         // Reset the indexer state.
  122.         FinderIndexer::resetState();
  123.  
  124.         // Import the finder plugins.
  125.         JPluginHelper::importPlugin('finder');
  126.  
  127.         // Starting Indexer.
  128.         $this->out(JText::_('FINDER_CLI_STARTING_INDEXER'), true);
  129.  
  130.         // Trigger the onStartIndex event.
  131.         JEventDispatcher::getInstance()->trigger('onStartIndex');
  132.  
  133.         // Remove the script time limit.
  134.         @set_time_limit(0);
  135.  
  136.         // Get the indexer state.
  137.         $state = FinderIndexer::getState();
  138.  
  139.         // Setting up plugins.
  140.         $this->out(JText::_('FINDER_CLI_SETTING_UP_PLUGINS'), true);
  141.  
  142.         // Trigger the onBeforeIndex event.
  143.         JEventDispatcher::getInstance()->trigger('onBeforeIndex');
  144.  
  145.         // Startup reporting.
  146.         $this->out(JText::sprintf('FINDER_CLI_SETUP_ITEMS', $state->totalItems, round(microtime(true) - $this->_time, 3)), true);
  147.  
  148.         // Get the number of batches.
  149.         $t = (int) $state->totalItems;
  150.         $c = (int) ceil($t / $state->batchSize);
  151.         $c = $c === 0 ? 1 : $c;
  152.  
  153.         try
  154.         {
  155.             // Process the batches.
  156.             for ($i = 0; $i < $c; $i++)
  157.             {
  158.                 // Set the batch start time.
  159.                 $this->_qtime = microtime(true);
  160.  
  161.                 // Reset the batch offset.
  162.                 $state->batchOffset = 0;
  163.  
  164.                 // Trigger the onBuildIndex event.
  165.                 JEventDispatcher::getInstance()->trigger('onBuildIndex');
  166.  
  167.                 // Batch reporting.
  168.                 $this->out(JText::sprintf('FINDER_CLI_BATCH_COMPLETE', ($i + 1), round(microtime(true) - $this->_qtime, 3)), true);
  169.             }
  170.         }
  171.         catch (Exception $e)
  172.         {
  173.             // Display the error
  174.             $this->out($e->getMessage(), true);
  175.  
  176.             // Reset the indexer state.
  177.             FinderIndexer::resetState();
  178.  
  179.             // Close the app
  180.             $this->close($e->getCode());
  181.         }
  182.  
  183.         // Total reporting.
  184.         $this->out(JText::sprintf('FINDER_CLI_PROCESS_COMPLETE', round(microtime(true) - $this->_time, 3)), true);
  185.  
  186.         // Reset the indexer state.
  187.         FinderIndexer::resetState();
  188.     }
  189. }
  190.  
  191. // Instantiate the application object, passing the class name to JCli::getInstance
  192. // and use chaining to execute the application.

Documentation generated on Tue, 19 Nov 2013 15:03:24 +0100 by phpDocumentor 1.4.3