PageRenderTime 31ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/demos/Zend/Gdata/Blogger.php

https://github.com/sidealice/zf2
PHP | 373 lines | 158 code | 47 blank | 168 comment | 13 complexity | 4f5f39c2b0b1516c5bb67383df7ea55a 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_Gdata
  17. * @subpackage Demos
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /*
  22. * This sample utilizes the Zend Gdata Client Library, which can be
  23. * downloaded from: http://framework.zend.com/download
  24. *
  25. * This sample is meant to show basic CRUD (Create, Retrieve, Update
  26. * and Delete) functionality of the Blogger data API, and can only
  27. * be run from the command line.
  28. *
  29. * To run the sample:
  30. * php Blogger.php --user=email@email.com --pass=password
  31. */
  32. /**
  33. * @see Zend_Loader
  34. */
  35. require_once 'Zend/Loader.php';
  36. /**
  37. * @see Zend_Gdata
  38. */
  39. Zend_Loader::loadClass('Zend_Gdata');
  40. /**
  41. * @see Zend_Gdata_Query
  42. */
  43. Zend_Loader::loadClass('Zend_Gdata_Query');
  44. /**
  45. * @see Zend_Gdata_ClientLogin
  46. */
  47. Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
  48. /**
  49. * Class that contains all simple CRUD operations for Blogger.
  50. *
  51. * @category Zend
  52. * @package Zend_Gdata
  53. * @subpackage Demos
  54. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  55. * @license http://framework.zend.com/license/new-bsd New BSD License
  56. */
  57. class SimpleCRUD
  58. {
  59. /**
  60. * $blogID - Blog ID used for demo operations
  61. *
  62. * @var string
  63. */
  64. public $blogID;
  65. /**
  66. * $gdClient - Client class used to communicate with the Blogger service
  67. *
  68. * @var Zend_Gdata_Client
  69. */
  70. public $gdClient;
  71. /**
  72. * Constructor for the class. Takes in user credentials and generates the
  73. * the authenticated client object.
  74. *
  75. * @param string $email The user's email address.
  76. * @param string $password The user's password.
  77. * @return void
  78. */
  79. public function __construct($email, $password)
  80. {
  81. $client = Zend_Gdata_ClientLogin::getHttpClient($email, $password, 'blogger');
  82. $this->gdClient = new Zend_Gdata($client);
  83. }
  84. /**
  85. * This function retrieves all the blogs associated with the authenticated
  86. * user and prompts the user to choose which to manipulate.
  87. *
  88. * Once the index is selected by the user, the corresponding blogID is
  89. * extracted and stored for easy access.
  90. *
  91. * @return void
  92. */
  93. public function promptForBlogID()
  94. {
  95. $query = new Zend_Gdata_Query('http://www.blogger.com/feeds/default/blogs');
  96. $feed = $this->gdClient->getFeed($query);
  97. $this->printFeed($feed);
  98. $input = getInput("\nSelection");
  99. //id text is of the form: tag:blogger.com,1999:user-blogID.blogs
  100. $idText = explode('-', $feed->entries[$input]->id->text);
  101. $this->blogID = $idText[2];
  102. }
  103. /**
  104. * This function creates a new Zend_Gdata_Entry representing a blog
  105. * post, and inserts it into the user's blog. It also checks for
  106. * whether the post should be added as a draft or as a published
  107. * post.
  108. *
  109. * @param string $title The title of the blog post.
  110. * @param string $content The body of the post.
  111. * @param boolean $isDraft Whether the post should be added as a draft or as a published post
  112. * @return string The newly created post's ID
  113. */
  114. public function createPost($title, $content, $isDraft=False)
  115. {
  116. // We're using the magic factory method to create a Zend_Gdata_Entry.
  117. // http://framework.zend.com/manual/en/zend.gdata.html#zend.gdata.introdduction.magicfactory
  118. $entry = $this->gdClient->newEntry();
  119. $entry->title = $this->gdClient->newTitle(trim($title));
  120. $entry->content = $this->gdClient->newContent(trim($content));
  121. $entry->content->setType('text');
  122. $uri = "http://www.blogger.com/feeds/" . $this->blogID . "/posts/default";
  123. if ($isDraft)
  124. {
  125. $control = $this->gdClient->newControl();
  126. $draft = $this->gdClient->newDraft('yes');
  127. $control->setDraft($draft);
  128. $entry->control = $control;
  129. }
  130. $createdPost = $this->gdClient->insertEntry($entry, $uri);
  131. //format of id text: tag:blogger.com,1999:blog-blogID.post-postID
  132. $idText = explode('-', $createdPost->id->text);
  133. $postID = $idText[2];
  134. return $postID;
  135. }
  136. /**
  137. * Prints the titles of all the posts in the user's blog.
  138. *
  139. * @return void
  140. */
  141. public function printAllPosts()
  142. {
  143. $query = new Zend_Gdata_Query('http://www.blogger.com/feeds/' . $this->blogID . '/posts/default');
  144. $feed = $this->gdClient->getFeed($query);
  145. $this->printFeed($feed);
  146. }
  147. /**
  148. * Retrieves the specified post and updates the title and body. Also sets
  149. * the post's draft status.
  150. *
  151. * @param string $postID The ID of the post to update. PostID in <id> field:
  152. * tag:blogger.com,1999:blog-blogID.post-postID
  153. * @param string $updatedTitle The new title of the post.
  154. * @param string $updatedContent The new body of the post.
  155. * @param boolean $isDraft Whether the post will be published or saved as a draft.
  156. * @return Zend_Gdata_Entry The updated post.
  157. */
  158. public function updatePost($postID, $updatedTitle, $updatedContent, $isDraft)
  159. {
  160. $query = new Zend_Gdata_Query('http://www.blogger.com/feeds/' . $this->blogID . '/posts/default/' . $postID);
  161. $postToUpdate = $this->gdClient->getEntry($query);
  162. $postToUpdate->title->text = $this->gdClient->newTitle(trim($updatedTitle));
  163. $postToUpdate->content->text = $this->gdClient->newContent(trim($updatedContent));
  164. if ($isDraft) {
  165. $draft = $this->gdClient->newDraft('yes');
  166. } else {
  167. $draft = $this->gdClient->newDraft('no');
  168. }
  169. $control = $this->gdClient->newControl();
  170. $control->setDraft($draft);
  171. $postToUpdate->control = $control;
  172. $updatedPost = $postToUpdate->save();
  173. return $updatedPost;
  174. }
  175. /**
  176. * This function uses query parameters to retrieve and print all posts
  177. * within a specified date range.
  178. *
  179. * @param string $startDate Beginning date, inclusive. Preferred format is a RFC-3339 date,
  180. * though other formats are accepted.
  181. * @param string $endDate End date, exclusive.
  182. * @return void
  183. */
  184. public function printPostsInDateRange($startDate, $endDate)
  185. {
  186. $query = new Zend_Gdata_Query('http://www.blogger.com/feeds/' . $this->blogID . '/posts/default');
  187. $query->setParam('published-min', $startDate);
  188. $query->setParam('published-max', $endDate);
  189. $feed = $this->gdClient->getFeed($query);
  190. $this->printFeed($feed);
  191. }
  192. /**
  193. * This function creates a new comment and adds it to the specified post.
  194. * A comment is created as a Zend_Gdata_Entry.
  195. *
  196. * @param string $postID The ID of the post to add the comment to. PostID
  197. * in the <id> field: tag:blogger.com,1999:blog-blogID.post-postID
  198. * @param string $commentText The text of the comment to add.
  199. * @return string The ID of the newly created comment.
  200. */
  201. public function createComment($postID, $commentText)
  202. {
  203. $uri = 'http://www.blogger.com/feeds/' . $this->blogID . '/' . $postID . '/comments/default';
  204. $newComment = $this->gdClient->newEntry();
  205. $newComment->content = $this->gdClient->newContent($commentText);
  206. $newComment->content->setType('text');
  207. $createdComment = $this->gdClient->insertEntry($newComment, $uri);
  208. echo 'Added new comment: ' . $createdComment->content->text . "\n";
  209. // Edit link follows format: /feeds/blogID/postID/comments/default/commentID
  210. $editLink = explode('/', $createdComment->getEditLink()->href);
  211. $commentID = $editLink[8];
  212. return $commentID;
  213. }
  214. /**
  215. * This function prints all comments associated with the specified post.
  216. *
  217. * @param string $postID The ID of the post whose comments we'll print.
  218. * @return void
  219. */
  220. public function printAllComments($postID)
  221. {
  222. $query = new Zend_Gdata_Query('http://www.blogger.com/feeds/' . $this->blogID . '/' . $postID . '/comments/default');
  223. $feed = $this->gdClient->getFeed($query);
  224. $this->printFeed($feed);
  225. }
  226. /**
  227. * This function deletes the specified comment from a post.
  228. *
  229. * @param string $postID The ID of the post where the comment is. PostID in
  230. * the <id> field: tag:blogger.com,1999:blog-blogID.post-postID
  231. * @param string $commentID The ID of the comment to delete. The commentID
  232. * in the editURL: /feeds/blogID/postID/comments/default/commentID
  233. * @return void
  234. */
  235. public function deleteComment($postID, $commentID)
  236. {
  237. $uri = 'http://www.blogger.com/feeds/' . $this->blogID . '/' . $postID . '/comments/default/' . $commentID;
  238. $this->gdClient->delete($uri);
  239. }
  240. /**
  241. * This function deletes the specified post.
  242. *
  243. * @param string $postID The ID of the post to delete.
  244. * @return void
  245. */
  246. public function deletePost($postID)
  247. {
  248. $uri = 'http://www.blogger.com/feeds/' . $this->blogID . '/posts/default/' . $postID;
  249. $this->gdClient->delete($uri);
  250. }
  251. /**
  252. * Helper function to print out the titles of all supplied Blogger
  253. * feeds.
  254. *
  255. * @param Zend_Gdata_Feed The feed to print.
  256. * @return void
  257. */
  258. public function printFeed($feed)
  259. {
  260. $i = 0;
  261. foreach($feed->entries as $entry)
  262. {
  263. echo "\t" . $i ." ". $entry->title->text . "\n";
  264. $i++;
  265. }
  266. }
  267. /**
  268. * Runs the sample.
  269. *
  270. * @return void
  271. */
  272. public function run()
  273. {
  274. echo "Note: This sample may Create, Read, Update and Delete data " .
  275. "stored in the account provided. Please exit now if you provided " .
  276. "an account which contains important data.\n\n";
  277. $this->promptForBlogID();
  278. echo "Creating a post.\n";
  279. $this->createPost('Hello, world!', 'I am on the intarweb!', False);
  280. echo "Creating a draft post.\n";
  281. $postID = $this->createPost('Salutations, world!', 'Does not sound right.. must work on title.', True);
  282. echo "Updating the previous post and publishing it.\n";
  283. $updatedPost = $this->updatePost($postID, 'Hello, world, it is.', 'There we go.', False);
  284. echo "The new title of the post is: " . $updatedPost->title->text . "\n";
  285. echo "The new body of the post is: " . $updatedPost->content->text . "\n";
  286. echo "Adding a comment to the previous post.\n";
  287. $this->createComment($postID, 'I am so glad this is public now.');
  288. echo "Adding another comment.\n";
  289. $commentID = $this->createComment($postID, 'This is a spammy comment.');
  290. echo "Deleting the previous comment.\n";
  291. $this->deleteComment($postID, $commentID);
  292. echo "Printing all posts.\n";
  293. $this->printAllPosts();
  294. echo "Printing posts between 2007-01-01 and 2007-03-01.\n";
  295. $this->printPostsInDateRange('2007-01-01','2007-06-30');
  296. echo "Deleting the post titled: " . $updatedPost->title->text . "\n";
  297. $this->deletePost($postID);
  298. }
  299. }
  300. /**
  301. * Gets credentials from user.
  302. *
  303. * @param string $text
  304. * @return string Index of the blog the user has chosen.
  305. */
  306. function getInput($text)
  307. {
  308. echo $text.': ';
  309. return trim(fgets(STDIN));
  310. }
  311. $user = null;
  312. $pass = null;
  313. // process command line options
  314. foreach ($argv as $argument) {
  315. $argParts = explode('=', $argument);
  316. if ($argParts[0] == '--user') {
  317. $user = $argParts[1];
  318. } else if ($argParts[0] == '--pass') {
  319. $pass = $argParts[1];
  320. }
  321. }
  322. if (($user == null) || ($pass == null)) {
  323. exit("php Blogger.php --user=[username] --pass=[password]\n");
  324. }
  325. $sample = new SimpleCRUD($user, $pass);
  326. $sample->run();