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

/common/libraries/plugin/google/demos/Zend/Gdata/Blogger.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 378 lines | 165 code | 45 blank | 168 comment | 12 complexity | ca332d0d5241542994d450673e81b96e MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  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. {
  166. $draft = $this->gdClient->newDraft('yes');
  167. }
  168. else
  169. {
  170. $draft = $this->gdClient->newDraft('no');
  171. }
  172. $control = $this->gdClient->newControl();
  173. $control->setDraft($draft);
  174. $postToUpdate->control = $control;
  175. $updatedPost = $postToUpdate->save();
  176. return $updatedPost;
  177. }
  178. /**
  179. * This function uses query parameters to retrieve and print all posts
  180. * within a specified date range.
  181. *
  182. * @param string $startDate Beginning date, inclusive. Preferred format is a RFC-3339 date,
  183. * though other formats are accepted.
  184. * @param string $endDate End date, exclusive.
  185. * @return void
  186. */
  187. public function printPostsInDateRange($startDate, $endDate)
  188. {
  189. $query = new Zend_Gdata_Query('http://www.blogger.com/feeds/' . $this->blogID . '/posts/default');
  190. $query->setParam('published-min', $startDate);
  191. $query->setParam('published-max', $endDate);
  192. $feed = $this->gdClient->getFeed($query);
  193. $this->printFeed($feed);
  194. }
  195. /**
  196. * This function creates a new comment and adds it to the specified post.
  197. * A comment is created as a Zend_Gdata_Entry.
  198. *
  199. * @param string $postID The ID of the post to add the comment to. PostID
  200. * in the <id> field: tag:blogger.com,1999:blog-blogID.post-postID
  201. * @param string $commentText The text of the comment to add.
  202. * @return string The ID of the newly created comment.
  203. */
  204. public function createComment($postID, $commentText)
  205. {
  206. $uri = 'http://www.blogger.com/feeds/' . $this->blogID . '/' . $postID . '/comments/default';
  207. $newComment = $this->gdClient->newEntry();
  208. $newComment->content = $this->gdClient->newContent($commentText);
  209. $newComment->content->setType('text');
  210. $createdComment = $this->gdClient->insertEntry($newComment, $uri);
  211. echo 'Added new comment: ' . $createdComment->content->text . "\n";
  212. // Edit link follows format: /feeds/blogID/postID/comments/default/commentID
  213. $editLink = explode('/', $createdComment->getEditLink()->href);
  214. $commentID = $editLink[8];
  215. return $commentID;
  216. }
  217. /**
  218. * This function prints all comments associated with the specified post.
  219. *
  220. * @param string $postID The ID of the post whose comments we'll print.
  221. * @return void
  222. */
  223. public function printAllComments($postID)
  224. {
  225. $query = new Zend_Gdata_Query('http://www.blogger.com/feeds/' . $this->blogID . '/' . $postID . '/comments/default');
  226. $feed = $this->gdClient->getFeed($query);
  227. $this->printFeed($feed);
  228. }
  229. /**
  230. * This function deletes the specified comment from a post.
  231. *
  232. * @param string $postID The ID of the post where the comment is. PostID in
  233. * the <id> field: tag:blogger.com,1999:blog-blogID.post-postID
  234. * @param string $commentID The ID of the comment to delete. The commentID
  235. * in the editURL: /feeds/blogID/postID/comments/default/commentID
  236. * @return void
  237. */
  238. public function deleteComment($postID, $commentID)
  239. {
  240. $uri = 'http://www.blogger.com/feeds/' . $this->blogID . '/' . $postID . '/comments/default/' . $commentID;
  241. $this->gdClient->delete($uri);
  242. }
  243. /**
  244. * This function deletes the specified post.
  245. *
  246. * @param string $postID The ID of the post to delete.
  247. * @return void
  248. */
  249. public function deletePost($postID)
  250. {
  251. $uri = 'http://www.blogger.com/feeds/' . $this->blogID . '/posts/default/' . $postID;
  252. $this->gdClient->delete($uri);
  253. }
  254. /**
  255. * Helper function to print out the titles of all supplied Blogger
  256. * feeds.
  257. *
  258. * @param Zend_Gdata_Feed The feed to print.
  259. * @return void
  260. */
  261. public function printFeed($feed)
  262. {
  263. $i = 0;
  264. foreach ($feed->entries as $entry)
  265. {
  266. echo "\t" . $i . " " . $entry->title->text . "\n";
  267. $i ++;
  268. }
  269. }
  270. /**
  271. * Runs the sample.
  272. *
  273. * @return void
  274. */
  275. public function run()
  276. {
  277. echo "Note: This sample may Create, Read, Update and Delete data " . "stored in the account provided. Please exit now if you provided " . "an account which contains important data.\n\n";
  278. $this->promptForBlogID();
  279. echo "Creating a post.\n";
  280. $this->createPost('Hello, world!', 'I am on the intarweb!', False);
  281. echo "Creating a draft post.\n";
  282. $postID = $this->createPost('Salutations, world!', 'Does not sound right.. must work on title.', True);
  283. echo "Updating the previous post and publishing it.\n";
  284. $updatedPost = $this->updatePost($postID, 'Hello, world, it is.', 'There we go.', False);
  285. echo "The new title of the post is: " . $updatedPost->title->text . "\n";
  286. echo "The new body of the post is: " . $updatedPost->content->text . "\n";
  287. echo "Adding a comment to the previous post.\n";
  288. $this->createComment($postID, 'I am so glad this is public now.');
  289. echo "Adding another comment.\n";
  290. $commentID = $this->createComment($postID, 'This is a spammy comment.');
  291. echo "Deleting the previous comment.\n";
  292. $this->deleteComment($postID, $commentID);
  293. echo "Printing all posts.\n";
  294. $this->printAllPosts();
  295. echo "Printing posts between 2007-01-01 and 2007-03-01.\n";
  296. $this->printPostsInDateRange('2007-01-01', '2007-06-30');
  297. echo "Deleting the post titled: " . $updatedPost->title->text . "\n";
  298. $this->deletePost($postID);
  299. }
  300. }
  301. /**
  302. * Gets credentials from user.
  303. *
  304. * @param string $text
  305. * @return string Index of the blog the user has chosen.
  306. */
  307. function getInput($text)
  308. {
  309. echo $text . ': ';
  310. return trim(fgets(STDIN));
  311. }
  312. $user = null;
  313. $pass = null;
  314. // process command line options
  315. foreach ($argv as $argument)
  316. {
  317. $argParts = explode('=', $argument);
  318. if ($argParts[0] == '--user')
  319. {
  320. $user = $argParts[1];
  321. }
  322. else
  323. if ($argParts[0] == '--pass')
  324. {
  325. $pass = $argParts[1];
  326. }
  327. }
  328. if (($user == null) || ($pass == null))
  329. {
  330. exit("php Blogger.php --user=[username] --pass=[password]\n");
  331. }
  332. $sample = new SimpleCRUD($user, $pass);
  333. $sample->run();