updates for just showing duplicate files
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 5 Aug 2015 13:53:11 +0000 (09:53 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 5 Aug 2015 13:53:11 +0000 (09:53 -0400)
controllers/Import.php

index 81a4053..7d6476e 100644 (file)
         private function _replaceSrc()
         {
             global $wpdb;
+            $files = $filesFound = $filesMissing = $fileDuplicates = array();
             $this->_connect();
-            echo '<p>Replace page src\'s</p>';
-            // find all pages with links to site pages
+            echo '<p>Replace Src\'s</p>';
             $sql = "
             SELECT *
               FROM {$wpdb->prefix}posts
              WHERE post_content LIKE '%<img%'
                AND post_type = 'page'";
             //$sql .= " AND post_title = 'NLEA Chamber Council'";
-            $sql .= " AND ID = 2101";
+            //$sql .= " AND ID = 2101";
 
             $results = $wpdb->get_results($sql, OBJECT);
             echo '<p>Number of Pages'.count($results).'</p>';
                     //var_dump($test);
                     $images = $dom->getElementsByTagName('img');
                     foreach ($images as $image) {
+                        $files[] = $image->getAttribute('src');
                         $width = $height = null;
-                        echo '<div style="border: 1px solid black; background-color: lightgrey; padding: 15px;width: 600px;margin: 0 10px">';
+                        //echo '<div style="border: 1px solid black; background-color: lightgrey; padding: 15px;width: 600px;margin: 0 10px">';
                         //echo '<pre>' . print_r($image, true)  . '</pre>';
                         if ($image->hasAttribute('class')) {
-                            echo '<pre>' . print_r($image->getAttribute('class'), true)  . '</pre>';
+                            //echo '<pre>' . print_r($image->getAttribute('class'), true)  . '</pre>';
                             if (preg_match('%wp-image-\d+%', $image->getAttribute('class'))) {
                                 //echo 'Continue';
                                 //echo '</div>';
-                                //continue;
+                                continue;
                             }
                         }
                         if ($image->hasAttribute('style')) {
                             $style = $image->getAttribute('style');
-                            var_dump($style);
-                            echo '<pre>' . print_r($style, true)  . '</pre>';
+                            //var_dump($style);
+                            //echo '<pre>' . print_r($style, true)  . '</pre>';
                             if (preg_match('%[^-]width[: ]+(\d+)%', $style, $wMatch)) {
                                 $width = $wMatch[1];
-                                echo '<pre>wMatch: ' . print_r($wMatch, true)  . '</pre>';
+                                //echo '<pre>wMatch: ' . print_r($wMatch, true)  . '</pre>';
                             }
                             if (preg_match('%[^-]height[: ]+(\d+)%', $style, $hMatch)) {
                                 $height = $hMatch[1];
                             }
-                            echo '<pre>width: ' . print_r($width, true)  . '</pre>';
-                            echo '<pre>height: ' . print_r($height, true)  . '</pre>';
+                            //echo '<pre>width: ' . print_r($width, true)  . '</pre>';
+                            //echo '<pre>height: ' . print_r($height, true)  . '</pre>';
                         }
                         if ($image->hasAttribute('width')) {
                             $width = $image->getAttribute('width');
                         if ($image->hasAttribute('src')) {
                             //echo '<pre>' . print_r($image->getAttribute('src'), true)  . '</pre>';
                             $imgName = basename($image->getAttribute('src'));
-                            echo '<pre>' . print_r($imgName, true)  . '</pre>';
+                            //echo '<pre>' . print_r($imgName, true)  . '</pre>';
                             $imgPathInfo = pathinfo($imgName);
-                            echo '<pre>' . print_r($imgPathInfo, true) . '</pre>';
+                            //echo '<pre>' . print_r($imgPathInfo, true) . '</pre>';
                             $wpImage = $this->_getAttachmentByName($imgPathInfo['filename']);
                             //echo '<pre>' . print_r($wpImage, true) . '</pre>';
                             //exit;
                             if (!$wpImage) {
-                                echo '<p>Image not found as attachment</p>';
-                                echo '<pre>' . print_r($imgPathInfo, true) . '</pre>';
+                                //echo '<p>Image not found as attachment</p>';
+                                //echo '<pre>' . print_r($imgPathInfo, true) . '</pre>';
                                 if (preg_match(';(is\d{2}-\d{10}-\d{5})(\d{1}|[-]\d+x\d+);', $imgPathInfo['filename'], $matches)) {
-                                    echo '<p>Duplicate Image found</p>';
-                                    echo '<pre>' . print_r($matches, true) . '</pre>';
+                                    //echo '<p>Duplicate Image found</p>';
+                                    //echo '<pre>' . print_r($matches, true) . '</pre>';
                                     $wpImage = $this->_getAttachmentByName($matches[1]);
                                     if ($wpImage) {
-                                        echo '<p>Found Image</p>';
+                                        //echo '<p>Found Image</p>';
+                                        $fileDuplicates[] = $image->getAttribute('src');
                                     } else {
-                                        echo '<p>Image still not found</p>';
+                                        //echo '<p>Image still not found</p>';
+                                        //echo '</div>';
+                                        $filesMissing[] = $image->getAttribute('src');
                                         continue;
                                     }
                                 }
                             }
                             if (!$wpImage) {
-                                echo '<p>Image not found as attachment 2</p>';
+                                //echo '<p>Image not found as attachment 2</p>';
                                 //echo '<pre>' . print_r($image->getAttribute('src'), true) . '</pre>';
                                 $parsed   = parse_url($image->getAttribute('src'));
                                 $filename = basename($parsed['path']);
                                 if (preg_match(';is\d{2,}-\d{10,}-\d{5,};', $filename)) {
                                     $rootUrl  = str_replace('/' . $filename, '', $image->getAttribute('src'));
-                                    echo '<pre>' . print_r($parsed, true) . '</pre>';
-                                    echo '<pre>' . print_r($filename, true) . '</pre>';
-                                    echo '<pre>' . print_r($rootUrl, true) . '</pre>';
+                                    //echo '<pre>' . print_r($parsed, true) . '</pre>';
+                                    //echo '<pre>' . print_r($filename, true) . '</pre>';
+                                    //echo '<pre>' . print_r($rootUrl, true) . '</pre>';
                                     //if (preg_match('/(\d{4})\/(\d{2})$/', $rootUrl, $matches)) {
                                         //echo '<pre>' . print_r($matches, true) . '</pre>';
                                         //$uploadDir = wp_upload_dir();
                                 }
                             }
                             if ($wpImage) {
+                                $filesFound[] = $image->getAttribute('src');
                                 //$strToReplace = $dom->saveHTML($image);
                                 //echo '<pre>'.htmlspecialchars($strToReplace).'</pre>';
-                                echo '<p>Image found: id '.$wpImage->ID.'</p>';
+                                //echo '<p>Image found: id '.$wpImage->ID.'</p>';
                                 if ($width && $height) {
-                                    echo '<p>width: ' . $width . '</p>';
-                                    echo '<p>height: ' . $height . '</p>';
+                                    //echo '<p>width: ' . $width . '</p>';
+                                    //echo '<p>height: ' . $height . '</p>';
                                     if ($width > 300 || $height > 300) {
-                                        echo '<p>Found Large</p>';
+                                        //echo '<p>Found Large</p>';
                                         $size = 'large';
                                         $newImage = wp_get_attachment_image_src($wpImage->ID, $size);
                                         $image->setAttribute('class', 'alignnone wp-image-' . $wpImage->ID);
                                             $image->setAttribute('src', $newImage[0]);
                                         }
                                     } else if ($width < 150 && $height < 150) {
-                                        echo '<p>Found Thumbnail</p>';
+                                        //echo '<p>Found Thumbnail</p>';
                                         $size = 'thumbnail';
                                         $newImage = wp_get_attachment_image_src($wpImage->ID, $size);
                                         $image->setAttribute('class', 'alignnone size-' . $size . ' wp-image-' . $wpImage->ID);
                                             $image->setAttribute('src', $newImage[0]);
                                         }
                                     } else {
-                                        echo '<p>Found Medium</p>';
+                                        //echo '<p>Found Medium</p>';
                                         $size = 'medium';
                                         $newImage = wp_get_attachment_image_src($wpImage->ID, $size);
                                         $image->setAttribute('class', 'alignnone size-' . $size . ' wp-image-' . $wpImage->ID);
                                 $image->setAttribute('height', $height);
                                 //echo htmlspecialchars($dom->saveHTML($image));
                             } else {
-                                echo '<p>no media found</p>';
+                                //echo '<p>no media found</p>';
+                                $filesMissing[] = $image->getAttribute('src');
                             }
                         }
-                        echo '</div>';
+                        //echo '</div>';
                     }
                     // remove the extra stuff Dom put in
                     $content = preg_replace(
                             $dom->saveHTML()
                         )
                     );
-                    echo '<pre>'.print_r(htmlspecialchars($content), true).'</pre>';
+                    //echo '<pre>'.print_r(htmlspecialchars($content), true).'</pre>';
                     $updatePost = array(
                         'ID'           => $post->ID,
                         'post_content' => $content
                     );
-                    wp_update_post($updatePost);
+                    //wp_update_post($updatePost);
                 }
+                //echo '<pre>Files: ' . print_r($files, true) . '</pre>';
+                echo '<pre>Files Found: ' . print_r($filesFound, true) . '</pre>';
+                echo '<pre>Duplicate Files: ' . print_r($fileDuplicates, true) . '</pre>';
+                echo '<pre>Files Missing: ' . print_r($filesMissing, true) . '</pre>';
             }
         }
 
             SELECT *
               FROM {$wpdb->prefix}posts
              WHERE post_type = 'attachment'
-            AND post_name = '{$name}'";
+            AND (post_name = '{$name}' OR post_title = '{$name}')";
             $results = $wpdb->get_results($sql, OBJECT);
             return (!empty($results)) ? $results[0] : false;
         }