-
Notifications
You must be signed in to change notification settings - Fork 2
/
importdata.php
executable file
·69 lines (56 loc) · 1.5 KB
/
importdata.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
$db_host = '172.20.0.8';
$db_name = 'phpmyadmin';
$db_user = 'tech';
$db_pass = 'public';
$entries = 10000000;
$entry_words_min = 250;
$entry_words_max = 1000;
/*
End configuration
*/
function get_rand_word( $len_min, $len_max ) {
$word = '';
for ( $i = 0; $i < ( rand( 0, $len_max - $len_min ) + $len_min ); $i++ ) {
$word .= chr(rand(65, 90));
}
return $word;
}
function get_title() {
$title = '';
for ( $i = 0; $i < ( rand( 4, 10 ) ); $i++ ) {
$title .= get_rand_word( 2, 9 ) . ' ';
}
return $title;
}
function get_fulltext() {
$fulltext = '';
for ( $i = 0; $i < ( rand( 250, 500 ) ); $i++ ) {
$fulltext .= get_rand_word( 2, 9 ) . ' ';
}
return $fulltext;
}
$dsn = 'mysql:dbname=' . $db_name . ';host=' . $db_host;
try {
$dbh = new PDO($dsn, $db_user, $db_pass);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
die();
}
$dbh->query('CREATE TABLE IF NOT EXISTS `sphinx` (
`id` int(10) unsigned NOT NULL auto_increment,
`title` varchar(150) collate utf8_bin NOT NULL,
`fulltext` text collate utf8_bin NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin');
$sth = $dbh->prepare('INSERT INTO `sphinx` (`title`,`fulltext`) VALUES (:title, :fulltext)');
$counter = 0;
for ( $i = 0; $i < $entries; $i++ ) {
$sth->execute(array(
':title' => get_title(),
':fulltext' => get_fulltext()
));
$counter++;
}
echo $counter . ' rows inserted';
?>