#!/usr/bin/php -q
*
* $Id: konz-convert.php,v 1.2 2002/07/03 23:25:18 nepto Exp $
*
* Can be used as an example haw to work with SQL database via PEAR DB class
* in normal script runned from shell.
*/
// Data source name configuration
$dsn = 'mysql://konzervat:x@localhost/konzervativizmus';
$table = 'konz_import';
$data_field = '__DATA';
// Command line arguments test :-)
if ($argc < 1) {
echo "Usage: ./konz_import.php\n";
die();
}
function string_convert($str)
{
//$str = str_replace('ź', '·', $str);
$str = str_replace('ź', '
', $str);
//$str = str_replace('„', '„', $str);
//$str = str_replace('“', '“', $str);
$str = str_replace('„', '"', $str);
$str = str_replace('“', '"', $str);
$str = str_replace('”', '"', $str);
$str = str_replace('–', '–', $str);
$str = str_replace('…', '.', $str);
$str = str_replace('‘', '’', $str);
$str = str_replace('¬', 'é', $str);
$str = str_replace(" \r\n", "
\n", $str);
$str = str_replace("\r\n", "\r\n\r\n", $str);
return $str;
}
// PEAR DB class
require_once 'DB.php';
require_once 'error.inc.php';
if (DB::isError($db = DB::connect($dsn))) {
error($db);
}
$db->setFetchMode(DB_FETCHMODE_ASSOC);
$query = sprintf('DELETE FROM %s', 'ezin_articles');
if (DB::isError($res = $db->query($query))) {
error($res);
}
$query = sprintf('DELETE FROM %s', 'ezin_authors');
if (DB::isError($res = $db->query($query))) {
error($res);
}
$query = sprintf('SELECT * FROM %s', $table);
if (DB::isError($res = $db->query($query))) {
error($res);
}
$authors = array('', 'Admin');
while ($res->fetchInto($row)) {
$rec = array();
$notice = 0;
// Texts
$rec['hometext'] = $row['cAnotacia'];
$rec['bodytext'] = isset($row['cAnotaciaSwitch'])
? $row['cAnotacia'] . "\n\r\n\r" : "";
$rec['bodytext'] .= $row['__DATA'];
$rec['bodytext'] = preg_replace('/([\w]+@([\w]+\.)+[\w]{2,3})/',
'$1', $rec['bodytext']);
$rec['bodytext'] = preg_replace('/(www\.[\w\.\/]+)/',
'$1', $rec['bodytext']);
if (preg_match('/([\w]+@([\w]+\.)+[\w]{2,3})/', $rec['bodytext'])
|| preg_match('/(www\.[\w\.\/]+)/', $rec['bodytext']))
$notice = 0 /* 1 */;
// Titles
if (isset($row['cTitulok_1'])) {
$rec['atitle'] = string_convert($row['cTitulok']);
$rec['title'] = string_convert($row['cTitulok_1']);
} else {
$rec['title'] = string_convert($row['cTitulok']);
}
// Dates
$date = (!empty($row['cDatum']) ? $row['cDatum'] : $row['cDatFirst']);
$dates = explode('.', $date);
$rec['datetime'] = $dates[2].'-'.$dates[1].'-'.$dates[0];
// Author ID
if (! isset($row['cZdroj'])) {
$author_id = 1;
} else {
$author_id = array_search($row['cZdroj'], $authors);
if ($author_id == false) {
$author_id = count($authors);
$authors[] = $row['cZdroj'];
}
}
// Section ID
$rec['bodytext'] = '' . $rec['bodytext'];
switch ($row['cKategoria']) {
default: $section_id = 1; break;
case 'Editor': $section_id = 1; break;
case 'Informácie\Archív': $section_id = 13; break;
case 'Informácie\Linky': $section_id = 10; break;
case 'Informácie\Literatúra': $section_id = 11; break;
case 'Informácie\Profily': $section_id = 12; break;
case 'Inątitút': $section_id = 9; break;
case 'Kalendár': $section_id = 9; break;
case 'Kalendár,Inątitút': $section_id = 9; break;
case 'Komentáre a úvahy': $section_id = 2; break;
case 'Komentáre a úvahy,Informácie\Literatúra': $section_id = 11; break;
case 'Komentáre a úvahy,Parlament': $section_id = 8; break;
case 'Komentáre a úvahy,Texty': $section_id = 4; break;
case 'Komentáre a úvahy,Zahraničie': $section_id = 6; break;
case 'Parlament': $section_id = 8; break;
case 'Spravodajstvo':
switch ($row['cPodkategoria']) {
case 'Z vlády': $section_id = 7; break;
case 'Z parlamentu': $section_id = 8; break;
default: $section_id = 3;break; /* invalid! */
}
break;
case 'Spravodajstvo,Zahraničie,Parlament': $section_id = 6; break;
case 'Spravodajstvo,Zahraničie,Vláda': $section_id = 6; break;
case 'Texty': $section_id = 4; break;
case 'Vláda': $section_id = 7; break;
case 'Vláda,Kalendár': $section_id = 9; break;
case 'Zahraničie': $section_id = 6; break;
case 'Zahraničie': $section_id = 6; break;
}
$rec['author_id'] = $author_id;
$rec['section_id'] = $section_id;
// Conversions
$rec['hometext'] = string_convert($rec['hometext']);
$rec['bodytext'] = string_convert($rec['bodytext']);
// Notice
if ($notice) {
echo $row['cTitulok'] . " [" . $row['cKategoria'] . "]\n";
}
$cols = '';
$vals = '';
foreach ($rec + array('status' => 'normal') as $col => $val) {
if (empty($val))
continue;
if ($cols != '')
$cols .= ', ';
if ($vals != '')
$vals .= ', ';
$cols .= $col;
$vals .= "'" . $db->quoteString($val) . "'";
}
$query = sprintf('INSERT INTO %s (%s) VALUES (%s)',
'ezin_articles', $cols, $vals);
if (DB::isError($res1 = $db->query($query))) {
error($res1);
}
}
foreach ($authors as $id => $name) {
if (empty($id))
continue;
$query = sprintf('INSERT INTO %s (id, name) VALUES (%s, %s)',
'ezin_authors', $db->quote($id), $db->quote($name));
if (DB::isError($res1 = $db->query($query))) {
error($res1);
}
}
$res->free();
?>