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