Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
hipo_job_reindex.inc
1 <?php
17 require_once SQ_SYSTEM_ROOT.'/core/hipo/hipo_job.inc';
18 
32 {
33 
34 
40  function HIPO_Job_ReIndex($code_name='')
41  {
42  $this->uses_trans = FALSE;
43  $this->HIPO_Job($code_name);
44  $GLOBALS['SQ_SYSTEM']->lm->includePackageStrings('search');
45  $this->_hipo_vars['job_dir'] = SQ_PACKAGES_PATH.'/search/hipo_jobs';
46 
47  }//end constructor
48 
49 
59  public static function paintConfig(&$o, $class, $write_access)
60  {
61  $o->openField(translate('sch_hipo_reindex_threshold'));
62 
63  if ($write_access) {
64  text_box($class.'[SQ_HIPO_REINDEX_THRESHOLD]', SQ_HIPO_REINDEX_THRESHOLD, 5);
65  } else {
66  echo SQ_HIPO_REINDEX_THRESHOLD;
67  }
68  echo ' '.translate('assets');
69 
70  $o->closeField();
71 
72  }//end paintConfig()
73 
74 
81  public static function getConfigVars()
82  {
83  return Array(
84  'SQ_HIPO_REINDEX_THRESHOLD' => Array('editable' => 1, 'default' => 5),
85  );
86 
87  }//end getConfigVars()
88 
89 
100  {
101  if (SQ_HIPO_REINDEX_THRESHOLD == 0) return 0;
102  if (!isset($this->_running_vars['todo_assetids'])) {
103  return 0;
104  }
105 
106  return ((count($this->_running_vars['todo_assetids']) / SQ_HIPO_REINDEX_THRESHOLD) * 100);
107 
108  }//end getThresholdPercentageRequired()
109 
110 
120  function getCodeName()
121  {
122  return parent::getCodeName().'-'. htmlspecialchars(implode(',', $this->_running_vars['root_assetid']));
123 
124  }//end getCodeName()
125 
126 
133  function getHipoName()
134  {
135  return translate('sch_hipo_name_reindex_assets');
136 
137  }//end getHipoName()
138 
139 
148  {
149  return Array(
150  Array(
151  'name' => translate('sch_hipo_reindexing_assets'),
152  'function_call' => Array(
153  'process_function' => 'processReIndex',
154  ),
155  'running_mode' => 'server',
156  'auto_step' => TRUE,
157  'percent_done' => 0,
158  'complete' => FALSE,
159  'message' => '',
160  'allow_cancel' => TRUE,
161  ),
162  );
163 
164  }//end getInitialStepData()
165 
166 
173  function freestyle()
174  {
175  // Put the HIPO in some context ;)
176  if (array_get_index('contextid', $this->_running_vars)) {
177  $this->_running_vars['contextid'] = $GLOBALS['SQ_SYSTEM']->getContextId();
178  }
179 
180  while (!empty($this->_running_vars['todo_assetids'])) {
181  if (!$this->processReIndex($this->_steps[0], get_class_lower($this))) {
182  return FALSE;
183  }
184  }
185  return TRUE;
186 
187  }//end freestyle()
188 
189 
196  function prepare()
197  {
198  // to start regenerating metadata, we need to have a root assetid - default to root folder if none is supplied
199  if (is_null($this->_running_vars['root_assetid']) || empty($this->_running_vars['root_assetid'])) {
200  $this->_running_vars['root_assetid'] = 1; // the root folder
201  }
202 
203  if (!is_array($this->_running_vars['root_assetid'])) {
204  $this->_running_vars['root_assetid'] = Array($this->_running_vars['root_assetid']);
205  }
206 
207  // If we have not been told which context to re-index, assume we're in
208  // it already (but this is not recommended if not freestyling, since it
209  // could be stuck somewhere where the context you want is not set)
210  if (array_get_index('contextid', $this->_running_vars)) {
211  $this->_running_vars['contextid'] = $GLOBALS['SQ_SYSTEM']->getContextId();
212  }
213 
214  $todo_assetids = Array();
215  foreach ($this->_running_vars['root_assetid'] as $assetid) {
216  // so we have an assetid to start at, but make sure it is a valid assetid
217  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
218  if (is_null($asset)) {
219  trigger_localised_error('SCH0022', E_USER_WARNING, $assetid);
220  return FALSE;
221  }
222 
223  if (!empty($this->_running_vars['root_only']) && $this->_running_vars['root_only']) {
224  $child_assets = $GLOBALS['SQ_SYSTEM']->am->getDependantChildren($asset->id);
225  } else {
226  $child_assets = $GLOBALS['SQ_SYSTEM']->am->getChildren($asset->id);
227  }
228  $todo_assetids += $child_assets;
229  $todo_assetids[$asset->id][0]['type_code'] = $asset->type();
230 
231  }//end foreach
232 
233  $this->_running_vars['todo_assetids'] = $todo_assetids;
234  $this->_running_vars['done_assetids'] = Array();
235 
236  return parent::prepare();
237 
238  }//end prepare()
239 
240 
250  function processReIndex(&$step_data, $prefix)
251  {
252  if (!empty($this->_running_vars['todo_assetids'])) {
253  $GLOBALS['SQ_SYSTEM']->changeContext($this->_running_vars['contextid']);
254 
255  $am =& $GLOBALS['SQ_SYSTEM']->am;
256  $sm = $am->getSystemAsset('search_manager');
257  if (is_null($sm)) return FALSE;
258 
259  // get next asset to reindex
260  reset($this->_running_vars['todo_assetids']);
261  $assetid = key($this->_running_vars['todo_assetids']);
262  $asset_type = $this->_running_vars['todo_assetids'][$assetid][0]['type_code'];
263  unset($this->_running_vars['todo_assetids'][$assetid]);
264  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, $asset_type);
265 
266  if ($asset) {
267  $step_data['message'] = translate('sch_reindexing_asset', $asset->name);
268  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
269  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
270  $sm->reindexAsset($asset, Array('all'));
271  $sm->reindexAttributes($asset, Array('all'), TRUE);
272  $sm->reindexContents($asset, Array('all'), TRUE);
273  $sm->reindexMetadata($asset->id, Array('all'));
274  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
275  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
276  }
277 
278  // add this assetid to the done array
279  $this->_running_vars['done_assetids'][] = $assetid;
280 
281  $GLOBALS['SQ_SYSTEM']->restoreContext();
282  }
283 
284  if (empty($this->_running_vars['todo_assetids'])) {
285  $step_data['percent_done'] = 100;
286  $step_data['complete'] = TRUE;
287  } else {
288  $total = count($this->_running_vars['todo_assetids']) + count($this->_running_vars['done_assetids']);
289  $step_data['percent_done'] =(count($this->_running_vars['done_assetids']) / $total) * 100;
290  $step_data['complete'] = FALSE;
291  }
292 
293  return TRUE;
294 
295  }//end processReIndex()
296 
297 
298 }//end class
299 
300 ?>