public function dump() { $columns = array(); foreach (ColumnGroup::search(1) as $columngroup) { $columns[$columngroup->id] = $columngroup->name; } $delta = 10000000; for ($i = 0; $i * $delta < 99999999; $i++) { $start = $i * $delta; $end = $start + $delta - 1; $tmpname1 = tempnam('', ''); $tmpname2 = tempnam('', ''); $file_name1 = 'files/' . str_pad($i * $delta, 8, '0', STR_PAD_LEFT) . '.json.gz'; $file_name2 = 'files/bussiness-' . str_pad($i * $delta, 8, '0', STR_PAD_LEFT) . '.json.gz'; $fp[1] = $fp[3] = gzopen($tmpname1, 'w'); $fp[2] = gzopen($tmpname2, 'w'); $unit_id = null; $unit = new StdClass(); foreach (UnitData::search("`id` >= {$start} AND `id` <= {$end}")->order("`id`, `column_id`")->volumemode(10000) as $unit_data) { if (!is_null($unit_id) and $unit_data->id != $unit_id) { fwrite($fp[$this->getType($unit_id)], str_pad($unit_id, 8, '0', STR_PAD_LEFT) . ',' . json_encode($unit, JSON_UNESCAPED_UNICODE) . "\n"); $unit = new StdClass(); } $unit_id = $unit_data->id; $unit->{$columns[$unit_data->column_id]} = json_decode($unit_data->value); } if (!is_null($unit_id)) { fwrite($fp[$this->getType($unit_id)], str_pad($unit_id, 8, '0', STR_PAD_LEFT) . ',' . json_encode($unit, JSON_UNESCAPED_UNICODE) . "\n"); } fclose($fp[1]); fclose($fp[2]); DropboxLib::putFile($tmpname1, $file_name1); DropboxLib::putFile($tmpname2, $file_name2); unlink($tmpname1); unlink($tmpname2); } }
} $month_start = mktime(0, 0, 0, date('m', $min_time), 1, date('Y', $min_time)); $month_end = strtotime('+1 month', $month_start); $filename = 'ptthot-' . date('Ym', $month_start) . '.csv.gz'; $temp = tmpfile(); $meta_data = stream_get_meta_data($temp); $tmp_filename = $meta_data['uri']; $stream = gzopen($tmp_filename, 'w'); fwrite($stream, "#board,timestamp,count\n"); foreach (RankData::search("`time` >= {$month_start} AND `time` < {$month_end}")->order(array('board', 'time'))->volumemode(10000) as $rankdata) { $last_time = max($last_time, $rankdata->time); fwrite($stream, "{$rankdata->board},{$rankdata->time},{$rankdata->count}\n"); } fclose($stream); DropboxLib::putFile($tmp_filename, $filename); fclose($temp); KeyValue::set('snapshot_at', $last_time); } //dump titlehistory $filename = 'ptthot-title.csv.gz'; $temp = tmpfile(); $meta_data = stream_get_meta_data($temp); $tmp_filename = $meta_data['uri']; $stream = gzopen($tmp_filename, 'w'); fwrite($stream, "#board,timestamp,title\n"); foreach (TitleHistory::search(1)->order(array("board", "title"))->volumemode(10000) as $titlehistory) { fputcsv($stream, array($titlehistory->board, $titlehistory->time, $titlehistory->title)); } fclose($stream); DropboxLib::putFile($tmp_filename, $filename); fclose($temp);
$unit_names[$unitdata->id] = $unitdata->value; } // 2 - 商業登記 foreach (UnitData::search(array('column_id' => 33))->searchIn('id', $units[2]) as $unitdata) { $unit_names[$unitdata->id] = $unitdata->value; } // 3 - 分公司 foreach (Unit::search(1)->searchIn('id', $units[3]) as $unit) { $unit_names[$unit->id] = json_encode($unit->name()); } // 4 - 教育部, 99 - 其他 foreach (UnitData::search(array('column_id' => 43))->searchIn('id', array_merge($units[4], $units[99])) as $unitdata) { $unit_names[$unitdata->id] = $unitdata->value; } foreach ($unit_names as $id => $value) { $rows = array(); $rows[] = str_pad($id, 8, '0', STR_PAD_LEFT); $rows[] = $type_names[$types[$id]]; $v = json_decode($value); if (is_array($v)) { $rows[] = $v[0]; } else { $rows[] = strval($v); } fputcsv($fp, $rows); } $units = $unit_names = $types = array(); } fclose($fp); DropboxLib::putFile($tmpname, $file_name); unlink($tmpname);