Services_Blogging
[ class tree: Services_Blogging ] [ index: Services_Blogging ] [ all elements ]

Source for file MetaWeblog.php

Documentation is available at MetaWeblog.php

  1. <?php
  2. require_once 'Services/Blogging/Driver/Exception.php';
  3. require_once 'Services/Blogging/ExtendedDriver.php';
  4. require_once 'Services/Blogging/Post.php';
  5. require_once 'Services/Blogging/XmlRpc.php';
  6. require_once 'XML/RPC.php';
  7.  
  8. /**
  9. * metaWeblog API implementation.
  10. * http://www.xmlrpc.com/metaWeblogApi
  11. * http://www.movabletype.org/mt-static/docs/mtmanual_programmatic.html
  12. *
  13. @category Services
  14. @package  Services_Blogging
  15. @author   Anant Narayanan <anant@php.net>
  16. @author   Christian Weiske <cweiske@php.net>
  17. @license  http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  18. @link     http://pear.php.net/package/Services_Blogging
  19. */
  20. {
  21.  
  22.     /**
  23.     * Internal list with user data.
  24.     * @var array 
  25.     */
  26.     protected $userdata = array();
  27.  
  28.     protected $arSupportedPostProperties = array(
  29.         Services_Blogging_Post::TITLE,
  30.         Services_Blogging_Post::CONTENT,
  31.         Services_Blogging_Post::DATE,
  32.         Services_Blogging_Post::URL,
  33.         Services_Blogging_Post::CATEGORIES,
  34.     );
  35.  
  36.  
  37.  
  38.     /**
  39.     * Constructor for the metaWeblog driver class.
  40.     *
  41.     * @param string $user   The username of the blog account.
  42.     * @param string $pass   The password of the blog account.
  43.     * @param string $server The URI of the server to connect to.
  44.     * @param string $path   The path to the XML-RPC server script.
  45.     *
  46.     * @throws Services_Blogging_Exception  If authentication fails
  47.     */
  48.     public function __construct($user$pass$server$path)
  49.     {
  50.         $this->userdata = array(
  51.             'user'      => $user,
  52.             'pass'      => $pass,
  53.             'server'    => $server,
  54.             'path'      => $path,
  55.             'rpc_user'  => new XML_RPC_Value($user'string'),
  56.             'rpc_pass'  => new XML_RPC_Value($pass'string'),
  57.             'rpc_blogid'=> new XML_RPC_Value($user'string'),
  58.         );
  59.  
  60.         $this->rpc_client = new XML_RPC_Client(
  61.             $this->userdata['path'],
  62.             $this->userdata['server']
  63.         );
  64.         //$this->rpc_client->setDebug(true);
  65.     }//public function __construct($userid, $pass, $server, $path)
  66.  
  67.  
  68.  
  69.     /**
  70.     * Save a new post into the blog.
  71.     *
  72.     * @param Services_Blogging_Post $post Post object to put online
  73.     *
  74.     * @return void 
  75.     *
  76.     * @throws Services_Blogging_Exception If an error occured
  77.     */
  78.     public function savePost(Services_Blogging_Post $post)
  79.     {
  80.         if ($post->id === null{
  81.             //post is new and has no Id => create new one
  82.             $request = new XML_RPC_Message('metaWeblog.newPost',
  83.                 array(
  84.                     $this->userdata['rpc_blogid'],
  85.                     $this->userdata['rpc_user'],
  86.                     $this->userdata['rpc_pass'],
  87.                     self::convertPostToStruct($post),
  88.                     new XML_RPC_Value(true'boolean')
  89.                 )
  90.             );
  91.             $nPostId Services_Blogging_XmlRpc::sendRequest(
  92.                 $request$this->rpc_client
  93.             );
  94.             $post->setId($nPostId);
  95.         else {
  96.             //edit the post; it already exists
  97.             $request = new XML_RPC_Message('metaWeblog.editPost',
  98.                 array(
  99.                     new XML_RPC_Value($post->id'string'),
  100.                     $this->userdata['rpc_user'],
  101.                     $this->userdata['rpc_pass'],
  102.                     self::convertPostToStruct($post),
  103.                     new XML_RPC_Value(true'boolean')
  104.                 )
  105.             );
  106.             Services_Blogging_XmlRpc::sendRequest($request$this->rpc_client);
  107.         }
  108.     }//public function savePost(Services_Blogging_Post $post)
  109.  
  110.  
  111.  
  112.     /**
  113.     * The getPost method is intended to retrive a given post as an object of
  114.     * the Services_Blogging_Post class; given the unique post id which is passed
  115.     * as a parameter to the function.
  116.     *
  117.     * @param string $id The PostID of the post to be retrieved. (As
  118.     *                     returned by newPost() defined in
  119.     *                     Services_Blogging_driver).
  120.     *
  121.     * @return Services_Blogging_Post The elements of the post returned as an
  122.     *                                 object of the Services_Blogging_Post class.
  123.     *
  124.     * @throws Services_Blogging_Exception If the post does not exist
  125.     */
  126.     public function getPost($id)
  127.     {
  128.         $request = new XML_RPC_Message('metaWeblog.getPost',
  129.             array(
  130.                 new XML_RPC_Value($id'int'),
  131.                 $this->userdata['rpc_user'],
  132.                 $this->userdata['rpc_pass'],
  133.             )
  134.         );
  135.         $arData Services_Blogging_XmlRpc::sendRequest(
  136.             $request$this->rpc_client
  137.         );
  138.         return $this->convertStructToPost($arData);
  139.     }//public function getPost($id)
  140.  
  141.  
  142.  
  143.     /**
  144.     * Delete a given post.
  145.     * The deletePost method in metaWeblog is just
  146.     *  an alias to the deletePost blogger method
  147.     *
  148.     * @param mixed $post Services_Blogging_Post object to delete,
  149.     *                      or post id (integer) to delete
  150.     *
  151.     * @return boolean True if deleted, false if not.
  152.     */
  153.     public function deletePost($post)
  154.     {
  155.         if (!($post instanceof Services_Blogging_Post)) {
  156.             $nPostId $post;
  157.             $post    = new Services_Blogging_Post();
  158.             $post->setId($nPostId);
  159.         }
  160.  
  161.         $request = new XML_RPC_Message('metaWeblog.deletePost',
  162.             array(
  163.                 //dummy API key
  164.                 new XML_RPC_Value('0123456789ABCDEF''string'),
  165.                 new XML_RPC_Value($post->id'int'),
  166.                 $this->userdata['rpc_user'],
  167.                 $this->userdata['rpc_pass'],
  168.                 new XML_RPC_Value(true'boolean')
  169.             )
  170.         );
  171.         Services_Blogging_XmlRpc::sendRequest($request$this->rpc_client);
  172.     }//public function deletePost($post)
  173.  
  174.  
  175.  
  176.     /**
  177.     * Returns an array of recent posts as Services_Blogging_Post objects
  178.     *
  179.     * @param int $number The number of posts to be retrieved.
  180.     *                      Defaults to 15
  181.     *
  182.     * @return Array An array of objects of the Services_Blogging_Post class that
  183.     *                 correspond to the number of posts requested.
  184.     */
  185.     public function getRecentPosts($number = 15)
  186.     {
  187.         $request = new XML_RPC_Message('metaWeblog.getRecentPosts',
  188.             array(
  189.                 $this->userdata['rpc_blogid'],
  190.                 $this->userdata['rpc_user'],
  191.                 $this->userdata['rpc_pass'],
  192.                 new XML_RPC_Value($number'int')
  193.             )
  194.         );
  195.         $arData Services_Blogging_XmlRpc::sendRequest(
  196.             $request$this->rpc_client
  197.         );
  198.         $arPosts = array();
  199.         foreach ($arData as $data{
  200.             $post               $this->convertStructToPost($data);
  201.             $arPosts[$post->id$post;
  202.         }
  203.         return $arPosts;
  204.     }//public function getRecentPosts($number = 15)
  205.  
  206.  
  207.  
  208.     /**
  209.     * The getRecentPostTitles method is intended to retrieve the given number of
  210.     * posts titles from a blog.
  211.     * The posts themselves can be retrieved with getPost() or getRecentPosts().
  212.     *
  213.     * There is no direct getRecentPostTitles method in metaWeblog. So
  214.     * we internally call getRecentPosts() and strip out ids and titles of
  215.     * the post. So this method is slow here, because all post data needs
  216.     * to be transmitted.
  217.     *
  218.     * @param int $number The number of posts to be retrieved.
  219.     *
  220.     * @return Array An array of int => strings representing the
  221.     *                 post ids (key) and their title (value).
  222.     */
  223.     public function getRecentPostTitles($number = 15)
  224.     {
  225.         $arPosts  $this->getRecentPosts($number);
  226.         $arTitles = array();
  227.         foreach ($arPosts as $post{
  228.             $arTitles[$post->id$post->{Services_Blogging_Post::TITLE};
  229.         }
  230.         return $arTitles;
  231.     }//public function getRecentPostTitles($number = 15)
  232.  
  233.  
  234.  
  235.     /**
  236.     * Returns an array of strings thay define
  237.     * the properties that a post to this blog may
  238.     * have.
  239.     *
  240.     * @param string $strPostType Type of post to create.
  241.     *                            @see getSupportedPostTypes()
  242.     *
  243.     * @return array Array of strings
  244.     */
  245.     public function getSupportedPostProperties($strPostType 'post')
  246.     {
  247.         return $this->arSupportedPostProperties;
  248.     }//public function getSupportedPostProperties(..)
  249.  
  250.  
  251.  
  252.     /**
  253.     * Checks if the given property name/id is supported
  254.     * for this driver.
  255.     *
  256.     * @param string $strProperty Property name/id to check
  257.     * @param string $strPostType Type of post to create.
  258.     *                            @see getSupportedPostTypes()
  259.     *
  260.     * @return boolean If the property is supported
  261.     */
  262.     public function isPostPropertySupported($strProperty$strPostType 'post')
  263.     {
  264.         return in_array($strProperty$this->arSupportedPostProperties);
  265.     }//public function isPostPropertySupported(..)
  266.  
  267.  
  268.  
  269.     /**
  270.     * Converts a struct returned by the webservice to
  271.     * a Services_Blogging_Post object
  272.     *
  273.     * @param array $arStruct Struct to convert
  274.     *
  275.     * @return Services_Blogging_Post Converted post
  276.     */
  277.     protected function convertStructToPost($arStruct)
  278.     {
  279.         $post = new Services_Blogging_Post($this);
  280.  
  281.         $post->{Services_Blogging_Post::CONTENT$arStruct['description'];
  282.         $post->{Services_Blogging_Post::TITLE}   $arStruct['title'];
  283.         //0123456789012345678
  284.         //20060514T09:19:33
  285.         $post->{Services_Blogging_Post::DATEmktime(
  286.             substr($arStruct['dateCreated'],  92)//hour
  287.             substr($arStruct['dateCreated']122)//minute
  288.             substr($arStruct['dateCreated']152)//second
  289.             substr($arStruct['dateCreated'],  42)//month
  290.             substr($arStruct['dateCreated'],  62)//day
  291.             substr($arStruct['dateCreated'],  04)  //year
  292.         );
  293.         $post->{Services_Blogging_Post::URL$arStruct['link'];
  294.         if (!isset($arStruct['categories'])) {
  295.             $arStruct['categories'= array();
  296.         }
  297.         $post->{Services_Blogging_Post::CATEGORIES$arStruct['categories'];
  298.         $post->setId($arStruct['postid']);
  299.  
  300.         return $post;
  301.     }//protected function convertStructToPost($arStruct)
  302.  
  303.  
  304.  
  305.     /**
  306.     * Converts Services_Blogging_Post object to
  307.     * an XML-RPC struct that can be sent to the server.
  308.     *
  309.     * @param Services_Blogging_Post $post Post object to convert
  310.     *
  311.     * @return void 
  312.     */
  313.     protected function convertPostToStruct($post)
  314.     {
  315.         $time $post->{Services_Blogging_Post::DATE};
  316.         if ($time == ''  || $time == 0{
  317.             $time time();
  318.         }
  319.         $categories $post->{Services_Blogging_Post::CATEGORIES};
  320.         if (!is_array($categories)) {
  321.             $categories = array();
  322.         else {
  323.             $catstr     $categories;
  324.             $categories = array();
  325.             foreach ($catstr as $cat{
  326.                 $categories[= new XML_RPC_Value($cat'string');
  327.             }
  328.         }
  329.  
  330.         return new XML_RPC_Value(
  331.             array(
  332.                 'categories'  => new XML_RPC_Value($categories'array'),
  333.                 'dateCreated' => new XML_RPC_Value(date('Ymd\\TH:i:s'$time)'dateTime.iso8601'),
  334.                 'description' => new XML_RPC_Value($post->{Services_Blogging_Post::CONTENT}'string'),
  335.                 'title'       => new XML_RPC_Value($post->{Services_Blogging_Post::TITLE}'string')
  336.             ),
  337.             'struct'
  338.         );
  339.     }//protected function convertPostToStruct($post)
  340.  
  341. }//class Services_Blogging_Driver_MetaWeblog extends Services_Blogging_ExtendedDriver
  342. ?>

Documentation generated on Mon, 11 Mar 2019 15:25:12 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.