PageRenderTime 66ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Feed/Pubsubhubbub/Publisher.php

https://bitbucket.org/rtsukui/jelly2
PHP | 418 lines | 222 code | 24 blank | 172 comment | 31 complexity | 9b368005c7f61389eea3bd90a214a78b MD5 | raw file
Possible License(s): BSD-3-Clause
  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_Feed_Pubsubhubbub
  17. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Publisher.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /**
  22. * @see Zend_Feed_Pubsubhubbub
  23. */
  24. require_once 'Zend/Feed/Pubsubhubbub.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Feed_Pubsubhubbub
  28. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Feed_Pubsubhubbub_Publisher
  32. {
  33. /**
  34. * An array of URLs for all Hub Servers used by the Publisher, and to
  35. * which all topic update notifications will be sent.
  36. *
  37. * @var array
  38. */
  39. protected $_hubUrls = array();
  40. /**
  41. * An array of topic (Atom or RSS feed) URLs which have been updated and
  42. * whose updated status will be notified to all Hub Servers.
  43. *
  44. * @var array
  45. */
  46. protected $_updatedTopicUrls = array();
  47. /**
  48. * An array of any errors including keys for 'response', 'hubUrl'.
  49. * The response is the actual Zend_Http_Response object.
  50. *
  51. * @var array
  52. */
  53. protected $_errors = array();
  54. /**
  55. * An array of topic (Atom or RSS feed) URLs which have been updated and
  56. * whose updated status will be notified to all Hub Servers.
  57. *
  58. * @var array
  59. */
  60. protected $_parameters = array();
  61. /**
  62. * Constructor; accepts an array or Zend_Config instance to preset
  63. * options for the Publisher without calling all supported setter
  64. * methods in turn.
  65. *
  66. * @param array|Zend_Config $options Options array or Zend_Config instance
  67. * @return void
  68. */
  69. public function __construct($config = null)
  70. {
  71. if ($config !== null) {
  72. $this->setConfig($config);
  73. }
  74. }
  75. /**
  76. * Process any injected configuration options
  77. *
  78. * @param array|Zend_Config $options Options array or Zend_Config instance
  79. * @return Zend_Feed_Pubsubhubbub_Publisher
  80. */
  81. public function setConfig($config)
  82. {
  83. if ($config instanceof Zend_Config) {
  84. $config = $config->toArray();
  85. } elseif (!is_array($config)) {
  86. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  87. throw new Zend_Feed_Pubsubhubbub_Exception('Array or Zend_Config object'
  88. . 'expected, got ' . gettype($config));
  89. }
  90. if (array_key_exists('hubUrls', $config)) {
  91. $this->addHubUrls($config['hubUrls']);
  92. }
  93. if (array_key_exists('updatedTopicUrls', $config)) {
  94. $this->addUpdatedTopicUrls($config['updatedTopicUrls']);
  95. }
  96. if (array_key_exists('parameters', $config)) {
  97. $this->setParameters($config['parameters']);
  98. }
  99. return $this;
  100. }
  101. /**
  102. * Add a Hub Server URL supported by Publisher
  103. *
  104. * @param string $url
  105. * @return Zend_Feed_Pubsubhubbub_Publisher
  106. */
  107. public function addHubUrl($url)
  108. {
  109. if (empty($url) || !is_string($url) || !Zend_Uri::check($url)) {
  110. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  111. throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "url"'
  112. .' of "' . $url . '" must be a non-empty string and a valid'
  113. .'URL');
  114. }
  115. $this->_hubUrls[] = $url;
  116. return $this;
  117. }
  118. /**
  119. * Add an array of Hub Server URLs supported by Publisher
  120. *
  121. * @param array $urls
  122. * @return Zend_Feed_Pubsubhubbub_Publisher
  123. */
  124. public function addHubUrls(array $urls)
  125. {
  126. foreach ($urls as $url) {
  127. $this->addHubUrl($url);
  128. }
  129. return $this;
  130. }
  131. /**
  132. * Remove a Hub Server URL
  133. *
  134. * @param string $url
  135. * @return Zend_Feed_Pubsubhubbub_Publisher
  136. */
  137. public function removeHubUrl($url)
  138. {
  139. if (!in_array($url, $this->getHubUrls())) {
  140. return $this;
  141. }
  142. $key = array_search($url, $this->_hubUrls);
  143. unset($this->_hubUrls[$key]);
  144. return $this;
  145. }
  146. /**
  147. * Return an array of unique Hub Server URLs currently available
  148. *
  149. * @return array
  150. */
  151. public function getHubUrls()
  152. {
  153. $this->_hubUrls = array_unique($this->_hubUrls);
  154. return $this->_hubUrls;
  155. }
  156. /**
  157. * Add a URL to a topic (Atom or RSS feed) which has been updated
  158. *
  159. * @param string $url
  160. * @return Zend_Feed_Pubsubhubbub_Publisher
  161. */
  162. public function addUpdatedTopicUrl($url)
  163. {
  164. if (empty($url) || !is_string($url) || !Zend_Uri::check($url)) {
  165. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  166. throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "url"'
  167. .' of "' . $url . '" must be a non-empty string and a valid'
  168. .'URL');
  169. }
  170. $this->_updatedTopicUrls[] = $url;
  171. return $this;
  172. }
  173. /**
  174. * Add an array of Topic URLs which have been updated
  175. *
  176. * @param array $urls
  177. * @return Zend_Feed_Pubsubhubbub_Publisher
  178. */
  179. public function addUpdatedTopicUrls(array $urls)
  180. {
  181. foreach ($urls as $url) {
  182. $this->addUpdatedTopicUrl($url);
  183. }
  184. return $this;
  185. }
  186. /**
  187. * Remove an updated topic URL
  188. *
  189. * @param string $url
  190. * @return Zend_Feed_Pubsubhubbub_Publisher
  191. */
  192. public function removeUpdatedTopicUrl($url)
  193. {
  194. if (!in_array($url, $this->getUpdatedTopicUrls())) {
  195. return $this;
  196. }
  197. $key = array_search($url, $this->_updatedTopicUrls);
  198. unset($this->_updatedTopicUrls[$key]);
  199. return $this;
  200. }
  201. /**
  202. * Return an array of unique updated topic URLs currently available
  203. *
  204. * @return array
  205. */
  206. public function getUpdatedTopicUrls()
  207. {
  208. $this->_updatedTopicUrls = array_unique($this->_updatedTopicUrls);
  209. return $this->_updatedTopicUrls;
  210. }
  211. /**
  212. * Notifies a single Hub Server URL of changes
  213. *
  214. * @param string $url The Hub Server's URL
  215. * @return void
  216. * @throws Zend_Feed_Pubsubhubbub_Exception Thrown on failure
  217. */
  218. public function notifyHub($url)
  219. {
  220. if (empty($url) || !is_string($url) || !Zend_Uri::check($url)) {
  221. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  222. throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "url"'
  223. .' of "' . $url . '" must be a non-empty string and a valid'
  224. .'URL');
  225. }
  226. $client = $this->_getHttpClient();
  227. $client->setUri($url);
  228. $response = $client->request();
  229. if ($response->getStatus() !== 204) {
  230. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  231. throw new Zend_Feed_Pubsubhubbub_Exception('Notification to Hub Server '
  232. . 'at "' . $url . '" appears to have failed with a status code of "'
  233. . $response->getStatus() . '" and message "'
  234. . $response->getMessage() . '"');
  235. }
  236. }
  237. /**
  238. * Notifies all Hub Server URLs of changes
  239. *
  240. * If a Hub notification fails, certain data will be retained in an
  241. * an array retrieved using getErrors(), if a failure occurs for any Hubs
  242. * the isSuccess() check will return FALSE. This method is designed not
  243. * to needlessly fail with an Exception/Error unless from Zend_Http_Client.
  244. *
  245. * @return void
  246. * @throws Zend_Feed_Pubsubhubbub_Exception Thrown if no hubs attached
  247. */
  248. public function notifyAll()
  249. {
  250. $client = $this->_getHttpClient();
  251. $hubs = $this->getHubUrls();
  252. if (empty($hubs)) {
  253. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  254. throw new Zend_Feed_Pubsubhubbub_Exception('No Hub Server URLs'
  255. . ' have been set so no notifcations can be sent');
  256. }
  257. $this->_errors = array();
  258. foreach ($hubs as $url) {
  259. $client->setUri($url);
  260. $response = $client->request();
  261. if ($response->getStatus() !== 204) {
  262. $this->_errors[] = array(
  263. 'response' => $response,
  264. 'hubUrl' => $url
  265. );
  266. }
  267. }
  268. }
  269. /**
  270. * Add an optional parameter to the update notification requests
  271. *
  272. * @param string $name
  273. * @param string|null $value
  274. * @return Zend_Feed_Pubsubhubbub_Publisher
  275. */
  276. public function setParameter($name, $value = null)
  277. {
  278. if (is_array($name)) {
  279. $this->setParameters($name);
  280. return $this;
  281. }
  282. if (empty($name) || !is_string($name)) {
  283. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  284. throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "name"'
  285. .' of "' . $name . '" must be a non-empty string');
  286. }
  287. if ($value === null) {
  288. $this->removeParameter($name);
  289. return $this;
  290. }
  291. if (empty($value) || (!is_string($value) && $value !== null)) {
  292. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  293. throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "value"'
  294. .' of "' . $value . '" must be a non-empty string');
  295. }
  296. $this->_parameters[$name] = $value;
  297. return $this;
  298. }
  299. /**
  300. * Add an optional parameter to the update notification requests
  301. *
  302. * @param array $parameters
  303. * @return Zend_Feed_Pubsubhubbub_Publisher
  304. */
  305. public function setParameters(array $parameters)
  306. {
  307. foreach ($parameters as $name => $value) {
  308. $this->setParameter($name, $value);
  309. }
  310. return $this;
  311. }
  312. /**
  313. * Remove an optional parameter for the notification requests
  314. *
  315. * @param string $name
  316. * @return Zend_Feed_Pubsubhubbub_Publisher
  317. */
  318. public function removeParameter($name)
  319. {
  320. if (empty($name) || !is_string($name)) {
  321. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  322. throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "name"'
  323. .' of "' . $name . '" must be a non-empty string');
  324. }
  325. if (array_key_exists($name, $this->_parameters)) {
  326. unset($this->_parameters[$name]);
  327. }
  328. return $this;
  329. }
  330. /**
  331. * Return an array of optional parameters for notification requests
  332. *
  333. * @return array
  334. */
  335. public function getParameters()
  336. {
  337. return $this->_parameters;
  338. }
  339. /**
  340. * Returns a boolean indicator of whether the notifications to Hub
  341. * Servers were ALL successful. If even one failed, FALSE is returned.
  342. *
  343. * @return bool
  344. */
  345. public function isSuccess()
  346. {
  347. if (count($this->_errors) > 0) {
  348. return false;
  349. }
  350. return true;
  351. }
  352. /**
  353. * Return an array of errors met from any failures, including keys:
  354. * 'response' => the Zend_Http_Response object from the failure
  355. * 'hubUrl' => the URL of the Hub Server whose notification failed
  356. *
  357. * @return array
  358. */
  359. public function getErrors()
  360. {
  361. return $this->_errors;
  362. }
  363. /**
  364. * Get a basic prepared HTTP client for use
  365. *
  366. * @return Zend_Http_Client
  367. */
  368. protected function _getHttpClient()
  369. {
  370. $client = Zend_Feed_Pubsubhubbub::getHttpClient();
  371. $client->setMethod(Zend_Http_Client::POST);
  372. $client->setConfig(array(
  373. 'useragent' => 'Zend_Feed_Pubsubhubbub_Publisher/' . Zend_Version::VERSION,
  374. ));
  375. $params = array();
  376. $params[] = 'hub.mode=publish';
  377. $topics = $this->getUpdatedTopicUrls();
  378. if (empty($topics)) {
  379. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  380. throw new Zend_Feed_Pubsubhubbub_Exception('No updated topic URLs'
  381. . ' have been set');
  382. }
  383. foreach ($topics as $topicUrl) {
  384. $params[] = 'hub.url=' . urlencode($topicUrl);
  385. }
  386. $optParams = $this->getParameters();
  387. foreach ($optParams as $name => $value) {
  388. $params[] = urlencode($name) . '=' . urlencode($value);
  389. }
  390. $paramString = implode('&', $params);
  391. $client->setRawData($paramString, 'application/x-www-form-urlencoded');
  392. return $client;
  393. }
  394. }