PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Service/Simpy.php

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