Permalink
Browse files

querying now works as expected

  • Loading branch information...
1 parent 563ecad commit a3258cda6952c83588566b0311876b10b1cf2097 @a-musing-moose committed Feb 25, 2011
@@ -27,8 +27,12 @@ public static function createDomFromJson($json)
if (isset($data['code']) && $data['code'] == Constants::API_RESPONSE_CODE_OK ) {
$node = self::createNode($data['result'], 'root');
} else {
- $messages = \implode(". ", $data['messages']);
- throw new \freebase\exception\ApiError($messages);
+ if (\is_array($data['messages'][0])) {
+ $message = $data['messages'][0]['message'];
+ } else {
+ $message = \implode(". ", $data['messages']);
+ }
+ throw new \freebase\exception\ApiError($message);
}
return $node;
}
@@ -12,25 +12,30 @@
*/
class Freebase
{
-
+ /**
+ * @var string
+ */
private $baseFetchUrl;
- private $baseSearchUrl;
+ /**
+ * @var string
+ */
+ private $baseApiUrl;
/**
* @param string $baseUrl
*/
- public function __construct($baseFetchUrl, $baseSearchUrl)
+ public function __construct($baseFetchUrl = "http://www.freebase.com/experimental/topic/standard/", $baseApiUrl = "http://api.freebase.com/api/service/")
{
if (\substr($baseFetchUrl, -1) !== '/') {
$baseFetchUrl .= '/';
}
$this->baseFetchUrl = $baseFetchUrl;
- if (\substr($baseSearchUrl, -1) !== '/') {
- $baseSearchUrl .= '/';
+ if (\substr($baseApiUrl, -1) !== '/') {
+ $baseApiUrl .= '/';
}
- $this->baseSearchUrl = $baseSearchUrl;
+ $this->baseApiUrl = $baseApiUrl;
}
/**
@@ -43,13 +48,19 @@ public function fetchById($id)
$id = \substr($id, 1); //strip first / if needed as already insured in constructor
}
$url = $this->baseFetchUrl . $id;
- $json = \file_get_contents($url);
+ $json = $this->doRequest($url);
return DomFactory::createDomFromJson($json);
}
+ /**
+ * @param Query $query
+ * @return \freebase\Node
+ */
public function fetchByQuery(Query $query)
{
-
+ $url = $this->baseApiUrl . 'mqlread';
+ $json = $this->doRequest($url, $query->__toJson());
+ return DomFactory::createDomFromJson($json);
}
/**
@@ -65,7 +76,7 @@ protected function doRequest($url, $jsonData = null)
\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, true);
if (null !== $jsonData) {
\curl_setopt($ch, \CURLOPT_POST, true);
- \curl_setopt($ch, \CURLOPT_POSTFIELDS, $jsonData);
+ \curl_setopt($ch, \CURLOPT_POSTFIELDS, array('query' => $jsonData));
}
$response = \curl_exec($ch);
\curl_close($ch);
@@ -73,12 +73,22 @@ public function setParent(\freebase\Node $parent)
//////////////////////////////
/**
+ * Returns all attributes as an array keyed by the attribute name
+ *
+ * @return array
+ */
+ public function getAttributes()
+ {
+ return $this->attributes;
+ }
+
+ /**
* @param mixed $value
* @return \freebase\Node
*/
public function setAttributeValue($key, $value)
{
- $this->properties[$key] = $value;
+ $this->attributes[$key] = $value;
return $this;
}
@@ -89,8 +99,8 @@ public function setAttributeValue($key, $value)
public function getAttributeValue($key)
{
$value = null;
- if (\array_key_exists($key, $this->properties)) {
- $value = $this->properties[$key];
+ if (\array_key_exists($key, $this->attributes)) {
+ $value = $this->attributes[$key];
}
return $value;
}
@@ -35,10 +35,8 @@ public function getCriteria()
public function __toJson()
{
$query = array(
- 'q1'=>array(
- 'query' => $this->criteria
- )
- );
+ 'query' => $this->criteria
+ );
return \json_encode($query);
}

0 comments on commit a3258cd

Please sign in to comment.