* Copyright (c) 2004 Platon SDG, http://platon.sk/ * Licensed under terms of GNU General Public License. * All rights reserved. * * Changelog: * 2004-12-31 - created * */ /* $Platon$ */ if ($argc < 2) { echo "Usage: php diary-import.php \n"; exit; } $lines = @file($argv[1]); if ($lines == false) { die("file does not exist\n"); } function my_strtolower(&$val, $key) /* {{{ */ { // $key ignored $val = strtolower(trim($val)); } /* }}} */ function output_insert($date, $par, $text) /* {{{ */ { if (strlen($text) <= 0) { echo "-- warning: empty \$text\n"; return; } echo 'INSERT INTO diary_texts (day, par, text) VALUES '; printf('("%d-%02d-%02d", %d, "%s");', $date['year'], $date['month'], $date['day'], $par, addslashes($text)); echo "\n"; } /* }}} */ function create_word_index($date = array(), $par = 0, $text = '') /* {{{ */ { static $words = array(0=>''); static $index = array(''=>0); if (count($date) == 0) { unset($words[0]); foreach ($words as $id => $word) { echo 'INSERT INTO diary_words (id, word) VALUES '; printf('(%d, "%s");', $id, substr($word, 1)); echo "\n"; } unset($index['']); foreach ($index as $word => $id) { echo 'INSERT INTO diary_words_2 (id, word) VALUES '; printf('(%d, "%s");', $id, substr($word, 1)); echo "\n"; } } $ar = split('[^a-zA-Z0-9_]', $text); if (array_walk($ar, 'my_strtolower') == false) { die("array_walk() failure\n"); } $ar = array_unique($ar); foreach ($ar as $word) { if (strlen($word) <= 3) { continue; } $word2 = "X$word"; if (! in_array($word2, $words, 1)) { $words[] = $word2; $index_no = array_search($word2, $words, 1); $index[$word2] = $index_no; } else { $index_no = $index[$word2]; } if ($index_no <= 0) { echo "[$word][$word2]\n"; var_dump($words); die(); } echo 'INSERT INTO diary_index (day, par, word_id) VALUES '; printf('("%d-%02d-%02d", %d, %d);', $date['year'], $date['month'], $date['day'], $par, $index_no); echo "\n"; } } /* }}} */ $date = array( 'day' => null, 'month' => null, 'year' => null ); for ($i = 0; $i < count($lines); $i++) { $line = trim($lines[$i]); if (strlen($line) <= 0) { continue; } if (preg_match('/^(\d\d)\/(\d\d)\/(\d\d\d\d).*$/', $line, $matches)) { output_insert($date, $par_cur, $text); create_word_index($date, $par_cur, $text); $date = array( 'day' => $matches[1], 'month' => $matches[2], 'year' => $matches[3] ); //printf("\n%d-%02d-%02d: ", $date['year'], $date['month'], $date['day']); $i++; // to skip underline $par_old = 0; $par_cur = 0; $text = ''; } else if (preg_match('/^(\d\d?)\. (.*)$/', $line, $matches)) { output_insert($date, $par_cur, $text); create_word_index($date, $par_cur, $text); $par_old = $par_cur; $par_cur = $matches[1]; $text = $matches[2]; $status = $par_cur == $par_old + 1 ? 'OK' : 'ERR'; //echo "[$par_cur:$status]"; } else { $text .= ' '; $text .= $line; } } //echo "\n"; output_insert($date, $par_cur, $text); create_word_index($date, $par_cur, $text); create_word_index(); /* Modeline for ViM {{{ * vim: set ts=4: * vim600: fdm=marker fdl=0 fdc=0: * }}} */ ?>