]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Cookie/db.php
Revert "Merged in Phergie changes"
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Cookie / db.php
1 <?php
2
3 if (!defined('__DIR__')) {
4     define('__DIR__', dirname(__FILE__));
5 }
6
7 // Create database schema
8 echo 'Creating database', PHP_EOL;
9 $file = __DIR__ . '/cookie.db';
10 if (file_exists($file)) {
11     unlink($file);
12 }
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)');
17
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);
23 }
24 $contents = file_get_contents($file);
25
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);
34
35 $cookies = $xpath->query('//table[@width="90%"]/tr/td[1]/a');
36
37 foreach ($cookies as $cookie) {
38     $name = $cookie->textContent;
39     foreach (range(0, mb_strlen($name) - 1) as $index) {
40         echo mb_strcut($name, $index, 1), PHP_EOL;
41     }
42     $name = str_replace(
43         array('(',')',"\n", 'cookies'),
44         array('','', ' ', 'cookie'),
45         $name
46     );
47     $name = iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $name);
48     $name = trim($name);
49     $name = rtrim($name, 's');
50
51     $link =  'http://en.wikipedia.org' . $cookie->getAttribute('href');
52     $insert->execute(array($name, $link));
53     echo 'added [' . $name . '] -> '. $link . PHP_EOL;
54 }
55
56 // Clean up
57 echo 'Cleaning up', PHP_EOL;
58 unlink($file);