Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
hipo_job_update_lookups.inc
1 <?php
17 require_once SQ_SYSTEM_ROOT.'/core/hipo/hipo_job.inc';
18 
33 {
34 
35 
41  function HIPO_Job_Update_Lookups($code_name='')
42  {
43  $this->HIPO_Job($code_name);
44 
45  }//end constructor
46 
47 
57  public static function paintConfig(&$o, $class, $write_access)
58  {
59  // metadata regeneration threshhold HIPO config entry
60  $o->openField(translate('lookups_updating_threshold'));
61 
62  if ($write_access) {
63  text_box($class.'[SQ_HIPO_LOOKUPS_THRESHOLD]', SQ_HIPO_LOOKUPS_THRESHOLD, 5);
64  } else {
65  echo SQ_HIPO_LOOKUPS_THRESHOLD;
66  }
67  echo ' '.translate('assets');
68 
69  $o->closeField();
70 
71  }//end paintConfig()
72 
73 
80  public static function getConfigVars()
81  {
82  return Array(
83  'SQ_HIPO_LOOKUPS_THRESHOLD' => Array('editable' => 1, 'default' => 1),
84  );
85 
86  }//end getConfigVars()
87 
88 
100  function getCodeName()
101  {
102  return parent::getCodeName().'-'.md5(implode('-', $this->_running_vars['assetids']));
103 
104  }//end getCodeName()
105 
106 
113  function getHipoName()
114  {
115  return translate('hipo_name_update_lookups');
116 
117  }//end getHipoName()
118 
119 
128  {
129  return Array(
130  Array(
131  'name' => translate('hipo_updating_lookups'),
132  'function_call' => Array(
133  'process_function' => 'processLookups',
134  ),
135  'running_mode' => 'server',
136  'auto_step' => TRUE,
137  'allow_cancel' => TRUE,
138  'percent_done' => 0,
139  'complete' => FALSE,
140  'message' => '',
141  ),
142  );
143 
144  }//end getInitialStepData()
145 
146 
156  {
157  if (SQ_HIPO_LOOKUPS_THRESHOLD == 0) return 0;
158  if (!isset($this->_running_vars['todo_assetids'])) {
159  return 0;
160  }
161  return ((count($this->_running_vars['todo_assetids']) / SQ_HIPO_LOOKUPS_THRESHOLD) * 100);
162 
163  }//end getThresholdPercentageRequired()
164 
165 
172  function freestyle()
173  {
174  while (!empty($this->_running_vars['todo_assetids'])) {
175  if (!$this->processLookups($this->_steps[0], get_class($this))) {
176  return FALSE;
177  }
178  }
179  return TRUE;
180 
181  }//end freestyle()
182 
183 
190  function prepare()
191  {
192  // to start updating lookups, we need to have an asset id to start updating lookups at
193  if (empty($this->_running_vars['assetids'])) {
194  trigger_localised_error('HIPO0001', E_USER_WARNING);
195  return FALSE;
196  }
197  $db = MatrixDAL::getDb();
198 
199  $todo_normal = Array();
200  $todo_shadows = Array();
201 
202  foreach ($this->_running_vars['assetids'] as $assetid) {
203  // check if we are updating lookups for a shadow asset, or a bridge
204  $id_parts = explode(':', $assetid);
205  if (isset($id_parts[1])) {
206  $todo_shadows = array_merge($todo_shadows, array_keys($GLOBALS['SQ_SYSTEM']->am->getChildren($assetid)));
207  } else {
208  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
209  if ($asset instanceof Bridge) {
210  if (!method_exists($asset, 'getChildren')) {
211  trigger_localised_error('SYS0204', E_USER_WARNING, $asset->name);
212  } else {
213  $todo_shadows = array_merge($todo_shadows, array_keys($asset->getChildren($assetid)));
214  }
215  }
216 
217  $where = 'l.minorid = :assetid';
218  $where = $GLOBALS['SQ_SYSTEM']->constructRollbackWhereClause($where, 't');
219  $where = $GLOBALS['SQ_SYSTEM']->constructRollbackWhereClause($where, 'l');
220  $sql = 'SELECT t.treeid
221  FROM '.SQ_TABLE_RUNNING_PREFIX.'ast_lnk_tree t INNER JOIN '.SQ_TABLE_RUNNING_PREFIX.'ast_lnk l ON t.linkid = l.linkid
222  '.$where;
223  $sql = db_extras_modify_limit_clause($sql, MatrixDAL::getDbType(), 1);
224 
225  try {
226  $query = MatrixDAL::preparePdoQuery($sql);
227  MatrixDAL::bindValueToPdo($query, 'assetid', $assetid);
228  $treeid = MatrixDAL::executePdoOne($query);
229  } catch (Exception $e) {
230  throw new Exception('Unable to get treeid for minorid: '.$assetid.' due to database error: '.$e->getMessage());
231  }
232 
233  $sql = 'SELECT l.minorid, MAX(LENGTH(t.treeid)) as length
234  FROM '.SQ_TABLE_RUNNING_PREFIX.'ast_lnk_tree t
235  INNER JOIN '.SQ_TABLE_RUNNING_PREFIX.'ast_lnk l ON t.linkid = l.linkid
236  AND (('.db_extras_bitand(MatrixDAL::getDbType(), 'l.link_type', SQ_SC_LINK_BACKEND_NAV).' > 0) OR
237  (('.db_extras_bitand(MatrixDAL::getDbType(), 'l.link_type', SQ_SC_LINK_SIGNIFICANT).' > 0) AND ';
238  $sql .= (MatrixDAL::getDbType() === 'oci') ? '(l.value IS NOT NULL)))' : '(l.value <> \'\'))) ';
239  $where = 't.treeid LIKE :treeid
240  GROUP BY l.minorid ORDER BY length';
241 
242  $where = $GLOBALS['SQ_SYSTEM']->constructRollbackWhereClause($where, 't');
243  $where = $GLOBALS['SQ_SYSTEM']->constructRollbackWhereClause($where, 'l');
244 
245  try {
246  $query = MatrixDAL::preparePdoQuery($sql.$where);
247  MatrixDAL::bindValueToPdo($query, 'treeid', $treeid.'%');
248  $new_assets = MatrixDAL::executePdoAssoc($query);
249  } catch (Exception $e) {
250  throw new Exception('Unable to get minorids for treeid: '.$treeid[0]['treeid'].' due to database error: '.$e->getMessage());
251  }
252 
253  $todo_normal = array_merge($todo_normal, $new_assets);
254  }//end else
255 
256  }//end foreach
257 
258  // Make sure lower assets are done after higher ones
259  $todo_assetids = Array();
260  if (!empty($todo_normal)) {
261  usort($todo_normal, create_function('$a, $b', 'return $a[\'length\'] > $b[\'length\'];'));
262  foreach($todo_normal as $asset_info) {
263  $todo_assetids[] = $asset_info['minorid'];
264  }
265  unset($todo_normal);
266  unset($new_assets);
267  }
268 
269  // Whether to add auto remap
270  $rm = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('remap_manager');
271  $auto_add_remaps = $rm->attr('remap_upon_webpath_change');
272 
273  $this->_running_vars['todo_assetids'] = array_unique(array_merge($todo_assetids, $todo_shadows));
274  if (!isset($this->_running_vars['auto_add_remaps'])) $this->_running_vars['auto_add_remaps'] = $auto_add_remaps;
275  $this->_running_vars['done_assetids'] = Array();
276  return parent::prepare();
277 
278  }//end prepare()
279 
280 
290  function processLookups(&$step_data, $prefix)
291  {
292  if (!empty($this->_running_vars['todo_assetids'])) {
293  $assetid = array_shift($this->_running_vars['todo_assetids']);
294  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
295 
296  if (!is_null($asset)) {
297  if (!$asset->updateLookups($this->_running_vars['auto_add_remaps'])) return FALSE;
298  $step_data['message'] = translate('hipo_updating_lookups_for', htmlentities($asset->name, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET));
299  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
300  unset($asset);
301  } else {
302  $step_data['message'] = translate('hipo_skipping_asset', $assetid);
303  $this->_addError(translate('hipo_cannot_update_lookups', $assetid));
304  }
305 
306  // add this assetid to the done array so we dont do it again
307  $this->_running_vars['done_assetids'][] = $assetid;
308  }
309 
310  if (empty($this->_running_vars['todo_assetids'])) {
311  $step_data['percent_done'] = 100;
312  $step_data['complete'] = TRUE;
313  } else {
314  $total = count($this->_running_vars['todo_assetids']) + count($this->_running_vars['done_assetids']);
315  $step_data['percent_done'] =(count($this->_running_vars['done_assetids']) / $total) * 100;
316  $step_data['complete'] = FALSE;
317  }
318 
319  return TRUE;
320 
321  }//end processLookups()
322 
323 
324 }//end class
325 
326 ?>