/** * Add a new pilot to the database or update the details of an existing one. * * @param string $name Pilot name * @param Corporation $corp Corporation object for this pilot's corporation * @param string $timestamp time this pilot's corp was updated * @param integer $externalID CCP external id * @param boolean $loadExternals Whether to check for an external ID * @return Pilot */ public static function add($name, $corp, $timestamp, $externalID = 0, $loadExternals = true) { if (!$name) { trigger_error("Attempt to add a pilot with no name. Aborting.", E_USER_ERROR); // If things are going this wrong, it's safer to die and prevent more harm die; } else { if (!$corp->getID()) { trigger_error("Attempt to add a pilot ({$name}) with no corp ({$corp}). Aborting.", E_USER_ERROR); // If things are going this wrong, it's safer to die and prevent more harm die; } } // Check if pilot exists with a non-cached query. $qry = DBFactory::getDBQuery(true); $name = stripslashes($name); // Insert or update a pilot with a cached query to update cache. $qryI = DBFactory::getDBQuery(true); $qry->execute("SELECT * FROM kb3_pilots WHERE plt_name = '" . $qry->escape($name) . "'"); if (!$qry->recordCount()) { $externalID = (int) $externalID; // If no external id is given then look it up. if (!$externalID && $loadExternals) { $myID = new API_NametoID(); $myID->setNames($name); $myID->fetchXML(); $myNames = $myID->getNameData(); $externalID = (int) $myNames[0]['characterID']; } // If we have an external id then check it isn't already in use. // If we find it then update the old corp with the new name and // return. if ($externalID) { $qry->execute("SELECT * FROM kb3_pilots WHERE plt_externalid = " . $externalID); if ($qry->recordCount()) { $row = $qry->getRow(); $pilot = Pilot::getByID($row['plt_id']); $qryI->execute("UPDATE kb3_pilots SET plt_name = '" . $qry->escape($name) . "' WHERE plt_externalid = " . $externalID); if ($qryI->affectedRows() > 0) { Cacheable::delCache($pilot); } $qryI->execute("UPDATE kb3_pilots SET plt_crp_id = " . $corp->getID() . ", plt_updated = " . "date_format( '" . $timestamp . "', '%Y.%m.%d %H:%i:%s') WHERE plt_externalid = " . $externalID . " AND plt_crp_id <> " . $corp->getID() . " AND ( plt_updated < date_format( '" . $timestamp . "', '%Y-%m-%d %H:%i') OR plt_updated is null )"); if ($qryI->affectedRows() > 0) { Cacheable::delCache($pilot); } return $pilot; } } $qry->execute("INSERT INTO kb3_pilots (plt_name, plt_crp_id, " . "plt_externalid, plt_updated) values ('" . $qry->escape($name) . "', " . $corp->getID() . ",\t" . $externalID . ",\n\t\t\t\t\tdate_format( '" . $timestamp . "', '%Y.%m.%d %H:%i:%s'))\n\t\t\t\t\tON DUPLICATE KEY UPDATE plt_crp_id=" . $corp->getID() . ",\n\t\t\t\t\tplt_externalid=" . $externalID . ",\n\t\t\t\t\tplt_updated=date_format( '" . $timestamp . "', '%Y.%m.%d %H:%i:%s')"); return new Pilot($qry->getInsertID(), $externalID, $name, $corp->getID()); } else { // Name found. $row = $qry->getRow(); $id = $row['plt_id']; if (!is_null($row['plt_updated'])) { $updated = strtotime($row['plt_updated'] . " UTC"); } else { $updated = 0; } if ($updated < strtotime($timestamp . " UTC") && $corp->getID() != $row['plt_crp_id']) { $qryI->execute("UPDATE kb3_pilots SET plt_crp_id = " . $corp->getID() . ", plt_updated = '" . $timestamp . "' WHERE plt_name = '" . $qry->escape($name) . "'" . " AND plt_crp_id <> " . $corp->getID() . " AND ( plt_updated < '" . $timestamp . "' OR plt_updated is null )"); } $plt = new Pilot($id, $externalID, $name, $corp); if (!$row['plt_externalid'] && $externalID) { $plt->executed = true; $plt->setCharacterID($externalID); } return $plt; } }