mx fetching db statements

This commit is contained in:
Steffen Schröder 2014-01-09 22:08:34 +01:00
parent 28a435ba16
commit f9041748f6

View File

@ -1,4 +1,5 @@
<?php
namespace ManiaControl\Maps;
use ManiaControl\ManiaControl;
@ -44,41 +45,69 @@ class ManiaExchangeInfoSearcher {
$this->maniaControl = $maniaControl;
}
public function updateMapObjectWithManiaExchangeIds() {
$maps = $this->maniaControl->mapManager->getMaps();
$mysqli = $this->maniaControl->database->mysqli;
$mapIds = array();
// Fetch mx ids
$fetchMapQuery = "SELECT `mxid` FROM `" . MapManager::TABLE_MAPS . "`
WHERE `index` = ?;";
$fetchMapStatement = $mysqli->prepare($fetchMapQuery);
if ($mysqli->error) {
trigger_error($mysqli->error);
return;
}
$fetchMapStatement->bind_param('i', $mapIndex);
foreach ($maps as $map) {
/** @var Map $map */
//TODO prepared statement read MX id from maps database
/**
*
* @var Map $map
*/
$mapIndex = $map->index;
$fetchMapStatement->execute();
if ($fetchMapStatement->error) {
trigger_error($fetchMapStatement->error);
continue;
}
$fetchMapStatement->store_result();
$fetchMapStatement->bind_result($mxId);
$fetchMapStatement->fetch();
$fetchMapStatement->free_result();
// FIXME: save ids like that?
$mapIds[$map->index] = $mxId;
}
$fetchMapStatement->close();
//code...
//fetch mx info
// Fetch mx data
// TODO: fetch mx info
// Save map data
$saveMapQuery = "UPDATE `" . MapManager::TABLE_MAPS . "`
SET `mxid` = ?
WHERE `index` = ?;";
$saveMapStatement = $mysqli->prepare($saveMapQuery);
if ($mysqli->error) {
trigger_error($mysqli->error);
return;
}
$saveMapStatement->bind_param('ii', $mxId, $mapIndex);
foreach ($maps as $map) {
/** @var Map $map */
//code...
//TODO prepared statment set MX id in maps database
/**
*
* @var Map $map
*/
// FIXME: set $mxId
$mxId = 1337;
$mapIndex = $map->index;
$saveMapStatement->execute();
if ($saveMapStatement->error) {
trigger_error($saveMapStatement->error);
}
}
$saveMapStatement->close();
}
/**
* Gets a Maplist from Mania Exchange
@ -88,7 +117,7 @@ class ManiaExchangeInfoSearcher {
* @param string $env
* @param int $maxMapsReturned
* @param int $searchOrder
* @return array|null
* @return array null
*/
public function getMaps($name = '', $author = '', $env = '', $maxMapsReturned = 100, $searchOrder = self::SEARCH_ORDER_UPDATED_NEWEST) {
// Get Title Id
@ -122,21 +151,11 @@ class ManiaExchangeInfoSearcher {
$mapInfo = $this->get_file($url);
// TODO errors
/*if ($file === false) {
$this->error = 'Connection or response error on ' . $url;
return array();
} elseif ($file === -1) {
$this->error = 'Timed out while reading data from ' . $url;
return array();
} elseif ($file == '') {
if (empty($maps)) {
$this->error = 'No data returned from ' . $url;
return array();
} else {
break;
}
}*/
/*
* if ($file === false) { $this->error = 'Connection or response error on ' . $url; return array(); } elseif ($file === -1) { $this->error =
* 'Timed out while reading data from ' . $url; return array(); } elseif ($file == '') { if (empty($maps)) { $this->error = 'No data returned
* from ' . $url; return array(); } else { break; } }
*/
$mxMapList = json_decode($mapInfo);
if ($mxMapList === null) {
@ -154,7 +173,6 @@ class ManiaExchangeInfoSearcher {
}
private function get_file($url) {
$url = parse_url($url);
$port = isset($url['port']) ? $url['port'] : 80;
$query = isset($url['query']) ? "?" . $url['query'] : "";
@ -164,7 +182,9 @@ class ManiaExchangeInfoSearcher {
return false;
}
fwrite($fp, 'GET ' . $url['path'] . $query . " HTTP/1.0\r\n" . 'Host: ' . $url['host'] . "\r\n" . 'Content-Type: application/json' . "\r\n" . 'User-Agent: ManiaControl v' . ManiaControl::VERSION . "\r\n\r\n");
fwrite($fp,
'GET ' . $url['path'] . $query . " HTTP/1.0\r\n" . 'Host: ' . $url['host'] . "\r\n" . 'Content-Type: application/json' . "\r\n" .
'User-Agent: ManiaControl v' . ManiaControl::VERSION . "\r\n\r\n");
stream_set_timeout($fp, 2);
$res = '';
$info['timed_out'] = false;
@ -176,7 +196,8 @@ class ManiaExchangeInfoSearcher {
if ($info['timed_out']) {
return -1;
} else {
}
else {
if (substr($res, 9, 3) != '200') {
return false;
}
@ -184,7 +205,6 @@ class ManiaExchangeInfoSearcher {
return trim($page[1]);
}
} // get_file
private function getEnvironment($env) {
switch ($env) {
case 'TMCanyon':
@ -198,20 +218,16 @@ class ManiaExchangeInfoSearcher {
return -1;
}
}
}
class MXMapInfo {
public $prefix, $id, $name, $userid, $author, $uploaded, $updated, $type, $maptype, $titlepack, $style, $envir, $mood, $dispcost, $lightmap, $modname, $exever, $exebld, $routes, $length, $unlimiter, $laps, $diffic, $lbrating, $trkvalue, $replaytyp, $replayid, $replaycnt, $acomment, $awards, $comments, $rating, $ratingex, $ratingcnt, $pageurl, $replayurl, $imageurl, $thumburl, $dloadurl;
/**
* Returns map object with all available data from MX map data
*
* @param String $prefix
* MX URL prefix
* @param Object $map
* The MX map data from MXInfoSearcher
* @param String $prefix MX URL prefix
* @param Object $map The MX map data from MXInfoSearcher
* @return MXMapInfo
*/
public function __construct($prefix, $mx) {
@ -219,7 +235,8 @@ class MXMapInfo {
if ($mx) {
if ($this->prefix == 'tm') {
$dir = 'tracks';
} else // 'sm' || 'qm'
}
else // 'sm' || 'qm'
{
$dir = 'maps';
}
@ -227,7 +244,8 @@ class MXMapInfo {
// temporary fix
if ($this->prefix == 'tm' || !property_exists($mx, "MapID")) {
$this->id = $mx->TrackID;
} else {
}
else {
$this->id = $mx->MapID;
}
@ -267,7 +285,8 @@ class MXMapInfo {
if ($this->trkvalue == 0 && $this->lbrating > 0) {
$this->trkvalue = $this->lbrating;
} elseif($this->lbrating == 0 && $this->trkvalue > 0) {
}
elseif ($this->lbrating == 0 && $this->trkvalue > 0) {
$this->lbrating = $this->trkvalue;
}
@ -283,7 +302,8 @@ class MXMapInfo {
if ($this->prefix == 'tm' && $this->replayid > 0) {
$this->replayurl = 'http://' . $this->prefix . '.mania-exchange.com/replays/download/' . $this->replayid;
} else {
}
else {
$this->replayurl = '';
}
}