Changeset 2867

Show
Ignore:
Timestamp:
09/10/05 22:45:32 (3 years ago)
Author:
ryan
Message:

Make RSS importer actually kinda work.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/wp-admin/import/rss.php

    r2800 r2867  
    11<?php 
     2 
     3// Example: 
     4// define('RSSFILE', '/home/example/public_html/rss.xml'); 
     5define('RSSFILE', 'rss.xml'); 
     6 
    27class RSS_Import { 
    38 
    4     var $authors = array (); 
    59    var $posts = array (); 
    610 
     
    1418    } 
    1519 
     20    function unhtmlentities($string) { // From php.net for < 4.3 compat 
     21        $trans_tbl = get_html_translation_table(HTML_ENTITIES); 
     22        $trans_tbl = array_flip($trans_tbl); 
     23        return strtr($string, $trans_tbl); 
     24    } 
     25     
    1626    function greet() { 
    1727        $this->header(); 
     
    2535<a href="admin.php?import=rss&amp;step=1">Begin RSS Import &raquo;</a> 
    2636<?php 
    27  
    2837        endif; 
    2938        $this->footer(); 
     
    3140 
    3241    function get_posts() { 
     42        global $wpdb; 
     43         
    3344        set_magic_quotes_runtime(0); 
    3445        $datalines = file(RSSFILE); // Read the file into an array 
     
    3647        $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata); 
    3748 
    38         preg_match_all('|<item>(.*?)</item>|is', $importdata, $posts); 
    39         $this->posts = $posts[1]; 
     49        preg_match_all('|<item>(.*?)</item>|is', $importdata, $this->posts); 
     50        $this->posts = $this->posts[1]; 
     51        $index = 0; 
     52        foreach ($this->posts as $post) { 
     53            preg_match('|<title>(.*?)</title>|is', $post, $post_title); 
     54            $post_title = $wpdb->escape(trim($post_title[1])); 
     55 
     56            preg_match('|<pubdate>(.*?)</pubdate>|is', $post, $post_date); 
     57 
     58            if ($post_date) { 
     59                $post_date = strtotime($post_date[1]); 
     60            } else { 
     61                // if we don't already have something from pubDate 
     62                preg_match('|<dc:date>(.*?)</dc:date>|is', $post, $post_date); 
     63                $post_date = preg_replace('|([-+])([0-9]+):([0-9]+)$|', '\1\2\3', $post_date[1]); 
     64                $post_date = str_replace('T', ' ', $post_date); 
     65                $post_date = strtotime($post_date); 
     66            } 
     67 
     68            $post_date = gmdate('Y-m-d H:i:s', $post_date); 
     69 
     70            preg_match_all('|<category>(.*?)</category>|is', $post, $categories); 
     71            $categories = $categories[1]; 
     72 
     73            if (!$categories) { 
     74                preg_match_all('|<dc:subject>(.*?)</dc:subject>|is', $post, $categories); 
     75                $categories = $categories[1]; 
     76            } 
     77 
     78            $cat_index = 0; 
     79            foreach ($categories as $category) { 
     80                $categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category)); 
     81                $cat_index++; 
     82            } 
     83 
     84            preg_match('|<guid.+?>(.*?)</guid>|is', $post, $guid); 
     85            if ($guid) 
     86                $guid = $wpdb->escape(trim($guid[1])); 
     87            else 
     88                $guid = ''; 
     89 
     90            preg_match('|<content:encoded>(.*?)</content:encoded>|is', $post, $post_content); 
     91            $post_content = str_replace(array ('<![CDATA[', ']]>'), '', $wpdb->escape(trim($post_content[1]))); 
     92 
     93            if (!$post_content) { 
     94                // This is for feeds that put content in description 
     95                preg_match('|<description>(.*?)</description>|is', $post, $post_content); 
     96                $post_content = $wpdb->escape($this->unhtmlentities(trim($post_content[1]))); 
     97            } 
     98 
     99            // Clean up content 
     100            $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content); 
     101            $post_content = str_replace('<br>', '<br />', $post_content); 
     102            $post_content = str_replace('<hr>', '<hr />', $post_content); 
     103 
     104            $post_author = 1; 
     105            $post_status = 'publish'; 
     106            $post_date_gmt = $post_date; // FIXME 
     107            $this->posts[$index] = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_status', 'guid', 'categories'); 
     108            $index++; 
     109        } 
    40110    } 
    41111 
    42112    function import_posts() { 
    43113        echo '<ol>'; 
    44         foreach ($this->posts as $post) 
    45             : $title = $date = $categories = $content = $post_id = ''; 
    46         echo "<li>Importing post... "; 
    47114 
    48         preg_match('|<title>(.*?)</title>|is', $post, $title); 
    49         $title = $wpdb->escape(trim($title[1])); 
    50         $post_name = sanitize_title($title); 
     115        foreach ($this->posts as $post) { 
     116            echo "<li>".__('Importing post...'); 
    51117 
    52         preg_match('|<pubdate>(.*?)</pubdate>|is', $post, $date); 
     118           extract($post); 
    53119 
    54         if ($date) 
    55             : $date = strtotime($date[1]); 
    56         else 
    57             : // if we don't already have something from pubDate 
    58             preg_match('|<dc:date>(.*?)</dc:date>|is', $post, $date); 
    59         $date = preg_replace('|([-+])([0-9]+):([0-9]+)$|', '\1\2\3', $date[1]); 
    60         $date = str_replace('T', ' ', $date); 
    61         $date = strtotime($date); 
    62         endif; 
     120            if ($post_id = post_exists($post_title, $post_content, $post_date)) { 
     121                echo __('Post already imported'); 
     122            } else { 
     123                $post_id = wp_insert_post($post); 
     124                if (!$post_id) 
     125                    die(__("Couldn't get post ID")); 
     126     
     127                if (0 != count($categories)) 
     128                    wp_create_categories($categories, $post_id); 
     129                echo __('Done !'); 
     130            } 
     131            echo '</li>'; 
     132        } 
    63133 
    64         $post_date = gmdate('Y-m-d H:i:s', $date); 
    65  
    66         preg_match_all('|<category>(.*?)</category>|is', $post, $categories); 
    67         $categories = $categories[1]; 
    68  
    69         if (!$categories) 
    70             : preg_match_all('|<dc:subject>(.*?)</dc:subject>|is', $post, $categories); 
    71         $categories = $categories[1]; 
    72         endif; 
    73  
    74         preg_match('|<guid.+?>(.*?)</guid>|is', $post, $guid); 
    75         if ($guid) 
    76             $guid = $wpdb->escape(trim($guid[1])); 
    77         else 
    78             $guid = ''; 
    79  
    80         preg_match('|<content:encoded>(.*?)</content:encoded>|is', $post, $content); 
    81         $content = str_replace(array ('<![CDATA[', ']]>'), '', $wpdb->escape(trim($content[1]))); 
    82  
    83         if (!$content) 
    84             : // This is for feeds that put content in description 
    85             preg_match('|<description>(.*?)</description>|is', $post, $content); 
    86         $content = $wpdb->escape(unhtmlentities(trim($content[1]))); 
    87         endif; 
    88  
    89         // Clean up content 
    90         $content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $content); 
    91         $content = str_replace('<br>', '<br />', $content); 
    92         $content = str_replace('<hr>', '<hr />', $content); 
    93  
    94         // This can mess up on posts with no titles, but checking content is much slower 
    95         // So we do it as a last resort 
    96         if ('' == $title) 
    97             : $dupe = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_content = '$content' AND post_date = '$post_date'"); 
    98         else 
    99             : $dupe = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' AND post_date = '$post_date'"); 
    100         endif; 
    101  
    102         // Now lets put it in the DB 
    103         if ($dupe) 
    104             : echo 'Post already imported'; 
    105         else 
    106             : $wpdb->query("INSERT INTO $wpdb->posts  
    107                     (post_author, post_date, post_date_gmt, post_content, post_title,post_status, comment_status, ping_status, post_name, guid) 
    108                     VALUES  
    109                     ('$post_author', '$post_date', DATE_ADD('$post_date', INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE), '$content', '$title', 'publish', '$comment_status', '$ping_status', '$post_name', '$guid')"); 
    110         $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' AND post_date = '$post_date'"); 
    111         if (!$post_id) 
    112             die("couldn't get post ID"); 
    113         if (0 != count($categories)) 
    114             : foreach ($categories as $post_category) 
    115                 : $post_category = unhtmlentities($post_category); 
    116         // See if the category exists yet 
    117         $cat_id = $wpdb->get_var("SELECT cat_ID from $wpdb->categories WHERE cat_name = '$post_category'"); 
    118         if (!$cat_id && '' != trim($post_category)) { 
    119             $cat_nicename = sanitize_title($post_category); 
    120             $wpdb->query("INSERT INTO $wpdb->categories (cat_name, category_nicename) VALUES ('$post_category', '$cat_nicename')"); 
    121             $cat_id = $wpdb->get_var("SELECT cat_ID from $wpdb->categories WHERE cat_name = '$post_category'"); 
    122         } 
    123         if ('' == trim($post_category)) 
    124             $cat_id = 1; 
    125         // Double check it's not there already 
    126         $exists = $wpdb->get_row("SELECT * FROM $wpdb->post2cat WHERE post_id = $post_id AND category_id = $cat_id"); 
    127  
    128         if (!$exists) { 
    129             $wpdb->query(" 
    130                         INSERT INTO $wpdb->post2cat 
    131                         (post_id, category_id) 
    132                         VALUES 
    133                         ($post_id, $cat_id) 
    134                         "); 
    135         } 
    136         endforeach; 
    137         else 
    138             : $exists = $wpdb->get_row("SELECT * FROM $wpdb->post2cat WHERE post_id = $post_id AND category_id = 1"); 
    139         if (!$exists) 
    140             $wpdb->query("INSERT INTO $wpdb->post2cat (post_id, category_id) VALUES ($post_id, 1) "); 
    141         endif; 
    142         echo 'Done!</li>'; 
    143         endif; 
    144  
    145         endforeach; 
    146134        echo '</ol>'; 
    147135 
    148136    } 
    149      
    150      
     137 
    151138    function import() { 
    152         // FIXME:  Don't die. 
    153         if ('' != RSSFILE && !file_exists(RSSFILE)) die("The file you specified does not seem to exist. Please check the path you've given."); 
    154         if ('' == RSSFILE) die("You must edit the RSSFILE line as described on the <a href='import-mt.php'>previous page</a> to continue."); 
    155      
     139        // FIXME:  Don't die 
     140        if ('' == RSSFILE) 
     141            die("You must edit the RSSFILE line as described on the <a href='import-mt.php'>previous page</a> to continue."); 
     142 
     143        if (!file_exists(RSSFILE)) 
     144            die("The file you specified does not seem to exist. Please check the path you've given."); 
     145 
    156146        $this->get_posts(); 
    157147        $this->import_posts(); 
    158         echo '<h3>All done. <a href="../">Have fun!</a></h3>'; 
     148        echo '<h3>All done. <a href="' . get_option('home') . '">Have fun!</a></h3>'; 
    159149    } 
    160      
     150 
    161151    function dispatch() { 
    162         if (empty($_GET['step'])) 
     152        if (empty ($_GET['step'])) 
    163153            $step = 0; 
    164154        else 
    165155            $step = (int) $_GET['step']; 
    166          
     156 
    167157        switch ($step) { 
    168             case 0
     158            case 0
    169159                $this->greet(); 
    170160                break; 
    171             case 1
     161            case 1
    172162                $this->import(); 
    173163                break; 
    174164        } 
    175165    } 
    176      
     166 
    177167    function RSS_Import() { 
    178168        // Nothing.  
     
    182172$rss_import = new RSS_Import(); 
    183173 
    184 register_importer('rss', 'RSS', 'Import posts from and RSS feed', array($rss_import, 'dispatch')); 
    185  
     174register_importer('rss', 'RSS', 'Import posts from and RSS feed', array ($rss_import, 'dispatch')); 
    186175?>