blog-app/seed.php

169 lines
4.2 KiB
PHP
Raw Permalink Normal View History

2021-09-16 20:27:51 +08:00
<?php
require 'vendor/autoload.php';
const NUM_OF_COMMENTS = 200;
const NUM_OF_POSTS = 100;
const NUM_OF_TAGS_CANDIDATE = 30;
const NUM_OF_USERS = 10;
const USER_PASSWORD = '1234';
$config = (new Core\Config)->get('database');
$db = new Core\Database\QueryBuilder(
new PDO(
"{$config['connection']};charset=utf8",
$config['username'],
$config['password'],
$config['options']
)
);
$schema = file_get_contents('schema.sql');
$sql = $db->prepare($schema);
$sql->execute();
$faker = Faker\Factory::create();
$rows = [];
$holders = [];
$usernames = [];
for ($i = 0; $i < NUM_OF_USERS; $i += 1) {
array_push($usernames, $faker->unique()->userName);
}
for ($i = 0; $i < NUM_OF_USERS; $i += 1) {
array_push(
$rows,
$usernames[$i],
$faker->unique()->freeEmail,
password_hash(USER_PASSWORD, PASSWORD_DEFAULT)
);
array_push($holders, '(NULL,?,?,?)');
}
$sql = $db->prepare('INSERT INTO users VALUES ' . implode(', ', $holders));
$sql->execute($rows);
$rows = [];
$holders = [];
$datetimes = [];
$from = NUM_OF_POSTS + 3;
for ($i = 0; $i < NUM_OF_POSTS; $i += 1) {
array_push(
$datetimes,
$faker->unique()
->dateTimeBetween("-{$from} days", '-3 days')
->format('Y-m-d H:i:s')
);
}
sort($datetimes);
$mh = curl_multi_init();
for ($i = 0; $i < NUM_OF_POSTS; $i += 1) {
$fetchURL = 'https://jaspervdj.be/lorem-markdownum/markdown.txt';
$multiCurl[$i] = curl_init();
curl_setopt($multiCurl[$i], CURLOPT_URL, $fetchURL);
curl_setopt($multiCurl[$i], CURLOPT_HEADER, 0);
curl_setopt($multiCurl[$i], CURLOPT_RETURNTRANSFER, 1);
curl_multi_add_handle($mh, $multiCurl[$i]);
}
$index = null;
do {
curl_multi_exec($mh, $index);
} while ($index > 0);
for ($i = 0; $i < NUM_OF_POSTS; $i += 1) {
$r = $faker->numberBetween(0, 9);
array_push(
$rows,
implode(' ', array_slice(explode(' ', $faker->unique()->realText()), 0, 6)),
curl_multi_getcontent($multiCurl[$i]),
$usernames[$r],
$datetimes[$i],
$datetimes[$i]
);
array_push($holders, '(NULL,?,?,?,?,?)');
curl_multi_remove_handle($mh, $multiCurl[$i]);
}
curl_multi_close($mh);
$sql = $db->prepare('INSERT INTO posts VALUES ' . implode(', ', $holders));
$sql->execute($rows);
$rows = [];
$holders = [];
$cdatetimes = [];
$cposts = [];
for ($i = 0; $i < NUM_OF_COMMENTS; $i += 1) {
$p = $faker->numberBetween(0, NUM_OF_POSTS - 1);
$d = $faker->numberBetween(0, 1);
$h = $faker->numberBetween(0, 23);
$m = $faker->numberBetween(0, 59);
$s = $faker->numberBetween(0, 59);
array_push(
$cdatetimes,
date_create_from_format('Y-m-d H:i:s', $datetimes[$p])
->add(new DateInterval("P{$d}DT{$h}H{$m}M{$s}S"))
->format('Y-m-d H:i:s')
);
array_push($cposts, $p + 1);
}
array_multisort($cdatetimes, $cposts);
for ($i = 0; $i < NUM_OF_COMMENTS; $i += 1) {
$r = $faker->numberBetween(0, NUM_OF_USERS - 1);
array_push(
$rows,
$faker->unique()->realText(100, 1),
$usernames[$r],
$cdatetimes[$i],
$cdatetimes[$i],
$cposts[$i]
);
array_push($holders, '(NULL,?,?,?,?,?)');
}
$sql = $db->prepare('INSERT INTO comments VALUES ' . implode(', ', $holders));
$sql->execute($rows);
$rows = [];
$holders = [];
$tags = array_values(array_unique($faker->words(NUM_OF_TAGS_CANDIDATE)));
$num_of_tags = count($tags);
for ($i = 0; $i < $num_of_tags; $i += 1) {
array_push($rows, null, $tags[$i]);
array_push($holders, '(?,?)');
}
$sql = $db->prepare('INSERT INTO tags VALUES ' . implode(', ', $holders));
$sql->execute($rows);
$rows = [];
$holders = [];
$links = [];
for ($i = 0; $i < 4 * NUM_OF_POSTS; $i += 1) {
$p = $faker->numberBetween(1, NUM_OF_POSTS);
$t = $faker->numberBetween(1, $num_of_tags);
array_push($links, [$p, $t]);
}
$rows = array_values(array_unique($links, SORT_REGULAR));
sort($rows);
for ($i = 0; $i < count($rows); $i += 1) {
array_push($holders, '(?,?)');
}
$sql = $db->prepare('INSERT INTO post_tag VALUES ' . implode(', ', $holders));
$sql->execute(call_user_func_array('array_merge', $rows));
echo "Done!\n";