private function add_rewrite_rule($name, $params)
 {
     $rule = RewriteRules::by_name($name);
     if (count($rule) == 1) {
         $rule = $rule[0];
         foreach ($params as $key => $param) {
             $rule->{$key} = $param;
         }
         $rule->update();
     } else {
         $rule = new RewriteRule($params);
         $rule->insert();
     }
 }
    /**
     * Imports a single post from the s9y database into the 
     * habari database.
     *
     * Note that this function calls import_comment() for each
     * comment attached to the imported post
     *
     * @param		post_info					QueryRecord of imported post information
     * @param		habari_user_id		The habari user ID of the post's author
     * @return	TRUE or FALSE if import of post succeeded
     */
    private function import_post($post_info = array(), $habari_user_id)
    {
        /* 
         * Import the post itself 
         */
        $post = new Post();
        $post->user_id = $habari_user_id;
        $post->guid = $post->guid;
        /* @TODO: This works to create a GUID, but man, it's weird. */
        $post->info->s9y_id = $post_info->id;
        $post->title = $this->transcode($post_info->title);
        $content = empty($post_info->extended) ? $post_info->body : $post_info->body . $post_info->extended;
        $post->content = $this->transcode($content);
        $post->status = $post_info->isdraft == "true" ? Post::status('draft') : Post::status('published');
        $post->content_type = Post::type('entry');
        $post->updated = date('Y-m-d H:i:s', $post_info->last_modified);
        $post->pubdate = $post_info->isdraft == "false" ? date('Y-m-d H:i:s', $post_info->timestamp) : NULL;
        if ($this->category_import && isset($categories) && $categories instanceof QueryRecord) {
            $post->tags = $categories->to_array();
        }
        if ($post->insert()) {
            if ($this->port_rewrites) {
                $rewrite = new RewriteRule(array('name' => 'from_s9yimporter_' . $post_info->id, 'parse_regex' => '%^archives/' . $post_info->id . '-(?P<r>.*)$%i', 'build_str' => $post->slug . '(/{$p})', 'handler' => 'actionHandler', 'action' => 'redirect', 'priority' => 1, 'is_active' => 1, 'rule_class' => RewriteRule::RULE_CUSTOM, 'description' => 'redirects /archives/' . $post_info->id . ' to /' . $post->slug));
                $rewrite->insert();
            }
            /*
             * If we are going to import taxonomy, , then first check to see if 
             * this post has any categories attached to it, and import the relationship
             * using the cached habari->s9y tag map. 
             *
             * mysql> desc s9y_entrycat;
             * +------------+---------+------+-----+---------+-------+
             * | Field      | Type    | Null | Key | Default | Extra |
             * +------------+---------+------+-----+---------+-------+
             * | entryid    | int(11) | NO   | PRI | 0       |       | 
             * | categoryid | int(11) | NO   | PRI | 0       |       | 
             * +------------+---------+------+-----+---------+-------+
             */
            $result = TRUE;
            if ($this->category_import) {
                $sql = <<<ENDOFSQL
SELECT c.categoryid
FROM {$this->s9y_db_prefix}category c 
INNER JOIN {$this->s9y_db_prefix}entrycat ec
ON c.categoryid = ec.categoryid
AND ec.entryid = ?
ENDOFSQL;
                if (FALSE !== ($categories = $this->s9ydb->get_results($sql, array($post_info->id), 'QueryRecord'))) {
                    foreach ($categories as $category) {
                        $result &= $this->import_post_category($post->id, $this->imported_categories[$category->categoryid]);
                        if ($this->port_rewrites && !isset($this->rewritten_categories[$category->categoryid])) {
                            // rss feed link:
                            $this->rewritten_categories[$category->categoryid] = 1;
                            $rew_url = URL::get('atom_feed_tag', array('tag' => $this->imported_category_names[$category->categoryid]), true, false, false);
                            $rewrite = new RewriteRule(array('name' => 'from_s9yimporter_category_feed', 'parse_regex' => '%^feeds/categories/' . $category->categoryid . '-(?P<r>.*)$%i', 'build_str' => $rew_url . '(/{$p})', 'handler' => 'actionHandler', 'action' => 'redirect', 'priority' => 1, 'is_active' => 1, 'rule_class' => RewriteRule::RULE_CUSTOM, 'description' => 'redirects s9y category feed to habari feed'));
                            $rewrite->insert();
                        }
                    }
                }
            }
            /* 
             * Grab the comments and insert `em 
             *  
             * mysql> desc s9y_comments;
             * +------------+----------------------+------+-----+---------+----------------+
             * | Field      | Type                 | Null | Key | Default | Extra          |
             * +------------+----------------------+------+-----+---------+----------------+
             * | id         | int(11)              | NO   | PRI | NULL    | auto_increment | 
             * | entry_id   | int(10) unsigned     | NO   | MUL | 0       |                | 
             * | parent_id  | int(10) unsigned     | NO   | MUL | 0       |                | 
             * | timestamp  | int(10) unsigned     | YES  |     | NULL    |                | 
             * | title      | varchar(150)         | YES  |     | NULL    |                | 
             * | author     | varchar(80)          | YES  |     | NULL    |                | 
             * | email      | varchar(200)         | YES  |     | NULL    |                | 
             * | url        | varchar(200)         | YES  |     | NULL    |                | 
             * | ip         | varchar(15)          | YES  |     | NULL    |                | 
             * | body       | text                 | YES  |     | NULL    |                | 
             * | type       | varchar(100)         | YES  | MUL | regular |                | 
             * | subscribed | enum('true','false') | NO   |     | true    |                | 
             * | status     | varchar(50)          | NO   | MUL |         |                | 
             * | referer    | varchar(200)         | YES  |     | NULL    |                | 
             * +------------+----------------------+------+-----+---------+----------------+
             */
            $sql = <<<ENDOFSQL
SELECT
\tid
, parent_id
, `timestamp`
, title
, author
, email
, url
, ip
, body
, type
, subscribed
, status
, referer
FROM {$this->s9y_db_prefix}comments
WHERE entry_id = ?
ENDOFSQL;
            if ($this->comments_ignore_unapproved) {
                $sql .= " AND status = 'Approved' ";
            }
            $comments = $this->s9ydb->get_results($sql, array($post_info->id), 'QueryRecord');
            if (count($comments) > 0) {
                echo "Starting import of <b>" . count($comments) . "</b> comments for post \"" . $post->title . "\"...";
                foreach ($comments as $comment) {
                    $result &= $this->import_comment($comment, $post->id);
                }
                if ($result) {
                    echo $this->success_html() . "<br />";
                } else {
                    echo $this->fail_html() . "<br />";
                }
                return $result;
            } else {
                return TRUE;
            }
        } else {
            /* Something went wrong on $post->insert() */
            EventLog::log($e->getMessage(), 'err', null, null, print_r(array($post, $e), 1));
            Session::error($e->getMessage());
            return FALSE;
        }
    }