Simpy.php 13 KB

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