/system/application/libraries/Zend/Service/Simpy.php

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