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

Source for file Blogging.php

Documentation is available at Blogging.php

  1. <?php
  2. require_once 'Services/Blogging/Exception.php';
  3.  
  4. /**
  5. *   Generic package for several Blogging APIs.
  6. *
  7. *   Create a new instance via
  8. *   Services_Blogging::factory($driver, $username, $password, $server, $path),
  9. *   or more easy via
  10. *   Services_Blogging::discoverDriver($url, $username, $password) .
  11. *
  12. *   Note that some Blogging APIs allow multiple blogs with one
  13. *   account. These drivers implement Services_Blogging_MultipleBlogsInterface
  14. *   - you need to call setBlogId($id) before you can use the driver in that case.
  15. *
  16. *   @category   Services
  17. *   @package    Services_Blogging
  18. *   @author     Christian Weiske <cweiske@php.net>
  19. *   @author     Anant Narayanan <anant@php.net>
  20. *   @license    LGPL
  21. *   @version    $Id$
  22. *
  23. *   @todo
  24. *    Missing drivers
  25. *    - MovableType
  26. *    - Conversant
  27. *    - Manila
  28. *    - MetaWiki
  29. *    - Antville
  30. */
  31. {
  32.  
  33.     /**
  34.      * Exception codes and messages that are thrown by the class.
  35.      */
  36.     const ERROR_DRIVER = 101;
  37.     const ERROR_BLOGHASNTAUTODISCOVERY = 102;
  38.     const ERROR_NOSUPPORTEDDRIVER = 103;
  39.  
  40.  
  41.  
  42.     /**
  43.      * The factory function that instantiates the appropriate class and returns
  44.      * the object, so that further methods may be executed. This function serves
  45.      * as the single entry point for the class.
  46.      *
  47.      * @param   string  $driver     The driver name, currently either "Blogger" or "metaWeblog".
  48.      * @param   string  $username   The username of the blog account to connect to.
  49.      * @param   string  $password   The password of the blog account to connect to.
  50.      * @param   string  $server     The URI of the blog's server.
  51.      * @param   string  $path       The location of the XML-RPC server script.
  52.      */
  53.     public static function factory($driver$username$password$server$path)
  54.     {
  55.         include_once 'Services/Blogging/Driver/' $driver '.php';
  56.         $strClass 'Services_Blogging_Driver_' $driver;
  57.         if (!class_exists($strClass)) {
  58.             throw new Services_Blogging_Exception('Invalid driver "' $driver '" specified!'self::ERROR_DRIVER);
  59.         }
  60.  
  61.         $class = new $strClass($username$password$server$path);
  62.         return $class;
  63.     }//public static function factory($driver, $username, $password, $server, $path)
  64.  
  65.  
  66.  
  67.     
  68.     /**
  69.     *   Autodiscover the driver settings from the given blog URL
  70.     *   and create a driver instance.
  71.     *
  72.     *   @param string   $url        Blog URL
  73.     *   @param string   $username   Username for the blog account
  74.     *   @param string   $password   Password for the blog account
  75.     *
  76.     *   @return Services_Blogging_Driver    The driver object if all goes ok
  77.     *
  78.     *   @throws Services_Blogging_Exception If an error occured
  79.     */
  80.     public static function discoverDriver($url$username$password)
  81.     {
  82.         $settings = self::discoverSettings($url);
  83.         if ($settings === false{
  84.             throw new Services_Blogging_Exception(
  85.                 'Autodiscovery of settings not supported by the blog',
  86.                 self::ERROR_BLOGHASNTAUTODISCOVERY
  87.             );
  88.         }
  89.         $driver   = self::getBestAvailableDriver($settings);
  90.         if ($driver === false{
  91.             throw new Services_Blogging_Exception(
  92.                 'None of the supported drivers available',
  93.                 self::ERROR_NOSUPPORTEDDRIVER
  94.             );
  95.         }
  96.  
  97.         return self::factory(
  98.             $driver,
  99.             $username,
  100.             $password,
  101.             $settings['apis'][$driver]['server'],
  102.             $settings['apis'][$driver]['path']
  103.         );
  104.     }//public static function discoverDriver($url, $username, $password)
  105.  
  106.  
  107.  
  108.     
  109.     /**
  110.     *   Tries to auto-discover the driver settings for the blog
  111.     *   at the given URL.
  112.     *   Internally, an RSD page is tried to load and read.
  113.     *
  114.     *   @param string $url  Url of the blog
  115.     *
  116.     *   @return mixed  FALSE if nothing found, OR array of settings:
  117.     *                    - engineName
  118.     *                    - engineLink
  119.     *                    - homePageLink
  120.     *                    - apis => array (key is name
  121.     *                        - name
  122.     *                        - preferred
  123.     *                        - apiLink (url)
  124.     *                        - server (for factory())
  125.     *                        - path   (for factory())
  126.     *
  127.     *   @link http://archipelago.phrasewise.com/display?page=oldsite/1330.html
  128.     */
  129.     public static function discoverSettings($url)
  130.     {
  131.         $content file_get_contents($url);
  132.         if ($content === false{
  133.             return false;
  134.         }
  135.  
  136.         //search for a line like this:
  137.         //<link rel="EditURI" type="application/rsd+xml" title="RSD"
  138.         // href="http://blog.bogo/xmlrpc.php?rsd" />
  139.         if (!preg_match_all('|<link\\s+rel="EditURI".+href="(.+?)"|'$content$matches)) {
  140.             return false;
  141.         }
  142.  
  143.         $rsdUrl reset($matches[1]);
  144.         $root = simplexml_load_string(file_get_contents($rsdUrl));
  145.         if ($root === false{
  146.             return false;
  147.         }
  148.  
  149.         $apis = array();
  150.         foreach ($root->service->apis->api as $api{
  151.             $ap = array();
  152.             $ap['name']      = (string)$api['name'];
  153.             if ($ap['name'== 'Movable Type'{
  154.                 $ap['name''MovableType';
  155.             }
  156.             $ap['preferred'$api['preferred'== 1 || $api['preferred'== 'true';
  157.             $ap['apiLink']   = (string)$api['apiLink'];
  158.  
  159.             //try to get server and path
  160.             $dslashpos strpos($ap['apiLink']'//');
  161.             if ($dslashpos === false{
  162.                 $nBegin = 0;
  163.             else {
  164.                 $nBegin $dslashpos + 2;
  165.             }
  166.             $slashpos strpos($ap['apiLink']'/'$nBegin);
  167.             $ap['server'substr($ap['apiLink']0$slashpos);
  168.             $ap['path']   substr($ap['apiLink']$slashpos);
  169.  
  170.             $apis[$ap['name']] $ap;
  171.         }
  172.  
  173.         $data = array(
  174.             'engineName'   => (string)$root->service->engineName,
  175.             'engineLink'   => (string)$root->service->engineLink,
  176.             'homePageLink' => (string)$root->service->homePageLink,
  177.             'apis'         => $apis
  178.         );
  179.         return $data;
  180.     }//public static function discoverSettings($url)
  181.  
  182.  
  183.  
  184.     
  185.     /**
  186.     *   Tries to return the best available driver for the given
  187.     *   settings array. The settings array is returned by
  188.     *   Services_Blogging::discoverSettings()
  189.     *
  190.     *   @param array    $arSettings     Settings array
  191.     *   @return string  The driver to use, false if none found
  192.     */
  193.     public static function getBestAvailableDriver($arSettings)
  194.     {
  195.         if (isset($arSettings['apis'])) {
  196.             $arSettings $arSettings['apis'];
  197.         }
  198.         //find preferred one
  199.         $driver = null;
  200.         foreach ($arSettings as $id => $api{
  201.             if ($api['preferred'=== true{
  202.                 if (self::driverExists($api['name'])) {
  203.                     return $api['name'];
  204.                 }
  205.                 unset($arSettings[$id]);
  206.             }
  207.         }
  208.         foreach ($arSettings as $id => $api{
  209.             if (self::driverExists($api['name'])) {
  210.                 return $api['name'];
  211.             }
  212.         }
  213.  
  214.         return false;
  215.     }//public static function getBestAvailableDriver($arSettings)
  216.  
  217.  
  218.  
  219.     
  220.     /**
  221.     *   Tries to include the driver file and checks if
  222.     *   the driver class exists.
  223.     *
  224.     *   @param string   $driver     Driver to check
  225.     *   @return boolean     If the driver exists
  226.     */
  227.     protected static function driverExists($driver)
  228.     {
  229.         @include_once 'Services/Blogging/Driver/' $driver '.php';
  230.         return class_exists('Services_Blogging_Driver_' $driver);
  231.     }//protected static function driverExists($driver)
  232.  
  233. }//class Services_Blogging
  234. ?>

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