#!/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/amrophever/amrophever-import.php,v 1.6 2007-09-23 23:15:47 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($ar, $row) // {{{ { global $db; $query = sprintf('SELECT id FROM ezin_articles WHERE access_key LIKE "ajg-%d-%d%%"', $row['id_clanok'], $row['language']); $id = $db->getOne($query); if (DB::isError($id)) { error($id); } $id = intval($id); if ($id > 0) { echo ' '; return $id; } echo ' '; $query = sprintf('SELECT id FROM ezin_articles' .' WHERE datetime = %s AND section_id = %d' .' AND (hometext_wordcount = %d AND bodytext_wordcount = %d)', //$db->quote(substr($ar['title'], 0, 5).'%'), $db->quote($ar['datetime']), $db->quote($ar['section_id']), //$db->quote($ar['author_id']), $db->quote($ar['hometext_wordcount']), $db->quote($ar['bodytext_wordcount']) ); $id = $db->getOne($query); if (DB::isError($id)) { error($id); } $id = intval($id); if ($id == 0 && $row['url']) { $query = sprintf('SELECT id FROM ezin_articles WHERE hometext LIKE %s', $db->quote('%'.$row['url'].'%')); $id = $db->getOne($query); if (DB::isError($id)) { error($id); } $id = intval($id); if ($id > 0) { echo " get-from-URL($row[url]): "; } } return $id; } // }}} function get_author($ar) // {{{ { global $db; if (strlen(trim($ar['autor'])) <= 0) { return 1; } while (($id = $db->getOne( sprintf('SELECT id FROM ezin_authors WHERE name LIKE %s', $db->quote($ar['autor'])))) <= 0) { $query = sprintf('INSERT INTO ezin_authors (name) VALUES (%s)', $db->quote($ar['autor'])); $db->query($query); } return $id; } // }}} function get_section($ar) // {{{ { $ret = -1; if ($ar['language'] == 1) { if (intval($ar['sekcia2']) > 0) { switch ($ar['sekcia2']) { case 1: $ret = 99; break; case 2: $ret = 102; break; case 3: $ret = 103; break; case 4: $ret = 106; break; default: die('wrong sekcia2: '.$ar['sekcia2']); } } else { switch ($ar['sekcia1']) { case 1: $ret = 20; break; case 2: $ret = 21; break; case 3: $ret = 98; break; default: die('wrong sekcia1: '.$ar['sekcia1']); } } } else if ($ar['language'] == 2) { if (intval($ar['sekcia2']) > 0) { switch ($ar['sekcia2']) { case 1: $ret = 100; break; case 2: $ret = 101; break; case 3: $ret = 104; break; case 4: $ret = 105; break; default: die('wrong sekcia2: '.$ar['sekcia2']); } } else { switch ($ar['sekcia1']) { case 1: $ret = 63; break; case 2: $ret = 64; break; case 3: $ret = 65; break; default: die('wrong sekcia1: '.$ar['sekcia1']); } } } else { die('wrong language'); } return $ret; } // }}} function get_access_key($row) // {{{ { return 'ajg-'.intval($row['id_clanok']) .'-'.intval($row['language']) .'-'.intval($row['sekcia1']) .'-'.intval($row['sekcia2']) .'-'.intval($row['sektor']); } // }}} // Data source name configuration $dsn = 'mysql://nepto_amrophever:x@localhost/nepto_amrophever'; // 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 ezin_cache'; if (DB::isError($res = $db->query($query))) { error($res); } /* $query = 'DELETE FROM ezin_authors WHERE id > 1'; if (DB::isError($res = $db->query($query))) { error($res); } */ /* * Articles migration */ $ids = array(); $idc = 0; $max = $db->getOne('SELECT MAX(id) FROM ezin_articles'); //echo "max = $max\n";exit; $query = 'SELECT * FROM clanky WHERE sekcia1 > 0'; if (DB::isError($res = $db->query($query))) { error($res); } while ($row = $res->fetchRow()) { echo "Processing ID $row[id_clanok]... "; if ($row['zobrazit'] != 1) { echo "\nWARNING: zobrazit != 1 - skipping\n"; continue; } $dual_lang = false; if ($row['language'] == 3) { // dual language article $row['language'] = 1; $dual_lang = true; } $ar['autor'] = trim($ar['autor']); $ar['autor'] = str_replace("\n", ' ', $ar['autor']); $ar['autor'] = str_replace("\r", ' ', $ar['autor']); $ar['autor'] = preg_replace('|\s*|mi', ' ', $ar['autor']); $out = array( //'id' => 1000+$row['id_clanok'], 'id' => null, // auto-increment 'author_id' => get_author($row), 'section_id' => get_section($row), 'bind_lang' => null, 'bind_theme' => null, 'accesses_count' => 0, 'sessions_count' => 0, 'message_count' => 0, 'status' => 'showed', 'datetime' => $row['datum'], 'hometext_wordcount' => str_word_count($row['anotacia']), 'bodytext_wordcount' => str_word_count($row['text']), 'hometext_flags' => 'iER', 'bodytext_flags' => 'iER', 'access_key' => get_access_key($row), 'template_key' => null, 'title' => $row['nadpis'], 'atitle' => null, 'hometext' => trim($row['anotacia']), 'bodytext' => trim($row['text']) ); if (strlen($row['url']) > 0) { //$out['bodytext'] = $out['hometext']; if (strlen(trim($out['bodytext'])) > 0) { $out['bodytext'] .= "\n\r\n\r$row[url]"; } else { $out['hometext'] .= "
\n\r"; $out['hometext'] .= sprintf('%s ', str_replace(' ', '+', trim($row['url'])), intval($row['pdf_velkost']) > 0 ? sprintf(' title="PDF~%d~kB"', $row['pdf_velkost']) : '', ($row['language'] == 1 ? 'čítať ďalej...' : 'read more...')); } } $out['hometext'] = preg_replace('||mi', '', $out['hometext']); $out['bodytext'] = preg_replace('|\s*
\s*|', "\n\r\n\r", $out[$key]); $out[$key] = preg_replace('|href="/|', 'target="_blank" href="http://www.jenewein.sk/', $out[$key]); $out[$key] = preg_replace('|
|', '', $out[$key]); } $out['hometext_wordcount'] = str_word_count($row['anotacia']); $out['bodytext_wordcount'] = str_word_count($row['text']); echo 'LOOKUP for ID ...'; $id = lookup_id($out, $row); echo "$id\n"; if ($id > 0) { if (in_array($id, $ids)) { echo " - already IN ARRAY!\n"; } else { array_push($ids, $id); $out['id'] = $id; } } elseif ($id == 0) { $idc++; } $query = createQuery('ezin_articles', $out); if (1 && DB::isError($result = $db->query($query))) { error($result); } if ($dual_lang) { $row['language'] = 2; $out['section_id'] = get_section($row); $out['access_key'] = get_access_key($row); printf(" dualang, reins:%03s ", $out['section_id']); echo 'LOOKUP for ID ...'; $id = lookup_id($out, $row); echo "$id\n"; if ($id > 0) { if (in_array($id, $ids)) { echo " - already IN ARRAY!\n"; } else { array_push($ids, $id); $out['id'] = $id; } } elseif ($id == 0) { $idc++; } $query = createQuery('ezin_articles', $out); if (1 && DB::isError($result = $db->query($query))) { error($result); } } } $query = 'DELETE FROM ezin_articles' .' WHERE section_id IN (20,21,63,64,100,99,102,101,104,103,106,105, 98, 65)' .' AND datetime IS NOT NULL AND datetime > "0000-00-00"' .' AND id NOT IN ('.join(',', $ids).')' .' AND id <= '.$max; // protected 97,22,98,65 echo "----------\nRUN THIS:\n$query\nidc = $idc\n"; if (0 && DB::isError($res = $db->query($query))) { error($res); } $db->disconnect(); /* Modeline for ViM {{{ * vim: set ts=4: * vim600: fdm=marker fdl=0 fdc=0: * }}} */ ?>