Start over with the redo
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 7 Aug 2015 19:43:54 +0000 (15:43 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 7 Aug 2015 19:43:54 +0000 (15:43 -0400)
Part 1 fetch and build all pages.
Part 2 import all media

controllers/Import.php
index.php

index c36b3a4..cea8439 100644 (file)
                 SELECT count(*)
                   FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_page_table']}
                  $WHERE";
-                echo '<pre>' . $sql . '</pre>';
+                //echo '<pre>' . $sql . '</pre>';
                 $totalPages = $this->_dbh->query($sql)->fetchColumn();
                 printf('<p>Found %d Pages</p>', $totalPages);
-                echo '<p><a href="admin.php?import=toolbox&amp;step=2">Part 2</a></p>';
+                //echo '<p><a href="admin.php?import=toolbox&amp;step=2">Part 2</a></p>';
             }
         }
 
             return ($id) ? $this->_getAttachmentById($id) : false;
         }
 
-        private function _handleMediaFile($file, $caption = '', $post_id = 0, $baseUrl = null)
-        {
+        private function _handleMediaFile(
+            $file,
+            $file_title,
+            $caption = '',
+            $post_id = 0,
+            $baseUrl = null
+        ) {
             $id = array_search($file, $this->_files);
             if ($id === false) {
                 set_time_limit(120);
                     'post_mime_type' => $wp_filetype['type'],
                     'guid'           => $url,
                     'post_parent'    => $post_id,
-                    'post_title'     => $title,
+                    'post_title'     => $file_title,
                     'post_name'      => $title,
                     'post_content'   => $content,
                     'post_excerpt'   => $caption,
             update_option(GLM_WP_IMPORT_POST_OPTION, $this->_post);
         }
 
+        private function _storeImages()
+        {
+            update_option(GLM_WP_IMPORT_IMAGES_OPTION, $this->_images);
+        }
+
+        private function _storeFiles()
+        {
+            update_option(GLM_WP_IMPORT_FILES_OPTION, $this->_files);
+        }
+
         private function _replaceKeywords()
         {
             global $wpdb;
         {
             global $wpdb;
             if ($this->_options['ckeditor_schema'] && $this->_options['ckeditor_images_table']) {
+                $this->_images = get_option(GLM_WP_IMPORT_IMAGES_OPTION, array());
                 $this->_connect();
-                $ckimagesSql = "
+                $sql = "
                 SELECT *
                   FROM {$this->_options['ckeditor_schema']}.{$this->_options['ckeditor_images_table']}";
-                $ckData = $this->_dbh->query($ckimagesSql)->fetchAll();
-                echo '<pre>' . print_r($ckData, true) . '</pre>';
+                $stmt = $this->_dbh->query($sql);
+                while ($row = $stmt->fetch()) {
+                    echo '<pre>' . print_r($row, true) . '</pre>';
+                    $fileName = $row['name_on_disk'];
+                    $fileTitle = ($row['file_name']) ? $row['file_name'] : $fileName;
+                    $wpImage = $this->_handleMediaFile(
+                        $fileName,
+                        $fileTitle,
+                        '',
+                        0,
+                        $this->_options['toolbox_image_url'] . '/CKImage/'
+                    );
+                    echo '<pre>' . print_r($wpImage, true) . '</pre>';
+                    $this->_images[$wpImage] = $fileName;
+                }
+                $this->_storeImages();
+            } else {
+                return false;
+            }
+        }
+
+        private function _getToolboxFiles()
+        {
+            global $wpdb;
+            echo '<pre>' . print_r($this->_options['toolbox_schema'], true) . '</pre>';
+            echo '<pre>' . print_r($this->_options['toolbox_files_table'], true) . '</pre>';
+            if ($this->_options['toolbox_files_table']) {
+                $this->_files = get_option(GLM_WP_IMPORT_FILES_OPTION, array());
+                $this->_connect();
+                $sql = "
+                SELECT *
+                  FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_files_table']}";
+                $stmt = $this->_dbh->query($sql);
+                while ($row = $stmt->fetch()) {
+                    echo '<pre>' . print_r($row, true) . '</pre>';
+                    $fileName = $row['filename'];
+                    $fileTitle = ($row['urltext']) ? $row['urltext'] : $fileName;
+                    $wpImage = $this->_handleMediaFile(
+                        $fileName,
+                        $fileTitle,
+                        '',
+                        0,
+                        $this->_options['toolbox_image_url'] . '/_ORIGINAL_/'
+                    );
+                    echo '<pre>' . print_r($wpImage, true) . '</pre>';
+                    $this->_files[$wpImage] = $fileName;
+                }
+                $this->_storeFiles();
             } else {
                 return false;
             }
         }
 
+        private function _getParagraphImages()
+        {
+            global $wpdb;
+            echo '<pre>' . print_r($this->_options['toolbox_schema'], true) . '</pre>';
+            echo '<pre>' . print_r($this->_options['toolbox_paragraphs_table'], true) . '</pre>';
+            if ($this->_options['toolbox_paragraphs_table']) {
+                $this->_images = get_option(GLM_WP_IMPORT_IMAGES_OPTION, array());
+                $this->_connect();
+                $sql = "
+                SELECT image,caption
+                  FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_paragraphs_table']}
+                 WHERE image != ''";
+                $stmt = $this->_dbh->query($sql);
+                while ($row = $stmt->fetch()) {
+                    echo '<pre>' . print_r($row, true) . '</pre>';
+                    $fileName = $row['image'];
+                    $fileTitle = ($row['caption']) ? $row['caption'] : $fileName;
+                    $wpImage = $this->_handleMediaFile(
+                        $fileName,
+                        $fileTitle,
+                        '',
+                        0,
+                        $this->_options['toolbox_image_url'] . '/tbs1/'
+                    );
+                    echo '<pre>' . print_r($wpImage, true) . '</pre>';
+                    $this->_images[$wpImage] = $fileName;
+                }
+                $this->_storeImages();
+            }
+        }
+
         public function dispatch()
         {
             $this->_header();
                 //$this->post = array();
                 //$this->_storePost();
                 $this->_greet();
-                $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&start=0">Begin Import</a></p>';
+                //$this->_readOptions();
+                //echo '<p><a href="admin.php?import=toolbox&amp;step=5&start=0">CkImages</a></p>';
+                $currentPostArray = get_option(GLM_WP_IMPORT_POST_OPTION, array());
+                $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());
+                echo '<pre>' . print_r($files, true) . '</pre>';
+                if (count($currentPostArray) > 0) {
+                    echo '<p>Pages have been imported</p>';
+                    echo '<p><a href="admin.php?import=toolbox&amp;step=2&start=0">Step Two</a></p>';
+                } else {
+                    echo '<p><a href="admin.php?import=toolbox&amp;step=1&start=0">Begin Import</a></p>';
+                }
                 break;
             case 1:
                 $numPagesImported = $this->_import();
                 echo '<p><a href="admin.php?import=toolbox&amp;step=2">Part 2</a></p>';
                 break;
             case 2:
-                $this->_replaceSrc();
+                echo '<p>Now processing the images from old database tables</p>';
+                $this->_images = array();
+                $this->_storeImages();
+                $this->_files = array();
+                $this->_storeFiles();
+                $this->_getCkImages();
+                $this->_getToolboxFiles();
+                $this->_getParagraphImages();
+                //$this->_replaceSrc();
                 //$this->_replaceKeywords();
                 //$this->replaceIsoUrls();
                 echo '<p>Done</p>';
index babe773..f8cda2b 100644 (file)
--- a/index.php
+++ b/index.php
@@ -11,6 +11,8 @@ define('GLM_WP_IMPORT_SETTINGS', 'glm_wp_import_settings');
 define('GLM_WP_IMPORT_CAPABILITY', 'manage_options');
 define('GLM_WP_IMPORT_OPTION_GROUP', 'glmwp_import_group');
 define('GLM_WP_IMPORT_POST_OPTION', 'glmwp_import_posts');
+define('GLM_WP_IMPORT_IMAGES_OPTION', 'glmwp_import_images');
+define('GLM_WP_IMPORT_FILES_OPTION', 'glmwp_import_files');
 
 if (is_admin()) {
     require_once 'controllers/Admin.php';