Permalink
Browse files

Support expanded user API

  • Loading branch information...
1 parent d1e52c9 commit 652114f62408d1ff82e2663511074b61217ce0ab @kenguest committed Oct 2, 2012
@@ -239,10 +239,10 @@ public function createNode($latitude, $longitude, array $tags = array())
public function getUser()
{
$config = $this->getConfig()->asArray();
- $url = $config['server']
- . 'api/'
- . $config['api_version']
- . '/user/details';
+ $url = $config['server']
+ . 'api/'
+ . $config['api_version']
+ . '/user/details';
$user = $config['user'];
$password = $config['password'];
try {
@@ -288,6 +288,33 @@ public function getUser()
return $obj;
}
+ public function getUserById($id)
+ {
+ $config = $this->getConfig()->asArray();
+ $url = $config['server']
+ . 'api/'
+ . $config['api_version']
+ . '/user/' . $id;
+ try {
+ $response = $this->getTransport()->getResponse(
+ $url,
+ HTTP_Request2::METHOD_GET
+ );
+ } catch (Services_OpenStreetMap_Exception $ex) {
+ switch ($ex->getCode()) {
+ case Services_OpenStreetMap_Transport::NOT_FOUND:
+ case Services_OpenStreetMap_Transport::UNAUTHORISED:
+ case Services_OpenStreetMap_Transport::GONE:
+ return false;
+ default:
+ throw $ex;
+ }
+ }
+ $obj = new Services_OpenStreetMap_User();
+ $obj->setXml(simplexml_load_string($response->getBody()));
+ return $obj;
+ }
+
/**
* Get details of specified way
*
@@ -145,7 +145,11 @@ public function commit()
// Generate URL that the osmChange document will be posted to
$cId = $this->getId();
if (!is_numeric($cId)) {
- throw new Services_OpenStreetMap_RuntimeException('Changeset ID of unexpected type.');
+ if ($cId !== null) {
+ $msg = 'Changeset ID of unexpected type. (';
+ $msg .= var_export($cId, true) . ')';
+ throw new Services_OpenStreetMap_RuntimeException($msg);
+ }
}
$config = $this->getConfig()->asArray();
$url = $config['server']
@@ -233,6 +237,13 @@ public function getOsmChangeXml()
return $this->osmChangeXml;
}
+ /**
+ * setOsmChangeXml
+ *
+ * @param string $xml OsmChange XML
+ *
+ * @return Services_OpenStreetMap_Changeset
+ */
public function setOsmChangeXml($xml)
{
$this->osmChangeXml = $xml;
@@ -130,6 +130,9 @@ public function getLanguages()
$langers = array();
$cxml = simplexml_load_string($this->xml);
$languages = $cxml->xpath('//user/languages');
+ if (empty($languages)) {
+ return null;
+ }
foreach ($languages[0]->children() as $child) {
if ($child->getName() == 'lang') {
$langers[] = (string) $child[0];
@@ -168,6 +171,130 @@ public function getLon()
}
/**
+ * Zoom level of 'home' setting for user.
+ *
+ * @return integer
+ */
+ public function getZoom()
+ {
+ $cxml = simplexml_load_string($this->xml);
+ $home = $cxml->xpath('//user/home');
+ if (empty($home)) {
+ return null;
+ }
+ return (integer) $home[0]->attributes()->zoom;
+ }
+
+ /**
+ * The number of changesets opened by the user.
+ *
+ * @return integer
+ */
+ public function getChangesets()
+ {
+ $cxml = simplexml_load_string($this->xml);
+ $changesets = $cxml->xpath('//user/changesets');
+ if (empty($changesets)) {
+ return null;
+ }
+ return (integer) $changesets[0]->attributes()->count;
+ }
+
+ /**
+ * The number of traces uploaded by the user.
+ *
+ * @return integer
+ */
+ public function getTraces()
+ {
+ $cxml = simplexml_load_string($this->xml);
+ $traces = $cxml->xpath('//user/traces');
+ if (empty($traces)) {
+ return null;
+ }
+ return (integer) $traces[0]->attributes()->count;
+ }
+
+ /**
+ * The [total] number of blocks received by the user.
+ *
+ * @return integer
+ */
+ public function getBlocksReceived()
+ {
+ $cxml = simplexml_load_string($this->xml);
+ $changesets = $cxml->xpath('//user/blocks/received');
+ if (empty($changesets)) {
+ return null;
+ }
+ return (integer) $changesets[0]->attributes()->count;
+ }
+
+ /**
+ * The number of active blocks received by the user.
+ *
+ * @return integer
+ */
+ public function getActiveBlocksReceived()
+ {
+ $cxml = simplexml_load_string($this->xml);
+ $changesets = $cxml->xpath('//user/blocks/received');
+ if (empty($changesets)) {
+ return null;
+ }
+ return (integer) $changesets[0]->attributes()->active;
+ }
+
+ /**
+ * The [total] number of blocks issued by the user.
+ *
+ * @return integer
+ */
+ public function getBlocksIssued()
+ {
+ $cxml = simplexml_load_string($this->xml);
+ $changesets = $cxml->xpath('//user/blocks/issued');
+ if (empty($changesets)) {
+ return null;
+ }
+ return (integer) $changesets[0]->attributes()->count;
+ }
+
+ /**
+ * The number of active blocks issued by the user.
+ *
+ * @return integer
+ */
+ public function getActiveBlocksIssued()
+ {
+ $cxml = simplexml_load_string($this->xml);
+ $changesets = $cxml->xpath('//user/blocks/issued');
+ if (empty($changesets)) {
+ return null;
+ }
+ return (integer) $changesets[0]->attributes()->active;
+ }
+
+ /**
+ * Array of names of roles associated with the user.
+ *
+ * @return array
+ */
+ public function getRoles()
+ {
+ $ret = array();
+ $cxml = simplexml_load_string($this->xml);
+ $roles = $cxml->xpath('//user/roles');
+ if (empty($roles)) {
+ return $ret;
+ }
+ foreach ($roles[0]->children() as $child) {
+ $ret[] = $child->getName();
+ }
+ return $ret;
+ }
+
+ /**
* return an array of the user's preferences.
*
* @return array
View
@@ -64,9 +64,17 @@ public function testUser()
);
$this->assertEquals($user->getAccountCreated(), "2003-09-02T15:27:52Z");
$this->assertEquals($user->getDescription(), "Yabba dabba do!");
- $this->assertEquals($user->getLon(), null);
- $this->assertEquals($user->getLat(), null);
+ $this->assertEquals($user->getLon(), -8.2284600830085);
+ $this->assertEquals($user->getLat(), 52.222687925572);
+ $this->assertEquals($user->getZoom(), 3);
+ $this->assertEquals($user->getChangesets(), 1910);
+ $this->assertEquals($user->getTraces(), 115);
+ $this->assertEquals($user->getBlocksReceived(), 1);
+ $this->assertEquals($user->getBlocksIssued(), 15);
+ $this->assertEquals($user->getActiveBlocksReceived(), 0);
+ $this->assertEquals($user->getActiveBlocksIssued(), 4);
$this->assertEquals($user->getLanguages(), array('en-US','en'));
+ $this->assertEquals($user->getRoles(), array('moderator'));
$this->assertEquals(
$user->getPreferences(),
array( "diary.default_language" => "en")
@@ -119,6 +127,32 @@ public function testUserHomeSet()
$this->assertEquals($user->getLat(), 1.234567);
$this->assertEquals($user->getLon(), -1.234567);
}
+
+ public function testUser11324()
+ {
+ $mock = new HTTP_Request2_Adapter_Mock();
+ $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
+ $mock->addResponse(fopen(__DIR__ . '/responses/user11324.xml', 'rb'));
+ $config = array(
+ 'adapter' => $mock,
+ 'server' => 'http://api06.dev.openstreetmap.org/',
+ 'user' => 'fred@example.com',
+ 'password' => 'w1lma4evah'
+ );
+
+ $osm = new Services_OpenStreetMap($config);
+ $user = $osm->getUserById(6367);
+ $this->assertEquals($user->getDisplayName(), 'kenguest');
+ $this->assertEquals($user->getId(), 11324);
+ $this->assertEquals($user->getChangesets(), 1910);
+ $this->assertEquals($user->getTraces(), 115);
+ $this->assertEquals($user->getBlocksReceived(), 0);
+ $this->assertEquals($user->getActiveBlocksReceived(), 0);
+ $this->assertNull($user->getBlocksIssued());
+ $this->assertNull($user->getActiveBlocksIssued());
+ $this->assertNull($user->getLanguages());
+ $this->assertEquals($user->getRoles(), array());
+ }
}
// vim:set et ts=4 sw=4:
?>
@@ -8,6 +8,16 @@ HTTP/1.1 200 OK
</description>
<contributor-terms agreed="true" pd="true"/>
<img href="http://www.openstreetmap.org/user/image/124/me.jpg"/>
+ <roles>
+ <moderator/>
+ </roles>
+ <changesets count="1910"/>
+ <traces count="115"/>
+ <blocks>
+ <received active="0" count="1"/>
+ <issued active="4" count="15"/>
+ </blocks>
+ <home lon="-8.2284600830085" lat="52.222687925572" zoom="3"/>
<languages>
<lang>en-US</lang>
<lang>en</lang>
@@ -0,0 +1,19 @@
+HTTP/1.1 200 OK
+
+<?xml version="1.0" encoding="UTF-8"?>
+<osm version="0.6" generator="OpenStreetMap server">
+ <user display_name="kenguest" account_created="2007-08-01T15:57:57Z" id="11324">
+ <description>I'm 'radagast' on the #osm and #osm-ie IRC channels - and kenguest on twitter and identica (and github).
+
+</description>
+ <contributor-terms agreed="true"/>
+ <img href="http://www.openstreetmap.org/attachments/users/images/000/011/324/original/02e3111587fe9079b73991ced69ab7f9.jpg"/>
+ <roles>
+ </roles>
+ <changesets count="1910"/>
+ <traces count="115"/>
+ <blocks>
+ <received active="0" count="0"/>
+ </blocks>
+ </user>
+</osm>

0 comments on commit 652114f

Please sign in to comment.