/cli/finder_indexer.php

https://github.com/gnomeontherun/joomla-cms · PHP · 196 lines · 75 code · 39 blank · 82 comment · 4 complexity · 7e18206acb125c7fc54072826ff2b4e3 MD5 · raw file

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