#!/usr/bin/php -q * Copyright (c) 2007 Platon Group, http://platon.sk/ * Licensed under terms of GNU General Public License. * All rights reserved. * */ /* $Platon: scripts/php/importers/energysi/energysi-import.php,v 1.1 2007-11-13 23:22:18 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('REPLACE INTO %s (%s) VALUES (%s)', $table, $keys, $vals); } return false; } // }}} function lookup_id($row) // {{{ { global $db; $query = sprintf('SELECT id FROM publicpi_energy2.ezin_articles WHERE access_key LIKE "esi-%d-%s%%"', $row['id_art'], $row['lang']); $id = $db->getOne($query); if (DB::isError($id)) { error($id); } $id = intval($id); return $id; } // }}} function get_section($id_art) // {{{ { global $db; static $map_ar = array( '100' /* Energy News */ => 22, '108' /* Slovak Energy Forum */ => 36, '10' /* About ESI */ => 23, '105' /* ESI Board */ => 31, '107' /* Energy Forums */ => 34, '20' /* Governance & Expertise */ => 35, '106' /* Energy Studies */ => 33, '104' /* Energy Events */ => 24, '30' /* ESI Initiatives */ => 25, '50' /* ESI Insight */ => 26, '60' /* Media & Publications */ => 27, '70' /* Partners */ => 28, '80' /* Annual Report */ => 29, '90' /* Contact */ => 30, '103' /* ESI Mission */ => 32, '31' /* Energy Security */ => 37, '32' /* Liberalization & Consequences */ => 38, '33' /* Transatlantic Energy Dialogue */ => 39, '34' /* Central Asia Stability */ => 40 ); $id_sec = $db->getOne('SELECT id_sec FROM sec_art WHERE id_art = '.intval($id_art)); if (isset($map_ar[$id_sec])) { return intval($map_ar[$id_sec]); } echo('WARNING - cannot find section:'.$id_sec); return 22; /* default */ } // }}} function get_access_key($row) // {{{ { return 'esi-'.intval($row['id_art']).'-'.$row['lang'].'-'.$row['show']; } // }}} // Data source name configuration $dsn = 'mysql://publicpi_energy:kofola@localhost/publicpi_energy'; // PEAR DB class require_once 'DB.php'; if (DB::isError($db = DB::connect($dsn))) { error($db); } $db->setFetchMode(DB_FETCHMODE_ASSOC); /* * Cleanup */ $query = 'DELETE FROM publicpi_energy2.ezin_cache'; if (DB::isError($res = $db->query($query))) { error($res); } /* * Articles migration */ $ids = array(); $idc = 0; $max = $db->getOne('SELECT MAX(id) FROM publicpi_energy2.ezin_articles'); //echo "max = $max\n";exit; $query = 'SELECT * FROM article'; if (DB::isError($res = $db->query($query))) { error($res); } while ($row = $res->fetchRow()) { echo "Processing ID $row[id_art]... "; $out = array( 'id' => lookup_id($row), 'author_id' => 1, /* admin */ 'section_id' => get_section($row['id_art']), 'bind_lang' => null, 'bind_theme' => null, 'accesses_count' => 0, 'sessions_count' => 0, 'message_count' => 0, 'status' => 'showed', 'datetime' => $row['date'], 'hometext_wordcount' => str_word_count($row['sh_text']), 'bodytext_wordcount' => str_word_count($row['full_text']), 'hometext_flags' => 'iER', 'bodytext_flags' => 'iER', 'access_key' => get_access_key($row), 'template_key' => null, 'title' => trim($row['subject']), 'atitle' => null, 'hometext' => trim($row['sh_text']), 'bodytext' => trim($row['full_text']), 'created_by' => 'convertor', 'created_dt' => '2007-11-17' ); foreach (array('hometext', 'bodytext') as $key) { $out[$key] = preg_replace('|]*-->|mi', '', $out[$key]); // comments removal $out[$key] = preg_replace('|

|mi', '', $out[$key]); $out[$key] = preg_replace('||mi', '', $out[$key]); $out[$key] = preg_replace('|]*>|mi', '', $out[$key]); $out[$key] = preg_replace('||mi', '', $out[$key]); $out[$key] = preg_replace('|]*>|mi', '', $out[$key]); $out[$key] = preg_replace('|]*>|mi', '', $out[$key]); $out[$key] = preg_replace('|]*>|mi', '', $out[$key]); $out[$key] = preg_replace('|]*>|mi', '', $out[$key]); $out[$key] = preg_replace('|]*>|mi', '', $out[$key]); $out[$key] = preg_replace('|]*>\s*|mi', '', $out[$key]); $out[$key] = preg_replace('|\s*]*>\s*|mi', "
", $out[$key]); $out[$key] = preg_replace('|\s*
(\s*
\s*)+|mi', "

", $out[$key]); $out[$key] = preg_replace('|\s*]*>\s*|mi', "

", $out[$key]); $out[$key] = preg_replace('|(

)+|', '

', $out[$key]); $out[$key] = preg_replace('/((
|

|~)?\s*)+$/mi', '', $out[$key]); $out[$key] = preg_replace('/^((
|

|~)?\s*)+/mi', '', $out[$key]); } if (strlen($out['bodytext']) <= 0) { $out['bodytext'] = $out['hometext']; $out['hometext'] = ''; } $out['hometext_wordcount'] = str_word_count($out['hometext']); $out['bodytext_wordcount'] = str_word_count($out['bodytext']); echo "\n"; $query = createQuery('publicpi_energy2.ezin_articles', $out); if (DB::isError($result = $db->query($query))) { error($result); } } $query = 'UPDATE publicpi_energy2.ezin_articles SET hometext = REPLACE(hometext, "

", "\r\n\r\n"), bodytext = REPLACE(bodytext, "

", "\r\n\r\n") WHERE created_by = "convertor" AND created_dt >= "2007-11-17"'; 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: * }}} */ ?>