3 if (!defined('__DIR__')) {
4 define('__DIR__', dirname(__FILE__));
7 // Create database schema
8 echo 'Creating database', PHP_EOL;
9 $file = __DIR__ . '/cookie.db';
10 if (file_exists($file)) {
13 $db = new PDO('sqlite:' . $file);
14 $db->exec('CREATE TABLE cookies (name VARCHAR(255), link VARCHAR(255))');
15 $db->exec('CREATE UNIQUE INDEX cookie_name ON cookies (name)');
16 $insert = $db->prepare('INSERT INTO cookies (name, link) VALUES (:name, :link)');
18 // Get Cookies list from http://en.wikipedia.org/wiki/List_of_cookies
19 echo 'Downloading data from Wikipedia', PHP_EOL;
20 $file = __DIR__ . '/cookieslist.txt';
21 if (!file_exists($file)) {
22 copy('http://en.wikipedia.org/wiki/List_of_cookies', $file);
24 $contents = file_get_contents($file);
26 // Extract data from data set
27 echo 'Processing Wikipedia\'s cookies list', PHP_EOL;
28 $contents = tidy_repair_string($contents);
29 libxml_use_internal_errors(true);
30 $doc = new DOMDocument;
31 $doc->loadHTML($contents);
32 libxml_clear_errors();
33 $xpath = new DOMXPath($doc);
35 $cookies = $xpath->query('//table[@width="90%"]/tr/td[1]/a');
37 foreach ($cookies as $cookie) {
38 $name = $cookie->textContent;
40 array('(',')',"\n", 'cookies'),
41 array('','', ' ', 'cookie'),
44 $name = iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $name);
46 $name = rtrim($name, 's');
48 $link = 'http://en.wikipedia.org' . $cookie->getAttribute('href');
49 $insert->execute(array($name, $link));
50 echo 'added [' . $name . '] -> '. $link . PHP_EOL;
54 echo 'Cleaning up', PHP_EOL;