From 8ebce4236c701e43fbdd23ecaced83f97f3fb85e Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Fri, 14 Aug 2015 09:35:41 -0400 Subject: [PATCH] Updates for emmet county --- NOTES.md | 3 ++ controllers/Import.php | 102 +++++++++++++++++++++++++++++++++++------ 2 files changed, 91 insertions(+), 14 deletions(-) diff --git a/NOTES.md b/NOTES.md index 71c51e3..ec8e4fb 100644 --- a/NOTES.md +++ b/NOTES.md @@ -60,6 +60,7 @@ X|Need to pull all image url’s out of the post_content sections and replace th x|Need to replace any ‘keywords’ with the correct url’s. x|Need to replace any of the old site url's from their name-number/ to the correct wordpress ones. x|Redo the import so if builds the pages without importing media then make that a second step + |Add new field for options. file upload url #### Process For Import: #### @@ -76,3 +77,5 @@ Step 2 I have all media being imported. Step 3 Updates all media src tags and caption tags Step 4 update keywords , site urls links inside, and update file refs +## For 08/11/2015 ## +Need to check the image references against the database and not the files array diff --git a/controllers/Import.php b/controllers/Import.php index 5191137..9e48baf 100644 --- a/controllers/Import.php +++ b/controllers/Import.php @@ -535,6 +535,9 @@ $start = filter_var($_REQUEST['start'], FILTER_VALIDATE_INT); //$pages = $this->_fetchAllPages(PAGES_PER_LOAD, $start); $pages = $this->_fetchAllPages(); + //foreach ($pages as $page) { + //echo '
' . print_r(htmlspecialchars($page['pageContent']), true) . '
'; + //} //echo '
' . print_r($pages, true) . '
'; //return; //exit; @@ -621,6 +624,8 @@ FROM {$wpdb->prefix}posts WHERE post_content LIKE '%\{{$page['keyword']}\}%' AND post_type = 'page'"; + $currentPostArray = get_option(GLM_WP_IMPORT_POST_OPTION, array()); + $sql .= " AND ID IN (" . implode(',', $currentPostArray) . ")"; //echo '
' . print_r($sql, true) . '
'; $results = $wpdb->get_results($sql, OBJECT); $total = count($results); @@ -663,6 +668,7 @@ private function _replaceUrls() { global $wpdb; + $currentPostArray = get_option(GLM_WP_IMPORT_POST_OPTION, array()); $this->_connect(); echo '

Replace page url\'s

'; $searchUrl = $this->_options['site_url']; @@ -673,6 +679,7 @@ FROM {$wpdb->prefix}posts WHERE post_content LIKE '%{$searchUrl}%' AND post_type = 'page'"; + $sql .= " AND ID IN (" . implode(',', $currentPostArray) . ")"; $results = $wpdb->get_results($sql, OBJECT); if (count($results) > 0) { $pattern = ';http://' . $parsedUrl['host'] . '/([^"]+);si'; @@ -920,6 +927,7 @@ private function _updateFilesHref() { global $wpdb; + $currentPostArray = get_option(GLM_WP_IMPORT_POST_OPTION, array()); $files = get_option(GLM_WP_IMPORT_FILES_OPTION, array()); echo '
' . print_r($files, true) . '
'; //$images = get_option(GLM_WP_IMPORT_IMAGES_OPTION, array()); @@ -930,33 +938,50 @@ FROM {$wpdb->prefix}posts WHERE post_content LIKE '%http://is0.gaslightmedia.com%' AND post_type = 'page'"; + $sql .= " AND ID IN (" . implode(',', $currentPostArray) . ")"; $results = $wpdb->get_results($sql, OBJECT); if (count($results) > 0) { foreach ($results as $post) { $content = $post->post_content; echo '
'; - if (preg_match_all('%href="([^"]+)"%', $content, $isMatch)) { + $urlPattern = '%]*)" ?[^>]*>([^>]+)%s'; + if (preg_match_all($urlPattern, $content, $isMatch)) { $isMatchCount = count($isMatch[0]); echo '

Matches is0 ' . $isMatchCount . '

