]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Tea/db.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Tea / 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__ . '/tea.db';
10 if (file_exists($file)) {
11     unlink($file);
12 }
13 $db = new PDO('sqlite:' . $file);
14 $db->exec('CREATE TABLE tea (name VARCHAR(255), link VARCHAR(255))');
15 $db->exec('CREATE UNIQUE INDEX tea_name ON tea (name)');
16 $insert = $db->prepare('INSERT INTO tea (name, link) VALUES (:name, :link)');
17
18 // Get raw teacuppa.com data set
19 echo 'Downloading teacuppa.com data set', PHP_EOL;
20 $file = __DIR__ . '/tea-list.html';
21 if (!file_exists($file)) {
22     copy('http://www.teacuppa.com/tea-list.asp', $file);
23 }
24 $contents = file_get_contents($file);
25
26 // Extract data from data set
27 echo 'Processing teacuppa.com data', 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 $teas = $xpath->query('//p[@class="page_title"]/following-sibling::table//a');
35 $db->beginTransaction();
36 foreach ($teas as $tea) {
37     $name = preg_replace(
38         array('/\s*\v+\s*/', '/\s+tea\s*$/i'),
39         array(' ', ''),
40         $tea->textContent
41     );
42     $link = 'http://teacuppa.com/' . $tea->getAttribute('href');
43     $insert->execute(array($name, $link));
44 }
45 $db->commit();
46
47 // Clean up
48 echo 'Cleaning up', PHP_EOL;
49 unlink($file);