GGshow reloaded GGshow reloaded

October 19, 2015

PHP 400 Quota Exceeded in Analytics Data Import

Issue:

  • Google Analytics Data Import failed after 50 upload.
  • Unable to upload large file for Analytics Data Import.
  • Intermittent error 400 with Management API.
PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/upload/analytics/v3/management/accounts/.../webproperties/.../customDataSources/.../uploads?uploadType=media: (400) Quota exceeded.' in /base/data/home/apps/.../google-api-php-client/src/Google/Http/REST.php:110 Stack trace: #0 /base/data/home/apps/.../google-api-php-client/src/Google/Http/REST.php(62): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request), Object(Google_Client)) #1 [internal function]: Google_Http_REST::doExecute(Object(Google_Client), Object(Google_Http_Request)) #2 /base/data/home/apps/.../google-api-php-client/src/Google/Task/Runner.php(174): call_user_func_array(Array, Array)

Reason:
There are limits and quotas for Google Analytics Configuration and Reporting API. When daily project requests and daily write requests exceeded limit, API will return error 400.

Solution:

  • Fine-tune your application for quota usage; or
  • Request additional quota; or
  • Go to bed, try again tomorrow 🙂

Reference:
Configuration and Reporting API Limits and Quotas

Filed under: Google Cloud Platform,PHP — Tags: , , , , — GG @ 5:14 pm

PHP 403 Insufficient Permission in Analytics Data Import

Issue:

PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/upload/analytics/v3/management/accounts/.../webproperties/.../customDataSources/.../uploads?uploadType=media: (403) Insufficient Permission' in /base/data/home/apps/.../google-api-php-client/src/Google/Http/REST.php:110 Stack trace: #0 /base/data/home/apps/.../google-api-php-client/src/Google/Http/REST.php(62): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request), Object(Google_Client)) #1 [internal function]: Google_Http_REST::doExecute(Object(Google_Client), Object(Google_Http_Request)) #2 /base/data/home/apps/.../google-api-php-client/src/Google/Task/Runner.php(174): call_user_func_array(Array, Array) #3 /base/data/home/apps/.../google-api-php-client/src/Google/Http/REST.php(46): Google_Task_Runner->run() #4 /base/data/ in /base/data/home/apps/.../google-api-php-client/src/Google/Http/REST.php on line 110

Reason:
Insufficient permission to perform an API query

Solution:

  • Ensure sufficient account permission granted for the service account (???@developer.gserviceaccount.com).
  • Ensure proper scopes defined in Service Account Authentication (Google_Auth_AssertionCredentials).
    Use Google_Service_Analytics::ANALYTICS ( or “https://www.googleapis.com/auth/analytics” ) instead of Google_Service_Analytics::ANALYTICS_READONLY ( or “https://www.googleapis.com/auth/analytics.readonly” ) if you need to perform an edit operation, e.g. Data Import.

Reference:

January 4, 2014

b2evolution to WordPress migration

I’ve been looking for b2evolution to WordPress importer for long time. What I can find on the web are only migration script for older version of b2evo/wp, hence I’ve decided to write one on my own, for importing data from b2evolution 4.0.5 to WordPress 3.7.1. I’ve tested the script with WordPress 3.8.0 too. I’m hereby sharing it to whoever needs it.

Instruction

  1. Place this file at the same directory with your wp-config.php
  2. Access the URL at web browser, e.g.: http://localhost/b2wp.php
  3. Fill up the form with database connection detail, and submit.
  4. Select import options, and start import.

Warning

  1. Backup all your databases safely before proceed.
  2. Try it with a fresh installation of WordPress instead of production site.
  3. Use this script at your own risk 🙂

Download

  • b2wp.php – Tested with b2evo 4.0.5 to wp 3.7.1/3.8.0
  • b2wp_kp.zip – provided by KP for b2evo 5.1.2 to wp 4.
<?php
/*
* b2wp.php
* Title        : B2evolution to WordPress Importer
* Version      : 1.0.2
* Author URL   : http://www.ggshow.com
* Description  : Import Authors, Categories, Posts, Tags, and Comments, from
*               B2evolution database to WordPress database.
*/

function slug($str) {
return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $str)));
}

