Permalink
Browse files

Made it possible to pull all the assets attached to posts into the lo…

…cal site.
  • Loading branch information...
1 parent 52a3f76 commit e8774b5509ddb573008fb4d14436ddc537bb432a @ajshort ajshort committed Feb 1, 2011
@@ -48,11 +48,24 @@ public function transform($item, $parent, $strategy) {
$post->write();
// Import comments across from the wordpress site.
+ if (isset($params['ImportComments'])) {
+ $this->importComments($item, $post);
+ }
+
+ // Scan the post for media files, and import them as well.
+ if (isset($params['ImportMedia'])) {
+ $this->importMedia($item, $post);
+ }
+ }
+
+ public function setImporter($importer) {
+ $this->importer = $importer;
+ }
+
+ protected function importComments($item, $post) {
$source = $item->getSource();
$client = $source->getClient();
- if (!isset($params['ImportComments'])) return;
-
$struct = new Zend_XmlRpc_Value_Struct(array(
'post_id' => $item->PostID,
'number' => 999999
@@ -75,8 +88,33 @@ public function transform($item, $parent, $strategy) {
}
}
- public function setImporter($importer) {
- $this->importer = $importer;
+ protected function importMedia($item, $post) {
+ $source = $item->getSource();
+ $params = $this->importer->getParams();
+ $folder = $params['AssetsPath'];
+ $content = $item->Content;
+
+ if ($folder) Folder::findOrMake($folder);
+
+ $url = trim(preg_replace('~^[a-z]+://~', null, $source->BaseUrl), '/');
+ $pattern = sprintf(
+ '~[a-z]+://%s/wp-content/uploads/[^"]+~', $url
+ );
+
+ if (!preg_match_all($pattern, $post->Content, $matches)) return;
+
+ foreach ($matches[0] as $match) {
+ if (!$contents = @file_get_contents($match)) continue;
+
+ $name = basename($match);
+ $path = Controller::join_links(ASSETS_PATH, $folder, $name);
+ $link = Controller::join_links(ASSETS_DIR, $folder, $name);
+
+ file_put_contents($path, $contents);
+ $post->Content = str_replace($match, $link, $post->Content);
+ }
+
+ $post->write();
}
}
@@ -54,6 +54,9 @@ public function stageChildren() {
public function getCMSFields() {
$fields = parent::getCMSFields();
+ Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
+ Requirements::javascript('wordpressconnector/javascript/WordpressPostContentSource.js');
+
if (!class_exists('BlogEntry')) {
$fields->addFieldToTab('Root.Import', new LiteralField(
'RequiresBlogImport',
@@ -65,7 +68,12 @@ public function getCMSFields() {
$fields->addFieldsToTab('Root.Import', array(
new DropdownField('MigrationTarget', 'Blog to import into', $map),
- new CheckboxField('ImportComments', 'Import comments attached to the posts?', true)
+ new CheckboxField('ImportComments',
+ 'Import comments attached to the posts?', true),
+ new CheckboxField('ImportMedia',
+ 'Import and rewrite references to wordpress media?', true),
+ new TextField('AssetsPath',
+ 'Upload wordpress files to', 'Uploads/Wordpress')
));
}
@@ -0,0 +1,9 @@
+;(function($) {
+ $("#Form_EditForm_ImportMedia").live("click", function() {
+ if ($(this).is(":checked")) {
+ $("#MediaPath").show();
+ } else {
+ $("#MediaPath").hide();
+ }
+ });
+})(jQuery);

0 comments on commit e8774b5

Please sign in to comment.