public function addComment(Comment $comment) { $comment->set('record_model', $this->_invoker->getTable()->getComponentName()); $comment->set('record_id', $this->_invoker->get('id')); $comment->save(); return $this->_invoker; }
function getList($pagina = 1, $nrpp = Contants::NRPP) { $registroIni = ($pagina - 1) * $nrpp; $r = array(); $this->bd->select($this->tabla, "*", "1=1", array(), "ID_comment", "{$registroIni}, {$nrpp}"); while ($row = $this->bd->getRow()) { $comment = new Comment(); $comment->set($row); $r[] = $comment; } return $r; }
function add_comment() { $this->assertLoggedIn(); try { $this->setTitle('Add Comment'); $content_id = $this->args('content_id'); $content_type = $this->args('content_type'); $content = Comment::getContent($content_id, $content_type); if (!is_object($content)) { throw new Exception("That content does not exist."); } if (!$content->isHydrated()) { throw new Exception("That content does not exist."); } if (!$content->canComment()) { throw new Exception("You cannot comment on this content."); } $form = $this->_createAddCommentForm($content_id, $content_type); //handle our form if ($form->checkSubmitAndValidate($this->args())) { $comment = new Comment(); $comment->set('content_id', $form->data('content_id')); $comment->set('content_type', $form->data('content_type')); $comment->set('comment', $form->data('comment')); $comment->set('user_id', User::$me->id); $comment->set('comment_date', date("Y-m-d H:i:s")); $comment->save(); Activity::log("commented on " . $content->getLink() . "."); $this->forwardToUrl($content->getUrl()); } $this->set('form', $form); } catch (Exception $e) { $this->setTitle('Add Comment - Error'); $this->set('megaerror', $e->getMessage()); } }
public function save() { $form = Form::load('logbook.views.AddBlogComment'); $capval = Captcha::validate(); if($form->validate()&&$capval) { Application::alterParam('comment_content',strip_tags(Application::param('comment_content'))); $item = new Comment(); $item->parse(); $item->set('comment_date',date('Y-m-d H:M:S')); $item->setSafe('author_id',Logbook::current()->authorId()); $item->save(); Application::setParam('author_id',Application::param('author_id')); Application::setParam('entry_id',Application::param('entry_id')); $this->redirectOnSave(); } }
/** * Create sample comments and display a process of creating * * @param integer Blog ID * @param integer Number of comments * @param integer Number of posts */ function tool_create_sample_comments($blog_ID, $num_comments, $num_posts) { global $DB, $posttypes_specialtypes, $localtimenow, $Hit, $Messages, $Debuglog; $BlogCache =& get_BlogCache(); $selected_Blog = $BlogCache->get_by_ID($blog_ID, false, false); if ($selected_Blog == NULL) { // Incorrect blog ID, Exit here return; } echo T_('Creating of the sample comments...'); evo_flush(); /** * Disable log queries because it increases the memory and stops the process with error "Allowed memory size of X bytes exhausted..." */ $DB->log_queries = false; $curr_orderby = $selected_Blog->get_setting('orderby'); if ($curr_orderby == 'RAND') { $curr_orderby .= '()'; } else { $curr_orderby = 'post_' . $curr_orderby; } $curr_orderdir = $selected_Blog->get_setting('orderdir'); // find the $num_posts latest posts in blog $SQL = new SQL(); $SQL->SELECT('post_ID'); $SQL->FROM('T_items__item'); $SQL->FROM_add('INNER JOIN T_categories ON post_main_cat_ID = cat_ID'); $SQL->WHERE('cat_blog_ID = ' . $DB->quote($blog_ID)); $SQL->WHERE_and('post_status = ' . $DB->quote('published')); // Set condition to not create sample comments for special posts $SQL->WHERE_and('post_ptyp_ID NOT IN ( ' . $DB->quote($posttypes_specialtypes) . ' )'); $SQL->ORDER_BY($curr_orderby . ' ' . $curr_orderdir . ', post_ID ' . $curr_orderdir); $SQL->LIMIT($num_posts); $items_result = $DB->get_results($SQL->get(), ARRAY_A, 'Find the x latest posts in blog'); $count = 1; $fix_content = 'This is an auto generated comment for testing the moderation features. http://www.test.com/test_comment_'; // go through on selected items foreach ($items_result as $row) { $item_ID = $row['post_ID']; $ItemCache =& get_ItemCache(); $commented_Item =& $ItemCache->get_by_ID($item_ID); // create $num_comments comments for each item for ($i = 0; $i < $num_comments; $i++) { $author = 'Test ' . $count; $email = 'test_' . $count . '@test.com'; $url = 'http://www.test-' . rand(1, 3) . '.com/test_comment_' . $count; $content = $fix_content . $count; for ($j = 0; $j < 50; $j++) { // create 50 random word $length = rand(1, 15); $word = generate_random_key($length, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'); $content = $content . ' ' . $word; } // create and save a new comment $Comment = new Comment(); $Comment->set_Item($commented_Item); $Comment->set('status', 'draft'); $Comment->set('author', $author); $Comment->set('author_email', $email); $Comment->set('author_url', $url); $Comment->set('content', $content); $Comment->set('date', date('Y-m-d H:i:s', $localtimenow)); $Comment->set('author_IP', $Hit->IP); $Comment->dbsave(); $count++; if ($count % 100 == 0) { // Display a process of creating by one dot for 100 comments echo ' .'; evo_flush(); } // Clear all debug messages, To avoid an error about full memory $Debuglog->clear('all'); } } echo ' OK.'; $Messages->add(sprintf(T_('Created %d comments.'), $count - 1), 'success'); }
/** * Create a new Comment and return an XML-RPC response * * @param array of params * - Item (object) * - User (object) Can be NULL for anonymous comments * - password (string) * - username (string) * - comment_parent (int) * - content (string) * - author (string) * - author_url (string) * - author_email (string) * @return xmlrpcmsg */ function xmlrpcs_new_comment($params = array(), &$commented_Item) { global $DB, $Plugins, $Messages, $Hit, $localtimenow, $require_name_email, $minimum_comment_interval; $params = array_merge(array('password' => '', 'username' => '', 'content' => '', 'comment_parent' => 0, 'author' => '', 'author_url' => '', 'author_email' => ''), $params); $comment = $params['content'] = trim($params['content']); if (!$commented_Item->can_comment(NULL)) { return xmlrpcs_resperror(5, T_('You cannot leave comments on this post!')); } $commented_Item->load_Blog(); // Make sure Blog is loaded (will be needed whether logged in or not) if (empty($params['username']) && empty($params['password'])) { // Anonymous comment // NO permission to edit! $perm_comment_edit = false; $User = NULL; $author = trim($params['author']); $email = trim($params['author_email']); if ($commented_Item->Blog->get_setting('allow_anon_url')) { $url = trim($params['author_url']); } else { $url = NULL; } // we need some id info from the anonymous user: if ($require_name_email) { // We want Name and EMail with comments if (empty($author)) { return xmlrpcs_resperror(5, T_('Please fill in your name.')); } if (empty($email)) { return xmlrpcs_resperror(5, T_('Please fill in your email.')); } } if (!empty($author) && antispam_check($author)) { return xmlrpcs_resperror(5, T_('Supplied name is invalid.')); } if (!empty($email) && (!is_email($email) || antispam_check($email))) { return xmlrpcs_resperror(5, T_('Supplied email address is invalid.')); } if (!stristr($url, '://') && !stristr($url, '@')) { // add 'http://' if no protocol defined for URL; but not if the user seems to be entering an email address alone $url = 'http://' . $url; } if (strlen($url) <= 8) { // ex: https:// is 8 chars $url = ''; } // Note: as part of the validation we require the url to be absolute; otherwise we cannot detect bozos typing in // a title for their comment or whatever... if ($error = validate_url($url, 'commenting')) { return xmlrpcs_resperror(5, T_('Supplied website address is invalid: ') . $error); } } else { $User =& $params['User']; $perm_comment_edit = $User->check_perm('blog_comment!published', 'edit', false, $commented_Item->Blog->ID); $author = $User->ID; $url = $User->url; $email = $User->email; } // Following call says "WARNING: this does *NOT* (necessarilly) make the HTML code safe.": $comment = check_html_sanity($comment, $perm_comment_edit ? 'posting' : 'commenting', $User); if ($comment === false) { // ERROR! Restore original comment for further editing: $comment = $params['content']; } if (empty($comment)) { // comment should not be empty! return xmlrpcs_resperror(5, T_('Please do not send empty comments.')); } $now = date2mysql($localtimenow); /* * Flood-protection * NOTE: devs can override the flood protection delay in /conf/_overrides_TEST.php * TODO: Put time check into query? * TODO: move that as far !!UP!! as possible! We want to waste minimum resources on Floods * TODO: have several thresholds. For example: * 1 comment max every 30 sec + 5 comments max every 10 minutes + 15 comments max every 24 hours * TODO: factorize with trackback */ $query = 'SELECT MAX(comment_date) FROM T_comments WHERE comment_author_IP = ' . $DB->quote($Hit->IP) . ' OR comment_author_email = ' . $DB->quote($email); $ok = 1; if ($then = $DB->get_var($query)) { $time_lastcomment = mysql2date("U", $then); $time_newcomment = mysql2date("U", $now); if ($time_newcomment - $time_lastcomment < $minimum_comment_interval) { $ok = 0; } } if (!$ok) { return xmlrpcs_resperror(5, sprintf(T_('You can only post a new comment every %d seconds.'), $minimum_comment_interval)); } /* end flood-protection */ /** * Create comment object. Gets validated, before recording it into DB: */ $Comment = new Comment(); $Comment->set('type', 'comment'); $Comment->set_Item($commented_Item); if ($User) { // User is logged in, we'll use his ID $Comment->set_author_User($User); } else { // User is not logged in: $Comment->set('author', $author); $Comment->set('author_email', $email); $Comment->set('author_url', $url); } if (!empty($params['comment_parent'])) { $Comment->set('in_reply_to_cmt_ID', intval($params['comment_parent'])); } $Comment->set('author_IP', $Hit->IP); $Comment->set('date', $now); $Comment->set('content', $comment); if ($perm_comment_edit) { // User has perm to moderate comments, publish automatically: $Comment->set('status', 'published'); } else { // Assign default status for new comments: $Comment->set('status', $commented_Item->Blog->get_setting('new_feedback_status')); } $action = 'submit_comment_post_' . $commented_Item->ID; // Trigger event: a Plugin could add a $category="error" message here.. $Plugins->trigger_event('BeforeCommentFormInsert', array('Comment' => &$Comment, 'original_comment' => $params['content'], 'is_preview' => false, 'action' => &$action)); if ($Messages->has_errors()) { return xmlrpcs_resperror(5, $Messages->get_string('Cannot create comment, please correct these errors:' . "\n", '', " // \n", 'xmlrpc')); } $Comment->dbinsert(); if ($Comment->ID) { // comment has not been deleted // Trigger event: a Plugin should cleanup any temporary data here.. $Plugins->trigger_event('AfterCommentFormInsert', array('Comment' => &$Comment, 'original_comment' => $params['content'])); /* * -------------------------- * New comment notifications: * -------------------------- */ // TODO: dh> this should only send published feedback probably and should also use "outbound_notifications_mode" // fp> yes for general users, but comment moderators need to receive notifications for new unpublished comments // asimo> this handle moderators and general users as well and use "outbound_notifications_mode" in case of general users // Moderators will get emails about every new comment // Subscribed user will only get emails about new published comments $executed_by_userid = empty($User) ? NULL : $User->ID; $Comment->handle_notifications(true, $executed_by_userid); } else { return xmlrpcs_resperror(99, 'Error while inserting comment: ' . $DB->last_error); } return new xmlrpcresp(new xmlrpcval($Comment->ID, 'int')); }
if (!empty($excerpt)) { $comment .= '<br />'; } } $comment .= $excerpt; $comment = format_to_post($comment, 1); // includes antispam if (empty($comment)) { // comment should not be empty! $Messages->add(T_('Please do not send empty comment'), 'error'); } /** * @global Comment Trackback object */ $Comment = new Comment(); $Comment->set('type', 'trackback'); $Comment->set_Item($commented_Item); $Comment->set('author', $blog_name); $Comment->set('author_url', $url); $Comment->set('author_IP', $Hit->IP); $Comment->set('date', date('Y-m-d H:i:s', $localtimenow)); $Comment->set('content', $comment); // Assign default status for new comments: $Comment->set('status', $commented_Item->Blog->get_setting('new_feedback_status')); // Trigger event, which may add a message of category "error": $Plugins->trigger_event('BeforeTrackbackInsert', array('Comment' => &$Comment)); // Display errors: if ($errstring = $Messages->get_string('Cannot insert trackback, please correct these errors:', '')) { trackback_response(1, $errstring); // tblue> Note: the spec at <http://www.sixapart.com/pronet/docs/trackback_spec> // only shows error code 1 in the example response
/** * Import WordPress data from XML file into b2evolution database */ function wpxml_import() { global $DB, $tableprefix; // Load classes: load_class('regional/model/_country.class.php', 'Country'); load_class('regional/model/_region.class.php', 'Region'); load_class('regional/model/_subregion.class.php', 'Subregion'); load_class('regional/model/_city.class.php', 'City'); // Set Blog from request blog ID $wp_blog_ID = param('wp_blog_ID', 'integer', 0); $BlogCache =& get_BlogCache(); $wp_Blog =& $BlogCache->get_by_ID($wp_blog_ID); // The import type ( replace | append ) $import_type = param('import_type', 'string', 'replace'); // Get XML file from request $xml_file = $_FILES['wp_file']; // Parse WordPress XML file into array $xml_data = wpxml_parser($xml_file['tmp_name']); $DB->begin(); if ($import_type == 'replace') { // Remove data from selected blog // Get existing categories $SQL = new SQL(); $SQL->SELECT('cat_ID'); $SQL->FROM('T_categories'); $SQL->WHERE('cat_blog_ID = ' . $DB->quote($wp_blog_ID)); $old_categories = $DB->get_col($SQL->get()); if (!empty($old_categories)) { // Get existing posts $SQL = new SQL(); $SQL->SELECT('post_ID'); $SQL->FROM('T_items__item'); $SQL->WHERE('post_main_cat_ID IN ( ' . implode(', ', $old_categories) . ' )'); $old_posts = $DB->get_col($SQL->get()); } echo T_('Removing the comments... '); evo_flush(); if (!empty($old_posts)) { $SQL = new SQL(); $SQL->SELECT('comment_ID'); $SQL->FROM('T_comments'); $SQL->WHERE('comment_post_ID IN ( ' . implode(', ', $old_posts) . ' )'); $old_comments = $DB->get_col($SQL->get()); $DB->query('DELETE FROM T_comments WHERE comment_post_ID IN ( ' . implode(', ', $old_posts) . ' )'); if (!empty($old_comments)) { $DB->query('DELETE FROM T_comments__votes WHERE cmvt_cmt_ID IN ( ' . implode(', ', $old_comments) . ' )'); } } echo T_('OK') . '<br />'; echo T_('Removing the posts... '); evo_flush(); if (!empty($old_categories)) { $DB->query('DELETE FROM T_items__item WHERE post_main_cat_ID IN ( ' . implode(', ', $old_categories) . ' )'); if (!empty($old_posts)) { // Remove the post's data from related tables $DB->query('DELETE FROM T_items__item_settings WHERE iset_item_ID IN ( ' . implode(', ', $old_posts) . ' )'); $DB->query('DELETE FROM T_items__prerendering WHERE itpr_itm_ID IN ( ' . implode(', ', $old_posts) . ' )'); $DB->query('DELETE FROM T_items__subscriptions WHERE isub_item_ID IN ( ' . implode(', ', $old_posts) . ' )'); $DB->query('DELETE FROM T_items__version WHERE iver_itm_ID IN ( ' . implode(', ', $old_posts) . ' )'); $DB->query('DELETE FROM T_postcats WHERE postcat_post_ID IN ( ' . implode(', ', $old_posts) . ' )'); $DB->query('DELETE FROM T_slug WHERE slug_itm_ID IN ( ' . implode(', ', $old_posts) . ' )'); } } echo T_('OK') . '<br />'; echo T_('Removing the categories... '); evo_flush(); $DB->query('DELETE FROM T_categories WHERE cat_blog_ID = ' . $DB->quote($wp_blog_ID)); echo T_('OK') . '<br />'; echo T_('Removing the tags that are no longer used... '); evo_flush(); if (!empty($old_posts)) { // Remove the tags // Get tags from selected blog $SQL = new SQL(); $SQL->SELECT('itag_tag_ID'); $SQL->FROM('T_items__itemtag'); $SQL->WHERE('itag_itm_ID IN ( ' . implode(', ', $old_posts) . ' )'); $old_tags_this_blog = array_unique($DB->get_col($SQL->get())); if (!empty($old_tags_this_blog)) { // Get tags from other blogs $SQL = new SQL(); $SQL->SELECT('itag_tag_ID'); $SQL->FROM('T_items__itemtag'); $SQL->WHERE('itag_itm_ID NOT IN ( ' . implode(', ', $old_posts) . ' )'); $old_tags_other_blogs = array_unique($DB->get_col($SQL->get())); $old_tags_other_blogs_sql = !empty($old_tags_other_blogs) ? ' AND tag_ID NOT IN ( ' . implode(', ', $old_tags_other_blogs) . ' )' : ''; // Remove the tags that are no longer used $DB->query('DELETE FROM T_items__tag WHERE tag_ID IN ( ' . implode(', ', $old_tags_this_blog) . ' )' . $old_tags_other_blogs_sql); } // Remove the links of tags with posts $DB->query('DELETE FROM T_items__itemtag WHERE itag_itm_ID IN ( ' . implode(', ', $old_posts) . ' )'); } echo T_('OK') . '<br /><br />'; } /* Import authors */ $authors = array(); $authors_IDs = array(); if (isset($xml_data['authors']) && count($xml_data['authors']) > 0) { global $Settings, $UserSettings; echo T_('Importing the users... '); evo_flush(); // Get existing users $SQL = new SQL(); $SQL->SELECT('user_login, user_ID'); $SQL->FROM('T_users'); $existing_users = $DB->get_assoc($SQL->get()); $authors_count = 0; foreach ($xml_data['authors'] as $author) { if (empty($existing_users[(string) $author['author_login']])) { // Insert new user into DB if User doesn't exist with current login name $GroupCache =& get_GroupCache(); if (!empty($author['author_group'])) { // Set user group from xml data if (($UserGroup =& $GroupCache->get_by_name($author['author_group'], false)) === false) { // If user's group doesn't exist yet, we should create new $UserGroup = new Group(); $UserGroup->set('name', $author['author_group']); $UserGroup->dbinsert(); } } else { // Set default user group is it is not defined in xml if (($UserGroup =& $GroupCache->get_by_name('Normal Users', false)) === false) { // Exit from import of users, because we cannot set default user group break; } } unset($author_created_from_country); if (!empty($author['author_created_from_country'])) { // Get country ID from DB by code $CountryCache =& get_CountryCache(); if (($Country =& $CountryCache->get_by_name($author['author_created_from_country'], false)) !== false) { $author_created_from_country = $Country->ID; } } // Get regional IDs by their names $author_regions = wp_get_regional_data($author['author_country'], $author['author_region'], $author['author_subregion'], $author['author_city']); $User = new User(); $User->set('login', $author['author_login']); $User->set('email', $author['author_email']); $User->set('firstname', $author['author_first_name']); $User->set('lastname', $author['author_last_name']); $User->set('pass', $author['author_pass']); $User->set_Group($UserGroup); $User->set('status', !empty($author['author_status']) ? $author['author_status'] : 'autoactivated'); $User->set('nickname', $author['author_nickname']); $User->set('url', $author['author_url']); $User->set('level', $author['author_level']); $User->set('locale', $author['author_locale']); $User->set('gender', $author['author_gender'] == 'female' ? 'F' : ($author['author_gender'] == 'male' ? 'M' : '')); if ($author['author_age_min'] > 0) { $User->set('age_min', $author['author_age_min']); } if ($author['author_age_max'] > 0) { $User->set('age_max', $author['author_age_max']); } if (isset($author_created_from_country)) { // User was created from this country $User->set('reg_ctry_ID', $author_created_from_country); } if (!empty($author_regions['country'])) { // Country $User->set('ctry_ID', $author_regions['country']); if (!empty($author_regions['region'])) { // Region $User->set('rgn_ID', $author_regions['region']); if (!empty($author_regions['subregion'])) { // Subregion $User->set('subrg_ID', $author_regions['subregion']); } if (!empty($author_regions['city'])) { // City $User->set('city_ID', $author_regions['city']); } } } $User->set('source', $author['author_source']); $User->set_datecreated($author['author_created_ts'], true); $User->set('lastseen_ts', $author['author_lastseen_ts']); $User->set('profileupdate_date', $author['author_profileupdate_date']); $User->dbinsert(); $user_ID = $User->ID; if (!empty($user_ID) && !empty($author['author_created_fromIPv4'])) { $UserSettings->set('created_fromIPv4', ip2int($author['author_created_fromIPv4']), $user_ID); } $authors_count++; } else { // Get ID of existing user $user_ID = $existing_users[(string) $author['author_login']]; } // Save user ID of current author $authors[$author['author_login']] = (string) $user_ID; $authors_IDs[$author['author_id']] = (string) $user_ID; } $UserSettings->dbupdate(); echo sprintf(T_('%d records'), $authors_count) . '<br />'; } /* Import categories */ $category_default = 0; // Get existing categories $SQL = new SQL(); $SQL->SELECT('cat_urlname, cat_ID'); $SQL->FROM('T_categories'); $SQL->WHERE('cat_blog_ID = ' . $DB->quote($wp_blog_ID)); $categories = $DB->get_assoc($SQL->get()); if (isset($xml_data['categories']) && count($xml_data['categories']) > 0) { echo T_('Importing the categories... '); evo_flush(); load_class('chapters/model/_chapter.class.php', 'Chapter'); load_funcs('locales/_charset.funcs.php'); $categories_count = 0; foreach ($xml_data['categories'] as $cat) { if (empty($categories[(string) $cat['category_nicename']])) { $Chapter = new Chapter(NULL, $wp_blog_ID); $Chapter->set('name', $cat['cat_name']); $Chapter->set('urlname', $cat['category_nicename']); $Chapter->set('description', $cat['category_description']); if (!empty($cat['category_parent']) && isset($categories[(string) $cat['category_parent']])) { // Set category parent ID $Chapter->set('parent_ID', $categories[(string) $cat['category_parent']]); } $Chapter->dbinsert(); // Save new category $categories[$cat['category_nicename']] = $Chapter->ID; if (empty($category_default)) { // Set first category as default $category_default = $Chapter->ID; } $categories_count++; } } if (empty($category_default)) { // Set first category as default foreach ($categories as $category_name => $category_ID) { $category_default = $category_ID; break; } } echo sprintf(T_('%d records'), $categories_count) . '<br />'; } /* Import tags */ $tags = array(); if (isset($xml_data['tags']) && count($xml_data['tags']) > 0) { echo T_('Importing the tags... '); evo_flush(); // Get existing tags $SQL = new SQL(); $SQL->SELECT('tag_name, tag_ID'); $SQL->FROM('T_items__tag'); $tags = $DB->get_assoc($SQL->get()); $tags_count = 0; foreach ($xml_data['tags'] as $tag) { if (empty($tags[(string) $tag['tag_name']])) { // Insert new tag into DB if tag doesn't exist with current name mysql_query('INSERT INTO ' . $tableprefix . 'items__tag ( tag_name ) VALUES ( ' . $DB->quote($tag['tag_name']) . ' )', $DB->dbhandle); $tag_ID = mysql_insert_id($DB->dbhandle); // Save new tag $tags[$tag['tag_name']] = (string) $tag_ID; $tags_count++; } } echo sprintf(T_('%d records'), $tags_count) . '<br />'; } /* Import posts */ $posts = array(); $comments = array(); if (isset($xml_data['posts']) && count($xml_data['posts']) > 0) { load_class('items/model/_item.class.php', 'Item'); // Set status's links between WP and b2evo names $post_statuses = array('publish' => 'published', 'pending' => 'deprecated', 'deprecated' => 'deprecated', 'protected' => 'protected', 'private' => 'private', 'redirected' => 'redirected', 'draft' => 'draft'); // Get post types $SQL = new SQL(); $SQL->SELECT('LOWER( ptyp_name ), ptyp_ID'); $SQL->FROM('T_items__type'); $post_types = $DB->get_assoc($SQL->get()); echo T_('Importing the posts... '); evo_flush(); foreach ($xml_data['posts'] as $post) { $author_ID = isset($authors[(string) $post['post_author']]) ? $authors[(string) $post['post_author']] : 1; $last_edit_user_ID = isset($authors[(string) $post['post_lastedit_user']]) ? $authors[(string) $post['post_lastedit_user']] : $author_ID; $post_main_cat_ID = $category_default; $post_extra_cat_IDs = array($post_main_cat_ID); $post_tags = array(); if (!empty($post['terms'])) { // Set categories and tags foreach ($post['terms'] as $term) { switch ($term['domain']) { case 'category': if (isset($categories[(string) $term['slug']])) { if ($post_main_cat_ID == $category_default) { // Set main category $post_main_cat_ID = $categories[(string) $term['slug']]; } else { // Set extra categories $post_extra_cat_IDs[] = $categories[(string) $term['slug']]; } } break; case 'post_tag': if (isset($tags[(string) $term['slug']])) { // Set tag $post_tags[] = $term['slug']; } break; } } } // Set post type ID $post_type_ID = isset($post_types[strtolower($post['post_type'])]) ? $post_types[strtolower($post['post_type'])] : '1'; // Get regional IDs by their names $item_regions = wp_get_regional_data($post['post_country'], $post['post_region'], $post['post_subregion'], $post['post_city']); $Item = new Item(); $Item->set('main_cat_ID', $post_main_cat_ID); $Item->set('creator_user_ID', $author_ID); $Item->set('lastedit_user_ID', $last_edit_user_ID); $Item->set('title', $post['post_title']); $Item->set('content', $post['post_content']); $Item->set('excerpt', $post['post_excerpt']); $Item->set('datestart', $post['post_date']); $Item->set('datecreated', !empty($post['post_datecreated']) ? $post['post_datecreated'] : $post['post_date']); $Item->set('datemodified', !empty($post['post_datemodified']) ? $post['post_datemodified'] : $post['post_date']); $Item->set('urltitle', !empty($post['post_urltitle']) ? $post['post_urltitle'] : $post['post_title']); $Item->set('status', isset($post_statuses[(string) $post['status']]) ? $post_statuses[(string) $post['status']] : 'draft'); $Item->set('comment_status', $post['comment_status'] == 'open' ? 'open' : 'closed'); $Item->set('ptyp_ID', $post_type_ID); if (empty($post['post_excerpt']) && !empty($post['post_content'])) { // Generate excerpt $Item->set('excerpt', wp_generate_excerpt($post['post_content'])); $Item->set('excerpt_autogenerated', '1'); } $Item->set('extra_cat_IDs', $post_extra_cat_IDs); $Item->set('dateset', $post['post_date_mode'] == 'set' ? 1 : 0); if (isset($authors[(string) $post['post_assigned_user']])) { $Item->set('assigned_user', $authors[(string) $post['post_assigned_user']]); } $Item->set('datedeadline', $post['post_datedeadline']); $Item->set('locale', $post['post_locale']); $Item->set('excerpt_autogenerated', $post['post_excerpt_autogenerated']); $Item->set('titletag', $post['post_titletag']); $Item->set('notifications_status', $post['post_notifications_status']); $Item->set('views', $post['post_views']); $Item->set('renderers', array($post['post_renderers'])); $Item->set('priority', $post['post_priority']); $Item->set('featured', $post['post_featured']); $Item->set('order', $post['post_order']); if (!empty($item_regions['country'])) { // Country $Item->set('ctry_ID', $item_regions['country']); if (!empty($item_regions['region'])) { // Region $Item->set('rgn_ID', $item_regions['region']); if (!empty($item_regions['subregion'])) { // Subregion $Item->set('subrg_ID', $item_regions['subregion']); } if (!empty($item_regions['city'])) { // City $Item->set('city_ID', $item_regions['city']); } } } if (count($post_tags) > 0) { $Item->tags = $post_tags; } $Item->dbinsert(); $posts[$post['post_id']] = $Item->ID; if (!empty($post['comments'])) { // Set comments $comments[$Item->ID] = $post['comments']; } } foreach ($xml_data['posts'] as $post) { // Set post parents if (!empty($post['post_parent']) && isset($posts[(string) $post['post_parent']])) { mysql_query('UPDATE ' . $tableprefix . 'items__item SET post_parent_ID = ' . $DB->quote($posts[(string) $post['post_parent']]) . ' WHERE post_ID = ' . $DB->quote($posts[(string) $post['post_id']]), $DB->dbhandle); } } echo sprintf(T_('%d records'), count($xml_data['posts'])) . '<br />'; } /* Import comments */ if (!empty($comments)) { echo T_('Importing the comments... '); evo_flush(); $comments_count = 0; $comments_IDs = array(); foreach ($comments as $post_ID => $comments) { if (empty($comments)) { // Skip if no comments continue; } foreach ($comments as $comment) { $comment_author_ID = 0; if (!empty($comment['comment_user_id']) && isset($authors_IDs[(string) $comment['comment_user_id']])) { // Author ID $comment_author_ID = $authors_IDs[(string) $comment['comment_user_id']]; } $comment_parent_ID = 0; if (!empty($comment['comment_parent']) && isset($comments_IDs[(string) $comment['comment_parent']])) { // Parent comment ID $comment_parent_ID = $comments_IDs[(string) $comment['comment_parent']]; } unset($comment_IP_country); if (!empty($comment['comment_IP_country'])) { // Get country ID by code $CountryCache =& get_CountryCache(); if ($Country =& $CountryCache->get_by_name($comment['comment_IP_country'], false)) { $comment_IP_country = $Country->ID; } } $Comment = new Comment(); $Comment->item_ID = $post_ID; $Comment->set('post_ID', $post_ID); if (!empty($comment_parent_ID)) { $Comment->set('in_reply_to_cmt_ID', $comment_parent_ID); } $Comment->set('date', $comment['comment_date']); if (!empty($comment_author_ID)) { $Comment->set('author_ID', $comment_author_ID); } $Comment->set('author', evo_substr($comment['comment_author'], 0, 100)); $Comment->set('author_IP', $comment['comment_author_IP']); $Comment->set('author_email', $comment['comment_author_email']); $Comment->set('content', $comment['comment_content']); if (empty($comment['comment_status'])) { // If comment status is empty (the export of wordpress doesn't provide this field) $Comment->set('status', $comment['comment_approved'] == '1' ? 'published' : 'draft'); } else { // Set status when we have predefined value $Comment->set('status', $comment['comment_status']); } if (!empty($comment_IP_country)) { // Country $Comment->set('IP_ctry_ID', $comment_IP_country); } $Comment->set('rating', $comment['comment_rating']); $Comment->set('featured', $comment['comment_featured']); $Comment->set('nofollow', $comment['comment_nofollow']); $Comment->set('helpful_addvotes', $comment['comment_helpful_addvotes']); $Comment->set('helpful_countvotes', $comment['comment_helpful_countvotes']); $Comment->set('spam_addvotes', $comment['comment_spam_addvotes']); $Comment->set('spam_countvotes', $comment['comment_spam_countvotes']); $Comment->set('karma', $comment['comment_karma']); $Comment->set('spam_karma', $comment['comment_spam_karma']); $Comment->set('allow_msgform', $comment['comment_allow_msgform']); $Comment->set('notif_status', $comment['comment_notif_status']); $Comment->dbinsert(); $comments_IDs[$comment['comment_id']] = $Comment->ID; $comments_count++; } } echo sprintf(T_('%d records'), $comments_count) . '<br />'; } $DB->commit(); }
// CHECK and FORMAT content $saved_comment = $comment; // Following call says "WARNING: this does *NOT* (necessarilly) make the HTML code safe.": $comment = check_html_sanity($comment, $perm_comment_edit ? 'posting' : 'commenting', $User); if ($comment === false) { // ERROR! Restore original comment for further editing: $comment = $saved_comment; } // Flood protection was here and SHOULD NOT have moved down! /** * Create comment object. Gets validated, before recording it into DB: */ $Comment = new Comment(); if ($reply_ID > 0) { // Set parent ID if this comment is reply to other comment $Comment->set('in_reply_to_cmt_ID', $reply_ID); } $Comment->set('type', $comment_type == 'meta' ? 'meta' : 'comment'); $Comment->set_Item($commented_Item); if ($User) { // User is logged in, we'll use his ID $Comment->set_author_User($User); } else { // User is not logged in: $Comment->set('author', $author); $Comment->set('author_email', $email); $Comment->set('author_url', $url); $Comment->set('allow_msgform', $comment_allow_msgform); } if ($commented_Item->can_rate()) { // Comment rating:
/** * Import WordPress data from XML file into b2evolution database */ function wpxml_import() { global $DB, $tableprefix; // Load classes: load_class('regional/model/_country.class.php', 'Country'); load_class('regional/model/_region.class.php', 'Region'); load_class('regional/model/_subregion.class.php', 'Subregion'); load_class('regional/model/_city.class.php', 'City'); // Set Blog from request blog ID $wp_blog_ID = param('wp_blog_ID', 'integer', 0); $BlogCache =& get_BlogCache(); $wp_Blog =& $BlogCache->get_by_ID($wp_blog_ID); // The import type ( replace | append ) $import_type = param('import_type', 'string', 'replace'); // Should we delete files on 'replace' mode? $delete_files = param('delete_files', 'integer', 0); $XML_file_path = get_param('wp_file'); $XML_file_name = basename($XML_file_path); if (preg_match('/\\.(xml|txt)$/i', $XML_file_name)) { // XML format // Check WordPress XML file if (!wpxml_check_xml_file($XML_file_path)) { // Errors are in XML file return; } // Use this folder to upload files if they exist in subfolder "/b2evolution_export_files" $attached_files_path = dirname($XML_file_path); } else { if (preg_match('/\\.zip$/i', $XML_file_name)) { // ZIP format // Extract ZIP and check WordPress XML file global $media_path; $ZIP_folder_path = $media_path . 'import/temp-' . md5(rand()); if (!unpack_archive($XML_file_path, $ZIP_folder_path, true, $XML_file_name)) { // Errors on unpack ZIP file return; } // Find valid XML file in ZIP package $ZIP_files_list = scandir($ZIP_folder_path); $xml_exists_in_zip = false; foreach ($ZIP_files_list as $ZIP_file) { if (preg_match('/\\.(xml|txt)$/i', $ZIP_file)) { // XML file is found in ZIP package if (wpxml_check_xml_file($ZIP_folder_path . '/' . $ZIP_file)) { // XML file is valid $XML_file_path = $ZIP_folder_path . '/' . $ZIP_file; $xml_exists_in_zip = true; break; } } } if (!$xml_exists_in_zip) { // No XML is detected in ZIP package echo '<p style="color:red">' . T_('XML file is not detected in your ZIP package.') . '</p>'; // Delete temporary folder that contains the files from extracted ZIP package rmdir_r($ZIP_folder_path); return; } // Use this folder to upload files, $ZIP_folder_path must be deleted after import $attached_files_path = $ZIP_folder_path; } else { // Unrecognized extension echo '<p style="color:red">' . sprintf(T_('%s has an unrecognized extension.'), '<b>' . $xml_file['name'] . '</b>') . '</p>'; return; } } // Parse WordPress XML file into array $xml_data = wpxml_parser($XML_file_path); $DB->begin(); if ($import_type == 'replace') { // Remove data from selected blog // Get existing categories $SQL = new SQL(); $SQL->SELECT('cat_ID'); $SQL->FROM('T_categories'); $SQL->WHERE('cat_blog_ID = ' . $DB->quote($wp_blog_ID)); $old_categories = $DB->get_col($SQL->get()); if (!empty($old_categories)) { // Get existing posts $SQL = new SQL(); $SQL->SELECT('post_ID'); $SQL->FROM('T_items__item'); $SQL->WHERE('post_main_cat_ID IN ( ' . implode(', ', $old_categories) . ' )'); $old_posts = $DB->get_col($SQL->get()); } echo T_('Removing the comments... '); evo_flush(); if (!empty($old_posts)) { $SQL = new SQL(); $SQL->SELECT('comment_ID'); $SQL->FROM('T_comments'); $SQL->WHERE('comment_item_ID IN ( ' . implode(', ', $old_posts) . ' )'); $old_comments = $DB->get_col($SQL->get()); $DB->query('DELETE FROM T_comments WHERE comment_item_ID IN ( ' . implode(', ', $old_posts) . ' )'); if (!empty($old_comments)) { $DB->query('DELETE FROM T_comments__votes WHERE cmvt_cmt_ID IN ( ' . implode(', ', $old_comments) . ' )'); $DB->query('DELETE FROM T_links WHERE link_cmt_ID IN ( ' . implode(', ', $old_comments) . ' )'); } } echo T_('OK') . '<br />'; echo T_('Removing the posts... '); evo_flush(); if (!empty($old_categories)) { $DB->query('DELETE FROM T_items__item WHERE post_main_cat_ID IN ( ' . implode(', ', $old_categories) . ' )'); if (!empty($old_posts)) { // Remove the post's data from related tables if ($delete_files) { // Get the file IDs that should be deleted from hard drive $SQL = new SQL(); $SQL->SELECT('DISTINCT link_file_ID'); $SQL->FROM('T_links'); $SQL->WHERE('link_itm_ID IN ( ' . implode(', ', $old_posts) . ' )'); $deleted_file_IDs = $DB->get_col($SQL->get()); } $DB->query('DELETE FROM T_items__item_settings WHERE iset_item_ID IN ( ' . implode(', ', $old_posts) . ' )'); $DB->query('DELETE FROM T_items__prerendering WHERE itpr_itm_ID IN ( ' . implode(', ', $old_posts) . ' )'); $DB->query('DELETE FROM T_items__subscriptions WHERE isub_item_ID IN ( ' . implode(', ', $old_posts) . ' )'); $DB->query('DELETE FROM T_items__version WHERE iver_itm_ID IN ( ' . implode(', ', $old_posts) . ' )'); $DB->query('DELETE FROM T_postcats WHERE postcat_post_ID IN ( ' . implode(', ', $old_posts) . ' )'); $DB->query('DELETE FROM T_slug WHERE slug_itm_ID IN ( ' . implode(', ', $old_posts) . ' )'); $DB->query('DELETE l, lv FROM T_links AS l LEFT JOIN T_links__vote AS lv ON lv.lvot_link_ID = l.link_ID WHERE l.link_itm_ID IN ( ' . implode(', ', $old_posts) . ' )'); $DB->query('DELETE FROM T_users__postreadstatus WHERE uprs_post_ID IN ( ' . implode(', ', $old_posts) . ' )'); } } echo T_('OK') . '<br />'; echo T_('Removing the categories... '); evo_flush(); $DB->query('DELETE FROM T_categories WHERE cat_blog_ID = ' . $DB->quote($wp_blog_ID)); echo T_('OK') . '<br />'; echo T_('Removing the tags that are no longer used... '); evo_flush(); if (!empty($old_posts)) { // Remove the tags // Get tags from selected blog $SQL = new SQL(); $SQL->SELECT('itag_tag_ID'); $SQL->FROM('T_items__itemtag'); $SQL->WHERE('itag_itm_ID IN ( ' . implode(', ', $old_posts) . ' )'); $old_tags_this_blog = array_unique($DB->get_col($SQL->get())); if (!empty($old_tags_this_blog)) { // Get tags from other blogs $SQL = new SQL(); $SQL->SELECT('itag_tag_ID'); $SQL->FROM('T_items__itemtag'); $SQL->WHERE('itag_itm_ID NOT IN ( ' . implode(', ', $old_posts) . ' )'); $old_tags_other_blogs = array_unique($DB->get_col($SQL->get())); $old_tags_other_blogs_sql = !empty($old_tags_other_blogs) ? ' AND tag_ID NOT IN ( ' . implode(', ', $old_tags_other_blogs) . ' )' : ''; // Remove the tags that are no longer used $DB->query('DELETE FROM T_items__tag WHERE tag_ID IN ( ' . implode(', ', $old_tags_this_blog) . ' )' . $old_tags_other_blogs_sql); } // Remove the links of tags with posts $DB->query('DELETE FROM T_items__itemtag WHERE itag_itm_ID IN ( ' . implode(', ', $old_posts) . ' )'); } echo T_('OK') . '<br />'; if ($delete_files) { // Delete the files echo T_('Removing the files... '); if (!empty($deleted_file_IDs)) { // Commit the DB changes before files deleting $DB->commit(); // Get the deleted file IDs that are linked to other objects $SQL = new SQL(); $SQL->SELECT('DISTINCT link_file_ID'); $SQL->FROM('T_links'); $SQL->WHERE('link_file_ID IN ( ' . implode(', ', $deleted_file_IDs) . ' )'); $linked_file_IDs = $DB->get_col($SQL->get()); // We can delete only the files that are NOT linked to other objects $deleted_file_IDs = array_diff($deleted_file_IDs, $linked_file_IDs); $FileCache =& get_FileCache(); foreach ($deleted_file_IDs as $deleted_file_ID) { if (!($deleted_File =& $FileCache->get_by_ID($deleted_file_ID, false, false))) { // Incorrect file ID echo '<p class="red">' . sprintf(T_('No file #%s found in DB. It cannot be deleted.'), $deleted_file_ID) . '</p>'; } if (!$deleted_File->unlink()) { // No permission to delete file echo '<p class="red">' . sprintf(T_('Could not delete the file «%s».'), $deleted_File->get_full_path()) . '</p>'; } // Clear cache to save memory $FileCache->clear(); } // Start new transaction for the data inserting $DB->begin(); } echo T_('OK') . '<br />'; } echo '<br />'; } /* Import authors */ $authors = array(); $authors_IDs = array(); if (isset($xml_data['authors']) && count($xml_data['authors']) > 0) { global $Settings, $UserSettings; echo T_('Importing the users... '); evo_flush(); // Get existing users $SQL = new SQL(); $SQL->SELECT('user_login, user_ID'); $SQL->FROM('T_users'); $existing_users = $DB->get_assoc($SQL->get()); $authors_count = 0; foreach ($xml_data['authors'] as $author) { if (empty($existing_users[(string) $author['author_login']])) { // Insert new user into DB if User doesn't exist with current login name $GroupCache =& get_GroupCache(); if (!empty($author['author_group'])) { // Set user group from xml data if (($UserGroup =& $GroupCache->get_by_name($author['author_group'], false)) === false) { // If user's group doesn't exist yet, we should create new $UserGroup = new Group(); $UserGroup->set('name', $author['author_group']); $UserGroup->dbinsert(); } } else { // Set default user group is it is not defined in xml if (($UserGroup =& $GroupCache->get_by_name('Normal Users', false)) === false) { // Exit from import of users, because we cannot set default user group break; } } unset($author_created_from_country); if (!empty($author['author_created_from_country'])) { // Get country ID from DB by code $CountryCache =& get_CountryCache(); if (($Country =& $CountryCache->get_by_name($author['author_created_from_country'], false)) !== false) { $author_created_from_country = $Country->ID; } } // Get regional IDs by their names $author_regions = wp_get_regional_data($author['author_country'], $author['author_region'], $author['author_subregion'], $author['author_city']); $User = new User(); $User->set('login', $author['author_login']); $User->set('email', $author['author_email']); $User->set('firstname', $author['author_first_name']); $User->set('lastname', $author['author_last_name']); $User->set('pass', $author['author_pass']); $User->set_Group($UserGroup); $User->set('status', !empty($author['author_status']) ? $author['author_status'] : 'autoactivated'); $User->set('nickname', $author['author_nickname']); $User->set('url', $author['author_url']); $User->set('level', $author['author_level']); $User->set('locale', $author['author_locale']); $User->set('gender', $author['author_gender'] == 'female' ? 'F' : ($author['author_gender'] == 'male' ? 'M' : '')); if ($author['author_age_min'] > 0) { $User->set('age_min', $author['author_age_min']); } if ($author['author_age_max'] > 0) { $User->set('age_max', $author['author_age_max']); } if (isset($author_created_from_country)) { // User was created from this country $User->set('reg_ctry_ID', $author_created_from_country); } if (!empty($author_regions['country'])) { // Country $User->set('ctry_ID', $author_regions['country']); if (!empty($author_regions['region'])) { // Region $User->set('rgn_ID', $author_regions['region']); if (!empty($author_regions['subregion'])) { // Subregion $User->set('subrg_ID', $author_regions['subregion']); } if (!empty($author_regions['city'])) { // City $User->set('city_ID', $author_regions['city']); } } } $User->set('source', $author['author_source']); $User->set_datecreated(empty($author['author_created_ts']) ? mktime() : intval($author['author_created_ts'])); $User->set('lastseen_ts', empty($author['author_lastseen_ts']) ? NULL : $author['author_lastseen_ts'], true); $User->set('profileupdate_date', empty($author['author_profileupdate_date']) ? date('Y-m-d', mktime()) : $author['author_profileupdate_date']); $User->dbinsert(); $user_ID = $User->ID; if (!empty($user_ID) && !empty($author['author_created_fromIPv4'])) { $UserSettings->set('created_fromIPv4', ip2int($author['author_created_fromIPv4']), $user_ID); } $authors_count++; } else { // Get ID of existing user $user_ID = $existing_users[(string) $author['author_login']]; } // Save user ID of current author $authors[$author['author_login']] = (string) $user_ID; $authors_IDs[$author['author_id']] = (string) $user_ID; } $UserSettings->dbupdate(); echo sprintf(T_('%d records'), $authors_count) . '<br />'; } /* Import files, Copy them all to media folder */ if (isset($xml_data['files']) && count($xml_data['files']) > 0) { echo T_('Importing the files... '); evo_flush(); if (!file_exists($attached_files_path . '/b2evolution_export_files')) { // Display an error if files are attached but folder doesn't exist echo '<p class="red">' . sprintf(T_('No folder %s found. It must exists to import the attached files properly.'), '<b>' . $attached_files_path . '/b2evolution_export_files' . '</b>') . '</p>'; } else { // The attached files are located in this subfolder $subfolder_path = '/b2evolution_export_files'; $files_count = 0; $files = array(); foreach ($xml_data['files'] as $file) { switch ($file['file_root_type']) { case 'shared': // Shared files $file_root_ID = 0; break; case 'user': // User's files if (isset($authors_IDs[$file['file_root_ID']])) { // If owner of this file exists in our DB $file_root_ID = $authors_IDs[$file['file_root_ID']]; break; } // Otherwise we should upload this file into blog's folder: // Otherwise we should upload this file into blog's folder: default: // 'collection', 'absolute', 'skins' // The files from other blogs and from other places must be moved in the folder of the current blog $file['file_root_type'] = 'collection'; $file_root_ID = $wp_blog_ID; break; } // Get FileRoot by type and ID $FileRootCache =& get_FileRootCache(); $FileRoot =& $FileRootCache->get_by_type_and_ID($file['file_root_type'], $file_root_ID); if (is_dir($attached_files_path . $subfolder_path . '/' . $file['zip_path'] . $file['file_path'])) { // Folder $file_destination_path = $FileRoot->ads_path; } else { // File $file_destination_path = $FileRoot->ads_path . $file['file_path']; } if (!file_exists($attached_files_path . $subfolder_path . '/' . $file['zip_path'] . $file['file_path'])) { // File doesn't exist echo '<p class="orange">' . sprintf(T_('Unable to copy file %s, because it does not exist.'), '<b>' . $file['zip_path'] . $file['file_path'] . '</b>') . '</p>'; // Skip it continue; } else { if (!copy_r($attached_files_path . $subfolder_path . '/' . $file['zip_path'] . $file['file_path'], $file_destination_path)) { // No permission to copy to this folder if (is_dir($attached_files_path . $subfolder_path . '/' . $file['zip_path'] . $file['file_path'])) { // Folder echo '<p class="orange">' . sprintf(T_('Unable to copy folder %s to %s. Please, check the permissions assigned to this folder.'), '<b>' . $file['zip_path'] . $file['file_path'] . '</b>', '<b>' . $file_destination_path . '</b>') . '</p>'; } else { // File echo '<p class="orange">' . sprintf(T_('Unable to copy file %s to %s. Please, check the permissions assigned to this folder.'), '<b>' . $file['zip_path'] . $file['file_path'] . '</b>', '<b>' . $file_destination_path . '</b>') . '</p>'; } // Skip it continue; } } // Create new File object, It will be linked to the items below $File = new File($file['file_root_type'], $file_root_ID, $file['file_path']); $File->set('title', $file['file_title']); $File->set('alt', $file['file_alt']); $File->set('desc', $file['file_desc']); $files[$file['file_ID']] = $File; $files_count++; } echo sprintf(T_('%d records'), $files_count) . '<br />'; if (isset($ZIP_folder_path) && file_exists($ZIP_folder_path)) { // This folder was created only to extract files from ZIP package, Remove it now rmdir_r($ZIP_folder_path); } } } /* Import categories */ $category_default = 0; load_class('chapters/model/_chapter.class.php', 'Chapter'); // Get existing categories $SQL = new SQL(); $SQL->SELECT('cat_urlname, cat_ID'); $SQL->FROM('T_categories'); $SQL->WHERE('cat_blog_ID = ' . $DB->quote($wp_blog_ID)); $categories = $DB->get_assoc($SQL->get()); if (isset($xml_data['categories']) && count($xml_data['categories']) > 0) { echo T_('Importing the categories... '); evo_flush(); load_funcs('locales/_charset.funcs.php'); $categories_count = 0; foreach ($xml_data['categories'] as $cat) { if (empty($categories[(string) $cat['category_nicename']])) { $Chapter = new Chapter(NULL, $wp_blog_ID); $Chapter->set('name', $cat['cat_name']); $Chapter->set('urlname', $cat['category_nicename']); $Chapter->set('description', $cat['category_description']); if (!empty($cat['category_parent']) && isset($categories[(string) $cat['category_parent']])) { // Set category parent ID $Chapter->set('parent_ID', $categories[(string) $cat['category_parent']]); } $Chapter->dbinsert(); // Save new category $categories[$cat['category_nicename']] = $Chapter->ID; if (empty($category_default)) { // Set first category as default $category_default = $Chapter->ID; } $categories_count++; } } echo sprintf(T_('%d records'), $categories_count) . '<br />'; } if (empty($category_default)) { // No categories in XML file, Try to use first category(from DB) as default foreach ($categories as $category_name => $category_ID) { $category_default = $category_ID; break; } } if (empty($category_default)) { // If category is still not defined then we should create default, because blog must has at least one category $new_Chapter = new Chapter(NULL, $wp_blog_ID); $new_Chapter->set('name', T_('Uncategorized')); $new_Chapter->set('urlname', $wp_Blog->get('urlname') . '-main'); $new_Chapter->dbinsert(); $category_default = $new_Chapter->ID; } /* Import tags */ $tags = array(); if (isset($xml_data['tags']) && count($xml_data['tags']) > 0) { echo T_('Importing the tags... '); evo_flush(); // Get existing tags $SQL = new SQL(); $SQL->SELECT('tag_name, tag_ID'); $SQL->FROM('T_items__tag'); $tags = $DB->get_assoc($SQL->get()); $tags_count = 0; foreach ($xml_data['tags'] as $tag) { if (empty($tags[(string) $tag['tag_name']])) { // Insert new tag into DB if tag doesn't exist with current name mysqli_query($DB->dbhandle, 'INSERT INTO ' . $tableprefix . 'items__tag ( tag_name ) VALUES ( ' . $DB->quote($tag['tag_name']) . ' )'); $tag_ID = mysqli_insert_id($DB->dbhandle); // Save new tag $tags[$tag['tag_name']] = (string) $tag_ID; $tags_count++; } } echo sprintf(T_('%d records'), $tags_count) . '<br />'; } /* Import posts */ $posts = array(); $comments = array(); if (isset($xml_data['posts']) && count($xml_data['posts']) > 0) { load_class('items/model/_item.class.php', 'Item'); // Set status's links between WP and b2evo names $post_statuses = array('publish' => 'published', 'pending' => 'review', 'draft' => 'draft', 'trash' => 'deprecated', 'community' => 'community', 'deprecated' => 'deprecated', 'protected' => 'protected', 'private' => 'private', 'review' => 'review', 'redirected' => 'redirected'); // Get post types $SQL = new SQL(); $SQL->SELECT('LOWER( ityp_name ), ityp_ID'); $SQL->FROM('T_items__type'); $post_types = $DB->get_assoc($SQL->get()); echo T_('Importing the posts... '); evo_flush(); foreach ($xml_data['posts'] as $post) { $author_ID = isset($authors[(string) $post['post_author']]) ? $authors[(string) $post['post_author']] : 1; $last_edit_user_ID = isset($authors[(string) $post['post_lastedit_user']]) ? $authors[(string) $post['post_lastedit_user']] : $author_ID; $post_main_cat_ID = $category_default; $post_extra_cat_IDs = array(); $post_tags = array(); if (!empty($post['terms'])) { // Set categories and tags foreach ($post['terms'] as $term) { switch ($term['domain']) { case 'category': if (isset($categories[(string) $term['slug']])) { if ($post_main_cat_ID == $category_default) { // Set main category $post_main_cat_ID = $categories[(string) $term['slug']]; } // Set extra categories $post_extra_cat_IDs[] = $categories[(string) $term['slug']]; } break; case 'post_tag': if (isset($tags[(string) $term['slug']])) { // Set tag $post_tags[] = $term['slug']; } break; } } } // Set post type ID $post_type_ID = isset($post_types[strtolower($post['post_type'])]) ? $post_types[strtolower($post['post_type'])] : '1'; // Get regional IDs by their names $item_regions = wp_get_regional_data($post['post_country'], $post['post_region'], $post['post_subregion'], $post['post_city']); $Item = new Item(); $Item->set('main_cat_ID', $post_main_cat_ID); $Item->set('creator_user_ID', $author_ID); $Item->set('lastedit_user_ID', $last_edit_user_ID); $Item->set('title', $post['post_title']); $Item->set('content', $post['post_content']); $Item->set('excerpt', $post['post_excerpt']); $Item->set('datestart', $post['post_date']); $Item->set('datecreated', !empty($post['post_datecreated']) ? $post['post_datecreated'] : $post['post_date']); $Item->set('datemodified', !empty($post['post_datemodified']) ? $post['post_datemodified'] : $post['post_date']); $Item->set('urltitle', !empty($post['post_urltitle']) ? $post['post_urltitle'] : $post['post_title']); $Item->set('url', $post['post_url']); $Item->set('status', isset($post_statuses[(string) $post['status']]) ? $post_statuses[(string) $post['status']] : 'review'); // If 'comment_status' has the unappropriate value set it to 'open' $Item->set('comment_status', in_array($post['comment_status'], array('open', 'closed', 'disabled')) ? $post['comment_status'] : 'open'); $Item->set('ityp_ID', $post_type_ID); if (empty($post['post_excerpt']) && !empty($post['post_content'])) { // Generate excerpt $Item->set('excerpt', excerpt($post['post_content'])); $Item->set('excerpt_autogenerated', '1'); } $Item->set('extra_cat_IDs', $post_extra_cat_IDs); $Item->set('dateset', $post['post_date_mode'] == 'set' ? 1 : 0); if (isset($authors[(string) $post['post_assigned_user']])) { $Item->set('assigned_user', $authors[(string) $post['post_assigned_user']]); } $Item->set('datedeadline', $post['post_datedeadline']); $Item->set('locale', $post['post_locale']); $Item->set('excerpt_autogenerated', $post['post_excerpt_autogenerated']); $Item->set('titletag', $post['post_titletag']); $Item->set('notifications_status', empty($post['post_notifications_status']) ? 'noreq' : $post['post_notifications_status']); $Item->set('renderers', array($post['post_renderers'])); $Item->set('priority', $post['post_priority']); $Item->set('featured', $post['post_featured']); $Item->set('order', $post['post_order']); if (!empty($item_regions['country'])) { // Country $Item->set('ctry_ID', $item_regions['country']); if (!empty($item_regions['region'])) { // Region $Item->set('rgn_ID', $item_regions['region']); if (!empty($item_regions['subregion'])) { // Subregion $Item->set('subrg_ID', $item_regions['subregion']); } if (!empty($item_regions['city'])) { // City $Item->set('city_ID', $item_regions['city']); } } } if (count($post_tags) > 0) { $Item->tags = $post_tags; } $Item->dbinsert(); $posts[$post['post_id']] = $Item->ID; if (!empty($files) && !empty($post['links'])) { // Link the files to the Item if it has them foreach ($post['links'] as $link) { if (isset($files[$link['link_file_ID']])) { // Link a file to Item $File = $files[$link['link_file_ID']]; $LinkOwner = new LinkItem($Item); $File->link_to_Object($LinkOwner, $link['link_order'], $link['link_position']); } } } if (!empty($post['comments'])) { // Set comments $comments[$Item->ID] = $post['comments']; } } foreach ($xml_data['posts'] as $post) { // Set post parents if (!empty($post['post_parent']) && isset($posts[(string) $post['post_parent']])) { mysqli_query($DB->dbhandle, 'UPDATE ' . $tableprefix . 'items__item SET post_parent_ID = ' . $DB->quote($posts[(string) $post['post_parent']]) . ' WHERE post_ID = ' . $DB->quote($posts[(string) $post['post_id']])); } } echo sprintf(T_('%d records'), count($xml_data['posts'])) . '<br />'; } /* Import comments */ if (!empty($comments)) { echo T_('Importing the comments... '); evo_flush(); $comments_count = 0; $comments_IDs = array(); foreach ($comments as $post_ID => $comments) { if (empty($comments)) { // Skip if no comments continue; } foreach ($comments as $comment) { $comment_author_user_ID = 0; if (!empty($comment['comment_user_id']) && isset($authors_IDs[(string) $comment['comment_user_id']])) { // Author ID $comment_author_user_ID = $authors_IDs[(string) $comment['comment_user_id']]; } $comment_parent_ID = 0; if (!empty($comment['comment_parent']) && isset($comments_IDs[(string) $comment['comment_parent']])) { // Parent comment ID $comment_parent_ID = $comments_IDs[(string) $comment['comment_parent']]; } unset($comment_IP_country); if (!empty($comment['comment_IP_country'])) { // Get country ID by code $CountryCache =& get_CountryCache(); if ($Country =& $CountryCache->get_by_name($comment['comment_IP_country'], false)) { $comment_IP_country = $Country->ID; } } $Comment = new Comment(); $Comment->set('item_ID', $post_ID); if (!empty($comment_parent_ID)) { $Comment->set('in_reply_to_cmt_ID', $comment_parent_ID); } $Comment->set('date', $comment['comment_date']); if (!empty($comment_author_user_ID)) { $Comment->set('author_user_ID', $comment_author_user_ID); } $Comment->set('author', utf8_substr($comment['comment_author'], 0, 100)); $Comment->set('author_IP', $comment['comment_author_IP']); $Comment->set('author_email', $comment['comment_author_email']); $Comment->set('content', $comment['comment_content']); if (empty($comment['comment_status'])) { // If comment status is empty (the export of wordpress doesn't provide this field) $Comment->set('status', $comment['comment_approved'] == '1' ? 'published' : 'draft'); } else { // Set status when we have predefined value $Comment->set('status', $comment['comment_status']); } if (!empty($comment_IP_country)) { // Country $Comment->set('IP_ctry_ID', $comment_IP_country); } $Comment->set('rating', $comment['comment_rating']); $Comment->set('featured', $comment['comment_featured']); $Comment->set('nofollow', $comment['comment_nofollow']); $Comment->set('helpful_addvotes', $comment['comment_helpful_addvotes']); $Comment->set('helpful_countvotes', $comment['comment_helpful_countvotes']); $Comment->set('spam_addvotes', $comment['comment_spam_addvotes']); $Comment->set('spam_countvotes', $comment['comment_spam_countvotes']); $Comment->set('karma', $comment['comment_karma']); $Comment->set('spam_karma', $comment['comment_spam_karma']); $Comment->set('allow_msgform', $comment['comment_allow_msgform']); $Comment->set('notif_status', empty($comment['comment_notif_status']) ? 'noreq' : $comment['comment_notif_status']); $Comment->dbinsert(); $comments_IDs[$comment['comment_id']] = $Comment->ID; $comments_count++; } } echo sprintf(T_('%d records'), $comments_count) . '<br />'; } echo '<p>' . T_('Import complete.') . '</p>'; $DB->commit(); }