PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/website/library/Zend/Service/Simpy.php

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