Simpy.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service
  17. * @subpackage Simpy
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Http_Client
  24. */
  25. require_once 'Zend/Http/Client.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Service
  29. * @subpackage Simpy
  30. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @link http://www.simpy.com/doc/api/rest/
  33. */
  34. class Zend_Service_Simpy
  35. {
  36. /**
  37. * Base URI to which API methods and parameters will be appended
  38. *
  39. * @var string
  40. */
  41. protected $_baseUri = 'http://simpy.com/simpy/api/rest/';
  42. /**
  43. * HTTP client for use in making web service calls
  44. *
  45. * @var Zend_Http_Client
  46. */
  47. protected $_http;
  48. /**
  49. * Constructs a new Simpy (free) REST API Client
  50. *
  51. * @param string $username Username for the Simpy user account
  52. * @param string $password Password for the Simpy user account
  53. * @return void
  54. */
  55. public function __construct($username, $password)
  56. {
  57. /**
  58. * @see Zend_Service_Rest
  59. */
  60. $this->_http = new Zend_Http_Client;
  61. $this->_http->setAuth($username, $password);
  62. }
  63. /**
  64. * Returns the HTTP client currently in use by this class for REST API
  65. * calls, intended mainly for testing.
  66. *
  67. * @return Zend_Http_Client
  68. */
  69. public function getHttpClient()
  70. {
  71. return $this->_http;
  72. }
  73. /**
  74. * Sends a request to the REST API service and does initial processing
  75. * on the response.
  76. *
  77. * @param string $op Name of the operation for the request
  78. * @param array $query Query data for the request (optional)
  79. * @throws Zend_Service_Exception
  80. * @return DOMDocument Parsed XML response
  81. */
  82. protected function _makeRequest($op, $query = null)
  83. {
  84. if ($query != null) {
  85. $query = array_diff($query, array_filter($query, 'is_null'));
  86. $query = '?' . http_build_query($query);
  87. }
  88. $this->_http->setUri($this->_baseUri . $op . '.do' . $query);
  89. $response = $this->_http->request('GET');
  90. if ($response->isSuccessful()) {
  91. $doc = new DOMDocument();
  92. $doc->loadXML($response->getBody());
  93. $xpath = new DOMXPath($doc);
  94. $list = $xpath->query('/status/code');
  95. if ($list->length > 0) {
  96. $code = $list->item(0)->nodeValue;
  97. if ($code != 0) {
  98. $list = $xpath->query('/status/message');
  99. $message = $list->item(0)->nodeValue;
  100. /**
  101. * @see Zend_Service_Exception
  102. */
  103. require_once 'Zend/Service/Exception.php';
  104. throw new Zend_Service_Exception($message, $code);
  105. }
  106. }
  107. return $doc;
  108. }
  109. /**
  110. * @see Zend_Service_Exception
  111. */
  112. require_once 'Zend/Service/Exception.php';
  113. throw new Zend_Service_Exception($response->getMessage(), $response->getStatus());
  114. }
  115. /**
  116. * Returns a list of all tags and their counts, ordered by count in
  117. * decreasing order
  118. *
  119. * @param int $limit Limits the number of tags returned (optional)
  120. * @link http://www.simpy.com/doc/api/rest/GetTags
  121. * @throws Zend_Service_Exception
  122. * @return Zend_Service_Simpy_TagSet
  123. */
  124. public function getTags($limit = null)
  125. {
  126. $query = array(
  127. 'limit' => $limit
  128. );
  129. $doc = $this->_makeRequest('GetTags', $query);
  130. /**
  131. * @see Zend_Service_Simpy_TagSet
  132. */
  133. require_once 'Zend/Service/Simpy/TagSet.php';
  134. return new Zend_Service_Simpy_TagSet($doc);
  135. }
  136. /**
  137. * Removes a tag.
  138. *
  139. * @param string $tag Tag to be removed
  140. * @link http://www.simpy.com/doc/api/rest/RemoveTag
  141. * @return Zend_Service_Simpy Provides a fluent interface
  142. */
  143. public function removeTag($tag)
  144. {
  145. $query = array(
  146. 'tag' => $tag
  147. );
  148. $this->_makeRequest('RemoveTag', $query);
  149. return $this;
  150. }
  151. /**
  152. * Renames a tag.
  153. *
  154. * @param string $fromTag Tag to be renamed
  155. * @param string $toTag New tag name
  156. * @link http://www.simpy.com/doc/api/rest/RenameTag
  157. * @return Zend_Service_Simpy Provides a fluent interface
  158. */
  159. public function renameTag($fromTag, $toTag)
  160. {
  161. $query = array(
  162. 'fromTag' => $fromTag,
  163. 'toTag' => $toTag
  164. );
  165. $this->_makeRequest('RenameTag', $query);
  166. return $this;
  167. }
  168. /**
  169. * Merges two tags into a new tag.
  170. *
  171. * @param string $fromTag1 First tag to merge.
  172. * @param string $fromTag2 Second tag to merge.
  173. * @param string $toTag Tag to merge the two tags into.
  174. * @link http://www.simpy.com/doc/api/rest/MergeTags
  175. * @return Zend_Service_Simpy Provides a fluent interface
  176. */
  177. public function mergeTags($fromTag1, $fromTag2, $toTag)
  178. {
  179. $query = array(
  180. 'fromTag1' => $fromTag1,
  181. 'fromTag2' => $fromTag2,
  182. 'toTag' => $toTag
  183. );
  184. $this->_makeRequest('MergeTags', $query);
  185. return $this;
  186. }
  187. /**
  188. * Splits a single tag into two separate tags.
  189. *
  190. * @param string $tag Tag to split
  191. * @param string $toTag1 First tag to split into
  192. * @param string $toTag2 Second tag to split into
  193. * @link http://www.simpy.com/doc/api/rest/SplitTag
  194. * @return Zend_Service_Simpy Provides a fluent interface
  195. */
  196. public function splitTag($tag, $toTag1, $toTag2)
  197. {
  198. $query = array(
  199. 'tag' => $tag,
  200. 'toTag1' => $toTag1,
  201. 'toTag2' => $toTag2
  202. );
  203. $this->_makeRequest('SplitTag', $query);
  204. return $this;
  205. }
  206. /**
  207. * Performs a query on existing links and returns the results or returns all
  208. * links if no particular query is specified (which should be used sparingly
  209. * to prevent overloading Simpy servers)
  210. *
  211. * @param Zend_Service_Simpy_LinkQuery $q Query object to use (optional)
  212. * @return Zend_Service_Simpy_LinkSet
  213. */
  214. public function getLinks(Zend_Service_Simpy_LinkQuery $q = null)
  215. {
  216. if ($q != null) {
  217. $query = array(
  218. 'q' => $q->getQueryString(),
  219. 'limit' => $q->getLimit(),
  220. 'date' => $q->getDate(),
  221. 'afterDate' => $q->getAfterDate(),
  222. 'beforeDate' => $q->getBeforeDate()
  223. );
  224. $doc = $this->_makeRequest('GetLinks', $query);
  225. } else {
  226. $doc = $this->_makeRequest('GetLinks');
  227. }
  228. /**
  229. * @see Zend_Service_Simpy_LinkSet
  230. */
  231. require_once 'Zend/Service/Simpy/LinkSet.php';
  232. return new Zend_Service_Simpy_LinkSet($doc);
  233. }
  234. /**
  235. * Saves a given link.
  236. *
  237. * @param string $title Title of the page to save
  238. * @param string $href URL of the page to save
  239. * @param int $accessType ACCESSTYPE_PUBLIC or ACCESSTYPE_PRIVATE
  240. * @param mixed $tags String containing a comma-separated list of
  241. * tags or array of strings containing tags
  242. * (optional)
  243. * @param string $urlNickname Alternative custom title (optional)
  244. * @param string $note Free text note (optional)
  245. * @link Zend_Service_Simpy::ACCESSTYPE_PUBLIC
  246. * @link Zend_Service_Simpy::ACCESSTYPE_PRIVATE
  247. * @link http://www.simpy.com/doc/api/rest/SaveLink
  248. * @return Zend_Service_Simpy Provides a fluent interface
  249. */
  250. public function saveLink($title, $href, $accessType, $tags = null, $urlNickname = null, $note = null)
  251. {
  252. if (is_array($tags)) {
  253. $tags = implode(',', $tags);
  254. }
  255. $query = array(
  256. 'title' => $title,
  257. 'href' => $href,
  258. 'accessType' => $accessType,
  259. 'tags' => $tags,
  260. 'urlNickname' => $urlNickname,
  261. 'note' => $note
  262. );
  263. $this->_makeRequest('SaveLink', $query);
  264. return $this;
  265. }
  266. /**
  267. * Deletes a given link.
  268. *
  269. * @param string $href URL of the bookmark to delete
  270. * @link http://www.simpy.com/doc/api/rest/DeleteLink
  271. * @return Zend_Service_Simpy Provides a fluent interface
  272. */
  273. public function deleteLink($href)
  274. {
  275. $query = array(
  276. 'href' => $href
  277. );
  278. $this->_makeRequest('DeleteLink', $query);
  279. return $this;
  280. }
  281. /**
  282. * Return a list of watchlists and their meta-data, including the number
  283. * of new links added to each watchlist since last login.
  284. *
  285. * @link http://www.simpy.com/doc/api/rest/GetWatchlists
  286. * @return Zend_Service_Simpy_WatchlistSet
  287. */
  288. public function getWatchlists()
  289. {
  290. $doc = $this->_makeRequest('GetWatchlists');
  291. /**
  292. * @see Zend_Service_Simpy_WatchlistSet
  293. */
  294. require_once 'Zend/Service/Simpy/WatchlistSet.php';
  295. return new Zend_Service_Simpy_WatchlistSet($doc);
  296. }
  297. /**
  298. * Returns the meta-data for a given watchlist.
  299. *
  300. * @param int $watchlistId ID of the watchlist to retrieve
  301. * @link http://www.simpy.com/doc/api/rest/GetWatchlist
  302. * @return Zend_Service_Simpy_Watchlist
  303. */
  304. public function getWatchlist($watchlistId)
  305. {
  306. $query = array(
  307. 'watchlistId' => $watchlistId
  308. );
  309. $doc = $this->_makeRequest('GetWatchlist', $query);
  310. /**
  311. * @see Zend_Service_Simpy_Watchlist
  312. */
  313. require_once 'Zend/Service/Simpy/Watchlist.php';
  314. return new Zend_Service_Simpy_Watchlist($doc->documentElement);
  315. }
  316. /**
  317. * Returns all notes in reverse chronological order by add date or by
  318. * rank.
  319. *
  320. * @param string $q Query string formatted using Simpy search syntax
  321. * and search fields (optional)
  322. * @param int $limit Limits the number notes returned (optional)
  323. * @link http://www.simpy.com/doc/api/rest/GetNotes
  324. * @link http://www.simpy.com/simpy/FAQ.do#searchSyntax
  325. * @link http://www.simpy.com/simpy/FAQ.do#searchFieldsLinks
  326. * @return Zend_Service_Simpy_NoteSet
  327. */
  328. public function getNotes($q = null, $limit = null)
  329. {
  330. $query = array(
  331. 'q' => $q,
  332. 'limit' => $limit
  333. );
  334. $doc = $this->_makeRequest('GetNotes', $query);
  335. /**
  336. * @see Zend_Service_Simpy_NoteSet
  337. */
  338. require_once 'Zend/Service/Simpy/NoteSet.php';
  339. return new Zend_Service_Simpy_NoteSet($doc);
  340. }
  341. /**
  342. * Saves a note.
  343. *
  344. * @param string $title Title of the note
  345. * @param mixed $tags String containing a comma-separated list of
  346. * tags or array of strings containing tags
  347. * (optional)
  348. * @param string $description Free-text note (optional)
  349. * @param int $noteId Unique identifier for an existing note to
  350. * update (optional)
  351. * @link http://www.simpy.com/doc/api/rest/SaveNote
  352. * @return Zend_Service_Simpy Provides a fluent interface
  353. */
  354. public function saveNote($title, $tags = null, $description = null, $noteId = null)
  355. {
  356. if (is_array($tags)) {
  357. $tags = implode(',', $tags);
  358. }
  359. $query = array(
  360. 'title' => $title,
  361. 'tags' => $tags,
  362. 'description' => $description,
  363. 'noteId' => $noteId
  364. );
  365. $this->_makeRequest('SaveNote', $query);
  366. return $this;
  367. }
  368. /**
  369. * Deletes a given note.
  370. *
  371. * @param int $noteId ID of the note to delete
  372. * @link http://www.simpy.com/doc/api/rest/DeleteNote
  373. * @return Zend_Service_Simpy Provides a fluent interface
  374. */
  375. public function deleteNote($noteId)
  376. {
  377. $query = array(
  378. 'noteId' => $noteId
  379. );
  380. $this->_makeRequest('DeleteNote', $query);
  381. return $this;
  382. }
  383. }