| Server IP : 104.21.21.239 / Your IP : 216.73.216.11 Web Server : Apache/2.4.68 (Amazon Linux) OpenSSL/3.5.5 System : Linux ip-172-31-69-123.ec2.internal 6.1.176-223.369.amzn2023.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Jul 24 13:34:27 UTC 2026 x86_64 User : ec2-user ( 1000) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/tgnewdev/admin/batch/ |
Upload File : |
<?php
use classes\lib\BatchJob;
use classes\lib\EmailEngine;
include(__DIR__ . '/../mod/_mod_security.php');
//ob_start();
echo 'hello world!' . PHP_EOL . 'here we are again';
$job_id = (int)$argv[2];
$batch_job = new BatchJob($job_id);
try {
$import_events = (int)$argv[3];
$import_locations = (int)$argv[4];
$import_categories = (int)$argv[5];
$import_articles = (int)$argv[6];
$erase_data = (int)$argv[7];
$remap_recurrence = (int)$argv[8];
//************* Wordpress DB connection *************
$wpdb = new PDO(WP_DB_CONNECT_STRING, WP_DB_USER, WP_DB_PWD);
$sql_tz = "SET time_zone = '".APP_TIMEZONE."'";
$wpdb->query($sql_tz);
$wpdb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$config = HTMLPurifier_Config::createDefault();
$purifier = new HTMLPurifier($config);
if ($erase_data === 1) :
$html = '<p>Erasing all data.</p>';
$batch_job->append_output($html);
$sql = "
TRUNCATE events_categories;
TRUNCATE events;
TRUNCATE categories;
TRUNCATE locations;
TRUNCATE articles;
TRUNCATE media;
";
$db->query($sql);
erase_files_in_directory(APP_ROOT_IMAGE_DIR . '/events/');
erase_files_in_directory(APP_ROOT_IMAGE_DIR . '/articles/');
erase_files_in_directory(APP_ROOT_IMAGE_DIR . '/locations/');
erase_files_in_directory(APP_ROOT_IMAGE_DIR . '/slider/');
erase_files_in_directory(APP_ROOT_IMAGE_DIR . '/content/');
erase_files_in_directory(APP_ROOT_IMAGE_DIR . '/cache/');
$html = '<p>Finished erasing all data.</p>';
$batch_job->append_output($html);
endif;
$sql_img = "
INSERT INTO media SET
media_cat = :media_cat,
media_type = :media_type,
media_path = :media_path,
media_filename = :media_filename,
media_desc = :media_desc,
hash = :hash
";
$stmt_img = $db->prepare($sql_img);
$wp_row_count = 0;
$count = 0;
$cancel = false;
if ($import_locations === 1 && !$cancel) :
$html = '<p>Importing Locations.</p>';
$batch_job->append_output($html);
$sql_wp = "
SELECT
l.*,
MAX(CASE WHEN pm.meta_key = '_thumbnail_id' THEN p.guid END) AS image_url,
MAX(CASE WHEN pm.meta_key = 'Phone' THEN pm.meta_value END) AS phone,
MAX(CASE WHEN pm.meta_key = 'URL' THEN pm.meta_value END) AS url,
MAX(CASE WHEN pm.meta_key = 'Google Map' THEN pm.meta_value END) AS google_map
FROM
wp_em_locations l
LEFT JOIN
wp_postmeta pm ON l.post_id = pm.post_id
LEFT JOIN
wp_posts p ON p.ID = pm.meta_value AND p.post_type = 'attachment'
GROUP BY
l.location_id, l.post_id
";
$wp_result = $wpdb->query($sql_wp);
$wp_row_count += $wp_result->rowCount();
$batch_job->set_total_recs($wp_row_count);
$html = '<p>Selected ' . $wp_result->rowCount() . ' Locations from Wordpress for import.</p>';
$batch_job->append_output($html);
$sql = "
INSERT INTO locations SET
name = :name,
slug = :slug,
featured_image = :featured_image,
html = :html,
address = :address,
city = :city,
state = :state,
zip = :zip,
country = :country,
region = :region,
phone = :phone,
url = :url,
google_map = :google_map,
latitude = :latitude,
longitude = :longitude,
status = :status,
wp_post_id = :wp_post_id,
wp_location_id = :wp_location_id
";
$stmt = $db->prepare($sql);
while (($wp_row = $wp_result->fetch(PDO::FETCH_OBJ)) && !$cancel) :
//************ process image *************
$save_image_file = [];
if (!empty($wp_row->image_url)) :
$image_url = clean_string($wp_row->image_url);
$save_image_file = image_url_to_file($image_url, 'img/content/');
if (!empty($save_image_file['filename']) && !$save_image_file['duplicate']) :
$data_img = [
':media_cat' => 'content',
':media_type' => 'img',
':media_path' => 'img/content/',
':media_filename' => $save_image_file['filename'],
':media_desc' => clean_string($wp_row->location_name),
':hash' => $save_image_file['hash'],
];
$stmt_img->execute($data_img);
endif;
endif;
//****************************************
$post_html = normalize_html($wp_row->post_content);
$post_html = $purifier->purify($post_html);
$post_html = html_image_parse_and_store($post_html, 'content', $wp_row->location_name);
$data = [
':name' => clean_string($wp_row->location_name),
':slug' => clean_string($wp_row->location_slug),
':featured_image' => !empty($save_image_file['filename']) ? $save_image_file['filename'] : null,
':html' => $post_html,
':address' => clean_string($wp_row->location_address),
':city' => clean_string($wp_row->location_town),
':state' => clean_string($wp_row->location_state),
':zip' => clean_string($wp_row->location_postcode),
':country' => clean_string($wp_row->location_country),
':region' => clean_string($wp_row->location_region),
':phone' => clean_string($wp_row->phone),
':url' => clean_string($wp_row->url),
':google_map' => clean_string($wp_row->google_map),
':latitude' => $wp_row->location_latitude,
':longitude' => $wp_row->location_longitude,
':status' => $wp_row->location_status ?? 0,
':wp_post_id' => $wp_row->post_id,
':wp_location_id' => $wp_row->location_id,
];
$stmt->execute($data);
$count++;
if ($count % 100 === 0) :
$batch_job->set_processed_recs($count);
endif;
if ($batch_job->cancel_requested()) :
$cancel = true;
$html = '<p>Job cancelled.</p>';
$batch_job->append_output($html);
break;
endif;
endwhile;
endif;
if ($import_events === 1 && !$cancel) :
$html = '<p>Importing Events.</p>';
$batch_job->append_output($html);
$sql_wp = "
SELECT
e.*,
MAX(CASE WHEN pm.meta_key = '_aioseo_title' THEN pm.meta_value END) AS seo_title,
MAX(CASE WHEN pm.meta_key = '_aioseo_description' THEN pm.meta_value END) AS seo_description,
MAX(CASE WHEN pm.meta_key = '_aioseo_keywords' THEN pm.meta_value END) AS seo_keywords,
MAX(CASE WHEN pm.meta_key = '_thumbnail_id' THEN p.guid END) AS image_url,
MAX(CASE WHEN pm.meta_key = '_event_location_url' THEN p.guid END) AS event_location_url,
MAX(CASE WHEN pm.meta_key = '_event_location_url_text' THEN p.guid END) AS event_location_url_text,
MAX(CASE WHEN pm.meta_key = 'Price' THEN pm.meta_value END) AS custom_field_price,
MAX(CASE WHEN pm.meta_key = 'Event URL' THEN pm.meta_value END) AS custom_field_event_url,
MAX(CASE WHEN pm.meta_key = 'Your Email' THEN pm.meta_value END) AS custom_field_your_email,
MAX(CASE WHEN pm.meta_key = 'Your Name' THEN pm.meta_value END) AS custom_field_your_name,
MAX(CASE WHEN pm.meta_key = 'Your Location' THEN pm.meta_value END) AS custom_field_your_location,
MAX(CASE WHEN t.name = 'Just Announced' THEN 1 END) AS just_announced
FROM
wp_em_events e
LEFT JOIN
wp_postmeta pm ON e.post_id = pm.post_id
LEFT JOIN
wp_posts p ON p.ID = pm.meta_value AND p.post_type = 'attachment'
LEFT JOIN
wp_term_relationships tr ON (e.post_id = tr.object_id)
LEFT JOIN
wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'event-tags')
LEFT JOIN
wp_terms t ON (tt.term_id = t.term_id AND t.name = 'Just Announced')
WHERE
e.event_status IS NOT NULL
GROUP BY
e.event_id, e.post_id
";
$wp_result = $wpdb->query($sql_wp);
$wp_row_count += $wp_result->rowCount();
$batch_job->set_total_recs($wp_row_count);
$html = '<p>Selected ' . $wp_result->rowCount() . ' Events from Wordpress for import.</p>';
$batch_job->append_output($html);
$sql = "
INSERT INTO events SET
name = :name,
slug = :slug,
featured_image = :featured_image,
html = :html,
status = :status,
show_in_slider = :show_in_slider,
start_time_local = :start_time_local,
end_time_local = :end_time_local,
start_time_utc = :start_time_utc,
end_time_utc = :end_time_utc,
all_day_event = :all_day_event,
event_timezone = :event_timezone,
location_id = :location_id,
location_type = :location_type,
location_url = :location_url,
location_url_text = :location_url_text,
recurrence = :recurrence,
recurrence_master_event_id = :recurrence_master_event_id,
recurrence_set_id = :recurrence_set_id,
recurrence_interval = :recurrence_interval,
recurrence_freq = :recurrence_freq,
recurrence_by_day = :recurrence_by_day,
recurrence_by_week_no = :recurrence_by_week_no,
recurrence_days = :recurrence_days,
seo_enabled = :seo_enabled,
seo_title = :seo_title,
seo_keywords = :seo_keywords,
seo_description = :seo_description,
wp_event_id = :wp_event_id,
wp_post_id = :wp_post_id,
wp_location_id = :wp_location_id,
wp_recurrence_id = :wp_recurrence_id,
wp_event_date_created = :wp_event_date_created,
price = :price,
event_url = :event_url,
submitted_email = :submitted_email,
submitted_name = :submitted_name,
submitted_location = :submitted_location
";
$stmt = $db->prepare($sql);
while (($wp_row = $wp_result->fetch(PDO::FETCH_OBJ)) && !$cancel) :
//************ process image *************
$save_image_file = [];
if (!empty($wp_row->image_url)) :
$image_url = clean_string($wp_row->image_url);
//$image_save_path = APP_ROOT_IMAGE_DIR . '/events/';
$save_image_file = image_url_to_file($image_url, 'img/content/');
if (!empty($save_image_file['filename']) && !$save_image_file['duplicate']) :
$data_img = [
':media_cat' => 'content',
':media_type' => 'img',
':media_path' => 'img/content/',
':media_filename' => $save_image_file['filename'],
':media_desc' => clean_string($wp_row->event_name),
':hash' => $save_image_file['hash'],
];
$stmt_img->execute($data_img);
endif;
endif;
//****************************************
$post_html = normalize_html($wp_row->post_content);
$post_html = $purifier->purify($post_html);
$post_html = html_image_parse_and_store($post_html, 'content', $wp_row->event_name);
$location_type = null;
if (!empty($wp_row->location_id) && empty($wp_row->event_location_type)) :
$location_type = 'physical';
elseif ($wp_row->event_location_type === 'url') :
$location_type = 'url';
endif;
$data = [
':name' => clean_string($wp_row->event_name),
':slug' => clean_string($wp_row->event_slug),
':featured_image' => !empty($save_image_file['filename']) ? $save_image_file['filename'] : null,
':html' => $post_html,
':status' => $wp_row->event_status,
':show_in_slider' => $wp_row->just_announced ?? 0,
':start_time_local' => $wp_row->event_start_date . ' ' . $wp_row->event_start_time,
':end_time_local' => $wp_row->event_end_date . ' ' . $wp_row->event_end_time,
':start_time_utc' => $wp_row->event_start,
':end_time_utc' => $wp_row->event_end,
':all_day_event' => $wp_row->event_all_day ?? 0,
':event_timezone' => clean_string($wp_row->event_timezone),
':location_id' => empty($wp_row->location_id) ? NULL : lookup_db_field('locations', 'wp_location_id', $wp_row->location_id, 'id'),
':location_type' => $location_type,
':location_url' => clean_string($wp_row->event_location_url),
':location_url_text' => clean_string($wp_row->event_location_url_text),
':recurrence' => $wp_row->recurrence,
':recurrence_master_event_id' => $wp_row->recurrence_id,
':recurrence_set_id' => $wp_row->recurrence_set_id,
':recurrence_interval' => $wp_row->recurrence_interval,
':recurrence_freq' => clean_string($wp_row->recurrence_freq),
':recurrence_by_day' => clean_string($wp_row->recurrence_byday),
':recurrence_by_week_no' => $wp_row->recurrence_byweekno,
':recurrence_days' => $wp_row->recurrence_days,
':seo_enabled' => 1,
':seo_title' => clean_string($wp_row->seo_title),
':seo_keywords' => clean_string($wp_row->seo_keywords),
':seo_description' => clean_string($wp_row->seo_description),
':wp_event_id' => $wp_row->event_id,
':wp_post_id' => $wp_row->post_id,
':wp_location_id' => $wp_row->location_id,
':wp_recurrence_id' => $wp_row->recurrence_id,
':wp_event_date_created' => $wp_row->event_date_created,
':price' => clean_string($wp_row->custom_field_price),
':event_url' => clean_string($wp_row->custom_field_event_url),
':submitted_email' => clean_string($wp_row->custom_field_your_email),
':submitted_name' => clean_string($wp_row->custom_field_your_name),
':submitted_location' => clean_string($wp_row->custom_field_your_location),
];
$stmt->execute($data);
$count++;
if ($count % 100 === 0) :
$batch_job->set_processed_recs($count);
endif;
if ($batch_job->cancel_requested()) :
$cancel = true;
$html = '<p>Job cancelled.</p>';
$batch_job->append_output($html);
break;
endif;
endwhile;
endif;
if ($import_categories === 1 && !$cancel) :
$html = '<p>Importing Categories.</p>';
$batch_job->append_output($html);
$sql_wp = "
SELECT
t.term_id,
t.name,
t.slug,
tt.description,
tt.taxonomy,
tt.parent
FROM
wp_terms t
JOIN
wp_term_taxonomy tt ON t.term_id = tt.term_id
WHERE
tt.taxonomy = 'event-categories'
";
$wp_result = $wpdb->query($sql_wp);
$wp_row_count += $wp_result->rowCount();
$batch_job->set_total_recs($wp_row_count);
$html = '<p>Selected ' . $wp_result->rowCount() . ' Categories from Wordpress for import.</p>';
$batch_job->append_output($html);
$sql = "
INSERT INTO categories SET
name = :name,
slug = :slug,
description = :description,
parent_category_id = :parent_category_id,
wp_term_id = :wp_term_id
";
$stmt = $db->prepare($sql);
while (($wp_row = $wp_result->fetch(PDO::FETCH_OBJ)) && !$cancel) :
$data = [
':name' => clean_string($wp_row->name),
':slug' => clean_string($wp_row->slug),
':description' => clean_string($wp_row->description),
':parent_category_id' => empty($wp_row->parent) ? NULL : lookup_db_field('categories', 'wp_term_id', $wp_row->parent, 'id'),
':wp_term_id' => $wp_row->term_id,
];
$stmt->execute($data);
$count++;
if ($count % 100 === 0) :
$batch_job->set_processed_recs($count);
endif;
if ($batch_job->cancel_requested()) :
$cancel = true;
$html = '<p>Job cancelled.</p>';
$batch_job->append_output($html);
break;
endif;
endwhile;
//********* categories - events xref *******
$html = '<p>Importing Categories - Events Xref.</p>';
$batch_job->append_output($html);
$sql_wp = "
SELECT
p.ID AS post_id,
p.post_title,
t.name AS category_name,
t.slug,
t.term_id
FROM
wp_posts p
JOIN
wp_term_relationships tr ON p.ID = tr.object_id
JOIN
wp_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
JOIN
wp_terms t ON tt.term_id = t.term_id
WHERE
p.post_type = 'event' AND
tt.taxonomy = 'event-categories'
";
$wp_result = $wpdb->query($sql_wp);
$wp_row_count += $wp_result->rowCount();
$batch_job->set_total_recs($wp_row_count);
$html = '<p>Selected ' . $wp_result->rowCount() . ' Categories-Events XREF from Wordpress for import.</p>';
$batch_job->append_output($html);
$sql = "
INSERT INTO events_categories SET
event_id = :event_id,
category_id = :category_id
";
$stmt = $db->prepare($sql);
while (($wp_row = $wp_result->fetch(PDO::FETCH_OBJ)) && !$cancel) :
$data = [
':event_id' => empty($wp_row->post_id) ? NULL : lookup_db_field('events', 'wp_post_id', $wp_row->post_id, 'id'),
':category_id' => empty($wp_row->term_id) ? NULL : lookup_db_field('categories', 'wp_term_id', $wp_row->term_id, 'id'),
];
$stmt->execute($data);
$count++;
if ($count % 100 === 0) :
$batch_job->set_processed_recs($count);
endif;
if ($batch_job->cancel_requested()) :
$cancel = true;
$html = '<p>Job cancelled.</p>';
$batch_job->append_output($html);
break;
endif;
endwhile;
endif;
if ($import_articles === 1) :
$html = '<p>Importing Articles.</p>';
$batch_job->append_output($html);
$sql_wp = "
SELECT
p.*,
MAX(CASE WHEN pm.meta_key = '_aioseo_title' THEN pm.meta_value END) AS seo_title,
MAX(CASE WHEN pm.meta_key = '_aioseo_description' THEN pm.meta_value END) AS seo_description,
MAX(CASE WHEN pm.meta_key = '_aioseo_keywords' THEN pm.meta_value END) AS seo_keywords,
MAX(CASE WHEN pm.meta_key = '_thumbnail_id' THEN p2.guid END) AS image_url,
MAX(CASE WHEN t.name = 'HOME' THEN 1 END) AS is_home
FROM wp_posts p
LEFT JOIN
wp_postmeta pm ON p.ID = pm.post_id
LEFT JOIN
wp_posts p2 ON p2.ID = pm.meta_value AND p2.post_type = 'attachment'
LEFT JOIN
wp_term_relationships tr ON (p.ID = tr.object_id)
LEFT JOIN
wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'post_tag')
LEFT JOIN
wp_terms t ON (tt.term_id = t.term_id AND t.name = 'HOME')
WHERE p.post_type IN ('post', 'page')
AND p.post_status NOT IN ('inherit', 'auto-draft')
GROUP BY
p.ID
ORDER BY p.ID ASC
";
$wp_result = $wpdb->query($sql_wp);
$wp_row_count += $wp_result->rowCount();
$batch_job->set_total_recs($wp_row_count);
$html = '<p>Selected ' . $wp_result->rowCount() . ' Articles from Wordpress for import.</p>';
$batch_job->append_output($html);
$sql = "
INSERT INTO articles SET
post_title = :post_title,
slug = :slug,
html = :html,
slider_image = :slider_image,
post_date = :post_date,
post_status = :post_status,
show_in_slider = :show_in_slider,
seo_enabled = :seo_enabled,
seo_title = :seo_title,
seo_keywords = :seo_keywords,
seo_description = :seo_description,
wp_post_id = :wp_post_id
";
$stmt = $db->prepare($sql);
while (($wp_row = $wp_result->fetch(PDO::FETCH_OBJ)) && !$cancel) :
//************ process image *************
$save_image_file = [];
if (!empty($wp_row->image_url)) :
$image_url = clean_string($wp_row->image_url);
$save_image_file = image_url_to_file($image_url, 'img/content/');
if (!empty($save_image_file['filename']) && !$save_image_file['duplicate']) :
$data_img = [
':media_cat' => 'content',
':media_type' => 'img',
':media_path' => 'img/content/',
':media_filename' => $save_image_file['filename'],
':media_desc' => clean_string($wp_row->post_title),
':hash' => $save_image_file['hash'],
];
$stmt_img->execute($data_img);
endif;
endif;
//********** loop through all images in the post ************
$post_html = normalize_html($wp_row->post_content);
$post_html = $purifier->purify($post_html);
$post_html = html_image_parse_and_store($post_html, 'content', $wp_row->post_title);
//****************************************
$data = [
':post_title' => clean_string($wp_row->post_title),
':slug' => clean_string($wp_row->post_name),
':html' => $post_html,
':slider_image' => !empty($save_image_file['filename']) ? $save_image_file['filename'] : null,
':post_date' => $wp_row->post_date,
':post_status' => array_keys(APP_ARTICLE_STATUSES, $wp_row->post_status)[0] ?? null,
':show_in_slider' => $wp_row->is_home ?? 0,
':seo_enabled' => 1,
':seo_title' => clean_string($wp_row->seo_title),
':seo_keywords' => clean_string($wp_row->seo_keywords),
':seo_description' => clean_string($wp_row->seo_description),
':wp_post_id' => $wp_row->ID,
];
$stmt->execute($data);
$count++;
if ($count % 100 === 0) :
$batch_job->set_processed_recs($count);
endif;
if ($batch_job->cancel_requested()) :
$cancel = true;
$html = '<p>Job cancelled.</p>';
$batch_job->append_output($html);
break;
endif;
endwhile;
endif;
if ($remap_recurrence === 1) :
$sql = "
UPDATE events e
JOIN (
SELECT id, wp_event_id
FROM events
) map ON map.wp_event_id = e.recurrence_master_event_id
SET e.recurrence_master_event_id = map.id
WHERE e.recurrence_master_event_id IS NOT NULL
";
$db->query($sql);
$sql = "
UPDATE events e SET e.recurrence_master_event_id = e.id WHERE e.recurrence = 1 AND e.recurrence_master_event_id IS NULL
";
$db->query($sql);
$sql = "
UPDATE events e SET e.recurrence = 1 WHERE e.recurrence_master_event_id IS NOT NULL AND e.recurrence <> 1
";
$db->query($sql);
endif;
$batch_job->set_processed_recs($count);
$html = '<p>process_wp_data_import job statistics:</p>';
$html .= '<p>Records selected: ' . $wp_row_count . '</p>';
$html .= '<p>Records inserted: ' . $count . '</p>';
//$html .= '<p>Emails in error: ' . $count_err . '</p>';
$batch_job->append_output($html);
$batch_job->finish($cancel);
} catch (Throwable $e) {
$batch_job->fail(nl2br(
'Error: ' . $e->getMessage() . PHP_EOL .
'File: ' . $e->getFile() . PHP_EOL .
'Line: ' . $e->getLine() . PHP_EOL .
'Stack trace:' . PHP_EOL . $e->getTraceAsString()
));
}
//$output = ob_get_clean();