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

Documentation generated on Sat, 27 Jan 2007 12:00:11 -0500 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.