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

Documentation generated on Mon, 11 Mar 2019 14:57:59 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.