$output = '';
$step = 1;
require_once( dirname(__FILE__) . '/wp-config.php' );
$target['db_host'] = DB_HOST;
$target['db_user'] = DB_USER;
$target['db_password'] = DB_PASSWORD;
$target['db_name'] = DB_NAME;
$wp_con = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$target['status'] = (mysqli_connect_errno()) ? 'Error' : 'Connected';
$output .= ($target['status'] == 'Error') ? 'Error connecting WordPress database!<br />' : '';
$source['db_host'] = ($_POST['source_dbhost'] == '') ? 'localhost' : htmlentities($_POST['source_dbhost']);
$source['db_user'] = htmlentities($_POST['source_dbuser']);
$source['db_password'] = htmlentities($_POST['source_dbpassword']);
$source['db_name'] = htmlentities($_POST['source_dbname']);
if (!empty($_POST['source_dbname'])) {
$b2_con = new mysqli($source['db_host'], $source['db_user'], $source['db_password'], $source['db_name']);
$source['status'] = (mysqli_connect_errno()) ? 'Error' : 'Connected';
$output .= ($source['status'] == 'Error') ? 'Error connecting B2evolution database!<br />' : '';
} else {
$source['status'] = '';
$output .= 'Please fill up B2evolution database connection detail!<br />';
}
$target['table_prefix'] = (!empty($_POST['target_table_prefix'])) ? $wp_con->real_escape_string($_POST['target_table_prefix']) : 'wp_';
$source['table_prefix'] = (!empty($_POST['source_table_prefix'])) ? $wp_con->real_escape_string($_POST['source_table_prefix']) : 'evo_';
if ($source['status'] == 'Connected') {
$sql = " SHOW TABLES LIKE \"" . $source['table_prefix'] . "%\"";
$res = $b2_con->query($sql);
if ($res->num_rows <= 0) {
$source['status'] = 'Error';
$output .= "Invalid B2evo table prefix.<br />";
} else {
$sql = "SELECT DISTINCT `" . $source['table_prefix'] . "blogs`.* FROM `" . $source['table_prefix'] . "blogs`";
$res = $b2_con->query($sql);
$b2_blog_selected = ($_POST['b2_blog'] == '') ? 1 : $_POST['b2_blog'];
$b2_blog_select = '<select name="b2_blog">';
while ($row = $res->fetch_array()) {
$b2_blog_selected_str = '';
if ($row['blog_ID'] == $b2_blog_selected) {
$b2_blog_selected_str = 'selected';
$b2_blog_selected_row = $row;
}
$b2_blog_select .= '<option value="' . $row['blog_ID'] . '" ' . ($b2_blog_selected_str) . ' >' . $row['blog_name'] . '</option>';
}
$b2_blog_select .= '</select>';
}
}
if ($target['status'] == 'Connected') {
$sql = " SHOW TABLES LIKE \"" . $target['table_prefix'] . "%\"";
$res = $wp_con->query($sql);
if ($res->num_rows <= 0) {
$target['status'] = 'Error';
$output .= "Invalid WordPress table prefix.<br />";
} else {
$sql = "SELECT DISTINCT `" . $target['table_prefix'] . "users`.* FROM `" . $target['table_prefix'] . "users`";
$res = $wp_con->query($sql);
$b2_user_selected = ($_POST['b2_user'] == '') ? 0 : $_POST['b2_user'];
$b2_user_select = '<select name="b2_user">';
$b2_user_select .= '<option value="0">Import B2evo users</option>';
while ($row = $res->fetch_array()) {
if ($row['ID'] == $b2_user_selected) {
$b2_user_selected_str = 'selected';
$b2_user_selected_row = $row;
} else {
$b2_user_selected_str = '';
}
$b2_user_select .= '<option value="' . $row['ID'] . '" ' . ($b2_user_selected_str) . ' >Assign posts to ' . $row['user_login'] . '</option>';
}
$b2_user_select .= '</select>';
}
}
if ($source['status'] == 'Connected' && $target['status'] == 'Connected') {
$step = 2;
}
if ($step >= 2) {
$b2_cat_selected = ($_POST['b2_cat'] == '') ? 0 : $_POST['b2_cat'];
$b2_cat_select = '<select name="b2_cat">';
$b2_cat_select .= '<option value="1" ' . ($b2_cat_selected == 1 ? 'selected' : '') . '>a: Uncategorized</option>';
$b2_cat_select .= '<option value="2" ' . ($b2_cat_selected == 2 ? 'selected' : '') . '>b: Create a new category</option>';
$b2_cat_select .= '<option value="3" ' . ($b2_cat_selected == 3 ? 'selected' : '') . '>c: Import B2evo categories</option>';
$b2_cat_select .= '<option value="4" ' . ($b2_cat_selected == 4 ? 'selected' : '') . '>b+c</option>';
$b2_cat_select .= '</select>';
$b2_checbox_tags = '<input type="checkbox" value="1" name="b2_tags" ' . (($_POST['b2_tags'] == 1) ? 'checked' : '') . ' />';
$b2_checbox_comments = '<input type="checkbox" value="1" name="b2_comments" ' . (($_POST['b2_comments'] == 1) ? 'checked' : '') . ' />';
if ($_POST['step'] == '3' && $target['status'] != 'Error' && $source['status'] != 'Error') {
$step = 3;
}
}
if ($step >= 3) {
if ($b2_cat_selected == 1) {
$root_id = 1;
$taxonomy[1] = 1;
} else if ($b2_cat_selected == 2 || $b2_cat_selected == 4) {
unset($data);
$row = $b2_blog_selected_row;
$data['blog_name'] = $wp_con->real_escape_string($row['blog_name']);
$data['blog_urlname'] = slug($wp_con->real_escape_string($row['blog_urlname']));
$data['blog_description'] = empty($row['blog_description'])?$wp_con->real_escape_string($row['blog_tagline']):$wp_con->real_escape_string($row['blog_description']);
$sql = " INSERT INTO `" . $target['table_prefix'] . "terms` (`name`,`slug`,`term_group`) VALUES ('" . $data['blog_name'] . "','" . $data['blog_urlname'] . "','0');";
$wp_con->query(trim(str_replace("\n", "", $sql)));
$root_id = $wp_con->insert_id;
$newcat[0] = $root_id;
$sql = " INSERT INTO `" . $target['table_prefix'] . "term_taxonomy` (`term_id`,`taxonomy`,`description`,`parent`,`count`) VALUES ('" . $newcat[0] . "','category','" . $data['blog_description'] . "',0,0);";
$wp_con->query(trim(str_replace("\n", "", $sql)));
$id = $wp_con->insert_id;
$taxonomy[$newcat[0]] = $id;
$output .= "1 new category created!<br />";
} else {
$root_id = 0;
$taxonomy[0] = 1;
}
if ($b2_cat_selected == 3 || $b2_cat_selected == 4) {
$sql = "SELECT DISTINCT `" . $source['table_prefix'] . "categories`.* FROM `" . $source['table_prefix'] . "categories` WHERE `cat_blog_ID` = " . $wp_con->real_escape_string($b2_blog_selected);
$res = $b2_con->query($sql);
$count = 0;
while ($row = $res->fetch_array()) {
$sql = " INSERT INTO `" . $target['table_prefix'] . "terms` (`name`,`slug`,`term_group`) VALUES ('" . $wp_con->real_escape_string($row['cat_name']) . "','" . $wp_con->real_escape_string($row['cat_urlname']) . "','0');";
$wp_con->query(trim(str_replace("\n", "", $sql)));
$id = $wp_con->insert_id;
$newcat[$row['cat_ID']] = $id;
$count++;
}
$sql = "SELECT DISTINCT `" . $source['table_prefix'] . "categories`.* FROM `" . $source['table_prefix'] . "categories` WHERE `cat_blog_ID` = " . $wp_con->real_escape_string($b2_blog_selected);
$res = $b2_con->query($sql);
while ($row = $res->fetch_array()) {
$data['parent_id'] = (isset($newcat[$row['cat_parent_ID']]) ? $newcat[$row['cat_parent_ID']] : $root_id);
$data['description'] = $wp_con->real_escape_string($row['cat_description']);
$sql = " INSERT INTO `" . $target['table_prefix'] . "term_taxonomy` (`term_id`,`taxonomy`,`description`,`parent`,`count`) VALUES ('" . $newcat[$row['cat_ID']] . "','category','" . $data['description'] . "','" . $data['parent_id'] . "',0);";
$wp_con->query(trim(str_replace("\n", "", $sql)));
$id = $wp_con->insert_id;
$taxonomy[$newcat[$row['cat_ID']]] = $id;
}
$output .= $count . " categories imported!<br />";
}
if ($b2_cat_selected != 1) {
$sql = "UPDATE `" . $target['table_prefix'] . "options` SET `option_value`='' WHERE `option_name`='category_children';";
$wp_con->query(trim(str_replace("\n", "", $sql)));
}
if ($wp_user_selected == 0) {
$sql = " SELECT DISTINCT `" . $source['table_prefix'] . "users`.* FROM `" . $source['table_prefix'] . "users` INNER JOIN `" . $source['table_prefix'] . "blogusers` ON `" . $source['table_prefix'] . "users`.`user_ID` = `" . $source['table_prefix'] . "blogusers`.`bloguser_user_ID` WHERE `" . $source['table_prefix'] . "blogusers`.`bloguser_blog_ID` = " . $b2_con->real_escape_string($b2_blog_selected) . " AND `user_login`<>'admin'";
$res = $b2_con->query($sql);
$count = 0;
while ($row = $res->fetch_array()) {
$sql = " SELECT * FROM `" . $target['table_prefix'] . "users` WHERE `user_login` = '" . $wp_con->real_escape_string($row['user_login']) . "';";
$pres = $wp_con->query(trim(str_replace("\n", "", $sql)));
if ($prow = $pres->fetch_array()) {
$id = $prow['ID'];
} else {
$sql = " INSERT INTO `" . $target['table_prefix'] . "users` (`user_login`,`user_pass`,`user_nicename`,`user_email`,`user_url`,`user_registered`,`user_status`,`display_name`) VALUES ('" . $wp_con->real_escape_string($row['user_login']) . "','" . $wp_con->real_escape_string($row['user_pass']) . "','" . $wp_con->real_escape_string($row['user_login']) . "','" . $wp_con->real_escape_string($row['user_email']) . "','" . $wp_con->real_escape_string($row['user_url']) . "','" . $wp_con->real_escape_string($row['dateYMDhour']) . "','0','" . $wp_con->real_escape_string($row['user_login']) . "');";
$wp_con->query(trim(str_replace("\n", "", $sql)));
$id = $wp_con->insert_id;
$sql = " INSERT INTO `" . $target['table_prefix'] . "usermeta` (`user_id`,`meta_key`,`meta_value`) VALUES ('" . $id . "', 'first_name', '" . $wp_con->real_escape_string($row['user_firstname']) . "');";
$wp_con->query(trim(str_replace("\n", "", $sql)));
$sql = " INSERT INTO `" . $target['table_prefix'] . "usermeta` (`user_id`,`meta_key`,`meta_value`) VALUES ('" . $id . "', 'last_name', '" . $wp_con->real_escape_string($row['user_lastname']) . "');";
$wp_con->query(trim(str_replace("\n", "", $sql)));
$sql = " INSERT INTO `" . $target['table_prefix'] . "usermeta` (`user_id`,`meta_key`,`meta_value`) VALUES ('" . $id . "', 'nickname', '" . $wp_con->real_escape_string($row['user_nickname']) . "');";
$wp_con->query(trim(str_replace("\n", "", $sql)));
$count++;
}
$newuser[$row['user_ID']] = $id;
}
if ($count > 0) {
$output .= $count . " users imported!<br />";
}
} else {
$newuser[0] = $wp_user_selected;
}
$sql = "SELECT DISTINCT `" . $source['table_prefix'] . "items__item`.* FROM `" . $source['table_prefix'] . "items__item` INNER JOIN `" . $source['table_prefix'] . "categories` ON `" . $source['table_prefix'] . "items__item`.`post_main_cat_ID` = `" . $source['table_prefix'] . "categories`.`cat_ID` WHERE `" . $source['table_prefix'] . "categories`.`cat_blog_ID` = " . $b2_blog_selected;
$res = $b2_con->query($sql);
$pcount = 0;
$tcount = 0;
$ccount = 0;
while ($row = $res->fetch_array()) {
unset($data);
if ($b2_user_selected == 0) {
$data['user'] = isset($newuser[$row['post_creator_user_ID']]) ? $newuser[$row['post_creator_user_ID']] : 1;
} else {
$data['user'] = $b2_user_selected;
}
$data['category'] = isset($newcat[$row['post_main_cat_ID']]) ? $newcat[$row['post_main_cat_ID']] : $root_id;
switch ($row['post_status']) {
case 'published':
case 'deprecated':
$data['status'] = 'publish';
break;
case 'protected':
case 'private':
$data['status'] = 'private';
break;
case 'redirected':
$row['post_content'] = "<a href='".htmlentities($row['post_url'])."'>".htmlentities($row['post_url'])."</a>";
default:
$data['status'] = 'draft';
}
$data['date'] = $row['post_datecreated'];
$data['date_gmt'] = gmdate("Y-m-d G:i:s ", strtotime($data['date']));
$data['modified'] = $row['post_datemodified'];
$data['modified_gmt'] = gmdate("Y-m-d G:i:s ", strtotime($data['modified']));
$psql = "SELECT COUNT(1) AS comment_count FROM `" . $source['table_prefix'] . "comments` WHERE `" . $source['table_prefix'] . "comments`.`comment_post_ID` = " . $row['post_ID'];
$pres = $b2_con->query($psql);
$prow = $pres->fetch_array();
$data['comment_count'] = $prow['comment_count'];
$data['post_content'] = $row['post_content'];
$data['post_excerpt'] = $row['post_excerpt'];
$data['title'] = $row['post_title'];
$psql = "INSERT INTO `" . $target['table_prefix'] . "posts` (`post_author`,`post_content`,`post_title`,`post_excerpt`,`post_status`,`post_date`,`post_date_gmt`,`post_modified`,`post_modified_gmt`,`comment_count`, `to_ping`, `pinged`, `post_content_filtered`) VALUES ('" . $data['user'] . "','" . $wp_con->real_escape_string($data['post_content']) . "','" . $wp_con->real_escape_string($data['title']) . "','" . $wp_con->real_escape_string($data['post_excerpt']) . "','" . $data['status'] . "','" . $data['date'] . "','" . $data['date_gmt'] . "','" . $data['modified'] . "','" . $data['modified_gmt'] . "','" . $data['comment_count'] . "', '', '', '');";
$wp_con->query(trim(str_replace("\n", "", $psql)));
$id = $wp_con->insert_id;
$eid = $row['post_ID'];
$psql = "INSERT INTO `wp_term_relationships` (`object_id`,`term_taxonomy_id`,`term_order`) VALUES (" . $id . "," . $taxonomy[$data['category']] . ",0);";
$wp_con->query(trim(str_replace("\n", "", $psql)));
$psql = "SELECT * FROM `" . $source['table_prefix'] . "postcats` WHERE `postcat_post_ID` = " . $row['post_ID'];
$pres = $b2_con->query($psql);
while ($prow = $pres->fetch_array()) {
$psql = "INSERT INTO `wp_term_relationships` (`object_id`,`term_taxonomy_id`,`term_order`) VALUES (" . $id . "," . $taxonomy[$newcat[$prow['postcat_cat_ID']]] . ",0);";
$wp_con->query(trim(str_replace("\n", "", $psql)));
}
$pcount++;
if ($_POST['b2_tags'] == 1) {
$psql = "SELECT `itag_itm_ID`, LOWER(cast(`" . $source['table_prefix'] . "items__tag`.`tag_name` as char)) AS tag_name FROM `" . $source['table_prefix'] . "items__tag` INNER JOIN `" . $source['table_prefix'] . "items__itemtag` ON `itag_tag_ID` = `tag_ID` WHERE `itag_itm_ID` = " . $row['post_ID'];
$pres = $b2_con->query($psql);
while ($prow = $pres->fetch_array()) {
unset($data);
$data['tag_name'] = $wp_con->real_escape_string($prow['tag_name']);
$data['tag_slug'] = slug($data['tag_name']);
$tsql = "SELECT * FROM `" . $target['table_prefix'] . "terms` WHERE `slug` = '" . $data['tag_slug'] . "';";
$tres = $wp_con->query(trim(str_replace("\n", "", $tsql)));
if ($trow = $tres->fetch_array()) {
$tid = $trow['term_id'];
} else {
$tsql = "INSERT INTO `" . $target['table_prefix'] . "terms` (`name`,`slug`,`term_group`) VALUES ('" . $data['tag_name'] . "','" . $data['tag_slug'] . "',0);";
$wp_con->query(trim(str_replace("\n", "", $tsql)));
$tid = $wp_con->insert_id;
$tcount++;
}
$tsql = "SELECT * FROM `" . $target['table_prefix'] . "term_taxonomy` WHERE `term_id` = '" . $tid . "' AND `taxonomy`='post_tag';";
$tres = $wp_con->query(trim(str_replace("\n", "", $tsql)));
if ($trow = $tres->fetch_array()) {
$ttid = $trow['term_taxonomy_id'];
} else {
$tsql = "INSERT INTO `" . $target['table_prefix'] . "term_taxonomy` (`term_id`,`taxonomy`,`description`,`parent`,`count`) VALUES (" . $tid . ",'post_tag','',0,0);";
$wp_con->query(trim(str_replace("\n", "", $tsql)));
$ttid = $wp_con->insert_id;
}
$tsql = "SELECT * FROM `" . $target['table_prefix'] . "term_relationships` WHERE `object_id` = '" . $id . "' AND `term_taxonomy_id`='" . $ttid . "';";
$tres = $wp_con->query(trim(str_replace("\n", "", $tsql)));
if ($trow = $tres->fetch_array()) {

} else {
$tsql = "INSERT INTO `" . $target['table_prefix'] . "term_relationships` (`object_id`,`term_taxonomy_id`,`term_order`) VALUES (" . $id . "," . $ttid . ",0);";
$wp_con->query(trim(str_replace("\n", "", $tsql)));
}
}
}
if ($_POST['b2_comments'] == 1) {
$psql = "SELECT `" . $source['table_prefix'] . "comments`.* , `" . $source['table_prefix'] . "users`.`user_nickname`, `" . $source['table_prefix'] . "users`.`user_url`, `" . $source['table_prefix'] . "users`.`user_email` FROM `" . $source['table_prefix'] . "comments` LEFT JOIN `" . $source['table_prefix'] . "users` ON `" . $source['table_prefix'] . "comments`.`comment_author_ID` = `" . $source['table_prefix'] . "users`.`user_ID` WHERE `" . $source['table_prefix'] . "comments`.`comment_post_ID`=" . $eid;
$pres = $b2_con->query($psql);
while ($prow = $pres->fetch_array()) {
unset($data);
if ($prow['comment_author_ID'] != NULL) {
$data['author'] = $wp_con->real_escape_string($prow['user_nickname']);
$data['url'] = $wp_con->real_escape_string($prow['user_url']);
$data['email'] = $wp_con->real_escape_string($prow['user_email']);
} else {
$data['author'] = $wp_con->real_escape_string($prow['comment_author']);
$data['url'] = $wp_con->real_escape_string($prow['comment_author_url']);
$data['email'] = $wp_con->real_escape_string($prow['comment_author_email']);
}
$data['time'] = $wp_con->real_escape_string($prow['comment_date']);
$data['gm_time'] = gmdate("Y-m-d G:i:s ", strtotime($data['time']));
$data['comment_type'] = $wp_con->real_escape_string($prow['comment_type']);
switch ($data['comment_type']) {
case 'trackback':
break;
case 'pingback':
case 'linkback':
$data['comment_type'] = 'pingback';
break;
case 'comment':
default:
$data['comment_type'] = '';
break;
}
$data['comment_status'] = $wp_con->real_escape_string($prow['comment_status']);
switch ($data['comment_status']) {
case 'published':
case 'deprecated':
$data['comment_status'] = '1';
break;
case 'protected':
case 'private':
$data['comment_status'] = 'private';
break;
case 'draft':
case 'redirected':
default:
$data['comment_status'] = '0';
}
$data['comment_author_IP'] = $wp_con->real_escape_string($prow['comment_author_IP']);
$data['comment_content'] = $wp_con->real_escape_string($prow['comment_content']);
$data['comment_karma'] = $wp_con->real_escape_string($prow['comment_karma']);
$csql = "INSERT INTO `" . $target['table_prefix'] . "comments` (`comment_post_ID`,`comment_author`,`comment_author_email`,`comment_author_url`,`comment_author_IP`,`comment_date`,`comment_date_gmt`,`comment_content`,`comment_karma`, `comment_approved`, `comment_type`) VALUES ('" . $id . "','" . $data['author'] . "','" . $data['email'] . "','" . $data['url'] . "','" . $data['comment_author_IP'] . "','" . $data['time'] . "','" . $data['gm_time'] . "','" . $data['comment_content'] . "','" . $data['comment_karma'] . "','" . $data['comment_status'] . "','" . $data['comment_type'] . "');";
$wp_con->query(trim(str_replace("\n", "", $csql)));
$ccount++;
}
}
}
$output .= $pcount . " posts imported!<br />";
if ($_POST['b2_tags'] == 1) {
$output .= $tcount . " tags imported!<br />";
}
if ($_POST['b2_comments'] == 1) {
$output .= $ccount . " comments imported!<br />";
}
}
mysqli_free_result($res);
mysqli_free_result($pres);
mysqli_free_result($tres);
mysqli_close($wp_con);
mysqli_close($b2_con);
?>
<!DOCTYPE html>
<html>
<head>
<title>b2wp - b2evolution to wordpress importer</title>
<meta charset="UTF-8">
<style>
body { font: 10pt sans-serif; }
table { width: 480px; border: #ccc 1px solid; margin:10px auto; }
th, td { width: 160px; }
caption { font: bold 16pt sans-serif; }
</style>
</head>

<body>
<form action="" method="post">
<table>
<caption>B2WP</caption>
<tr>
<th></th>
<th>B2evolution</th>
<th>Wordpress</th>
</tr>
<tr>
<td>Host</td>
<td><input name="source_dbhost" type="text" <?php if ($step >= 2) echo 'readonly'; ?> value="<?php echo $source['db_host']; ?>" /></td>
<td><input type="text" disabled value="<?php echo $target['db_host']; ?>" /></td>
</tr>
<tr>
<td>User</td>
<td><input name="source_dbuser" type="text" <?php if ($step >= 2) echo 'readonly'; ?> value="<?php echo $source['db_user']; ?>" /></td>
<td><input type="text" disabled value="<?php echo $target['db_user']; ?>" /></td>
</tr>
<tr>
<td>Password</td>
<td><input name="source_dbpassword" type="password" <?php if ($step >= 2) echo 'readonly'; ?> value="<?php echo $source['db_password']; ?>" /></td>
<td><input type="password" disabled value="<?php echo $target['db_password']; ?>" /></td>
</tr>
<tr>
<td>Database</td>
<td><input name="source_dbname" type="text" <?php if ($step >= 2) echo 'readonly'; ?> value="<?php echo $source['db_name']; ?>" /></td>
<td><input type="text" disabled value="<?php echo $target['db_name']; ?>" /></td>
</tr>
<tr>
<td>Table Prefix</td>
<td><input type="text" name="source_table_prefix" <?php if ($step >= 2) echo 'readonly'; ?> value="<?php echo $source['table_prefix']; ?>" /></td>
<td><input type="text" name="target_table_prefix" <?php if ($step >= 2) echo 'readonly'; ?> value="<?php echo $target['table_prefix']; ?>" /></td>
</tr>
<tr>
<td>Status</td>
<td><input type="text" disabled value="<?php echo $source['status']; ?>" /></td>
<td><input type="text" disabled value="<?php echo $target['status']; ?>" /></td>
</tr>
<?php if ($step == 1) { ?>
<tr>
<td></td>
<td colspan="2"><input type="submit" value="Connect" /></td>
</tr>
<?php } else if ($step >= 2) { ?>
<tr>
<td colspan="3"><input type="hidden" name="step" value="3"/><hr /></td>
</tr>
<tr>
<td>B2evo Blog</td>
<td colspan="2">
<?php echo $b2_blog_select; ?>
</td>
</tr>
<tr>
<td valign="top">Import Options</td>
<td colspan="2">
<input type="checkbox" checked disabled> <label for="b2_posts">Posts</label><br />
<input type="checkbox" checked disabled> <label for="b2_user">Authors</label> <?php echo $b2_user_select; ?><br />
<input type="checkbox" checked disabled> <label for="b2_cat">Categories</label> <?php echo $b2_cat_select; ?><br />
<?php echo $b2_checbox_tags; ?> <label for="b2_tags">Tags</label><br />
<?php echo $b2_checbox_comments; ?> <label for="b2_comments">Comments</label><br />
</td>
</tr>
<tr>
<td></td>
<td colspan="2">
</td>
</tr>
<tr>
<td></td>
<td colspan="2">
<input type="submit" value="Import" />
<input type="button" value="Back" onclick="location.href='b2wp.php';" />
</td>
</tr>
<?php } ?>
<?php if (!empty($output)) { ?>
<tr>
<td colspan="3"><hr /><?php echo $output; ?></td>
</tr>
<?php } ?>
</table>
</form>
</body>
</html>

This is NOT a WordPress importer plugin, this is simply a PHP script to extract content (Authors, Categories, Posts, Tags, and Comments) from a b2evolution database and insert into WordPress database. This migration script does NOT catered importing of uploaded media files. Feel free to modify it as per your needs. You might want to try these WordPress plugins Add From Server or Import External Images for migrating your uploaded files.

Special thanks
Thanks to RealNitro’s b2evo2wp.php script, it was my primary source of reference for understanding b2evo/wp database schema.
Thanks to KP for sharing b2evolution 5.1.2 to WorPress 4 migration script.

Filed under: MySQL,PHP,Web — Tags: , , , , — GG @ 6:53 am

November 18, 2013

PHP client denied by server configuration

Issue:

  • AH01630: client denied by server configuration
  • Forbidden
    You don’t have permission to access /___ on this server.
    Apache/2.4.6 (Ubuntu) Server at www.___.com Port ___

Reason:

  • New way to do access control in Apache 2.4 and above.

Solution:

  • Edit Apache configuration , replace Allow from all to Require all granted

More Info:

Filed under: PHP,Web — Tags: , , , — GG @ 3:15 pm

October 19, 2013

PHP none could be negotiated

Issue:
Negotiation: discovered file(s) matching request: /var/www/index (None could be negotiated).

Reason:
PHP mime type has not been enabled.

Solution:
Edit Apache configuration (apache2.conf / httpd.conf / mime.conf), add or uncomment line below:

AddType application/x-httpd-php .php
Filed under: PHP — Tags: , , — GG @ 3:27 pm

August 7, 2013

PHP unexpected end of file

Issue:

  • Parse error: syntax error, unexpected end of file in ___ on line ___

Reason:

  • short_open_tags has been disabled in PHP configuration on the server.
  • Short form of PHP open tag (<? ?>) is not allowed.
  • PHP does not recognize code between short tag <? and ?>

Solution:

  • Avoid using short tag, replace short tag (<? and ?>) with full tag (<?php and ?>), and <?=… ?> with <?php echo …; ?>.

    or

  • For PHP v5.4.0 or newer
    Edit php.ini
    find
    short_open_tag = Off
    change it to:
    short_open_tag = On

    If short_open_tag is enabled, you will not be able to use JSAPI tag (<?js=…?>) or XML tag (<?xml … ?>) inline.
    You will be required to echo/print it with PHP, for example: <?php echo ‘<?xml version=”1.0″?>’; ?>.

More Info:

December 11, 2012

PHP regular expression to strip/trim protocol from the beginning of an URL.

To remove http://, https://, ftp://, sftp://, www

echo preg_replace('#^[^:/.]*[:/]+(www.)?#i','',$url);

Examples:

function strip_url($url){
  return return preg_replace('#^[^:/.]*[:/]+(www.)?#i','',$url);
}
echo strip_url('http://www.ggshow.com');
echo strip_url('https://www.ggshow.com');
echo strip_url('ftp://www.ggshow.com');
echo strip_url('sftp://www.ggshow.com');
echo strip_url('http://ggshow.com');
echo strip_url('http:/www.ggshow.com');
echo strip_url('http:www.ggshow.com');
echo strip_url('http:ggshow.com');
echo strip_url('HTTP://WWW.ggshow.com');
echo strip_url('anything://www.ggshow.com');
echo strip_url('anything//www.ggshow.com');
echo strip_url('//www.ggshow.com');
echo strip_url('/www.ggshow.com');

Every echo statement above will output ggshow.com

Filed under: PHP — Tags: , , , , , , , , , , — GG @ 8:56 pm

July 4, 2012

Zend Framework – CLI – Module

  • Create module
    zf create module <moduleName>
  • Create dbTable class in module
    zf create db-table <className> <tableName> <moduleName>
  • Create model in module
    zf create model <mapperName> <moduleName>
  • Create controller in module
    zf create controller <controllerName> index-action-included[=1] <moduleName>
  • Create form in module
    zf create form <formName> <moduleName>
  • Create action in module
    zf create action <actionName> <controllerName>[=Index] view-included[=1] <moduleName>
  • Create view in module
    zf create view <controllerName> <actionName> <moduleName>
Filed under: PHP,Web,Zend Framework — Tags: , , , , , , , , — GG @ 2:50 pm

Zend Framework – View Helper – Placeholder

Example usage for place holders:

  • doctype()
  • headTitle()
  • headMeta()
  • headLink()
  • headStyle()
  • headScript()
  • inlineScript()

DOCTYPE

$view->doctype('HTML5');

Title

$view->headTitle('Site Title');
$view->headTitle('Site Title')
	->setSeparator(' :: ');

Meta

$view->headMeta()->appendName('keywords', 'page, keywords')
	->appendName('description', 'page description')
	->appendName('author', 'author name');

Refresh

$view->headMeta()->appendHttpEquiv('Refresh','30');

Redirect

$view->headMeta()->appendHttpEquiv('Refresh','0;url=http://www.ggshow.com');

Disable client side cache

$view->headMeta()->appendHttpEquiv('expires','-1')
	->appendHttpEquiv('pragma', 'no-cache')
	->appendHttpEquiv('Cache-Control', 'no-cache');

Setting content type and character set

$view->headMeta()->setCharset('utf-8');
$view->headMeta()->appendHttpEquiv('Content-Type', 'text/html; charset=UTF-8')
	->appendHttpEquiv('Content-Language', 'en-US');

Favicon with headLink

$view->headLink(array('rel' => 'favicon', 'href' => 'favicon.ico'),'PREPEND');
$view->headLink(array('rel' => 'shortcut icon', 'type' => 'image/x-icon', 'href' => 'favicon.ico'),'PREPEND');

CSS with headLink

$view->headLink()->appendStylesheet('style.css');

// appendStylesheet, prependStylesheet, setStylesheet, offsetStylesheet, appendAlternative, prependAlternative, setAlternative, offsetAlternative

CSS with headStyle

$view->headStyle->captureStart();
?>
body { margin: 0; }
<?php
$view->headStyle->captureEnd();

JavaScript

$view->headScript()->appendFile(“script.js”);
$view->headScript()->prependFile(“script.js”);
$view->inlineScript()->appendFile(“script.js”);
$this->headScript()->appendFile('script.js', 'text/javascript', array('conditional' => 'lt IE 7') );

Example output (within layout/view)

echo $this->headTitle();
echo $this->headLink();
echo $this->headMeta();
echo htmlentities($view->headMeta()->setPostfix("n"));

Example layout with placeholders

<?php echo $this->doctype() ?>
<html>
	<head>
		<?php echo $this->headTitle() ?>
		<?php echo $this->headScript() ?>
		<?php echo $this->headStyle() ?>
	</head>
	<body>
		<?php echo $this->layout()->content; ?>
		<?php echo $this->inlineScript() ?>
	</body>
</html>

Zend Framework – View

To get the view object

  1. witin view script
    $view = $this;
  2. within action controller
    $view = $this->view;
  3. within bootstrap
    $this->bootstrap(‘view’);
    $view = $this->getResource(‘view’);
  4. within resource plugin
    $this->getBootstrap()->bootstrap('view');
    $view = $this->getBootstrap()->getResource('view');
Filed under: PHP,Web,Zend Framework — Tags: , , , , — GG @ 2:30 pm
Older Posts »

© 2024 GGSHOW | Powered by WordPress