/** * TODO: be more careful * INSERT INTO {tag2post} / SELECT $master_tag->ID,post_ID FROM {tag2post} WHERE tag_id = $tag->id" and then "DELETE FROM {tag2post} WHERE tag_id = $tag->id" * Renames tags. * If the master tag exists, the tags will be merged with it. * If not, it will be created first. * * @param Array tags The tag text, slugs or ids to be renamed * @param mixed master The Tag to which they should be renamed, or the slug, text or id of it **/ public static function rename($master, $tags) { if (!is_array($tags)) { $tags = array($tags); } $tag_names = array(); // get array of existing tags first to make sure we don't conflict with a new master tag foreach ($tags as $tag) { $posts = array(); $post_ids = array(); $tag = Tags::get_one($tag); // get all the post ID's tagged with this tag $posts = DB::get_results('SELECT post_id FROM {tag2post} WHERE tag_id = ?', array($tag->id)); if (count($posts) > 0) { // build a list of all the post_id's we need for the new tag foreach ($posts as $post) { $post_ids[] = $post->post_id; } $tag_names[] = $tag->tag; } Tags::delete($tag); } // get the master tag $master_tag = Tags::get_one($master); if (!isset($master_tag->slug)) { // it didn't exist, so we assume it's tag text and create it $master_tag = Tag::create(array('tag_slug' => Utils::slugify($master), 'tag_text' => $master)); $master_ids = array(); } else { // get the posts the tag is already on so we don't duplicate them $master_posts = DB::get_results('SELECT post_id FROM {tag2post} WHERE tag_id = ?', array($master_tag->id)); $master_ids = array(); foreach ($master_posts as $master_post) { $master_ids[] = $master_post->post_id; } } if (count($post_ids) > 0) { // only try and add the master tag to posts it's not already on $post_ids = array_diff($post_ids, $master_ids); // link the master tag to each distinct post we removed tags from foreach ($post_ids as $post_id) { DB::query('INSERT INTO {tag2post} ( tag_id, post_id ) VALUES ( ?, ? )', array($master_tag->id, $post_id)); } } EventLog::log(sprintf(_n('Tag %s has been renamed to %s.', 'Tags %s have been renamed to %s.', count($tags)), implode($tag_names, ', '), $master), 'info', 'tag', 'habari'); }
/** * Returns a Tag object based on a supplied ID * * @param Integer tag_id The ID of the tag to retrieve * @return A Tag object */ public static function get_by_id($tag) { return Tags::get_one($tag); }
/** * Parse tag parameters from a URL string * * @param String $tags The URL parameter string * * @return Array. Associative array of included and excluded tags */ public static function parse_url_tags($tags, $objectify = false) { $tags = explode(' ', $tags); $exclude_tag = array(); $include_tag = array(); foreach ($tags as $tag) { if (MultiByte::substr($tag, 0, 1) == '-') { $tag = MultiByte::substr($tag, 1); $exclude_tag[] = $objectify ? Tags::get_one(Utils::slugify($tag)) : Utils::slugify($tag); } else { $include_tag[] = $objectify ? Tags::get_one(Utils::slugify($tag)) : Utils::slugify($tag); } } return compact('include_tag', 'exclude_tag'); }
/** * Create the UI for stage two of the WP import process * * This stage kicks off the actual import process. * * @return string The UI for the second stage of the import process */ private function stage3() { $valid_fields = array('db_name', 'db_host', 'db_port', 'db_user', 'db_pass', 'db_prefix', 's9y_version', 's9y_root_web', 's9y_input_version', 'category_import', 'comments_ignore_unapproved', 'rewrites_import', 'merge_user', 'merge_user_matched', 'import_user'); $inputs = $this->get_valid_inputs($valid_fields); extract($inputs); /* * Cache some local private variables for use in * the import_xxx() private functions */ $this->comments_ignore_unapproved = $comments_ignore_unapproved; $this->category_import = $category_import; $this->s9y_db_prefix = $db_prefix; $this->port_rewrites = $rewrites_import; if ($rewrites_import) { // atom feed link: $rew_url = URL::get('atom_feed', array('index' => 1), true, false, false); $rewrite = new RewriteRule(array('name' => 'from_s9yimporter_atom_feed', 'parse_regex' => '%^feeds/atom(?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 atom feed to habari feed')); $rewrite->insert(); // rss feed link: $rew_url = Plugins::is_loaded('RSS 2.0') ? URL::get('rss_feed', array('index' => 1), true, false, false) : URL::get('atom_feed', array('index' => 1), true, false, false); $rewrite = new RewriteRule(array('name' => 'from_s9yimporter_rss_feed', 'parse_regex' => '%^feeds/index.rss(?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 rss feed to habari feed')); $rewrite->insert(); // comments feed link: $rew_url = Plugins::is_loaded('RSS 2.0') ? URL::get('rss_feed_comments', array(), true, false, false) : URL::get('atom_feed_comments', array(), true, false, false); $rewrite = new RewriteRule(array('name' => 'from_s9yimporter_comments_feed', 'parse_regex' => '%^feeds/comments.rss(?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 rss feed to habari feed')); $rewrite->insert(); } if (FALSE !== ($this->s9ydb = $this->s9y_connect($db_host, $db_name, $db_user, $db_pass, $db_prefix, $db_port))) { /* * First step is to go through our import_user and * merge_user arrays and see if we need to merge the * incoming authoring user with an existing user in * Habari, ignore the user, or create a new Habari user. * * $import_user= array( [imported_user_id] => [1 | null], [next_imported_user_id] => [1 | null], ...) * $merge_user= array( [imported_user_id] => [habari_user_id | "__new_user"], ...) */ $users = array(); foreach ($import_user as $import_user_id => $import_this) { /* Is this s9y user selected for import? */ if ($import_this != 1) { continue; } $users[$import_user_id] = array('imported_user_id' => $import_user_id); /* Was there a direct match for this imported user? */ if (in_array($import_user_id, array_keys($merge_user_matched))) { $users[$import_user_id]['habari_user_id'] = $merge_user_matched[$import_user_id]; } /* Is this s9y user manually selected to merge with a habari user? */ if (isset($merge_user) && in_array($import_user_id, array_keys($merge_user))) { if ($merge_user[$import_user_id] != '__new_user') { $users[$import_user_id]['habari_user_id'] = $merge_user[$import_user_id]; } } } echo "Starting import transaction.<br />"; $this->s9ydb->begin_transaction(); if ($category_import) { /* * If we are importing the categories as taxonomy, * let's go ahead and import the base category tags * now, and during the post import, we'll attach * the category tag to the relevant posts. * * mysql> desc s9y_category; * +----------------------+--------------+------+-----+---------+----------------+ * | Field | Type | Null | Key | Default | Extra | * +----------------------+--------------+------+-----+---------+----------------+ * | categoryid | int(11) | | PRI | NULL | auto_increment | * | category_name | varchar(255) | YES | | NULL | | * | category_icon | varchar(255) | YES | | NULL | | * | category_description | text | YES | | NULL | | * | authorid | int(11) | YES | MUL | NULL | | * | category_left | int(11) | YES | MUL | 0 | | * | category_right | int(11) | YES | | 0 | | * | parentid | int(11) | | MUL | 0 | | * +----------------------+--------------+------+-----+---------+----------------+ */ $sql = <<<ENDOFSQL SELECT categoryid, category_name FROM `{$db_prefix}category ENDOFSQL; if (FALSE !== ($imported_categories = $this->s9ydb->get_results($sql, array(), 'QueryRecord'))) { $num_categories_imported = 0; foreach ($imported_categories as $imported_category) { if ($tag_check = Tags::get_one($imported_category->category_name)) { // tag already exists $this->imported_categories[$imported_category->categoryid] = $tag_check->id; $this->imported_category_names[$imported_category->categoryid] = $imported_category->category_name; ++$num_categories_imported; continue; } if ($new_tag = Tag::create(array('tag_text' => $imported_category->category_name))) { $this->imported_categories[$imported_category->categoryid] = $new_tag->id; $this->imported_category_names[$imported_category->categoryid] = $imported_category->category_name; ++$num_categories_imported; } else { $this->s9ydb->rollback(); return FALSE; } } printf("%d categories imported as tags...<br />", $num_categories_imported); } } /* * Now that we have an array of the users to import, * let's grab some information about those users and * call the import_user() method for each one. * * mysql> desc s9y_authors; * +-----------------+-----------------+------+-----+---------+----------------+ * | Field | Type | Null | Key | Default | Extra | * +-----------------+-----------------+------+-----+---------+----------------+ * | realname | varchar(255) | NO | | | | * | username | varchar(20) | YES | | NULL | | * | password | varchar(32) | YES | | NULL | | * | authorid | int(11) | NO | PRI | NULL | auto_increment | * | mail_comments | int(1) | YES | | 1 | | * | mail_trackbacks | int(1) | YES | | 1 | | * | email | varchar(128) | NO | | | | * | userlevel | int(4) unsigned | NO | | 0 | | * | right_publish | int(1) | YES | | 1 | | * +-----------------+-----------------+------+-----+---------+----------------+ */ $sql = <<<ENDOFSQL SELECT a.authorid, a.realname, a.username, a.email FROM `{$db_prefix}authors` a ENDOFSQL; $sql .= " WHERE a.authorid IN (" . implode(',', array_keys($users)) . ")"; $import_users = $this->s9ydb->get_results($sql, array(), 'QueryRecord'); $result = TRUE; foreach ($import_users as $import_user) { $result &= $this->import_user($import_user->authorid, $import_user, isset($users[$import_user->authorid]['habari_user_id']) ? (int) $users[$import_user->authorid]['habari_user_id'] : NULL); } if ($result) { echo "Committing import transaction.<br />"; $this->s9ydb->commit(); return "import finished."; /* Display success */ } else { echo "Rolling back failed import transaction.<br />"; $this->s9ydb->rollback(); /* Display failure -- but how..? */ return FALSE; } } }