Updates for emmet county
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 14 Aug 2015 13:35:41 +0000 (09:35 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 14 Aug 2015 13:35:41 +0000 (09:35 -0400)
NOTES.md
controllers/Import.php

index 71c51e3..ec8e4fb 100644 (file)
--- 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
index 5191137..9e48baf 100644 (file)
             $start = filter_var($_REQUEST['start'], FILTER_VALIDATE_INT);
             //$pages = $this->_fetchAllPages(PAGES_PER_LOAD, $start);
             $pages = $this->_fetchAllPages();
+            //foreach ($pages as $page) {
+                //echo '<pre>' . print_r(htmlspecialchars($page['pageContent']), true) . '</pre>';
+            //}
             //echo '<pre>' . print_r($pages, true) . '</pre>';
             //return;
             //exit;
                   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 '<pre>' . print_r($sql, true) . '</pre>';
                 $results = $wpdb->get_results($sql, OBJECT);
                 $total = count($results);
         private function _replaceUrls()
         {
             global $wpdb;
+            $currentPostArray = get_option(GLM_WP_IMPORT_POST_OPTION, array());
             $this->_connect();
             echo '<p>Replace page url\'s</p>';
             $searchUrl = $this->_options['site_url'];
               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';
         private function _updateFilesHref()
         {
             global $wpdb;
+            $currentPostArray = get_option(GLM_WP_IMPORT_POST_OPTION, array());
             $files = get_option(GLM_WP_IMPORT_FILES_OPTION, array());
             echo '<pre>' . print_r($files, true) . '</pre>';
             //$images = get_option(GLM_WP_IMPORT_IMAGES_OPTION, array());
               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 '<div style="border: 1px solid green; padding: 9px; margin: 10px;">';
-                    if (preg_match_all('%href="([^"]+)"%', $content, $isMatch)) {
+                    $urlPattern = '%<a href="(' . $this->_options['toolbox_image_url'] . '/[^">]*)" ?[^>]*>([^>]+)</a>%s';
+                    if (preg_match_all($urlPattern, $content, $isMatch)) {
                         $isMatchCount = count($isMatch[0]);
                         echo '<p>Matches is0 ' . $isMatchCount . '</p>';
                         for ($index = 0; $index < $isMatchCount; ++$index) {
                             echo '<pre>'.htmlspecialchars($isMatch[1][$index]).'</pre>';
                             //echo '<pre>'.htmlspecialchars(basename($isMatch[1][$index])).'</pre>';
                             //echo '<pre>'.print_r(pathinfo($isMatch[1][$index]), true).'</pre>';
-                            $key = array_search(basename($isMatch[1][$index]), $files);
+                            $fileName = basename($isMatch[1][$index]);
+                            echo '<pre>File Name: ' . print_r($fileName, true)  . '</pre>';
+                            $fileTitle = $isMatch[2][$index];
+                            echo '<pre>File Title: ' . print_r($fileTitle, true)  . '</pre>';
+                            $key = $this->_getAttachmentByName($fileName);
+                            echo '<pre>Key: ' . print_r($key, true)  . '</pre>';
+                            // 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 '<pre>'.htmlspecialchars($content).'</pre>';
                     echo '</div>';
-                    $updatePost = array(
-                        'ID'           => $post->ID,
-                        'post_content' => $content
-                    );
-                    //wp_update_post($updatePost);
                 }
             }
         }
         {
             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 '%<img%'
                AND post_type = 'page'";
+            $sql .= " AND ID IN (" . implode(',', $currentPostArray) . ")";
             $results = $wpdb->get_results($sql, OBJECT);
             if (count($results) > 0) {
                 foreach ($results as $post) {
                                 echo '<pre>Sub Src Match: ' . print_r($srcMatch[1], true)  . '</pre>';
                                 $fileName = basename($srcMatch[1]);
                                 echo '<pre>File Name: ' . print_r($fileName, true)  . '</pre>';
-                                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
                             echo '<pre>Image Match: ' . $imageCount  . '</pre>';
                             echo '<pre>'.htmlspecialchars($matches[0][$index]).'</pre>';
                             if (preg_match('%src="([^"]+)"%', $matches[1][$index], $srcMatch)) {
-                                //echo '<pre>Sub Src Match: ' . print_r($srcMatch[1], true)  . '</pre>';
+                                echo '<pre>Sub Src Match: ' . print_r($srcMatch[1], true)  . '</pre>';
                                 $fileName = basename($srcMatch[1]);
-                                //echo '<pre>File Name: ' . print_r($fileName, true)  . '</pre>';
-                                if ($imagesKey = array_search($fileName, $images)) {
+                                echo '<pre>File Name: ' . print_r($fileName, true)  . '</pre>';
+                                $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
              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)
             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 '<p><a href="admin.php?import=toolbox&amp;step=5&start=0">CkImages</a></p>';
+                echo '<p><a href="admin.php?import=toolbox&amp;step=1">Begin Import</a></p>';
                 $currentPostArray = get_option(GLM_WP_IMPORT_POST_OPTION, array());
+                echo '<pre>' . print_r($currentPostArray, true) . '</pre>';
                 $images = get_option(GLM_WP_IMPORT_IMAGES_OPTION, array());
                 echo '<pre>' . print_r($images, true) . '</pre>';
                 $files = get_option(GLM_WP_IMPORT_FILES_OPTION, array());
                 }
                 break;
             case 1:
+                echo '<p>Reset</p>';
+                $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 '<p>' . $numPagesImported . ' Pages Imported</p>';
                 echo '<p><a href="admin.php?import=toolbox&amp;step=2">Step Two (Importing Images)</a></p>';
                 break;
             case 2:
+                echo '<p>Done</p>';
+                echo '<p><a href="admin.php?import=toolbox&amp;step=3">Step Three (Updating Image Ref)</a></p>';
+                //echo '<p><a href="admin.php?import=toolbox&amp;step=3">Part 3</a></p>';
+                break;
+            case 'z':
                 echo '<p>Now processing the images from old database tables</p>';
                 $this->_images = array();
                 $this->_storeImages();
             case 4:
                 echo '<p>Phase Four</p>';
                 echo '<p>Files</p>';
-                $this->_updateFilesHref();
+                //$this->_updateFilesHref();
                 echo '<p>Site Url</p>';
+                // Replace the page Url's
                 $this->_replaceUrls();
+                // Replace the Keywords
                 $this->_replaceKeywords();
                 echo '<p>Done</p>';
                 break;