Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
hipo_job_funnelback_rebuild_cache.inc
1 <?php
18 require_once SQ_SYSTEM_ROOT.'/core/hipo/hipo_job.inc';
19 
32 {
33 
34 
40  function HIPO_Job_Rebuild_Cache($code_name='')
41  {
42  $this->uses_trans = FALSE;
43  $this->HIPO_Job($code_name);
44  $GLOBALS['SQ_SYSTEM']->lm->includePackageStrings('funnelback');
45  $this->_hipo_vars['job_dir'] = SQ_PACKAGES_PATH.'/funnelback/hipo_jobs';
46 
47  }//end constructor
48 
49 
59  public static function paintConfig(&$o, $class, $write_access)
60  {
61  $o->openField(translate('fnb_hipo_rebuild_cache'));
62 
63  if ($write_access) {
64  text_box($class.'[SQ_HIPO_REBUILD_FUNNELBACK_CACHE]', SQ_HIPO_REBUILD_FUNNELBACK_CACHE, 5);
65  } else {
66  echo SQ_HIPO_REBUILD_FUNNELBACK_CACHE;
67  }
68 
69  $o->closeField();
70 
71  }//end paintConfig()
72 
73 
80  public static function getConfigVars()
81  {
82  return Array(
83  'SQ_HIPO_REBUILD_FUNNELBACK_CACHE' => Array('editable' => 1, 'default' => 5),
84  );
85 
86  }//end getConfigVars()
87 
88 
99  {
100  return 0;
101 
102  }//end getThresholdPercentageRequired()
103 
104 
114  function getCodeName()
115  {
116  return parent::getCodeName().'-'.implode(',', $this->_running_vars['root_assetid']);
117 
118  }//end getCodeName()
119 
120 
127  function getHipoName()
128  {
129  return translate('fnb_hipo_rebuild_cache');
130 
131  }//end getHipoName()
132 
133 
142  {
143  return Array(
144  Array(
145  'name' => translate('fnb_hipo_rebuild_cache_step_name0'),
146  'function_call' => Array(
147  'process_function' => 'processFlushTable',
148  ),
149  'running_mode' => 'server',
150  'auto_step' => TRUE,
151  'percent_done' => 0,
152  'complete' => FALSE,
153  'message' => '',
154  'allow_cancel' => TRUE,
155  ),
156  Array(
157  'name' => translate('fnb_hipo_rebuild_cache_step_name1'),
158  'function_call' => Array(
159  'process_function' => 'processRebuild',
160  ),
161  'running_mode' => 'server',
162  'auto_step' => TRUE,
163  'percent_done' => 0,
164  'complete' => FALSE,
165  'message' => '',
166  'allow_cancel' => TRUE,
167  ),
168  );
169 
170  }//end getInitialStepData()
171 
172 
179  function freestyle()
180  {
181  // Put the HIPO in some context ;)
182  if (array_get_index('contextid', $this->_running_vars)) {
183  $this->_running_vars['contextid'] = $GLOBALS['SQ_SYSTEM']->getContextId();
184  }
185 
186  // Empty the existing table
187  $this->processFlushTable($this->_steps[0], get_class_lower($this));
188 
189  while (!empty($this->_running_vars['todo_assetids'])) {
190  if (!$this->processRebuild($this->_steps[0], get_class_lower($this))) {
191  return FALSE;
192  }
193  }
194 
195  return TRUE;
196 
197  }//end freestyle()
198 
199 
206  function prepare()
207  {
208  // to start rebuilding the table, we need to have a root assetid - default to root folder if none is supplied
209  if (is_null($this->_running_vars['root_assetid']) || empty($this->_running_vars['root_assetid'])) {
210  $this->_running_vars['root_assetid'] = 1; // the root folder
211  }
212 
213  if (!is_array($this->_running_vars['root_assetid'])) {
214  $this->_running_vars['root_assetid'] = Array($this->_running_vars['root_assetid']);
215  }
216 
217  // If we have not been told which context to re-index, assume we're in
218  // it already (but this is not recommended if not freestyling, since it
219  // could be stuck somewhere where the context you want is not set)
220  if (array_get_index('contextid', $this->_running_vars)) {
221  $this->_running_vars['contextid'] = $GLOBALS['SQ_SYSTEM']->getContextId();
222  }
223 
224  $this->_running_vars['todo_assetids'] = Array();
225  $this->_running_vars['done_assetids'] = Array();
226  foreach ($this->_running_vars['root_assetid'] as $assetid) {
227  // so we have an assetid to start at, but make sure it is a valid assetid
228  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
229  if (is_null($asset)) {
230  trigger_localised_error('FNB0022', E_USER_WARNING, $assetid);
231  return FALSE;
232  }
233 
234  $child_assets = $GLOBALS['SQ_SYSTEM']->am->getChildren($asset->id);
235  $child_assets[$asset->id][0]['type_code'] = $asset->type();
236  $this->_running_vars['todo_assetids'] += $child_assets;
237  }//end foreach
238 
239  return parent::prepare();
240 
241  }//end prepare()
242 
243 
253  function processFlushTable(&$step_data, $prefix)
254  {
255  // If this hipo is running against the root folder, then flush the whole table, otherwise let update asset handle the deleting
256  if (in_array('1', $this->_running_vars['root_assetid'])) {
257  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
258  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
259  MatrixDAL::executeSql('DELETE FROM sq_fnb_idx');
260  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
261  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
262  }//end if
263  if ($step_data['percent_done'] == 0) {
264  $step_data['percent_done'] = 50;
265  } else {
266  $step_data['percent_done'] = 100;
267  $step_data['complete'] = TRUE;
268  }
269  return TRUE;
270 
271  }//end processFlushTable()
272 
273 
283  function processRebuild(&$step_data, $prefix)
284  {
285  if (!empty($this->_running_vars['todo_assetids'])) {
286  $GLOBALS['SQ_SYSTEM']->changeContext(array_get_index($this->_running_vars, 'contextid', 0));
287 
288  $am =& $GLOBALS['SQ_SYSTEM']->am;
289  $fm = $am->getSystemAsset('funnelback_manager');
290  if (is_null($fm)) return FALSE;
291 
292  // get next asset to rebuild
293  reset($this->_running_vars['todo_assetids']);
294  $assetid = key($this->_running_vars['todo_assetids']);
295  $asset_type = $this->_running_vars['todo_assetids'][$assetid][0]['type_code'];
296  unset($this->_running_vars['todo_assetids'][$assetid]);
297  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, $asset_type);
298 
299  if ($asset) {
300  $step_data['message'] = translate('fnb_hipo_refreshing_asset', $asset->name);
301  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
302  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
303  $fm->updateAsset($asset, Array('update'));
304  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
305  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
306  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset, TRUE);
307  }
308 
309  // add this assetid to the done array
310  $this->_running_vars['done_assetids'][] = $assetid;
311 
312  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($fm, TRUE);
313  $GLOBALS['SQ_SYSTEM']->restoreContext();
314  }
315 
316  if (empty($this->_running_vars['todo_assetids'])) {
317  $step_data['percent_done'] = 100;
318  $step_data['complete'] = TRUE;
319  } else {
320  $total = count($this->_running_vars['todo_assetids']) + count($this->_running_vars['done_assetids']);
321  $step_data['percent_done'] =(count($this->_running_vars['done_assetids']) / $total) * 100;
322  $step_data['complete'] = FALSE;
323  }
324 
325  return TRUE;
326 
327  }//end processRebuild()
328 
329 
330 }//end class
331 
332 ?>