YGGo! Distributed Web Search Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

509 lines
17 KiB

<?php
// Load system dependencies
require_once(__DIR__ . '/../config/app.php');
require_once(__DIR__ . '/../library/cli.php');
require_once(__DIR__ . '/../library/mysql.php');
require_once(__DIR__ . '/../library/filter.php');
require_once(__DIR__ . '/../library/ftp.php');
require_once(__DIR__ . '/../library/vendor/simple_html_dom.php');
// CLI only to prevent https server connection timeout
if (php_sapi_name() != 'cli') {
CLI::danger(_('supported command line interface only'));
CLI::break();
exit;
}
1 year ago
// Stop CLI execution on cleaner process running
$semaphore = sem_get(crc32('crontab.cleaner'), 1);
if (false === sem_acquire($semaphore, true)) {
1 year ago
CLI::danger(_('stop crontab.cleaner is running in another thread.'));
CLI::break();
exit;
}
1 year ago
// Stop CLI execution on crawler process running
$semaphore = sem_get(crc32('crontab.crawler'), 1);
if (false === sem_acquire($semaphore, true)) {
1 year ago
CLI::danger(_('stop crontab.crawler is running in another thread.'));
CLI::break();
exit;
}
1 year ago
// Lock multi-thread execution
$semaphore = sem_get(crc32('cli.yggo'), 1);
if (false === sem_acquire($semaphore, true)) {
1 year ago
CLI::danger(_('process locked by another thread.'));
CLI::break();
exit;
}
// Connect database
$db = new MySQL(DB_HOST, DB_PORT, DB_NAME, DB_USERNAME, DB_PASSWORD);
// CLI begin
1 year ago
if (!empty($argv[1])) {
1 year ago
switch ($argv[1]) {
1 year ago
case 'db':
1 year ago
if (empty($argv[2])) {
1 year ago
switch ($argv[2]) {
1 year ago
case 'optimize':
1 year ago
CLI::notice(_('optimize database tables...'));
1 year ago
$db->optimize();
1 year ago
CLI::success(_('tables successfully optimized!'));
1 year ago
break;
}
}
1 year ago
break;
case 'crontab':
1 year ago
if (empty($argv[2])) {
1 year ago
switch ($argv[2]) {
1 year ago
case 'crawl':
1 year ago
CLI::notice(_('crawler queue step begin...'));
1 year ago
include_once(__DIR__ . '/../crontab/crawler.php');
1 year ago
CLI::notice(_('crawler queue step begin...'));
break;
1 year ago
case 'clean':
1 year ago
CLI::notice(_('cleaner queue step begin...'));
1 year ago
include_once(__DIR__ . '/../crontab/cleaner.php');
1 year ago
CLI::notice(_('cleaner queue step completed.'));
1 year ago
break;
}
}
1 year ago
break;
case 'hostPageSnap':
1 year ago
if (empty($argv[2])) {
1 year ago
switch ($argv[2]) {
1 year ago
case 'repair':
1 year ago
// @TODO
CLI::danger(_('this function upgraded but not tested after snaps refactor.'));
CLI::danger(_('make sure you have backups then remove this alert.'));
CLI::break();
exit;
1 year ago
switch ($argv[3]) {
1 year ago
case 'db':
1 year ago
// Normalize & cleanup DB
CLI::notice(_('scan database registry for missed snap files...'));
1 year ago
foreach ($db->getHosts() as $host) {
1 year ago
foreach ($db->getHostPages($host->hostId) as $hostPage) {
1 year ago
foreach ($db->getHostPageSnaps($hostPage->hostPageId) as $hostPageSnap) {
1 year ago
// Prepare filenames
$hostPageSnapPath = 'hps/' . substr(trim(chunk_split($hostPageSnap->hostPageSnapId, 1, '/'), '/'), 0, -1);
$hostPageSnapFile = $hostPageSnapPath . substr($hostPageSnap->hostPageSnapId, -1) . '.zip';
1 year ago
// Define variables
$hostPageSnapStorageFilesExists = false;
1 year ago
// Check file exists
foreach (json_decode(SNAP_STORAGE) as $node => $storages) {
1 year ago
foreach ($storages as $location => $storage) {
1 year ago
// Generate storage id
$crc32name = crc32(sprintf('%s.%s', $node, $location));
1 year ago
switch ($node) {
1 year ago
case 'localhost':
1 year ago
// @TODO implemented, not tested
$hostPageSnapFile = $storage->directory . $hostPageSnapFile;
1 year ago
if (file_exists($hostPageSnapFile)) {
1 year ago
$hostPageSnapStorageFilesExists = true;
1 year ago
if (!$db->findHostPageSnapStorageByCRC32Name($hostPageSnap->hostPageSnapId, $crc32name)) {
1 year ago
if ($db->addHostPageSnapStorage($hostPageSnap->hostPageSnapId, $crc32name, $hostPageSnap->timeAdded)) {
1 year ago
CLI::warning(sprintf(_('add index hostPageSnapId #%s file: %s node: %s location: %s;'), $hostPageSnap->hostPageSnapId, $hostPageSnapFile, $node, $location));
}
1 year ago
} else {
1 year ago
CLI::success(sprintf(_('skip related index hostPageSnapId #%s file: %s node: %s location: %s;'), $hostPageSnap->hostPageSnapId, $hostPageSnapFile, $node, $location));
}
}
1 year ago
break;
1 year ago
case 'ftp':
1 year ago
$ftp = new Ftp();
1 year ago
if ($ftp->connect($storage->host, $storage->port, $storage->username, $storage->password, $storage->directory, $storage->timeout, $storage->passive)) {
1 year ago
if ($ftp->size($hostPageSnapFile)) {
1 year ago
$hostPageSnapStorageFilesExists = true;
1 year ago
if (!$db->findHostPageSnapStorageByCRC32Name($hostPageSnap->hostPageSnapId, $crc32name)) {
1 year ago
if ($db->addHostPageSnapStorage($hostPageSnap->hostPageSnapId, $crc32name, $hostPageSnap->timeAdded)) {
1 year ago
CLI::warning(sprintf(_('add index hostPageSnapId #%s file: %s node: %s location: %s;'), $hostPageSnap->hostPageSnapId, $hostPageSnapFile, $node, $location));
}
} else {
1 year ago
CLI::success(sprintf(_('skip related index hostPageSnapId #%s file: %s node: %s location: %s;'), $hostPageSnap->hostPageSnapId, $hostPageSnapFile, $node, $location));
}
}
1 year ago
// Prevent snap deletion from registry on FTP connection lost
} else {
1 year ago
CLI::danger(sprintf(_('could not connect to storage %s location %s. operation stopped to prevent the data lose.'), $hostPageSnapStorageName, $location));
CLI::break();
exit;
}
1 year ago
$ftp->close();
1 year ago
break;
}
}
}
1 year ago
// Files not exists
if (!$hostPageSnapStorageFilesExists) {
1 year ago
// Delete snap from registry
try {
1 year ago
$db->beginTransaction();
1 year ago
foreach ($db->getHostPageSnapStorages($hostPageSnap->hostPageSnapId) as $hostPageSnapStorage) {
1 year ago
$db->deleteHostPageSnapDownloads($hostPageSnapStorage->hostPageSnapStorageId);
}
1 year ago
$db->deleteHostPageSnapStorages($hostPageSnap->hostPageSnapId);
$db->deleteHostPageSnap($hostPageSnap->hostPageSnapId);
1 year ago
CLI::warning(sprintf(_('delete hostPageSnapId: #%s timeAdded: %s as not found in file storages;'), $hostPageSnap->hostPageSnapId, $hostPageSnap->timeAdded));
1 year ago
$db->commit();
1 year ago
} catch(Exception $e) {
1 year ago
$db->rollBack();
1 year ago
var_dump($e);
}
}
}
}
}
1 year ago
break;
case 'fs':
1 year ago
// Cleanup FS
CLI::notice(_('scan storage for snap files missed in the DB...'));
1 year ago
// Copy files to each storage
foreach (json_decode(SNAP_STORAGE) as $node => $storages) {
1 year ago
foreach ($storages as $location => $storage) {
1 year ago
// Generate storage id
$crc32name = crc32(sprintf('%s.%s', $node, $location));
1 year ago
switch ($node) {
1 year ago
case 'localhost':
1 year ago
// @TODO
1 year ago
break;
1 year ago
case 'ftp':
1 year ago
$ftp = new Ftp();
1 year ago
if ($ftp->connect($storage->host, $storage->port, $storage->username, $storage->password, $storage->directory, $storage->timeout, $storage->passive)) {
1 year ago
foreach ($ftp->nlistr($storage->directory) as $filename) {
1 year ago
if (false !== preg_match(sprintf('!/hps/([\d]+)\.zip$!ui', $storage->directory), $filename, $matches)) {
1 year ago
if (!empty($matches[1])) { // hostPageSnapId
1 year ago
if (!$db->getHostPageSnap($matches[1])) {
1 year ago
if ($ftp->delete($filename)) {
1 year ago
CLI::warning(sprintf(_('delete snap file: #%s from node %s location %s not found in registry;'), $filename, $node, $location));
1 year ago
} else {
1 year ago
CLI::danger(sprintf(_('delete snap file: #%s from node %s location %s not found in registry;'), $filename, $node, $location));
}
1 year ago
} else {
1 year ago
CLI::success(sprintf(_('skip snap file: #%s available in node %s location %s;'), $filename, $node, $location));
}
}
}
}
}
1 year ago
$ftp->close();
1 year ago
break;
}
}
}
1 year ago
CLI::success(_('missed snap files successfully deleted!'));
break;
}
break;
case 'reindex':
1 year ago
//@TODO
1 year ago
break;
}
1 year ago
}
1 year ago
break;
case 'hostPage':
if (!empty($argv[2])) {
switch ($argv[2]) {
case 'rank':
if (!empty($argv[3])) {
switch ($argv[3]) {
case 'reindex':
CLI::notice(_('hostPage rank fields reindex begin...'));
foreach ($db->getHosts() as $host) {
foreach ($db->getHostPages($host->hostId) as $hostPage) {
// @TODO add common method
$hostPageRank = 0;
// Get referrers
foreach ($db->getHostPagesToHostPageByHostPageIdTarget($hostPage->hostPageId) as $hostPageToHostPageByHostPageIdTarget) {
// Get source page details
if ($hostPageSource = $db->getHostPage($hostPageToHostPageByHostPageIdTarget->hostPageIdSource)) {
// Increase PR on external referrer only
if ($hostPageSource->hostId != $hostPage->hostId) {
$hostPageRank++;
}
// Delegate page rank value from redirected pages
if (false !== strpos($hostPageSource->httpCode, '30')) {
$hostPageRank += $hostPageSource->rank;
}
}
}
// Update registry
if ($db->updateHostPageRank($hostPage->hostPageId, $hostPageRank)) {
CLI::warning(sprintf(_('update hostPage #%s rank from %s to %s;'), $hostPage->hostPageId, $hostPage->rank, $hostPageRank));
} else {
# CLI::success(sprintf(_('keep hostPage #%s rank %s;'), $hostPage->hostPageId, $hostPageRank));
}
}
}
CLI::notice(_('hostPage rank fields successfully updated!'));
CLI::break();
exit;
break;
}
1 year ago
}
break;
}
1 year ago
}
1 year ago
break;
case 'hostPageDom':
1 year ago
if (empty($argv[2])) {
1 year ago
switch ($argv[2]) {
1 year ago
case 'generate':
1 year ago
$selectors = [];
1 year ago
foreach ((array) explode(';', !empty($argv[3]) ? $argv[3] : (string) CRAWL_HOST_PAGE_DOM_SELECTORS) as $selector) {
1 year ago
if (!empty($selector)) {
1 year ago
$selectors[] = trim($selector);
}
}
1 year ago
if ($selectors) {
1 year ago
// Init variables
$hostPagesProcessedTotal = 0;
$hostPageDOMAddedTotal = 0;
1 year ago
// Begin selectors extraction
foreach ($db->getHostPagesByIndexed() as $hostPage) {
1 year ago
if (false !== stripos(Filter::mime($hostPage->mime), 'text/html')) {
1 year ago
if ($hostPageDescription = $db->getLastPageDescription($hostPage->hostPageId)) {
1 year ago
$hostPagesProcessedTotal++;
1 year ago
if (!empty($hostPageDescription->data)) {
1 year ago
$html = str_get_html(base64_decode($hostPageDescription->data));
1 year ago
foreach ($selectors as $selector) {
1 year ago
foreach($html->find($selector) as $element) {
1 year ago
if (!empty($element->innertext)) {
1 year ago
$hostPageDOMAddedTotal++;
1 year ago
$db->addHostPageDom($hostPage->hostPageId,
time(),
$selector,
trim(CRAWL_HOST_PAGE_DOM_STRIP_TAGS ? strip_tags(
preg_replace('/[\s]+/',
' ',
str_replace(['<br />', '<br/>', '<br>', '</'],
[' ', ' ', ' ', ' </'],
$element->innertext))) : $element->innertext));
}
}
}
}
}
}
}
1 year ago
CLI::success(sprintf(_('Host pages processed: %s'), $hostPagesProcessedTotal));
CLI::success(sprintf(_('Host page DOM elements added: %s'), $hostPageDOMAddedTotal));
exit;
}
1 year ago
CLI::danger(_('CRAWL_HOST_PAGE_DOM_SELECTORS not provided in the configuration file'));
CLI::break();
1 year ago
exit;
1 year ago
break;
case 'truncate':
1 year ago
$db->truncateHostPageDom();
1 year ago
CLI::success(_('hostPageDom table successfully truncated'));
CLI::break();
1 year ago
exit;
1 year ago
break;
}
}
break;
}
}
// Default message
CLI::default('__ ______________ __');
CLI::default('\ \/ / ____/ ____/___ / /');
CLI::default(' \ / / __/ / __/ __ \/ /' );
CLI::default(' / / /_/ / /_/ / /_/ /_/' );
CLI::default('/_/\____/\____/\____(_)' );
CLI::break();
CLI::default('available options:');
1 year ago
CLI::break();
CLI::default(' help - this message');
CLI::break();
CLI::default(' db ');
CLI::default(' optimize - optimize all tables');
CLI::break();
CLI::default(' crontab ');
CLI::default(' crawl - execute step in crawler queue');
CLI::default(' clean - execute step in cleaner queue');
CLI::break();
CLI::default(' hostPage ');
CLI::default(' rank ');
CLI::default(' reindex - reindex hostPage.rank fields');
1 year ago
CLI::break();
CLI::default(' hostPageSnap ');
CLI::default(' repair ');
CLI::default(' db - scan database registry for new or deprecated snap files');
CLI::default(' fs - check all storages for snap files not registered in hostPageSnapStorage, cleanup filesystem');
CLI::default(' reindex - search for host pages without snap records, add found pages to the crawl queue');
CLI::break();
CLI::default(' hostPageDom ');
CLI::default(' generate [selectors] - make hostPageDom index based on related hostPage.data field');
CLI::default(' truncate - flush hostPageDom table');
CLI::break();
CLI::default('get support: https://github.com/YGGverse/YGGo/issues');
CLI::break();
CLI::break();