mirror of https://github.com/YGGverse/YGGo.git
ghost
2 years ago
14 changed files with 561 additions and 278 deletions
@ -0,0 +1,22 @@
@@ -0,0 +1,22 @@
|
||||
source hostPage |
||||
{ |
||||
type = mysql |
||||
|
||||
sql_host = localhost |
||||
sql_user = |
||||
sql_pass = |
||||
sql_db = |
||||
sql_port = 3306 # optional, default is 3306 |
||||
|
||||
sql_query = \ |
||||
SELECT hostPageId, metaTitle, metaDescription, metaKeywords, data, uri \ |
||||
FROM hostPage |
||||
|
||||
sql_attr_uint = hostPageId |
||||
} |
||||
|
||||
index hostPage |
||||
{ |
||||
source = hostPage |
||||
path = |
||||
} |
Binary file not shown.
@ -0,0 +1,196 @@
@@ -0,0 +1,196 @@
|
||||
<?php |
||||
|
||||
class MySQL { |
||||
|
||||
private PDO $_db; |
||||
|
||||
public function __construct(string $host, int $port, string $database, string $username, string $password) { |
||||
|
||||
$this->_db = new PDO('mysql:dbname=' . $database . ';host=' . $host . ';port=' . $port . ';charset=utf8', $username, $password, [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']); |
||||
$this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
||||
$this->_db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
||||
$this->_db->setAttribute(PDO::ATTR_TIMEOUT, 600); |
||||
} |
||||
|
||||
// System |
||||
public function beginTransaction() { |
||||
|
||||
$this->_db->beginTransaction(); |
||||
} |
||||
|
||||
public function commit() { |
||||
|
||||
$this->_db->commit(); |
||||
} |
||||
|
||||
public function rollBack() { |
||||
|
||||
$this->_db->rollBack(); |
||||
} |
||||
|
||||
// Host |
||||
public function getHost(int $crc32url) { |
||||
|
||||
$query = $this->_db->prepare('SELECT * FROM `host` WHERE `crc32url` = ? LIMIT 1'); |
||||
|
||||
$query->execute([$crc32url]); |
||||
|
||||
return $query->fetch(); |
||||
} |
||||
|
||||
public function addHost(string $scheme, string $name, mixed $port, int $crc32url, int $timeAdded, mixed $timeUpdated, int $crawlPageLimit, string $crawlPageMetaOnly, string $status, mixed $robots) { |
||||
|
||||
$query = $this->_db->prepare('INSERT INTO `host` (`scheme`, `name`, `port`, `crc32url`, `timeAdded`, `timeUpdated`, `crawlPageLimit`, `crawlPageMetaOnly`, `status`, `robots`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'); |
||||
|
||||
$query->execute([$scheme, $name, $port, $crc32url, $timeAdded, $timeUpdated, $crawlPageLimit, $crawlPageMetaOnly, $status, $robots]); |
||||
|
||||
return $this->_db->lastInsertId(); |
||||
} |
||||
|
||||
// Pages |
||||
public function getTotalHostPages(int $hostId) { |
||||
|
||||
$query = $this->_db->prepare('SELECT COUNT(*) AS `total` FROM `hostPage` WHERE `hostId` = ?'); |
||||
|
||||
$query->execute([$hostId]); |
||||
|
||||
return $query->fetch()->total; |
||||
} |
||||
|
||||
public function getTotalPagesByHttpCode(mixed $httpCode) { |
||||
|
||||
if (is_null($httpCode)) { |
||||
|
||||
$query = $this->_db->query('SELECT COUNT(*) AS `total` FROM `hostPage` WHERE `httpCode` IS NULL'); |
||||
|
||||
} else { |
||||
|
||||
$query = $this->_db->prepare('SELECT COUNT(*) AS `total` FROM `hostPage` WHERE `httpCode` = ?'); |
||||
|
||||
$query->execute([$httpCode]); |
||||
|
||||
} |
||||
|
||||
return $query->fetch()->total; |
||||
} |
||||
|
||||
public function getTotalPages() { |
||||
|
||||
$query = $this->_db->prepare('SELECT COUNT(*) AS `total` FROM `hostPage`'); |
||||
|
||||
$query->execute(); |
||||
|
||||
return $query->fetch()->total; |
||||
} |
||||
|
||||
public function getHostPage(int $hostId, int $crc32uri) { |
||||
|
||||
$query = $this->_db->prepare('SELECT * FROM `hostPage` WHERE `hostId` = ? AND `crc32uri` = ? LIMIT 1'); |
||||
|
||||
$query->execute([$hostId, $crc32uri]); |
||||
|
||||
return $query->fetch(); |
||||
} |
||||
|
||||
public function getFoundHostPage(int $hostPageId) { |
||||
|
||||
$query = $this->_db->prepare('SELECT `hostPage`.`metaTitle`, |
||||
`hostPage`.`metaDescription`, |
||||
`hostPage`.`data`, |
||||
`hostPage`.`uri`, |
||||
`host`.`scheme`, |
||||
`host`.`name`, |
||||
`host`.`port` |
||||
|
||||
FROM `hostPage` |
||||
JOIN `host` ON (`host`.`hostId` = `hostPage`.`hostId`) |
||||
|
||||
WHERE `hostPage`.`hostPageId` = ? |
||||
|
||||
LIMIT 1'); |
||||
|
||||
$query->execute([$hostPageId]); |
||||
|
||||
return $query->fetch(); |
||||
} |
||||
|
||||
public function addHostPage(int $hostId, |
||||
int $crc32uri, |
||||
string $uri, |
||||
int $timeAdded, |
||||
mixed $timeUpdated = null, |
||||
mixed $httpCode = null, |
||||
mixed $rank = null, |
||||
mixed $metaTitle = null, |
||||
mixed $metaDescription = null, |
||||
mixed $metaKeywords = null, |
||||
mixed $data = null) { |
||||
|
||||
$query = $this->_db->prepare('INSERT INTO `hostPage` (`hostId`, |
||||
`crc32uri`, |
||||
`uri`, |
||||
`timeAdded`, |
||||
`timeUpdated`, |
||||
`httpCode`, |
||||
`rank`, |
||||
`metaTitle`, |
||||
`metaDescription`, |
||||
`metaKeywords`, |
||||
`data`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'); |
||||
|
||||
$query->execute([$hostId, $crc32uri, $uri, $timeAdded, $timeUpdated, $httpCode, $rank, $metaTitle, $metaDescription, $metaKeywords, $data]); |
||||
|
||||
return $this->_db->lastInsertId(); |
||||
} |
||||
|
||||
public function updateHostPage( int $hostPageId, |
||||
mixed $metaTitle, |
||||
mixed $metaDescription, |
||||
mixed $metaKeywords, |
||||
mixed $data) { |
||||
|
||||
$query = $this->_db->prepare('UPDATE `hostPage` SET `metaTitle` = ?, |
||||
`metaDescription` = ?, |
||||
`metaKeywords` = ?, |
||||
`data` = ? WHERE `hostPageId` = ? LIMIT 1'); |
||||
|
||||
$query->execute([$metaTitle, $metaDescription, $metaKeywords, $data, $hostPageId]); |
||||
|
||||
return $query->rowCount(); |
||||
} |
||||
|
||||
// Crawl tools |
||||
public function getCrawlQueue(int $limit, int $timeFrom) { |
||||
|
||||
$query = $this->_db->prepare('SELECT `hostPage`.`hostPageId`, |
||||
`hostPage`.`uri`, |
||||
`host`.`scheme`, |
||||
`host`.`name`, |
||||
`host`.`port`, |
||||
`host`.`crawlPageLimit`, |
||||
`host`.`crawlPageMetaOnly`, |
||||
`host`.`robots` |
||||
|
||||
FROM `hostPage` |
||||
JOIN `host` ON (`host`.`hostId` = `hostPage`.`hostId`) |
||||
|
||||
WHERE (`hostPage`.`timeUpdated` IS NULL OR `hostPage`.`timeUpdated` < ? ) AND `host`.`status` <> 0 |
||||
|
||||
ORDER BY `hostPage`.`hostPageId` |
||||
|
||||
LIMIT ' . (int) $limit); |
||||
|
||||
$query->execute([$timeFrom]); |
||||
|
||||
return $query->fetchAll(); |
||||
} |
||||
|
||||
public function updateCrawlQueue(string $hostPageId, int $timeUpdated, int $httpCode) { |
||||
|
||||
$query = $this->_db->prepare('UPDATE `hostPage` SET `timeUpdated` = ?, `httpCode` = ? WHERE `hostPageId` = ? LIMIT 1'); |
||||
|
||||
$query->execute([$timeUpdated, $httpCode, $hostPageId]); |
||||
|
||||
return $query->rowCount(); |
||||
} |
||||
} |
@ -0,0 +1,73 @@
@@ -0,0 +1,73 @@
|
||||
<?php |
||||
|
||||
class Parser { |
||||
|
||||
static public function hostURL(string $string) { |
||||
|
||||
$result = [ |
||||
'string' => null, |
||||
'scheme' => null, |
||||
'name' => null, |
||||
'port' => null, |
||||
]; |
||||
|
||||
if ($hostScheme = parse_url($string, PHP_URL_SCHEME)) { |
||||
|
||||
$result['string'] = $hostScheme . '://'; |
||||
|
||||
$result['scheme'] = $hostScheme; |
||||
|
||||
} else { |
||||
|
||||
return false; |
||||
} |
||||
|
||||
if ($hostName = parse_url($string, PHP_URL_HOST)) { |
||||
|
||||
$result['string'] .= $hostName; |
||||
|
||||
$result['name'] = $hostName; |
||||
|
||||
} else { |
||||
|
||||
return false; |
||||
} |
||||
|
||||
if ($hostPort = parse_url($string, PHP_URL_PORT)) { |
||||
|
||||
$result['string'] .= ':' . $hostPort; |
||||
|
||||
$result['port'] = $hostPort; |
||||
|
||||
} |
||||
|
||||
return (object) $result; |
||||
} |
||||
|
||||
static public function uri(string $string) { |
||||
|
||||
$result = [ |
||||
'string' => '/', |
||||
'path' => '/', |
||||
'query' => null, |
||||
]; |
||||
|
||||
if ($path = parse_url($string, PHP_URL_PATH)) { |
||||
|
||||
$result['string'] = $path; |
||||
|
||||
$result['path'] = $path; |
||||
|
||||
} |
||||
|
||||
if ($query = parse_url($string, PHP_URL_QUERY)) { |
||||
|
||||
$result['string'] .= '?' . $query; |
||||
|
||||
$result['query'] = '?' . $query; |
||||
|
||||
} |
||||
|
||||
return (object) $result; |
||||
} |
||||
} |
@ -0,0 +1,31 @@
@@ -0,0 +1,31 @@
|
||||
<?php |
||||
|
||||
class SphinxQL { |
||||
|
||||
private $_sphinx; |
||||
|
||||
public function __construct(string $host, int $port) { |
||||
|
||||
$this->_sphinx = new PDO('mysql:host=' . $host . ';port=' . $port . ';charset=utf8', false, false, [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']); |
||||
$this->_sphinx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
||||
$this->_sphinx->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
||||
} |
||||
|
||||
public function searchHostPages(string $keyword, int $start, int $limit) { |
||||
|
||||
$query = $this->_sphinx->prepare('SELECT * FROM `hostPage` WHERE MATCH(?) LIMIT ' . (int) $start . ',' . (int) $limit); |
||||
|
||||
$query->execute([$keyword]); |
||||
|
||||
return $query->fetchAll(); |
||||
} |
||||
|
||||
public function searchHostPagesTotal(string $keyword) { |
||||
|
||||
$query = $this->_sphinx->prepare('SELECT COUNT(*) AS `total` FROM `hostPage` WHERE MATCH(?)'); |
||||
|
||||
$query->execute([$keyword]); |
||||
|
||||
return $query->fetch()->total; |
||||
} |
||||
} |
@ -1,170 +0,0 @@
@@ -1,170 +0,0 @@
|
||||
<?php |
||||
|
||||
class SQLite { |
||||
|
||||
private PDO $_db; |
||||
|
||||
public function __construct(string $database, string $username, string $password) { |
||||
|
||||
$this->_db = new PDO('sqlite:' . $database, $username, $password); |
||||
$this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
||||
$this->_db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
||||
$this->_db->setAttribute(PDO::ATTR_TIMEOUT, 600); |
||||
|
||||
$this->_db->query(' |
||||
CREATE TABLE IF NOT EXISTS "page" ( |
||||
"pageId" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, |
||||
"crc32url" INTEGER NOT NULL UNIQUE, |
||||
"httpCode" INTEGER, |
||||
"timeAdded" INTEGER NOT NULL, |
||||
"timeUpdated" INTEGER, |
||||
"title" TEXT, |
||||
"data" TEXT, |
||||
"description" TEXT, |
||||
"keywords" TEXT, |
||||
"url" TEXT NOT NULL |
||||
) |
||||
'); |
||||
|
||||
$this->_db->query(' |
||||
CREATE TABLE IF NOT EXISTS "image" ( |
||||
"imageId" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, |
||||
"crc32src" INTEGER NOT NULL UNIQUE, |
||||
"pageId" INTEGER NOT NULL, |
||||
"alt" TEXT NOT NULL, |
||||
"src" TEXT NOT NULL |
||||
) |
||||
'); |
||||
|
||||
// FTS5 |
||||
$this->_db->query(' |
||||
CREATE VIRTUAL TABLE IF NOT EXISTS `ftsPage` USING fts5(`url`, `title`, `description`, `keywords`, `data`, tokenize=`unicode61`, content=`page`, content_rowid=`pageId`) |
||||
'); |
||||
|
||||
$this->_db->query(' |
||||
CREATE TRIGGER IF NOT EXISTS `pageInsert` AFTER INSERT ON `page` BEGIN |
||||
INSERT INTO ftsPage(`rowid`, `url`, `title`, `description`, `keywords`, `data`) VALUES (`new`.`pageId`, `new`.`url`, `new`.`title`, `new`.`description`, `new`.`keywords`, `new`.`data`); |
||||
END |
||||
'); |
||||
|
||||
$this->_db->query(' |
||||
CREATE TRIGGER IF NOT EXISTS `pageDelete` AFTER DELETE ON `page` BEGIN |
||||
INSERT INTO ftsPage(`ftsPage`, `rowid`, `url`, `title`, `description`, `keywords`, `data`) VALUES ("delete", `old`.`pageId`, `old`.`url`, `old`.`title`, `old`.`description`, `old`.`keywords`, `old`.`data`); |
||||
END |
||||
'); |
||||
|
||||
$this->_db->query(' |
||||
CREATE TRIGGER IF NOT EXISTS `pageUpdate` UPDATE ON `page` BEGIN |
||||
INSERT INTO ftsPage(`ftsPage`, `rowid`, `url`, `title`, `description`, `keywords`, `data`) VALUES ("delete", `old`.`pageId`, `old`.`url`, `old`.`title`, `old`.`description`, `old`.`keywords`, `old`.`data`); |
||||
INSERT INTO ftsPage(`rowid`, `url`, `title`, `description`, `keywords`, `data`) VALUES (`new`.`pageId`, `new`.`url`, `new`.`title`, `new`.`description`, `new`.`keywords`, `new`.`data`); |
||||
END |
||||
'); |
||||
} |
||||
|
||||
public function getTotalPagesByHttpCode(mixed $httpCode) { |
||||
|
||||
if (is_null($httpCode)) { |
||||
|
||||
$query = $this->_db->query('SELECT COUNT(*) AS `total` FROM `page` WHERE `httpCode` IS NULL'); |
||||
|
||||
} else { |
||||
|
||||
$query = $this->_db->prepare('SELECT COUNT(*) AS `total` FROM `page` WHERE `httpCode` = ?'); |
||||
|
||||
$query->execute([$httpCode]); |
||||
|
||||
} |
||||
|
||||
return $query->fetch()->total; |
||||
} |
||||
|
||||
public function getTotalPages() { |
||||
|
||||
$query = $this->_db->prepare('SELECT COUNT(*) AS `total` FROM `page`'); |
||||
|
||||
$query->execute(); |
||||
|
||||
return $query->fetch()->total; |
||||
} |
||||
|
||||
public function updatePage(int $pageId, string $title, string $description, string $keywords, string $data, int $timeUpdated) { |
||||
|
||||
$query = $this->_db->prepare('UPDATE `page` SET `title` = ?, `description` = ?, `data` = ?, `timeUpdated` = ? WHERE `pageId` = ?'); |
||||
|
||||
$query->execute([$title, $description, $data, $timeUpdated, $pageId]); |
||||
|
||||
return $query->rowCount(); |
||||
} |
||||
|
||||
public function addPage(string $title, string $description, string $keywords, string $data, int $timeAdded) { |
||||
|
||||
$query = $this->_db->prepare('INSERT INTO `page` (`title`, `description`, `data`, `timeAdded`) VALUES (?, ?, ?, ?)'); |
||||
|
||||
$query->execute([$title, $description, $data, $timeAdded]); |
||||
|
||||
return $this->_db->lastInsertId(); |
||||
} |
||||
|
||||
public function initPage(string $url, int $crc32url, int $timeAdded) { |
||||
|
||||
$query = $this->_db->prepare('INSERT OR IGNORE INTO `page` (`url`, `crc32url`, `timeAdded`) VALUES (?, ?, ?)'); |
||||
|
||||
$query->execute([$url, $crc32url, $timeAdded]); |
||||
|
||||
return $this->_db->lastInsertId(); |
||||
} |
||||
|
||||
public function addImage(int $pageId, string $src, int $crc32src, string $alt) { |
||||
|
||||
$query = $this->_db->prepare('INSERT OR IGNORE INTO `image` (`pageId`, `src`, `crc32src`, `alt`) VALUES (?, ?, ?, ?)'); |
||||
|
||||
$query->execute([$pageId, $src, $crc32src, $alt]); |
||||
|
||||
return $this->_db->lastInsertId(); |
||||
} |
||||
|
||||
public function deleteImages(int $pageId) { |
||||
|
||||
$query = $this->_db->prepare('DELETE FROM `image` WHERE `pageId` = ?'); |
||||
|
||||
$query->execute([$pageId]); |
||||
|
||||
return $query->rowCount(); |
||||
} |
||||
|
||||
public function getPageQueue(int $limit, int $timeFrom) { |
||||
|
||||
$query = $this->_db->prepare('SELECT * FROM `page` WHERE `timeUpdated` IS NULL OR `timeUpdated` < ? ORDER BY `pageId` LIMIT ' . (int) $limit); |
||||
|
||||
$query->execute([$timeFrom]); |
||||
|
||||
return $query->fetchAll(); |
||||
} |
||||
|
||||
public function updatePageQueue(string $pageId, int $timeUpdated, int $httpCode) { |
||||
|
||||
$query = $this->_db->prepare('UPDATE `page` SET `timeUpdated` = ?, `httpCode` = ? WHERE `pageId` = ? LIMIT 1'); |
||||
|
||||
$query->execute([$timeUpdated, $httpCode, $pageId]); |
||||
|
||||
return $query->rowCount(); |
||||
} |
||||
|
||||
public function searchPages(string $q, int $start = 0, int $limit = 100) { |
||||
|
||||
$query = $this->_db->prepare('SELECT `title`, `description`, `url` FROM `ftsPage` WHERE `data` MATCH ? ORDER BY `rank` LIMIT ' . (int) $start . ',' . (int) $limit); |
||||
|
||||
$query->execute([$q]); |
||||
|
||||
return $query->fetchAll(); |
||||
} |
||||
|
||||
public function searchPagesTotal(string $q) { |
||||
|
||||
$query = $this->_db->prepare('SELECT COUNT(*) AS `total` FROM `ftsPage` WHERE `data` MATCH ?'); |
||||
|
||||
$query->execute([$q]); |
||||
|
||||
return $query->fetch()->total; |
||||
} |
||||
} |
Loading…
Reference in new issue