Permalink
Browse files

Merge pull request #1 from tractorcow/3.0

Merge from tractorcow FIXED: Compatibility with Silverstripe 3.0 + additional features
  • Loading branch information...
2 parents e3a070d + dfd224d commit 7ab89131cc6fb96c64ba3c26f0b2377a7f730792 @camfindlay camfindlay committed Aug 22, 2012
Showing with 597 additions and 175 deletions.
  1. +3 −0 README
  2. +9 −3 _config.php
  3. +12 −0 code/WpImportedObject.php
  4. +110 −75 code/WpImporter.php
  5. +155 −97 code/WpParser.php
  6. +51 −0 code/tasks/DeletePostsTask.php
  7. +152 −0 tests/WordpressExport.xml
  8. +105 −0 tests/WpParserTests.php
View
3 README
@@ -11,11 +11,14 @@ Saophalkun Ponlu (Nickname: phalkunz)
Additional Updates:
Cam Findlay (Nickname: camfindlay)
<cam (at) camfindlay.com>
+Damian Mooyman (Nickname: tractorcow)
+<damian (dot) mooyman (at) gmail (dot) com>
Requirements
-----------------------------------------------
mod_rewrite
+Silverstripe 3.0
Documentation
-----------------------------------------------
View
@@ -1,4 +1,10 @@
<?php
- /* Add wordpress import extension to BlogHolder page type */
- Object::add_extension('BlogHolder', 'WpImporter');
-?>
+// Add wordpress import extension to BlogHolder page type
+if (class_exists('BlogHolder'))
+{
+ Object::add_extension('BlogHolder', 'WpImporter');
+ Object::add_extension('BlogEntry', 'WpImportedObject');
+}
+
+if(class_exists('Comment'))
+ Object::add_extension('Comment', 'WpImportedObject');
View
@@ -0,0 +1,12 @@
+<?php
+
+class WpImportedObject extends DataExtension
+{
+ function extraStatics($class = null, $extension = null) {
+ return array(
+ 'db' => array(
+ 'WordpressID' => 'Int'
+ )
+ );
+ }
+}
View
@@ -1,98 +1,133 @@
<?php
require('WpParser.php');
-/*
+/*
* Decorates a BlogHolder page type, specified in _config.php
- */
-class WpImporter extends DataObjectDecorator {
+ */
- function updateCMSFields(&$fields) {
- $html_str = '<iframe name="WpImport" src="WpImporter_Controller/index/'.$this->owner->ID.'" width="500"> </iframe>';
- $fields->addFieldToTab('Root.Content.Import', new LiteralField("ImportIframe",$html_str));
+class WpImporter extends DataExtension
+{
+
+ function updateCMSFields(FieldList $fields) {
+ $html_str = '<iframe name="WpImport" src="WpImporter_Controller/index/' . $this->owner->ID . '" width="500"> </iframe>';
+ $fields->addFieldToTab('Root.Import', new LiteralField("ImportIframe", $html_str));
}
+
}
-class WpImporter_Controller extends Controller {
- // Do security check in case this controller is called by unauthorised user using direct url
+class WpImporter_Controller extends Controller
+{
+
function init() {
parent::init();
- if(!Permission::check("ADMIN")) Security::permissionFailure();
+
+ // Do security check in case this controller is called by unauthorised user using direct url
+ if (!Permission::check("ADMIN"))
+ Security::permissionFailure();
+
+ // Check for requirements
+ if (!class_exists('BlogHolder'))
+ user_error('Please install the blog module before importing from Wordpress', E_USER_ERROR);
}
-
- /*
- * Required
- */
- function Link() {
- return $this->class.'/';
+
+ protected function getBlogHolderID() {
+ if (isset($_REQUEST['BlogHolderID']))
+ return $_REQUEST['BlogHolderID'];
+
+ return $this->request->param('ID');
}
/*
* Outputs an file upload form
*/
+
function UploadForm() {
- return new Form($this, "UploadForm", new FieldSet(
- new FileField("XMLFile", 'Wordpress XML file'),
- new HiddenField("BlogHolderID", '', $this->urlParams['ID'])
- ), new FieldSet(
- new FormAction('doUpload', 'Import Wordpress XML file')
- ));
+ return new Form($this, "UploadForm",
+ new FieldList(
+ new FileField("XMLFile", 'Wordpress XML file'),
+ new HiddenField("BlogHolderID", '', $this->getBlogHolderID())
+ ),
+ new FieldList(
+ new FormAction('doUpload', 'Import Wordpress XML file')
+ )
+ );
+ }
+
+ protected function getOrCreateComment($wordpressID) {
+ if ($wordpressID && $comment = DataObject::get('Comment')->filter(array('WordpressID' => $wordpressID))->first())
+ return $comment;
+
+ return Comment::create();
+ }
+
+ protected function importComments($post, $entry) {
+ if (!class_exists('Comment'))
+ return;
+
+ $comments = $post['Comments'];
+ foreach ($comments as $comment)
+ {
+ $page_comment = $this->getOrCreateComment($comment['WordpressID']);
+ $page_comment->update($comment);
+ $page_comment->ParentID = $entry->ID;
+ $page_comment->write();
+ }
}
-
+
+ protected function getOrCreatePost($wordpressID) {
+ if ($wordpressID && $post = DataObject::get('BlogEntry')->filter(array('WordpressID' => $wordpressID))->first())
+ return $post;
+
+ return BlogEntry::create();
+ }
+
+ protected function importPost($post) {
+ // create a blog entry
+ $entry = $this->getOrCreatePost($post['WordpressID']);
+
+ $entry->ParentID = $this->getBlogHolderID();
+
+ // $posts array and $entry have the same key/field names
+ // so we can use update here.
+
+ $entry->update($post);
+ $entry->write();
+ if ($post['IsPublished'])
+ $entry->publish("Stage", "Live");
+
+ $this->importComments($post, $entry);
+
+ return $entry;
+ }
+
function doUpload($data, $form) {
- // Gets a blog holders ID
- $blogHolderID = $data['BlogHolderID'];
-
+
// Checks if a file is uploaded
- if(is_uploaded_file($_FILES['XMLFile']['tmp_name'])) {
- echo '<p>Processing...<br/></p>';
- flush();
- $file = $_FILES['XMLFile'];
- // check file type. only xml file is allowed
- if ($file['type'] != 'text/xml') {
- echo 'Please select Wordpress XML file';
- die;
- }
-
- $wp = new WpParser($file['tmp_name']);
- $posts = $wp->parse();
-
- // For testing only
- // TODO: remove $count
- //$count = 0;
- foreach ($posts as $post) {
- $comments = $post['Comments'];
- // create a blog entry
- $entry = new BlogEntry();
- $entry->ParentID = $blogHolderID;
- // $posts array and $entry have the same key/field names
- // so we can use update here.
-
- $entry->update($post);
- $entry->write();
- $entry->publish("Stage", "Live");
-
- // page comment(s)
- foreach ($comments as $comment) {
- $page_comment = new PageComment();
- $page_comment->ParentID = $entry->ID;
- $page_comment->update($comment);
- $page_comment->write();
- }
- // count is used for testing only
- // TODO: remove the next 2 lines
- //$count++;
- //if($count==30) break;
- }
-
- // delete the temporaray uploaded file
- unlink($file['tmp_name']);
- // print sucess message
- echo 'Complete!<br/>';
- echo 'Please refresh the admin page to see the new blog entries.';
+ if (!is_uploaded_file($_FILES['XMLFile']['tmp_name']))
+ return;
+
+ echo '<p>Processing...<br/></p>';
+ flush();
+ $file = $_FILES['XMLFile'];
+ // check file type. only xml file is allowed
+ if ($file['type'] != 'text/xml')
+ {
+ echo 'Please select Wordpress XML file';
+ die;
}
-
-
+
+ // Parse posts
+ $wp = new WpParser($file['tmp_name']);
+ $posts = $wp->parse();
+ foreach ($posts as $post)
+ $this->importPost($post);
+
+ // delete the temporaray uploaded file
+ unlink($file['tmp_name']);
+
+ // print sucess message
+ echo 'Complete!<br/>';
+ echo 'Please refresh the admin page to see the new blog entries.';
}
-}
-?>
+}
Oops, something went wrong.

0 comments on commit 7ab8913

Please sign in to comment.