Permalink
Browse files

FIXED: Upgraded module to work with Silverstripe 3.0 including replac…

…ement of deprecated API as well as handling of new fields

FIXED: Correctly handles missing modules
UPDATED: Refactoring of code to break down logic of import
  • Loading branch information...
1 parent e3a070d commit 3adf783ee301dbfa39652c70f1a87626d74825c2 @tractorcow tractorcow committed Aug 22, 2012
Showing with 203 additions and 172 deletions.
  1. +3 −0 README
  2. +4 −3 _config.php
  3. +84 −70 code/WpImporter.php
  4. +112 −99 code/WpParser.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,5 @@
<?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');
View
@@ -4,95 +4,109 @@
/*
* Decorates a BlogHolder page type, specified in _config.php
*/
-class WpImporter extends DataObjectDecorator {
+class WpImporter extends DataExtension {
- 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));
+ 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
+
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')
+ )
+ );
}
- 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.';
+ protected function importComments($post, $entry)
+ {
+ if(!class_exists('Comment'))
+ return;
+
+ $comments = $post['Comments'];
+ foreach ($comments as $comment) {
+ $page_comment = new Comment();
+ $page_comment->update($comment);
+ $page_comment->ParentID = $entry->ID;
+ $page_comment->write();
}
+ }
+
+ protected function importPost($post)
+ {
+ // create a blog entry
+ $entry = new BlogEntry();
+ $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();
+ $entry->publish("Stage", "Live");
+
+ $this->importComments($post, $entry);
+ return $entry;
}
+
+ function doUpload($data, $form) {
+
+ // Checks if a file is uploaded
+ 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 3adf783

Please sign in to comment.