PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/DevApp/library/Zend/Service/Simpy.php

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