<?php include_once '../lib/util.php'; $db = new DBAdmin(); $db->truncate('textdata'); echo 'Updating `textdata` table ... '; $q = $db->query('SELECT `id`, `text` FROM `raw`'); $db->begin(); while ($row = $q->fetch_assoc()) { /* CRLFをLFに統一 */ $text = str_replace("\r\n", "\n", $row['text']); /* それぞれの項目に区切る */ $paragraphs = preg_split('/\\-{50}\\r?\\n(?=【.*?】\\r?\\n\\-{50}\\r?\\n)/', $text); array_shift($paragraphs); $data = array(); foreach ($paragraphs as $i => $paragraph) { /* keyとvalueを取り出す */ $pattern = '/^【(.*?)】\\n(?:\\-{50}\\n\\s*([\\s\\S]*?))?\\s*$/'; preg_match($pattern, $paragraph, $matches); if (isset($matches[1]) === FALSE) { $matches[1] = ''; } if (isset($matches[2]) === FALSE) { $matches[2] = NULL; } list(, $key, $value) = $matches; if (!$key && !$value) { continue; } /* 英数字を半角に、カタカナを全角に変換 */ $key = trim(kana($key));
<?php include_once '../lib/util.php'; $db = new DBAdmin(); $db->truncate('schedule'); echo 'Updating `schedule` table ... '; $q = $db->selectAll('htmldata'); $db->begin(); while ($row = $q->fetch_assoc()) { $schedules = trim(kana($row['schedule'])); if ($schedules === '') { $db->insert('schedule', array('id' => $row['id'])); } foreach (explode(' ', $schedules) as $schedule) { if ($schedule) { preg_match('/^(?:(集)|(日|月|火|水|木|金|土))?(?:(?:(\\d+)|(不定))' . '(後)?(?:~(\\d+)(前)?)?)?$/', $schedule, $m); if ($m) { $m = array_pad($m, 8, 0); for ($i = (int) $m[3]; $i <= max((int) $m[6], (int) $m[3]); $i++) { $data = array('id' => $row['id'], 'day' => $m[2] ? mb_strpos('日月火水木金土', $m[2]) : NULL, 'period' => $i ? $i : NULL, 'early' => $i == $m[6] && (bool) $m[7], 'late' => $i == $m[3] && (bool) $m[5], 'intensive' => (bool) $m[1], 'irregular' => (bool) $m[4], 'description' => $schedule); $db->insert('schedule', $data); } } else { echo "{$schedule}\n"; } } } echo "[31G[K{$row['id']}"; } $db->commit(); $db->close();
<?php include_once '../lib/util.php'; $db = new DBAdmin(); $db->truncate('room'); $db->truncate('classroom'); echo 'Updating `classroom` table ... '; $q = $db->query('SELECT `list`.`id`, `list`.`place`, `htmldata`.`classroom` FROM `list` JOIN `htmldata` ON `list`.`id` = `htmldata`.`id`'); $db->begin(); while ($row = $q->fetch_assoc()) { $place = kana($row['place']); $rooms = trim(kana($row['classroom'])); $did = $db->single('SELECT `department_id` FROM `department` WHERE `name` = ?', $place); if (is_null($did)) { if ($place) { $did = $db->single('SELECT `department_id` FROM `department` WHERE ' . implode(' AND ', array_map(function ($s) use($db) { return "`name` LIKE '%" . $db->escape($s) . "%'"; }, preg_split('/\\(|\\)/', $place)))); } } foreach (explode(' ', $rooms) as $room) { if (is_null($did) && !$room) { continue; } $db->insert('room', array('department_id' => $did, 'name' => $room)); $did = is_null($did) ? 'IS NULL' : "= '" . $db->escape($did) . "'"; $rid = $db->single("SELECT `room_id` FROM `room`\n\t\t\tWHERE `department_id` {$did} AND `name` = ?", $room); $db->insert('classroom', array('id' => $row['id'], 'room_id' => $rid)); }