Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
hipo_job_funnelback_reindex.inc
1 <?php
18 require_once SQ_SYSTEM_ROOT.'/core/hipo/hipo_job.inc';
19 
32 {
33 
34 
40  function HIPO_Job_Funnelback_Reindex($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_reindex'));
62 
63  if ($write_access) {
64  text_box($class.'[SQ_HIPO_REINDEX_FUNNELBACK]', SQ_HIPO_REINDEX_FUNNELBACK, 1);
65  } else {
66  echo SQ_HIPO_REINDEX_FUNNELBACK;
67  }
68 
69  $o->closeField();
70 
71  }//end paintConfig()
72 
73 
80  public static function getConfigVars()
81  {
82  return Array(
83  'SQ_HIPO_REINDEX_FUNNELBACK' => 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().'-funnelback-reindex';
117 
118  }//end getCodeName()
119 
120 
127  function getHipoName()
128  {
129  return translate('fnb_hipo_reindex');
130 
131  }//end getHipoName()
132 
133 
142  {
143  return Array(
144  Array(
145  'name' => translate('fnb_hipo_reindex_step_name0'),
146  'function_call' => Array(
147  'process_function' => 'processReindex',
148  ),
149  'running_mode' => 'server',
150  'auto_step' => TRUE,
151  'percent_done' => 0,
152  'complete' => FALSE,
153  'message' => '',
154  'allow_cancel' => TRUE,
155  ),
156  );
157 
158  }//end getInitialStepData()
159 
160 
167  function freestyle()
168  {
169  // Put the HIPO in some context ;)
170  if (array_get_index('contextid', $this->_running_vars)) {
171  $this->_running_vars['contextid'] = $GLOBALS['SQ_SYSTEM']->getContextId();
172  }
173 
174  while (!empty($this->_running_vars['todo_collections'])) {
175  if (!$this->processReindex($this->_steps[0], get_class_lower($this))) {
176  return FALSE;
177  }
178  }
179 
180  return TRUE;
181 
182  }//end freestyle()
183 
184 
191  function prepare()
192  {
193  // Defaults
194  $this->_running_vars['todo_collections'] = Array();
195  $this->_running_vars['done_collections'] = Array();
196  $fm = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('funnelback_manager');
197  if (is_null($fm)) {
198  trigger_localised_error('FNB0020', E_USER_WARNING);
199  return FALSE;
200  }//end if
201  $collections = $fm->getCollections();
202 
203  // If we have not been told which context to re-index, assume we're in
204  // it already (but this is not recommended if not freestyling, since it
205  // could be stuck somewhere where the context you want is not set)
206  if (array_get_index('contextid', $this->_running_vars)) {
207  $this->_running_vars['contextid'] = $GLOBALS['SQ_SYSTEM']->getContextId();
208  }
209 
210  // To reindex, get a collection or if none specified do the whole system
211  if (!isset($this->_running_vars['collections']) || (is_null($this->_running_vars['collections']) || empty($this->_running_vars['collections']))) {
212  // No URL specified, do the lot, fool!
213  foreach ($collections as $collection_id => $collection_info) {
214  // Meta collections not reindex here, so skip
215  $collection_type = array_get_index($collection_info, 'type', 'web');
216  if ($collection_type == 'meta') continue;
217 
218  $collection_valid = array_get_index($collection_info, 'valid', FALSE);
219  if ($collection_valid === TRUE) {
220  $this->_running_vars['todo_collections'][] = Array($collection_id => $collection_info);
221  }//end if
222  }//end foreach
223  } else {
224  // Specified URL(s), do these ones, fool!
225  if (!is_array($this->_running_vars['collections'])) {
226  // If one specified, make it an array
227  $this->_running_vars['collections'] = Array($this->_running_vars['collections']);
228  }//end if
229 
230  foreach ($this->_running_vars['collections'] as $todo_collection) {
231  // we have the collection, go thru each collection and find the correct entry (make sure the collection is valid)
232  foreach ($collections as $collection_id => $collection_info) {
233  // Meta collections not reindex here, so skip
234  $collection_type = array_get_index($collection_info, 'type', 'web');
235  if ($collection_type == 'meta') continue;
236 
237  $collection_valid = array_get_index($collection_info, 'valid', FALSE);
238  if ($collection_valid === TRUE && $collection_id == $todo_collection) {
239  $this->_running_vars['todo_collections'][] = Array($collection_id => $collection_info);
240  }//end if
241  }//end foreach
242  }//end foreach
243  }//end if
244 
245  return parent::prepare();
246 
247  }//end prepare()
248 
249 
259  function processReindex(&$step_data, $prefix)
260  {
261  $GLOBALS['SQ_SYSTEM']->changeContext(array_get_index($this->_running_vars, 'contextid', 0));
262 
263  $am =& $GLOBALS['SQ_SYSTEM']->am;
264  $fm = $am->getSystemAsset('funnelback_manager');
265  if (is_null($fm)) {
266  return FALSE;
267  }//end if
268 
269  // get next url to reindex, if none to do just flow through and finish the job neat and tidy ;)
270  if (!empty($this->_running_vars['todo_collections'])) {
271  reset($this->_running_vars['todo_collections']);
272  $next = key($this->_running_vars['todo_collections']);
273  $collection_id = key($this->_running_vars['todo_collections'][$next]);
274  $collection_info = $this->_running_vars['todo_collections'][$next][$collection_id];
275  $url = array_get_index($collection_info, 'url', Array());
276  $assetid = array_get_index($collection_info, 'asset', 0);
277  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, '', TRUE);
278  unset($this->_running_vars['todo_collections'][$next]);
279 
280  if (!empty($url) && !is_null($asset)) {
281  // Let people know what you are thinking ;-)
282  $step_data['message'] = translate('fnb_hipo_reindexing_url', htmlspecialchars($asset->attr('name')));
283 
284  // Build the parameters
285  $output = '';
286  $xml = $fm->generateXMLCache($url, $assetid, FALSE);
287  $log_file = SQ_LOG_PATH.'/funnelback.log';
288  $bin_path = $fm->getFunnelbackPath();
289  $coll_path = $fm->getCollectionDataPath($collection_id);
290  $coll_log_path = $fm->getCollectionDataPath($collection_id, 'log');
291  $data_path = $fm->getCollectionDataPath($collection_id, 'idx_new');
292  $live_path = $fm->getCollectionDataPath($collection_id, 'idx');
293  if (empty($data_path)) return FALSE;
294  $options = $fm->getIndexingOptions();
295 
296  // Check for permissions and show a fail if cannot write
297  if (!is_writable($data_path)) {
298  trigger_localised_error('FNB0030', E_USER_WARNING);
299  return FALSE;
300  }//end if
301 
302  // Create the XML on the filesystem
303  file_put_contents($data_path.'/sitemap.xml', $xml);
304  // Calculate max line length of metadata summary
305  preg_match('/[0-9]+/', exec('wc -L '.$data_path.'/sitemap.xml'), $matches);
306  $maxLineLength = isset($matches[0]) ? $matches[0] : NULL;
307 
308  // Apply login credentials
309  $user_id = $fm->attr('user');
310  if (!empty($user_id)) {
311  $user = $GLOBALS['SQ_SYSTEM']->am->getAsset($user_id, '', TRUE);
312  if (!is_null($user)) {
313  $username = $user->attr('username');
314  $password = $fm->attr('password');
315  file_put_contents($data_path.'/creds', $username.' '.$password);
316  $options[] = '-CF='.$data_path.'/creds';
317  $options[] = '-loginPrefix='.SQ_CONF_LOGIN_SUFFIX;
318  }//end if
319  }//end if
320 
321  // Add in the click log if exists
322  if (file_exists($coll_log_path.'/click.log')) {
323  $options[] = '-F'.$coll_log_path.'/click.log';
324  }//end if
325 
326  // Make sure we ONLY index with at least one key (public assets will have a 'public' key)
327  $options[] = '-security_level=1';
328 
329  // Build the command line
330  $command = $bin_path.'/bin/padre-iw';
331  $command .= ' -secmap '.escapeshellarg($data_path.'/sitemap.xml');
332  $command .= ' '.escapeshellarg($collection_id); // Index Stem
333  $command .= ' '.implode(' ', $options); // Command line Options
334  $command .= isset($maxLineLength) ? ' -mdsfml'.$maxLineLength : ''; // Max line length of metadata summary file
335 
336  // Start the reindex process
337  $status = $fm->runCommand($command, $output, NULL, $log_file, $data_path, Array('SITE_SEARCH_ROOT'=>$coll_path));
338 
339  // Regardless of status, remove the credential file
340  if (file_exists($data_path.'/creds')) {
341  unlink($data_path.'/creds');
342  }//end if
343 
344  if ($status === 0) {
345  // Successfully completed
346  $make_live_cmd = 'rm -rf idx_old; mv idx idx_old; mv idx_new idx';
347  $final_status = $fm->runCommand($make_live_cmd, $output, NULL, $log_file, $coll_path);
348  if ($status !== 0) {
349  // Failed making live
350  trigger_localised_error('FNB0032', E_USER_WARNING);
351  return FALSE;
352  }//end if
353  } else {
354  // Fail....
355  trigger_localised_error('FNB0031', E_USER_WARNING);
356  return FALSE;
357  }//end if
358 
359  // Forget some information
360  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset, TRUE);
361  }//end if
362 
363  // add this assetid to the done array
364  $this->_running_vars['done_collections'][] = $collection_id;
365  }//end if
366 
367  $GLOBALS['SQ_SYSTEM']->restoreContext();
368 
369  if (empty($this->_running_vars['todo_collections'])) {
370  $step_data['percent_done'] = 100;
371  $step_data['complete'] = TRUE;
372  } else {
373  $total = count($this->_running_vars['todo_collections']) + count($this->_running_vars['done_collections']);
374  $step_data['percent_done'] =(count($this->_running_vars['done_collections']) / $total) * 100;
375  $step_data['complete'] = FALSE;
376  }
377 
378  return TRUE;
379 
380  }//end processReindex()
381 
382 
383 }//end class
384 
385 ?>