Skip to content

Commit 37b3417

Browse files
committed
Added multiprovider support and command line switches. Fixes #1.
1 parent b1372bd commit 37b3417

File tree

1 file changed

+81
-35
lines changed

1 file changed

+81
-35
lines changed

bashquote

Lines changed: 81 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,24 @@
22
<?php
33

44
// Use http_proxy environment variable
5+
56
if ($proxy = str_replace("http://", "tcp://", getenv('http_proxy')))
6-
stream_context_set_default(array('http' => array('proxy' => $proxy,'request_fulluri' => true)));
7+
stream_context_set_default(array('http' => array('proxy' => $proxy,'request_fulluri' => true)));
8+
9+
$opts = getopt('p:hf',array('help'));
10+
$provider = (isset($opts['p']) ? $opts['p'] : 'bash');
11+
12+
if (isset($opts['h']) || isset($opts['help'])) {
13+
echo "-h / --help This helptext\n";
14+
echo "-p provider Choose provider (bash, qdb or ingdal)\n";
15+
return;
16+
}
717

8-
$quotes = new BashOrgQuotes();
18+
if (isset($opts['f']) || isset($opts['flush'])) {
19+
unlink(sys_get_temp_dir() . PATH_SEPARATOR . "bashquote_cache");
20+
}
21+
22+
$quotes = new Quotes($provider);
923
$quote = $quotes->getQuote();
1024
echo $quote."\n";
1125

@@ -14,20 +28,24 @@ echo $quote."\n";
1428
*
1529
* A generic class for fetching quotes
1630
*/
17-
abstract class Quotes {
31+
class Quotes {
1832

1933
private $quotefile = ''; // Quote cache location
2034
private $quotes = array(); // Quotes
35+
private $provider = '';
2136

2237
/**
2338
* Class constructur
2439
*
40+
* @param string $provider
41+
* @throws Exception
2542
* Opens the quote cache.
2643
*/
27-
function __construct() {
44+
function __construct($provider) {
2845
if (file_exists(getenv('HOME')."/.bashqdb")) // Clean up old style cache
2946
unlink(getenv('HOME')."/.bashqdb");
3047

48+
$this->provider = $provider;
3149
$this->quotefile = sys_get_temp_dir() . PATH_SEPARATOR . "bashquote_cache";
3250
$this->quotefp = fopen($this->quotefile,"c+");
3351
// Only one instance accessing the cache at a time
@@ -74,49 +92,77 @@ abstract class Quotes {
7492
* Checks if the cache is empty, and if so, fills it.
7593
*/
7694
function fillIfEmpty() {
77-
if (!$this->quotes)
78-
$this->quotes = $this->fetchQuotes();
95+
if (!$this->quotes[$this->provider])
96+
$this->quotes[$this->provider] = $this->fetchQuotes();
7997
}
8098

8199
/**
82100
* Get a single quote
83101
*/
84102
function getQuote() {
85-
$quote = array_pop($this->quotes);
103+
$quote = NULL;
104+
if (isset($this->quotes[$this->provider])) {
105+
$quote = array_pop($this->quotes[$this->provider]);
106+
}
86107
if (!$quote) { // This is not good enough error handling
87108
$this->fillIfEmpty();
88-
$quote = array_pop($this->quotes);
109+
$quote = array_pop($this->quotes[$this->provider]);
89110
}
90111
return $quote;
91112
}
92113

93-
}
114+
/**
115+
* Fetch and parse a batch of quotes
116+
*/
117+
function fetchQuotes() {
118+
$conf = self::getProviderConfig($this->provider);
119+
libxml_use_internal_errors(TRUE);
120+
$doc = new DOMDocument;
121+
$doc->loadHTMLFile($conf['url']);
122+
$xpath = new DOMXPath($doc);
123+
$elements = $xpath->query($conf['xpath']);
124+
$quotes = array();
125+
foreach ($elements as $element) {
126+
print_r($element);
127+
$quote = trim($element->textContent);
128+
switch ($conf['type']) {
129+
case 'pre':
130+
$quote = html_entity_decode($quote);
131+
break;
132+
case 'html':
133+
$quote = str_replace(array("\n", "\r"), "", $quote);
134+
$quote = html_entity_decode($quote);
135+
$quote = str_replace(array("<br />", "<br>"), "\n", $quote);
136+
}
137+
$quotes[] = $quote;
138+
}
139+
return $quotes;
140+
}
94141

95-
/**
96-
* Class for handling quotes from bash.org
97-
*
98-
* Implementation of class Quotes
99-
*/
100-
class BashOrgQuotes extends Quotes {
101-
102-
/**
103-
* Fetch and parse a batch of random quotes from bash.org
104-
*/
105-
function fetchQuotes() {
106-
libxml_use_internal_errors(TRUE);
107-
$doc = new DOMDocument;
108-
$doc->loadHTMLFile("http://bash.org/?random1");
109-
$xpath = new DOMXPath($doc);
110-
$elements = $xpath->query('//p[@class="qt"]');
111-
$quotes = array();
112-
foreach ($elements as $element) {
113-
$quote = $element->textContent;
114-
$quote = html_entity_decode($quote);
115-
$quote = str_replace("<br />", "\n", $quote);
116-
$quotes[] = $quote;
117-
}
118-
return $quotes;
119-
}
142+
/**
143+
* Get provider definitions
144+
*/
145+
static function getProviderConfig($provider) {
146+
$configs = array(
147+
'bash' => array(
148+
'url' => 'http://bash.org/?random1',
149+
'xpath' => '//p[@class="qt"]',
150+
'type' => 'pre'
151+
),
152+
'ingdal' => array(
153+
'url' => 'http://bash.ingdal.net/?random2',
154+
'xpath' => '//div[@class="quote_quote"]',
155+
'type' => 'pre'
156+
),
157+
'qdb' => array(
158+
'url' => 'http://qdb.us/qdb.xml?action=random&fixed=0&client=bashquote',
159+
'xpath' => '//description',
160+
'type' => 'html'
161+
),
162+
);
163+
if (isset($configs[$provider]))
164+
return $configs[$provider];
165+
return NULL;
166+
}
120167
}
121168

122-
?>

0 commit comments

Comments
 (0)