mirror of
https://github.com/silverstripe/silverstripe-blog
synced 2024-10-22 11:05:58 +02:00
FEATURE: Add send trackback notification
This commit is contained in:
parent
dc879cb49f
commit
68df540615
@ -265,4 +265,3 @@ class BlogEntry_Controller extends Page_Controller {
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
|
@ -1,13 +1,84 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* TODO: Multiple trackback urls for each blog entry
|
||||
*/
|
||||
class TrackBackDecorator extends DataObjectDecorator {
|
||||
|
||||
static $trackback_server_class = 'TrackbackHTTPServer';
|
||||
|
||||
function extraStatics() {
|
||||
return array(
|
||||
'db' => array(
|
||||
'TrackbackURL' => 'Varchar(2048)',
|
||||
'PungTrackbackURL' => 'Varchar(2048)'
|
||||
),
|
||||
'has_many' => array(
|
||||
'TrackBacks' => 'TrackBackPing'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function updateCMSFields($fields) {
|
||||
// Trackback URL field
|
||||
if($this->owner->TrackBacksEnabled()) {
|
||||
$fields->addFieldToTab("Root.Content.Main", new TextField("TrackbackURL", _t("BlogEntry.TRACKBACKURL", "Trackback URL")), "Content");
|
||||
}
|
||||
else {
|
||||
$fields->addFieldToTab("Root.Content.Main", new ReadonlyField("TrackbackURLReadOnly", _t("BlogEntry.TRACKBACKURL", "Trackback URL"), _t("BlogEntry.TRACKBACKURL_DISABLED", "To use this feature, please check 'Enable TrackBacks' check box on the blog holder.")), "Content");
|
||||
}
|
||||
}
|
||||
|
||||
function onBeforePublish() {
|
||||
$owner = $this->owner;
|
||||
|
||||
if(!empty($owner->TrackbackURL) && $owner->TrackBacksEnabled() && $owner->ShouldTrackbackNotify()) {
|
||||
|
||||
if($this->trackbackNotify()) {
|
||||
$owner->PungTrackbackURL = $owner->TrackbackURL;
|
||||
$owner->write();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Trackback notify the specified trackback url
|
||||
* @param boolean | true on success, otherwise false
|
||||
*/
|
||||
function trackbackNotify() {
|
||||
$owner = $this->owner;
|
||||
|
||||
$content = new HTMLText('Content');
|
||||
$content->setValue($owner->Content);
|
||||
$excerpt = $content->FirstParagraph();
|
||||
|
||||
if($owner->Parent() && $owner->ParentID > 0) {
|
||||
$blogName = $owner->Parent()->Title;
|
||||
}
|
||||
else {
|
||||
$blogName = "";
|
||||
}
|
||||
|
||||
$postData = array(
|
||||
'url' => $owner->AbsoluteLink(),
|
||||
'title' => $owner->Title,
|
||||
'excerpt' => $excerpt,
|
||||
'blog_name' => $blogName
|
||||
);
|
||||
|
||||
$controller = Object::create(self::$trackback_server_class);
|
||||
$response = $controller->request($owner->TrackbackURL, $postData);
|
||||
|
||||
if($response->getStatusCode() == '200' && stripos($response->getBody(), "<error>0</error>") !== false) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
function ShouldTrackbackNotify() {
|
||||
return (trim($this->owner->TrackbackURL) != '' && $this->owner->TrackbackURL != $this->owner->PungTrackbackURL);
|
||||
}
|
||||
|
||||
function updateMetaTags(&$tags) {
|
||||
$tags .= $this->owner->renderWith('TrackBackRdf');
|
||||
@ -49,4 +120,32 @@ class TrackBackDecorator extends DataObjectDecorator {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example:
|
||||
* $controller = Object::create('TrackbackHTTPClient');
|
||||
* $response = $controller->request(new SS_HTTPRequest('POST', $url, null, $postData));
|
||||
*/
|
||||
class TrackbackHTTPServer {
|
||||
|
||||
function __construct() {}
|
||||
|
||||
/**
|
||||
* @param string
|
||||
* @param array
|
||||
* @return SS_HTTPResponse
|
||||
*/
|
||||
function request($url, $data) {
|
||||
$ch = curl_init($url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
||||
$response = curl_exec($ch);
|
||||
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
|
||||
curl_close($ch);
|
||||
|
||||
return new SS_HTTPResponse($response, $statusCode);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
@ -28,30 +28,4 @@ class BlogEntryTest extends SapphireTest {
|
||||
BlogEntry::$allow_wysiwyg_editing = $tmpFlag;
|
||||
}
|
||||
|
||||
function testTrackback() {
|
||||
$blog = $this->objFromFixture('BlogHolder', 'mainblog');
|
||||
$blog->TrackBacksEnabled = true;
|
||||
$blog->write();
|
||||
|
||||
$entry = $this->objFromFixture('BlogEntry', 'testpost');
|
||||
$response = $entry->trackbackping();
|
||||
|
||||
$this->assertContains("<error>1</error>", $response);
|
||||
|
||||
$_POST['url'] = 'test trackback post url';
|
||||
$_POST['title'] = 'test trackback post title';
|
||||
$_POST['excerpt'] = 'test trackback post excerpt';
|
||||
$_POST['blog_name'] = 'test trackback blog name';
|
||||
|
||||
$response = $entry->trackbackping();
|
||||
$this->assertContains("<error>0</error>", $response);
|
||||
|
||||
$trackback = DataObject::get_one('TrackBackPing');
|
||||
$this->assertEquals('test trackback post url', $trackback->Url);
|
||||
$this->assertEquals('test trackback post title', $trackback->Title);
|
||||
$this->assertEquals('test trackback post excerpt', $trackback->Excerpt);
|
||||
$this->assertEquals('test trackback blog name', $trackback->BlogName);
|
||||
|
||||
unset($_POST);
|
||||
}
|
||||
}
|
107
tests/BlogTrackbackTest.php
Normal file
107
tests/BlogTrackbackTest.php
Normal file
@ -0,0 +1,107 @@
|
||||
<?php
|
||||
/**
|
||||
* @package blog
|
||||
* @subpackage tests
|
||||
*/
|
||||
class BlogTrackbackTest extends SapphireTest {
|
||||
static $fixture_file = 'blog/tests/BlogTest.yml';
|
||||
|
||||
function testTrackback() {
|
||||
$blog = $this->objFromFixture('BlogHolder', 'mainblog');
|
||||
$blog->TrackBacksEnabled = true;
|
||||
$blog->write();
|
||||
|
||||
$entry = $this->objFromFixture('BlogEntry', 'testpost');
|
||||
$response = $entry->trackbackping();
|
||||
|
||||
$this->assertContains("<error>1</error>", $response);
|
||||
|
||||
$_POST['url'] = 'test trackback post url';
|
||||
$_POST['title'] = 'test trackback post title';
|
||||
$_POST['excerpt'] = 'test trackback post excerpt';
|
||||
$_POST['blog_name'] = 'test trackback blog name';
|
||||
|
||||
$response = $entry->trackbackping();
|
||||
$this->assertContains("<error>0</error>", $response);
|
||||
|
||||
$trackback = DataObject::get_one('TrackBackPing');
|
||||
$this->assertEquals('test trackback post url', $trackback->Url);
|
||||
$this->assertEquals('test trackback post title', $trackback->Title);
|
||||
$this->assertEquals('test trackback post excerpt', $trackback->Excerpt);
|
||||
$this->assertEquals('test trackback blog name', $trackback->BlogName);
|
||||
|
||||
unset($_POST);
|
||||
}
|
||||
|
||||
function testShouldTrackbackNotify() {
|
||||
$blog = $this->objFromFixture('BlogHolder', 'mainblog');
|
||||
$blog->TrackBacksEnabled = true;
|
||||
|
||||
$entry = $this->objFromFixture('BlogEntry', 'testpost');
|
||||
$this->assertFalse($entry->ShouldTrackbackNotify());
|
||||
|
||||
$entry = $this->objFromFixture('BlogEntry', 'testpost');
|
||||
$entry->TrackbackURL = ' ';
|
||||
$this->assertFalse($entry->ShouldTrackbackNotify());
|
||||
|
||||
$entry = $this->objFromFixture('BlogEntry', 'testpost');
|
||||
$entry->TrackbackURL = 'someurl';
|
||||
$this->assertTrue($entry->ShouldTrackbackNotify());
|
||||
}
|
||||
|
||||
function testTrackbackNotify() {
|
||||
$tmpServerClass = TrackBackDecorator::$trackback_server_class;
|
||||
TrackBackDecorator::$trackback_server_class = "TestTrackbackHTTPServer";
|
||||
|
||||
$blog = $this->objFromFixture('BlogHolder', 'mainblog');
|
||||
$blog->TrackBacksEnabled = true;
|
||||
|
||||
$entry = $this->objFromFixture('BlogEntry', 'testpost');
|
||||
$entry->TrackbackURL = 'testGoodTrackbackURL';
|
||||
$this->assertTrue($entry->trackbackNotify());
|
||||
|
||||
$entry->TrackbackURL = 'testBadTrackbackURL';
|
||||
$this->assertFalse($entry->trackbackNotify());
|
||||
|
||||
$entry->TrackbackURL = 'testNonExistingTrackbackURL';
|
||||
$this->assertFalse($entry->trackbackNotify());
|
||||
|
||||
TrackBackDecorator::$trackback_server_class = $tmpServerClass;
|
||||
}
|
||||
}
|
||||
|
||||
class TestTrackbackHTTPServer extends TrackbackHTTPServer implements TestOnly {
|
||||
|
||||
function request($url, $data) {
|
||||
if($url == 'testGoodTrackbackURL') {
|
||||
$response = $this->goodTrackback();
|
||||
$statusCode = '200';
|
||||
}
|
||||
else if($url == 'testBadTrackbackURL') {
|
||||
$response = $this->badTrackback();
|
||||
$statusCode = '200';
|
||||
}
|
||||
else {
|
||||
$response = $this->badTrackback();
|
||||
$statusCode = '404';
|
||||
}
|
||||
|
||||
return new SS_HTTPResponse($response, $statusCode);
|
||||
}
|
||||
|
||||
private function goodTrackback() {
|
||||
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>
|
||||
<response>
|
||||
<error>0</error>
|
||||
<message></message>
|
||||
</response>";
|
||||
}
|
||||
|
||||
private function badTrackback() {
|
||||
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>
|
||||
<response>
|
||||
<error>1</error>
|
||||
<message>Some error text</message>
|
||||
</response>";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user