Ejemplo n.º 1
0
 public static function checkUpstreamRepos()
 {
     $channelId[1] = "stable";
     $channelId[2] = "unstable";
     $channelId[3] = "development";
     $addons = AddonManager::getAll();
     foreach ($addons as $addon) {
         $versionInfo = $addon->getVersionInfo();
         if (isset($versionInfo->upstream)) {
             $upstream = $versionInfo->upstream;
             //For consistency I like using echo() with parenthesis and sticking with 'ID' instead of 'Id'
             echo $addon->getId() . "\n";
             $url = $upstream->url;
             if (isset($upstream->mod)) {
                 $url .= "?mods=" . $upstream->mod;
             }
             if (strpos($url, "http") !== 0) {
                 $url = "http://" . $url;
             }
             $opts = array('http' => array('method' => "GET", 'header' => "Accept-language: en\r\n" . "User-Agent: Torque/1.3\r\n"));
             $context = stream_context_create($opts);
             $response = file_get_contents($url, false, $context);
             if ($response !== false) {
                 if (($res = json_decode($response)) !== null) {
                     $a = "add-ons";
                     foreach ($res->{$a} as $ad) {
                         if ($ad->name == $addon->getFilename()) {
                             foreach ($ad->channels as $channel => $info) {
                                 if (in_array($channel, $upstream->branch)) {
                                     $localBranchId = array_search($channel, $upstream->branch);
                                     echo "remote [{$channel}]:" . $info->version . "\n";
                                     echo "local [{$localBranchId}]:" . $addon->getBranchInfo($localBranchId)->version . "\n";
                                     if ($info->version !== $addon->getBranchInfo($localBranchId)->version) {
                                         AddonManager::doUpstreamUpdate($addon->getId(), $localBranchId, $info->file, $info->version);
                                     }
                                 }
                             }
                         }
                     }
                 } else {
                     echo $response;
                     $lines = explode("\n", $response);
                     //basically we're going to construct a fake json response
                     foreach ($lines as $line) {
                         $line = trim($line);
                         if (!isset($res)) {
                             if (strpos($line, "<addon:") === 0) {
                                 $ad = substr($line, 7, strlen($line) - 8);
                                 $res = new stdClass();
                                 $res->name = "{$ad}.zip";
                             }
                         } else {
                             if (strpos($line, "</addon>") === 0) {
                                 break;
                             }
                             if (!isset($currentChannel)) {
                                 if (strpos($line, "<channel:") === 0) {
                                     $ch = substr($line, 9, strlen($line) - 10);
                                     $currentChannel = new stdClass();
                                     $currentChannel->name = $ch;
                                 }
                             } else {
                                 if (strpos($line, "</channel>") === 0) {
                                     $res->channels[$currentChannel->name] = $currentChannel;
                                     unset($currentChannel);
                                 }
                                 if (strpos($line, "<version:") === 0) {
                                     $ch = substr($line, 9, strlen($line) - 10);
                                     $currentChannel->version = $ch;
                                 }
                                 if (strpos($line, "<file:") === 0) {
                                     $file = substr($line, 6, strlen($line) - 7);
                                     $currentChannel->file = $file;
                                 }
                             }
                         }
                     }
                     if (isset($res)) {
                         foreach ($res->channels as $channel => $info) {
                             $arr = (array) $upstream->branch;
                             if (in_array($channel, $arr)) {
                                 $localBranchId = array_search($channel, $arr);
                                 echo "remote [{$channel}]:" . $info->version . "\n";
                                 echo "local [{$localBranchId}]:" . $addon->getBranchInfo($localBranchId)->version . "\n";
                                 //if($info->version !== $addon->getBranchInfo($localBranchId)->version) {
                                 AddonManager::doUpstreamUpdate($addon->getId(), $localBranchId, $info->file, $info->version);
                                 //}
                             }
                         }
                     }
                 }
             } else {
             }
         }
     }
 }