PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_finder/controllers/indexer.json.php

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