예제 #1
0
 public function execute($parameters, $db)
 {
     if (sizeof($parameters) == 0 || $parameters[0] == "") {
         CLI::out("Usage: |g|help <command>|n| To see a list of commands, use: |g|list", true);
     }
     $command = $parameters[0];
     switch ($command) {
         case "add":
             if (isset($parameters[1])) {
                 $url = $parameters[1];
             } else {
                 $validTypes = array("pilot", "corp", "alliance");
                 CLI::out("|g|You are now adding a feed with the quick feed creator(TM), if you already have a feed you want to add, use add <feed> instead.|n|");
                 retryekid:
                 $ekid = CLI::prompt("Do you have the EVE-KILL ID for the entity you want to add?", "yes");
                 if ($ekid != "yes" && $ekid != "no") {
                     CLI::out("|r|Error, call type is not supported. Please retry|n|");
                     goto retryekid;
                 }
                 if ($ekid == "no") {
                     $validTypes = array("pilotname", "corpname", "alliancename");
                 }
                 retryCall:
                 CLI::out("|g|Valid calls:|n| " . implode(", ", $validTypes));
                 $type = CLI::prompt("Type of call. |g|(Refer to the above list)|n|");
                 if (!in_array($type, $validTypes)) {
                     CLI::out("|r|Error, call type is not supported. Please retry|n|");
                     goto retryCall;
                 }
                 if ($ekid == "yes") {
                     $nameID = CLI::prompt("ID of entity you wish to add");
                 } else {
                     $nameID = urlencode(CLI::prompt("Name of the entity you with to add"));
                 }
                 retryApi:
                 $all = CLI::prompt("All Kills?", "yes");
                 if ($all != "yes" && $all != "no") {
                     CLI::out("|r|Error, call type is not supported. Please retry|n|");
                     goto retryApi;
                 }
                 $apiOnly = null;
                 if ($all == "yes") {
                     $apiOnly = "&allkills";
                 }
                 $domain = CLI::prompt("Enter URL of the killboard you're fetching from. |g|Ex. eve-kill.net|n|", "eve-kill.net");
                 $url = "http://{$domain}/?a=idfeed&{$type}={$nameID}{$apiOnly}";
             }
             if (filter_var($url, FILTER_VALIDATE_URL)) {
                 $db->execute("INSERT INTO zz_feeds (url, edkStyle) VALUES (:url, 1)", array(":url" => $url));
                 CLI::out("Now inserting |g|{$url}|n| to the database.", true);
             } else {
                 CLI::out("|r|Invalid URL, please try again|n|", true);
             }
             break;
         case "remove":
             $id = NULL;
             if (isset($parameters[1])) {
                 $id = $parameters[1];
             }
             if (is_null($id)) {
                 CLI::out("Please refer to feed list to show all the feeds you have added to your board. To remove one, use: |g|feed remove <id>|n|");
             } elseif (!is_numeric($id)) {
                 CLI::out("|r|ID needs to be an int..|n|");
             } else {
                 $url = $db->queryField("SELECT url FROM zz_feeds WHERE id = :id AND edkStyle = 1", "url", array(":id" => $id));
                 if (is_null($url)) {
                     CLI::out("|r|Feed is already removed.", true);
                 }
                 CLI::out("Removing feed: |g|{$url}");
                 $db->execute("DELETE FROM zz_feeds WHERE id = :id", array(":id" => $id));
             }
             break;
         case "list":
             $list = $db->query("SELECT * FROM zz_feeds WHERE edkStyle = 1");
             foreach ($list as $url) {
                 CLI::out($url["id"] . "::|g|" . $url["url"]);
             }
             break;
         case "fetch":
             CLI::out("|g|Initiating feed fetching|n|");
             $doSleep = false;
             $size = 1;
             $count = 1;
             $feeds = $db->query("SELECT id, url, lastFetchTime FROM zz_feeds WHERE edkStyle IS true", array(), 0);
             if (sizeof($feeds) > 1) {
                 $doSleep = true;
                 $size = sizeof($feeds);
             }
             foreach ($feeds as $feed) {
                 $url = $feed["url"];
                 $lastFetchTime = strtotime($feed["lastFetchTime"]) + 600;
                 $currentTime = time();
                 $insertCount = 0;
                 if ($lastFetchTime <= $currentTime) {
                     CLI::out("Fetching from |g|{$url}|n|");
                     try {
                         $data = self::fetchUrl($url);
                         $xml = new SimpleXMLElement($data, null, false, "", false);
                         $result = new \Pheal\Core\Result($xml);
                         $insertCount = self::processAPI($result, $db);
                         $db->execute("UPDATE zz_feeds SET lastFetchTime = :time WHERE url = :url", array(":time" => date("Y-m-d H:i:s"), ":url" => $url));
                         if ($insertCount > 0) {
                             CLI::out("Inserted |g|{$insertCount}|n| new kills from |g|{$url}|n|");
                             Log::log("Inserted {$insertCount} new kills from {$url}");
                         } else {
                             CLI::out("No new kills from |r|{$url}|n|");
                         }
                     } catch (Exception $ex) {
                         CLI::out("|r|Error with {$url}: " . $ex->getMessage());
                     }
                     if ($doSleep && $size != $count) {
                         CLI::out("|g|Sleeping for 10 seconds|n| before fetching another url.. (Otherwise we're hammering..)");
                         sleep(10);
                         // yes yes, 10 seconds of sleeping, what?! this is only here to stop hammering. Feel free to hammer tho by commenting this, but you'll just get banned..
                     }
                     $count++;
                 }
             }
             break;
     }
 }