[TYPO3-english] question about a cli file

tom liu tomliutest at gmail.com
Mon Oct 1 02:24:39 CEST 2012


Hi,

I want to run a cli file using ssh:
/home/need/public_html/demo/typo3/cli_dispatch.phpsh career_fairs 94402 200
This cli file will put a feed xml file into my databse, 'tx_cal_event', but
somehow it does not work. It can not put the xml file into database.
Can someone give me some suggestion on what should be wrong? I put the main
souce code below.
Thanks.


cli_careerfairs.php
<?php
if (!defined('TYPO3_cliMode')) die('You cannot run this script directly!');
require_once(PATH_t3lib.'class.t3lib_cli.php');
require_once(t3lib_extMgm::extPath('career_fairs').'lib/class.tx_careerfairs_lib.php');
class cli_careerfairs extends t3lib_cli {
 function cli_careerfairs() {
  parent::t3lib_cli();
 }
 function cli_main($argv) {
  $extConf =
unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$this->extKey]);
  $CFList = tx_careerfairs_lib::fetchCF($this->cli_args['_DEFAULT'][1],
$this->cli_args['_DEFAULT'][2]);
  foreach ($CFList as $cf) {
   $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid', 'tx_cal_event',
'remote_url=\''.$cf['url'].'\'');
   if (!$GLOBALS['TYPO3_DB']->sql_num_rows($res)) {
    list($month, $day, $year) = t3lib_div::trimExplode('/',
$cf['date_mmddYYYY'], TRUE);
    $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_cal_event', array(
     'pid' => 10,
     'tstamp' => time(),
     'crdate' => time(),
     'start_date' => $year.$month.$day,
     'end_date' => $year.$month.$day,
     'start_time' => self::convertTime($cf['time_start']),
     'end_time' => self::convertTime($cf['time_end']),
     'timezone' => 'UTC',
     'title' => $cf['title'],
     'calendar_id' => 1,
     'description' => $cf['description'],
     'freq' => 'none',
     'rdate_type' => 'none',
     'deviation' => 0,
     'l18n_diffsource' =>
'a:32:{s:4:"type";N;s:5:"title";N;s:6:"hidden";N;s:16:"sys_language_uid";N;s:10:"start_date";N;s:10:"start_time";N;s:6:"allday";N;s:8:"end_date";N;s:8:"end_time";N;s:11:"calendar_id";N;s:11:"category_id";N;s:11:"description";N;s:4:"freq";N;s:10:"rdate_type";N;s:9:"deviation";N;s:13:"exception_cnt";N;s:8:"location";N;s:11:"location_id";N;s:12:"location_pid";N;s:13:"location_link";N;s:9:"organizer";N;s:12:"organizer_id";N;s:13:"organizer_pid";N;s:14:"organizer_link";N;s:5:"image";N;s:12:"imagealttext";N;s:14:"imagetitletext";N;s:12:"imagecaption";N;s:10:"attachment";N;s:17:"attachmentcaption";N;s:11:"monitor_cnt";N;s:15:"shared_user_cnt";N;}',
     'remote_url' => $cf['url'],
     'address' => $cf['address'],
     'city' => $cf['city'],
     'state' => $cf['state'],
     'zip' => $cf['zip'],
    ));
   }
  }
 }
 function convertTime($time) {
  list($hour, $minute, $ap) = preg_split('/:| /', $time);
  if (strtoupper($ap) == 'AM') {
   if ($hour == 12) $hour = 0;
  } elseif (strtoupper($ap) == 'PM') {
   if ($hour != 12) $hour += 12;
  }
  return $hour*3600 + $minute*60;
 }
}
if (defined('TYPO3_MODE') &&
$TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/career_fairs/cli/cli_careerfairs.php'])
{
 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/career_fairs/cli/cli_careerfairs.php']);
}
$cleanerObj = t3lib_div::makeInstance('cli_careerfairs');
$cleanerObj->cli_main($_SERVER['argv']);
?>



class.tx_careerfairs_lib.php

<?php
class tx_careerfairs_lib {
 const feed = 'http://www.nationalcareerfairs.com/api/all_events/';
 const charset = 'utf-8';
 const wrapper = 'event';
 const title = 'title';
 const date = 'date';
 const company = 'venue';
 const address = 'address';
 const city = 'city';
 const state = 'state';
 const zip = 'zip';
 const url = 'url';
 const description = 'description';
 function fetchCF($zip, $radius) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, self::feed);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  ob_start();
  curl_exec($ch);
  curl_close($ch);
  $feed = ob_get_contents();
  ob_end_clean();
  $parser = xml_parser_create();
  $vals = array();
  xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0);
  xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, self::charset);
  try {
   xml_parse_into_struct($parser, $feed, $vals);
  } catch (Exception $e) {
   echo 'Line '.xml_get_current_line_number($parser).':
'.xml_error_string(xml_get_error_code($parser))."\n";
   xml_parser_free($parser);
   break;
  }
  xml_parser_free($parser);
  $stack = array();
  $stacktop = 0;
  $current = array();
  $CFList = array();
  foreach($vals as $key => $val) {
   $tagName = $val['tag'];
   switch ($val['type']) {
    case 'open':
     $current[$tagName] = array();
     $stack[$stacktop++] = $current;
     $current = array();
    break;
    case 'close':
     $data = $current;
     $current = $stack[--$stacktop];
     end($current);
     if ($tagName == self::wrapper) {
      if (self::getDistance($zip, $data[self::zip]) > $radius) break;
      $title = t3lib_div::trimExplode('-', $data[self::title]);
      $data[self::title] = $title[0];
      $data[self::description] = trim(str_replace(
       array("\n", '<br>', '<br />', 'Meet face to face with dozens of
employers in the '.substr($data[self::title], 0, -12).' area.', 'Interview
for several positions with many employers IN ONE DAY!!!'),
       array('', "\n", "\n", '', ''),
       $data[self::description]
      ));
      $CFList[] = $data;
     } else {
      $current[key($current)] = $data;
     }
     unset($data);
    break;
    case 'complete':
     $current[$tagName] = $val['value'];
    break;
   }
  }
  return $CFList;
 }
 function getDistance($startZip, $destZip) {
  $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
   'Latitude, Longitude',
   'wc_zip',
   'ZIPCode = \''.$startZip.'\''
  );
  if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
   $startLat = $row['Latitude'];
   $startLong = $row['Longitude'];
  } else {
   return 9999;
  }
  $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
   'Latitude, Longitude',
   'wc_zip',
   'ZIPCode = \''.$destZip.'\''
  );
  if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
   $destLat = $row['Latitude'];
   $destLong = $row['Longitude'];
  } else {
   return 9999;
  }
  $dblLat1 = floatval($startLat);
  $dblLong1 = floatval($startLong);
  $dblLat1 = $dblLat1 * M_PI / 180;
  $dblLong1 = $dblLong1 * M_PI / 180;
  $EARTH_RADIUS_MILES = 3963;
  $dblLat2 = floatval($destLat);
  $dblLong2 = floatval($destLong);
  $distance = 0;
  $dblLat2 = $dblLat2 * M_PI / 180;
  $dblLong2 = $dblLong2 * M_PI / 180;
  if ($dblLat1 != $dblLat2 && $dblLong1 != $dblLong2) {
   $distance = sin($dblLat1) * sin($dblLat2) + cos($dblLat1) *
cos($dblLat2) * cos($dblLong2 - $dblLong1);
   $distance = $EARTH_RADIUS_MILES * (-1 * atan($distance / sqrt(1 -
$distance * $distance)) + M_PI / 2);
  }
  return round($distance);
 }
}
if (defined('TYPO3_MODE') &&
$TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/career_fairs/class.tx_careerfairs_lib.php'])
{
 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/career_fairs/class.tx_careerfairs_lib.php']);
}
?>


Tom


More information about the TYPO3-english mailing list