27 error_reporting(E_ALL);
28 if (ini_get(
'memory_limit') !=
'-1') ini_set(
'memory_limit',
'-1');
30 if ((php_sapi_name() !=
'cli')) {
31 trigger_error(
"You can only run this script from the command line\n", E_USER_ERROR);
34 $SYSTEM_ROOT = (isset($_SERVER[
'argv'][1])) ? $_SERVER[
'argv'][1] :
'';
37 $SYSTEM_ROOT = (isset($_SERVER[
'argv'][1])) ? $_SERVER[
'argv'][1] :
'';
38 if (empty($SYSTEM_ROOT)) {
39 echo
"ERROR: You need to supply the path to the System Root as the first argument\n";
44 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.
'/core/include/init.inc')) {
45 echo
"ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
51 define(
'SYNC_FILE', $SYSTEM_ROOT.
'/data/temp/reindex_search.assetids');
53 $ROOT_NODES = (isset($_SERVER[
'argv'][2])) ? $_SERVER[
'argv'][2] : 1;
54 $root_node_ids = array_unique(explode(
',', trim($ROOT_NODES,
' ,')));
55 if (in_array(1, $root_node_ids)) {
56 echo
"Do you want to reindex the whole system (yes/no) ";
57 $process = trim(fgets(STDIN, 4094));
58 if (trim(strtolower($process)) !=
'yes') {
59 echo
"Aborting script.\n";
64 $root_node_ids = array(1);
68 $BATCH_SIZE = (isset($_SERVER[
'argv'][3])) ? $_SERVER[
'argv'][3] : 100;
69 if ($BATCH_SIZE <= 0) {
80 require_once $SYSTEM_ROOT.
'/core/include/init.inc';
82 $root_user = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'root_user');
83 $GLOBALS[
'SQ_SYSTEM']->setCurrentUser($root_user);
86 $sm = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'search_manager');
87 if (!$sm->attr(
'indexing')) {
88 echo
"Search indexing is not turned on.\n\n";
89 file_put_contents(SYNC_FILE, serialize(Array(
'assetids' => Array(),
'contextids' => Array())));
93 $all_assetids = Array();
94 foreach ($root_node_ids as $root_node_id) {
97 if (!$GLOBALS[
'SQ_SYSTEM']->am->getAssetInfo($root_node_id)) {
98 echo
"\nWARNING: The asset #".$root_node_id.
" is not valid assetid\n";
102 $all_assetids = array_merge($all_assetids, array_keys($GLOBALS[
'SQ_SYSTEM']->am->getChildren($root_node_id)));
103 $all_assetids[] = $root_node_id;
107 $all_assetids = array_unique($all_assetids);
108 $asset_count = count($all_assetids);
112 $batched_assetids = Array();
113 while($start_index < $asset_count) {
114 $batched_assetids[] = array_slice($all_assetids, $start_index, $BATCH_SIZE);
115 $start_index += $BATCH_SIZE;
117 $GLOBALS[
'SQ_SYSTEM']->restoreCurrentUser();
120 $contextids = array_keys($GLOBALS[
'SQ_SYSTEM']->getAllContexts());
123 file_put_contents(SYNC_FILE, serialize(Array(
'assetids' => $batched_assetids,
'contextids' => $contextids)));
130 $data = unserialize(file_get_contents(SYNC_FILE));
131 $batched_assetids = $data[
'assetids'];
132 $all_contextids = $data[
'contextids'];
134 echo
"Batch size: ".$BATCH_SIZE.
"\n";
135 echo
"Total assets to reindex: ".(count($batched_assetids, COUNT_RECURSIVE) - count($batched_assetids)).
"\n\n";
137 if (!empty($batched_assetids)) {
138 echo
"Reindexing ...";
140 foreach($batched_assetids as $assetids) {
145 require_once $SYSTEM_ROOT.
'/core/include/init.inc';
147 $root_user = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'root_user');
148 $GLOBALS[
'SQ_SYSTEM']->setCurrentUser($root_user);
150 $sm = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'search_manager');
153 foreach($all_contextids as $contextid) {
154 $GLOBALS[
'SQ_SYSTEM']->changeContext($contextid);
156 foreach($assetids as $assetid) {
157 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
158 if (is_null($asset)) {
161 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
162 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
164 $sm->reindexAsset($asset, Array(
'all'));
165 $sm->reindexAttributes($asset, Array(
'all'), TRUE);
166 $sm->reindexContents($asset, Array(
'all'), TRUE);
167 $sm->reindexMetadata($asset->id, Array(
'all'));
169 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
170 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
172 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($asset);
176 $GLOBALS[
'SQ_SYSTEM']->restoreContext();
179 $GLOBALS[
'SQ_SYSTEM']->restoreCurrentUser();
189 if (file_exists(SYNC_FILE)) {
207 $child_pid = pcntl_fork();
209 switch ($child_pid) {
211 trigger_error(
"Forking failed!");
219 pcntl_waitpid(-1, $status);
230 function printUsage()
232 echo
"Usage: php ".basename(__FILE__).
" <SYSTEM_ROOT> [ROOT_NODES] [BATCH_SIZE]\n\n";
233 echo
"\t<SYSTEM_ROOT>:\t The root directory of the Matrix system.\n";
234 echo
"\t[ROOT_NODES]:\t Comma seperated root node assetids to reindex. If ommited, whole system will be reindexed.\n";
235 echo
"\t[BATCH_SIZE]:\t Number of assets to include in a batch. Default size is 100\n\n";