129 lines
3.8 KiB
PHP
129 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace Shikiryu\WebGobbler;
|
|
|
|
abstract class Collector
|
|
{
|
|
/**
|
|
* Collector constructor.
|
|
*
|
|
* @param \Shikiryu\WebGobbler\Pool $pool
|
|
*/
|
|
public function __construct(Pool $pool)
|
|
{
|
|
$this->pool = $pool;
|
|
}
|
|
|
|
/**
|
|
* @return \Shikiryu\WebGobbler\Config
|
|
*/
|
|
public function getConfig(): Config
|
|
{
|
|
return $this->pool->getConfig();
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
abstract public function getName();
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getPoolDirectory()
|
|
{
|
|
return sprintf('%s/%s', $this->pool->getPoolDirectory(), $this->getName());
|
|
}
|
|
|
|
/**
|
|
* @return int
|
|
*/
|
|
abstract public function getRandomImage();
|
|
|
|
/**
|
|
* @param int $number
|
|
*
|
|
* @return int
|
|
*/
|
|
abstract public function getRandomImages(int $number);
|
|
|
|
|
|
/**
|
|
* Generates a random word.
|
|
* This method can be used by all derived classes.
|
|
* Usefull to get random result from search engines when you do not have
|
|
* a dictionnary at hand.
|
|
* The generated word can be a number (containing only digits),
|
|
* a word (containing only letters) or both mixed.
|
|
* Output: string (a random word)
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function generateRandomWord()
|
|
{
|
|
$word = '1';
|
|
try {
|
|
if (random_int(0, 100) < 30) { // Sometimes use only digits
|
|
if (random_int(0, 100) < 30) {
|
|
$word = random_int(1, 999);
|
|
} else {
|
|
$word = random_int(1, 999999);
|
|
}
|
|
} else { // Generate a word containing letters
|
|
$word = '';
|
|
$charset = 'abcdefghijklmnopqrstuvwxyz'; // Search for random word containing letter only.
|
|
if (random_int(0, 100) < 60) { // Sometimes include digits with letters
|
|
$charset = 'abcdefghijklmnopqrstuvwxyz' . 'abcdefghijklmnopqrstuvwxyz' . '0123456789'; // *2 to have more letters than digits
|
|
}
|
|
$charset = str_split($charset);
|
|
for ($i = 0, $l = random_int(2, 5); $i <= $l; $i++) { // Only generate short words (2 to 5 characters)
|
|
$word .= $charset[array_rand($charset)];
|
|
}
|
|
}
|
|
} catch (\Exception $e) {
|
|
}
|
|
|
|
return $word;
|
|
}
|
|
|
|
/**
|
|
* @param array $parts
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function reverse_url(array $parts) {
|
|
if (array_key_exists('query', $parts) && is_array($parts['query'])) {
|
|
$parts['query'] = http_build_query($parts['query']);
|
|
}
|
|
return (isset($parts['scheme']) ? "{$parts['scheme']}:" : '') .
|
|
((isset($parts['user']) || isset($parts['host'])) ? '//' : '') .
|
|
(isset($parts['user']) ? "{$parts['user']}" : '') .
|
|
(isset($parts['pass']) ? ":{$parts['pass']}" : '') .
|
|
(isset($parts['user']) ? '@' : '') .
|
|
(isset($parts['host']) ? "{$parts['host']}" : '') .
|
|
(isset($parts['port']) ? ":{$parts['port']}" : '') .
|
|
(isset($parts['path']) ? "{$parts['path']}" : '') .
|
|
(isset($parts['query']) ? "?{$parts['query']}" : '') .
|
|
(isset($parts['fragment']) ? "#{$parts['fragment']}" : '');
|
|
}
|
|
|
|
/**
|
|
* @param \Shikiryu\WebGobbler\Pool $pool
|
|
*
|
|
* @return \Shikiryu\WebGobbler\Collector[]
|
|
*/
|
|
public static function getAllCollectors(Pool $pool)
|
|
{
|
|
$collectors = [];
|
|
foreach (glob(__DIR__ . '/Collector/*.php') as $class) {
|
|
$classname = sprintf('Shikiryu\\WebGobbler\\Collector\\%s', basename($class, '.php'));
|
|
$tmp_class = new $classname($pool);
|
|
if (is_subclass_of($tmp_class, __CLASS__)) {
|
|
$collectors[] = $tmp_class;
|
|
}
|
|
}
|
|
|
|
return $collectors;
|
|
}
|
|
}
|