ENHANCEMENT: Extendeded MetaWeblog API support to include the ability of third party blogging services to post blog entries

This commit is contained in:
Glenn Bautista 2011-03-03 13:14:36 +13:00
parent eef7d57b80
commit efdcd76e43
3 changed files with 121 additions and 10 deletions

View File

@ -19,6 +19,7 @@ The blog module allows you to post blogs on your SilverStripe. It includes the a
* Archive widget
* Blog management widget
* RSS widget (will likely move in future)
* MetaWeblog API support. (posting new blog entries using Posterous, and other blogging services that use this API).
## Configuration Options
@ -38,6 +39,8 @@ We have chosen to go with the following page types to include with the blog modu
* BlogEntry: This is simply an entry/post for the blog.
In order to activate RSD you have to put $RSDLink in Page.ss, or BlogHolder.ss (if it is located in templates, and not Layout). After doing this, just point the blog service to the BlogHolder's url during that service's configuration.
## Simple form for adding a post
@ -65,6 +68,14 @@ for example: mysite/blog/2007/6 would show blog entries for June 2007
or: mysite/blog/2007 would show blog entries for 2007
## Setting up MetaWeblog and RSD (Really Simple Discovery)
MetaWeblogController::$MODERATE_PUBLISHING - indicates whether blog entries posted via the API will need manual approvial via the CMS. (default: false)
MetaWeblogController::$RESTRICT_POST_TO_OWNER - restrict postings to the user defined as 'owner' in the blog holder (default: false)
## Comments and Spam Protection
See [:pagecomment](/pagecomment) for creating Askimet-protected comments for every page.

View File

@ -157,6 +157,7 @@ class BlogHolder_Controller extends BlogTree_Controller {
'tag',
'date',
'metaweblog',
'rsd',
'postblog' => 'BLOGMANAGEMENT',
'post' => 'BLOGMANAGEMENT',
'BlogEntryForm' => 'BLOGMANAGEMENT',
@ -286,6 +287,36 @@ class BlogHolder_Controller extends BlogTree_Controller {
Director::redirect($this->Link());
}
public function RSDLink() {
$url = $this->BaseHref() . $this->URLSegment . '/rsd';
return '<link rel="EditURI" type="application/rsd+xml" title="RSD" href="' . $url . '" />';
}
public function RSD() {
$homepage = Director::absoluteBaseURL();
$controller = $homepage . "metaweblog";
return <<<RSD
<?xml version="1.0" ?>
<rsd version="1.0" xmlns="http://archipelago.phrasewise.com/rsd" >
<service>
<engineName>Silverstripe Blog</engineName>
<engineLink>http://silverstripe.org/blog-module/</engineLink>
<homePageLink>{$homepage}</homePageLink>
<apis>
<api name="MetaWeblog" preferred="true" apiLink="{$controller}" blogID="{$this->ID}" >
<settings>
<notes>This MetaWeblog implementation is currently limited. It is part of the Silverstripe Blog module</notes>
</settings>
</api>
</apis>
</service>
</rsd>
RSD;
}
}

View File

@ -7,7 +7,11 @@ require_once(BASE_PATH . '/blog/thirdparty/xmlrpc/xmlrpc_wrappers.php');
/**
* MetaWeblogController provides the MetaWeblog API for SilverStripe blogs.
*/
class MetaWeblogController extends Controller {
class MetaWeblogController extends Controller {
static $MODERATE_PUBLISHING = false;
static $RESTRICT_POST_TO_OWNER = false;
function index($request) {
Debug::log(Debug::text($request));
@ -21,7 +25,10 @@ class MetaWeblogController extends Controller {
),
'metaWeblog.getCategories' => array(
'function' => array($this, 'getCategories')
)
),
'metaWeblog.newPost' => array(
'function' => array($this, 'newPost')
)
), false);
// Use nice php functions, and call the service
@ -31,15 +38,49 @@ class MetaWeblogController extends Controller {
// Tell SilverStripe not to try render a template
return false;
}
/**
* Authenticate the user.
*
* If blogid is not null, make sure that the user is the author of the blog
*
* @param <string> $username
* @param <string> $password
* @param <int> $blogid
* @return <DataObject> $member
*/
private function authenticate($username, $password, $blogid=null) {
$member = MemberAuthenticator::authenticate(array(
'Email' => $username,
'Password' => $password,
));
if (!$member){
Debug::log(Debug::text("Authentication Failed for " . $username));
return false;
}
Session::set("loggedInAs",$member->ID);
if ($blogid && self::$RESTRICT_POST_TO_OWNER) {
$blogHolder = DataObject::get_one("BlogHolder", "`BlogHolder`.`ID` = " . (int) $blogid);
if ($blogHolder->OwnerID == $member->ID){
return $member;
}
}else {
return $member;
}
return false;
}
/**
* Get a list of BlogHolders the user has access to.
*/
function getUsersBlogs($appkey, $username, $password) {
$member = MemberAuthenticator::authenticate(array(
'Email' => $username,
'Password' => $password,
));
$member = $this->authenticate($username, $password);
// TODO Throw approriate error.
if(!$member) die();
@ -66,10 +107,7 @@ class MetaWeblogController extends Controller {
* Get the most recent posts on a blog.
*/
function getRecentPosts($blogid, $username, $password, $numberOfPosts) {
$member = MemberAuthenticator::authenticate(array(
'Email' => $username,
'Password' => $password,
));
$member = $this->authenticate($username, $password);
// TODO Throw approriate error.
if(!$member) die();
@ -101,6 +139,37 @@ class MetaWeblogController extends Controller {
//TODO dummy function
return array();
}
/**
* Post a new Blog entry onto a Blog
*
* @param <int> $blogid
* @param <string> $username
* @param <string> $password
* @param <array> $struct
* @param <boolean> $publish
* @return Boolean
*/
public function newPost($blogid, $username, $password, $struct, $publish) {
$member = $this->authenticate($username, $password, $blogid);
// TODO Throw approriate error.
if (!$member) die();
$blogEntry = new BlogEntry();
$blogEntry->setField("Title", $struct['title']);
$blogEntry->setField("Content", $struct['description']);
$blogEntry->setField("ParentID", $blogid);
$blogEntry->write();
if($publish && !self::$MODERATE_PUBLISHING){
$blogEntry->doPublish();
}
return true;
}
}
?>