/** * delete all deactivated maps * >> php index.php "/cron/deleteMapData" * @param $f3 */ function deleteMapData($f3) { $pfDB = DB\Database::instance()->getDB('PF'); $sqlDeleteDisabledMaps = "DELETE FROM\n map\n WHERE\n map.active = 0 AND\n TIMESTAMPDIFF(DAY, map.updated, NOW() ) > :deletion_time"; $pfDB->exec($sqlDeleteDisabledMaps, ['deletion_time' => self::DAYS_UNTIL_MAP_DELETION]); $deletedMapsCount = $pfDB->count(); // Log ------------------------ $log = Controller\LogController::getLogger('cron_' . __FUNCTION__); $log->write(sprintf(self::LOG_TEXT_MAPS, __FUNCTION__, $deletedMapsCount)); }
/** * connect to a database * @param $dns * @param $name * @param $user * @param $password * @return SQL */ protected function connect($dns, $name, $user, $password) { try { $db = new SQL($dns . $name, $user, $password, [\PDO::MYSQL_ATTR_COMPRESS => TRUE]); } catch (\PDOException $e) { // DB connection error LogController::getLogger('error')->write($e->getMessage()); } return $db; }
/** * debug log function * @param $text */ public static function log($text) { Controller\LogController::getLogger('debug')->write($text); }
/** * get a Logger object by Hive key * -> set in pathfinder.ini * @param string $type * @return \Log|null */ static function getLogger($type) { return LogController::getLogger($type); }
/** * debug log function * @param string $text * @param string $type */ public static function log($text, $type = null) { $type = isset($type) ? $type : 'DEBUG'; Controller\LogController::getLogger($type)->write($text); }
/** * perform curl() request * -> caches response by returned HTTP Cache header data * @param string $url * @param array|null $options * @param array $additionalOptions * @param int $retryCount request counter for failed crest call * @return array|FALSE|mixed */ public function request($url, array $options = null, $additionalOptions = [], $retryCount = 0) { $f3 = \Base::instance(); if (!$f3->exists($hash = $this->getCacheKey($url, $options))) { // retry same request until request limit is reached $retry = false; $result = parent::request($url, $options); $result['timeout'] = false; $statusCode = $this->getStatusCodeFromHeaders($result['headers']); switch ($statusCode) { case 100: case 200: // request succeeded -> check if response should be cached $ttl = $this->getCacheTimeFromHeaders($result['headers']); if ($ttl > 0 && !empty(json_decode($result['body'], true))) { $f3->set($hash, $result, $ttl); } break; case 401: case 415: // unauthorized $errorMsg = $this->getErrorMessageFromJsonResponse($statusCode, $options['method'], $url, json_decode($result['body'])); LogController::getLogger('ERROR')->write($errorMsg); break; case 500: case 501: case 502: case 503: case 505: $retry = true; if ($retryCount == self::RETRY_COUNT_MAX) { $errorMsg = $this->getErrorMessageFromJsonResponse($statusCode, $options['method'], $url, json_decode($result['body'])); LogController::getLogger('ERROR')->write($errorMsg); // trigger error if ($additionalOptions['suppressHTTPErrors'] !== true) { $f3->error($statusCode, $errorMsg); } } break; case 504: case 0: $retry = true; if ($retryCount == self::RETRY_COUNT_MAX) { // timeout -> response should not be cached $result['timeout'] = true; $errorMsg = $this->getErrorMessageFromJsonResponse(504, $options['method'], $url, json_decode($result['body'])); // log error LogController::getLogger('ERROR')->write($errorMsg); if ($additionalOptions['suppressHTTPErrors'] !== true) { $f3->error(504, $errorMsg); } } break; default: // unknown status $errorMsg = $this->getErrorMessageFromJsonResponse($statusCode, $options['method'], $url); LogController::getLogger('ERROR')->write($errorMsg); break; } if ($retry && $retryCount < self::RETRY_COUNT_MAX) { $retryCount++; $this->request($url, $options, $additionalOptions, $retryCount); } } else { $result = $f3->get($hash); } return $result; }
/** * imports all relevant map stats from CCPs API * >> php index.php "/cron/importSystemData" * @param $f3 */ function importSystemData($f3) { $time_start = microtime(true); // prepare system jump log table $systemsData = $this->prepareSystemLogTables(); $time_end = microtime(true); $execTimePrepareSystemLogTables = $time_end - $time_start; $pfDB = DB\Database::instance()->getDB('PF'); // get current jump Data ------------------------------------------------------- $time_start = microtime(true); $apiPath = $f3->get('PATHFINDER.API.CCP_XML') . '/map/Jumps.xml.aspx'; $apiResponse = \Web::instance()->request($apiPath, $this->apiRequestOptions); $jumpData = []; $updateJumps = false; if ($apiResponse['body']) { $xml = simplexml_load_string($apiResponse['body']); $rowApiData = $xml->result->rowset; foreach ($rowApiData->children() as $systemApiData) { $attributeApiData = $systemApiData->attributes(); $systemId = $attributeApiData->solarSystemID->__toString(); $shipJumps = $attributeApiData->shipJumps->__toString(); $jumpData[$systemId] = $shipJumps; } $updateJumps = true; } $time_end = microtime(true); $execTimeGetJumpData = $time_end - $time_start; // get current kill Data ------------------------------------------------------- $time_start = microtime(true); $apiPath = $f3->get('PATHFINDER.API.CCP_XML') . '/map/Kills.xml.aspx'; $apiResponse = \Web::instance()->request($apiPath, $this->apiRequestOptions); $killData = []; $updateKills = false; if ($apiResponse['body']) { $xml = simplexml_load_string($apiResponse['body']); $rowApiData = $xml->result->rowset; foreach ($rowApiData->children() as $systemApiData) { $attributeApiData = $systemApiData->attributes(); $systemId = $attributeApiData->solarSystemID->__toString(); $shipKills = $attributeApiData->shipKills->__toString(); $podKills = $attributeApiData->podKills->__toString(); $factionKills = $attributeApiData->factionKills->__toString(); $killData[$systemId] = ['shipKills' => $shipKills, 'podKills' => $podKills, 'factionKills' => $factionKills]; } $updateKills = true; } $time_end = microtime(true); $execTimeGetKillData = $time_end - $time_start; // update system log tables ----------------------------------------------------- $time_start = microtime(true); // make sure last update is (at least) 1h ago $pfDB->begin(); foreach ($this->logTables as $key => $tableName) { $sql = "UPDATE\n " . $tableName . "\n SET\n value24 = value23,\n value23 = value22,\n value22 = value21,\n value21 = value20,\n value20 = value19,\n value19 = value18,\n value18 = value17,\n value17 = value16,\n value16 = value15,\n value15 = value14,\n value14 = value13,\n value13 = value12,\n value12 = value11,\n value11 = value10,\n value10 = value9,\n value9 = value8,\n value8 = value7,\n value7 = value6,\n value6 = value5,\n value5 = value4,\n value4 = value3,\n value3 = value2,\n value2 = value1,\n value1 = :value\n WHERE\n systemId = :systemId\n "; foreach ($systemsData as $systemData) { if ($key == 'jumps' && $updateJumps) { // update jump data (if available) $currentJumps = 0; if (array_key_exists($systemData['systemId'], $jumpData)) { $currentJumps = $jumpData[$systemData['systemId']]; } $pfDB->exec($sql, array(':systemId' => $systemData['systemId'], ':value' => $currentJumps), 0, false); } else { if ($updateKills) { // update kill data (if available) $currentKills = 0; if (array_key_exists($systemData['systemId'], $killData)) { $currentKillData = $killData[$systemData['systemId']]; $currentKills = $currentKillData[$key]; } $pfDB->exec($sql, array(':systemId' => $systemData['systemId'], ':value' => $currentKills), 0, false); } } } } $pfDB->commit(); $time_end = microtime(true); $execTimeUpdateTables = $time_end - $time_start; // Log ------------------------ $log = Controller\LogController::getLogger('cron_' . __FUNCTION__); $log->write(sprintf(self::LOG_TEXT, __FUNCTION__, $execTimePrepareSystemLogTables, $execTimeGetJumpData, $execTimeGetKillData, $execTimeUpdateTables)); }
/** * get logger for DB logging * @return \Log */ static function getLogger() { return LogController::getLogger('ERROR'); }