'; for ($index = 0; $index < $isMatchCount; ++$index) { echo '
'.htmlspecialchars($isMatch[1][$index]).'
'; //echo '
'.htmlspecialchars(basename($isMatch[1][$index])).'
'; //echo '
'.print_r(pathinfo($isMatch[1][$index]), true).'
'; - $key = array_search(basename($isMatch[1][$index]), $files); + $fileName = basename($isMatch[1][$index]); + echo '
File Name: ' . print_r($fileName, true)  . '
'; + $fileTitle = $isMatch[2][$index]; + echo '
File Title: ' . print_r($fileTitle, true)  . '
'; + $key = $this->_getAttachmentByName($fileName); + echo '
Key: ' . print_r($key, true)  . '
'; + // check on format of filename + if (!$key && ($fileName && $fileTitle)) { + $key = $this->_handleMediaFile( + $fileName, + $fileTitle, + '', + $post->ID, + $this->_options['site_url'] . '/uploads/' + ); + } if ($key !== false) { var_dump($key); $newSrc = wp_get_attachment_url($key); $content = str_replace($isMatch[1][$index], $newSrc, $content); } } + $updatePost = array( + 'ID' => $post->ID, + 'post_content' => $content + ); + wp_update_post($updatePost); } echo '
'.htmlspecialchars($content).'
'; echo '
'; - $updatePost = array( - 'ID' => $post->ID, - 'post_content' => $content - ); - //wp_update_post($updatePost); } } } @@ -965,11 +990,13 @@ { global $wpdb; $images = get_option(GLM_WP_IMPORT_IMAGES_OPTION, array()); + $currentPostArray = get_option(GLM_WP_IMPORT_POST_OPTION, array()); $sql = " SELECT * FROM {$wpdb->prefix}posts WHERE post_content LIKE '%get_results($sql, OBJECT); if (count($results) > 0) { foreach ($results as $post) { @@ -989,7 +1016,18 @@ echo '
Sub Src Match: ' . print_r($srcMatch[1], true)  . '
'; $fileName = basename($srcMatch[1]); echo '
File Name: ' . print_r($fileName, true)  . '
'; - if ($imagesKey = array_search($fileName, $images)) { + $imagesKey = $this->_getAttachmentByName($fileName); + var_dump($imagesKey); + if (!$imagesKey) { + $imagesKey = $this->_handleMediaFile( + $fileName, + $fileName, + '', + $post->ID, + $this->_options['toolbox_image_url'] . '/tbs1/' + ); + } + if ($imagesKey) { var_dump($imagesKey); $newImage = wp_get_attachment_image_src($imagesKey, 'medium'); // need to replace the src tags in content with the new image tags @@ -1013,10 +1051,21 @@ echo '
Image Match: ' . $imageCount  . '
'; echo '
'.htmlspecialchars($matches[0][$index]).'
'; if (preg_match('%src="([^"]+)"%', $matches[1][$index], $srcMatch)) { - //echo '
Sub Src Match: ' . print_r($srcMatch[1], true)  . '
'; + echo '
Sub Src Match: ' . print_r($srcMatch[1], true)  . '
'; $fileName = basename($srcMatch[1]); - //echo '
File Name: ' . print_r($fileName, true)  . '
'; - if ($imagesKey = array_search($fileName, $images)) { + echo '
File Name: ' . print_r($fileName, true)  . '
'; + $imagesKey = $this->_getAttachmentByName($fileName); + var_dump($imagesKey); + if (!$imagesKey) { + $imagesKey = $this->_handleMediaFile( + $fileName, + $fileName, + '', + $post->ID, + $this->_options['toolbox_image_url'] . '/tbs1/' + ); + } + if ($imagesKey) { //var_dump($imagesKey); $newUrl = wp_get_attachment_url($imagesKey); // need to replace the src tags in content with the new image tags @@ -1052,7 +1101,8 @@ WHERE post_type = 'attachment' AND (post_name = '{$name}' OR post_title = '{$name}')"; $results = $wpdb->get_results($sql, OBJECT); - return (!empty($results)) ? $results[0] : false; + return (!empty($results)) ? $results[0]->ID : false; + //return (!empty($results)) ? $results[0] : false; } private function _getAttachmentById($id) @@ -1223,9 +1273,22 @@ switch($step) { case 0: $this->_greet(); + //$this->_post = array( + //49 => 5546, + //50 => 5547, + //51 => 5548, + //52 => 5549, + //210 => 5550, + //388 => 5551, + //562 => 5552, + //563 => 5553 + //); + //$this->_storePost(); //$this->_readOptions(); //echo '

CkImages

'; + echo '

Begin Import

'; $currentPostArray = get_option(GLM_WP_IMPORT_POST_OPTION, array()); + echo '
' . print_r($currentPostArray, true) . '
'; $images = get_option(GLM_WP_IMPORT_IMAGES_OPTION, array()); echo '
' . print_r($images, true) . '
'; $files = get_option(GLM_WP_IMPORT_FILES_OPTION, array()); @@ -1239,12 +1302,21 @@ } break; case 1: + echo '

Reset

'; + $this->_post = $this->_images = $this->_files = array(); + $this->_storePost(); + $this->_storeImages(); + $this->_storeFiles(); $numPagesImported = $this->_import(); - $start = filter_var($_REQUEST['start'], FILTER_VALIDATE_INT); echo '

' . $numPagesImported . ' Pages Imported

'; echo '

Step Two (Importing Images)

'; break; case 2: + echo '

Done

'; + echo '

Step Three (Updating Image Ref)

'; + //echo '

Part 3

'; + break; + case 'z': echo '

Now processing the images from old database tables

'; $this->_images = array(); $this->_storeImages(); @@ -1268,9 +1340,11 @@ case 4: echo '

Phase Four

'; echo '

Files

'; - $this->_updateFilesHref(); + //$this->_updateFilesHref(); echo '

Site Url

'; + // Replace the page Url's $this->_replaceUrls(); + // Replace the Keywords $this->_replaceKeywords(); echo '

Done

'; break; -- 2.17.1