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