#!/usr/bin/php -q * Copyright (c) 2005 Platon Group, http://platon.sk/ * Licensed under terms of GNU General Public License. * All rights reserved. * * Changelog: * 2005-10-30 - created * */ /* $Platon: scripts/php/importers/talkive/talkive-import.php,v 1.1 2005/10/31 04:35:51 nepto Exp $ */ function error($err_obj) // {{{ { if (is_object($err_obj)) { $error_string = $err_obj->getMessage() . "\n" . $err_obj->getDebugInfo(); } else { $error_string = $err_obj; } echo "\nFATAL ERROR: $error_string\n"; die(); } // }}} function createQuery($table, $data) // {{{ { $keys = ''; $vals = ''; foreach ($data as $key => $val) { if (is_null($val) || !strncmp($key, '__', 2)) { continue; } if (is_array($val)) { $val = array_unique($val); sort($val); $val = join("\n", $val); } if ($keys != '') $keys .= ', '; if ($vals != '') $vals .= ', '; $keys .= $key; //$val = DB::quoteString($val); $val = addslashes($val); $vals .= "'$val'"; } if ($keys != '') { return sprintf('INSERT INTO %s (%s) VALUES (%s)', $table, $keys, $vals); } return false; } // }}} // Data source name configuration $dsn = 'mysql://talkive:x@localhost/talkive'; // PEAR DB class require_once 'DB.php'; if (DB::isError($db = DB::connect($dsn))) { error($db); } $db->setFetchMode(DB_FETCHMODE_ASSOC); /* * Articles mogration */ $query = 'DELETE FROM ezin_articles WHERE section_id = 2'; if (DB::isError($res = $db->query($query))) { error($res); } $query = 'SELECT * FROM n_spravy'; if (DB::isError($res = $db->query($query))) { error($res); } while ($row = $res->fetchRow()) { echo "Processing ID $row[id]...\n"; $out = array( 'id' => null, // auto-increment 'author_id' => 1, 'section_id' => 2, 'bind_lang' => null, 'bind_theme' => null, 'accesses_count' => null, 'sessions_count' => null, 'message_count' => null, 'status' => 'showed', 'datetime' => '2005-10-30 20:56:01', 'hometext_wordcount' => 0, 'bodytext_wordcount' => 0, 'hometext_flags' => 'i', 'bodytext_flags' => 'i', 'access_key' => $row['obr'], 'template_key' => null, 'title' => $row['nadpis'], 'atitle' => null, 'hometext' => $row['text'], 'bodytext' => $row['alltext'] ); if (preg_match('/^(.*)\.(jpg|gif)$/', $out['access_key'], $matches)) { $out['access_key'] = $matches[1]; } if (preg_match('|^(\d{1,2})\.(\d{1,2})\.(\d{2,4}) (.*)$|', $out['title'], $matches)) { if (strlen($matches[3]) != 4) { if ($matches[3] == '05') { $matches[3] = '2005'; } else { die($matches[3]); } } $out['datetime'] = sprintf('%4d-%02d-%02d', $matches[3], $matches[2], $matches[1]); $out['title'] = $matches[4]; } foreach (array('hometext', 'bodytext') as $key) { $out[$key] = preg_replace('|\s*
\s*|', "\n\r\n\r", $out[$key]); } $query = createQuery('ezin_articles', $out); if (DB::isError($result = $db->query($query))) { error($result); } } /* * Newsletter migration */ $query = 'DELETE FROM ezin_nl_emails'; if (DB::isError($res = $db->query($query))) { error($res); } $query = 'SELECT * FROM newsletter'; if (DB::isError($res = $db->query($query))) { error($res); } while ($row = $res->fetchRow()) { $email = $row['email']; $email = trim($email); $email = strtolower($email); $ar = split("[ \t\n\r]", $email); $email = $ar[0]; if (! preg_match('|^.+@.+\..+$|', $email)) { echo "Skipping e-mail '$email'...\n"; continue; } echo "Processing e-mail '$email'...\n"; $out = array( 'id' => null, // auto-increment 'section_id' => null, 'email' => $email, 'dt_added' => null, 'ip' => '127.0.0.1', 'user_agent' => 'Platon/Migrator' ); $query = createQuery('ezin_nl_emails', $out); if (DB::isError($result = $db->query($query))) { error($result); } } $query = 'UPDATE ezin_nl_emails SET dt_added = SYSDATE()'; if (DB::isError($result = $db->query($query))) { error($result); } /* * Products migration */ $queries = array( 'DELETE FROM simple_categories', 'DELETE FROM simple_products', 'INSERT INTO simple_categories (category_id,parent_id,ord,highlight,name, created_by_username,changed_by_username,created_date,changed_date) VALUES(1,0,1,1,"Mobilne telefony","system","system",SYSDATE(),SYSDATE())', 'INSERT INTO simple_categories (category_id,parent_id,ord,highlight,name, created_by_username,changed_by_username,created_date,changed_date) VALUES(2,0,2,1,"PDA","system","system",SYSDATE(),SYSDATE())', ); foreach ($queries as $query) { if (DB::isError($res = $db->query($query))) { error($res); } } /* Phones */ $query = 'SELECT * FROM items'; if (DB::isError($res = $db->query($query))) { error($res); } $categories = array(); for ($category_id = 0; $row = $res->fetchRow(); ) { $row['znacka'] = ucfirst(strtolower(trim($row['znacka']))); if (in_array($row['znacka'], array_keys($categories))) { $category_id = $categories[$row['znacka']]; } else { echo "Creating category '$row[znacka]'...\n"; $out = array( 'category_id' => null, // auto-increment 'parent_id' => 1, 'name' => $row['znacka'], 'highlight' => 1, 'created_by' => 0, 'changed_by' => 0, 'created_by_username' => 'migrator', 'changed_by_username' => 'migrator', ); $query = createQuery('simple_categories', $out); if (DB::isError($result = $db->query($query))) { error($result); } $query = 'SELECT LAST_INSERT_ID()'; $category_id = $db->getOne($query); $categories[$row['znacka']] = $category_id; } echo "Processing item '$row[typ]'...\n"; $out = array( 'product_id' => null, // auto-increment 'category_id' => $category_id, 'name' => strlen($row['dokopy']) > 0 ? $row['dokopy'] : $row['typ'], 'highlight' => strlen($row['akcie']) > 0 ? 1 : 0, 'note1' => $row['display'], 'note2' => $row['java'], 'note3' => $row['foto'], 'note4' => $row['pasma'], 'note5' => $row['gprs'], 'note6' => $row['pamat'], 'note7' => $row['mms'], 'note8' => $row['bluetooth'], 'picture' => $row['obr'], 'price' => $row['cena'] * 1.19, 'created_by' => 0, 'changed_by' => 0, 'created_by_username' => 'migrator', 'changed_by_username' => 'migrator', ); $query = createQuery('simple_products', $out); if (DB::isError($result = $db->query($query))) { error($result); } } /* PDA */ $query = 'SELECT * FROM n_pda'; if (DB::isError($res = $db->query($query))) { error($res); } $categories = array(); for ($category_id = 0; $row = $res->fetchRow(); ) { $row['znacka'] = ucfirst(strtolower(trim($row['znacka']))); if (in_array($row['znacka'], array_keys($categories))) { $category_id = $categories[$row['znacka']]; } else { echo "Creating category '$row[znacka]'...\n"; $out = array( 'category_id' => null, // auto-increment 'parent_id' => 2, 'name' => $row['znacka'], 'highlight' => 1, 'created_by' => 0, 'changed_by' => 0, 'created_by_username' => 'migrator', 'changed_by_username' => 'migrator', ); $query = createQuery('simple_categories', $out); if (DB::isError($result = $db->query($query))) { error($result); } $query = 'SELECT LAST_INSERT_ID()'; $category_id = $db->getOne($query); $categories[$row['znacka']] = $category_id; } echo "Processing item '$row[typ]'...\n"; $out = array( 'product_id' => null, // auto-increment 'category_id' => $category_id, 'name' => strlen($row['dokopy']) > 0 ? $row['dokopy'] : $row['typ'], 'highlight' => strlen($row['akcie']) > 0 ? 1 : 0, 'note1' => $row['p1'], 'note2' => $row['p2'], 'note3' => $row['p3'], 'note4' => $row['p4'], 'note5' => $row['p5'], 'note6' => $row['p6'], 'note7' => $row['p7'], 'note8' => $row['p8'], 'picture' => $row['obr'], 'price' => $row['cena'], 'created_by' => 0, 'changed_by' => 0, 'created_by_username' => 'migrator', 'changed_by_username' => 'migrator', ); $query = createQuery('simple_products', $out); if (DB::isError($result = $db->query($query))) { error($result); } } /* Products finalization */ $queries = array( 'UPDATE simple_products SET created_date = SYSDATE() WHERE created_date IS NULL', 'UPDATE simple_products SET changed_date = SYSDATE() WHERE changed_date IS NULL', 'UPDATE simple_products SET ord = product_id WHERE ord IS NULL', 'UPDATE simple_categories SET created_date = SYSDATE() WHERE created_date IS NULL', 'UPDATE simple_categories SET changed_date = SYSDATE() WHERE changed_date IS NULL', 'UPDATE simple_categories SET ord = category_id WHERE ord IS NULL', ); foreach ($queries as $query) { if (DB::isError($res = $db->query($query))) { error($res); } } $db->disconnect(); /* Modeline for ViM {{{ * vim: set ts=4: * vim600: fdm=marker fdl=0 fdc=0: * }}} */ ?>