implement manifest API

This commit is contained in:
ghost 2023-04-25 19:35:52 +03:00
parent 5875dd58c9
commit a2fc14c8cf
4 changed files with 129 additions and 33 deletions

View File

@ -51,7 +51,7 @@ Could be enabled or disabled by `API_ENABLED` option
/api.php /api.php
``` ```
##### Search API ##### Search
Returns search results. Returns search results.
@ -65,7 +65,7 @@ GET query={string} - optional, search request, empty if not provided
GET page={int} - optional, search results page, 1 if not provided GET page={int} - optional, search results page, 1 if not provided
``` ```
##### Hosts distribution API ##### Hosts distribution
Returns node hosts collected with fields provided in `API_HOSTS_FIELDS` option. Returns node hosts collected with fields provided in `API_HOSTS_FIELDS` option.
@ -77,6 +77,18 @@ Could be enabled or disabled by `API_HOSTS_ENABLED` option
GET action=hosts - required GET action=hosts - required
``` ```
##### Application manifest
Returns node information.
Could be enabled or disabled by `API_MANIFEST_ENABLED` option
###### Request attributes
```
GET action=manifest - required
```
#### Roadmap / ideas #### Roadmap / ideas
* [x] Web pages full text ranking search * [x] Web pages full text ranking search

View File

@ -5,6 +5,16 @@ ini_set('display_errors', '1');
ini_set('display_startup_errors', '1'); ini_set('display_startup_errors', '1');
error_reporting(E_ALL); error_reporting(E_ALL);
// Application
/*
* Unique project name
*
* using to ident the app in the YGGo ecosystem
*
*/
define('APPLICATION_NAME', 'My YGGo host');
// Website // Website
/* /*
@ -199,4 +209,14 @@ define('API_HOSTS_FIELDS',
`host`.`robotsPostfix`, `host`.`robotsPostfix`,
`host`.`timeAdded`, `host`.`timeAdded`,
`host`.`timeUpdated`, `host`.`timeUpdated`,
(SELECT COUNT(*) FROM `hostPage` WHERE `hostPage`.`hostId` = `host`.`hostId`) AS `hostPagesTotal`'); // string: *|field names comma separated (SELECT COUNT(*) FROM `hostPage` WHERE `hostPage`.`hostId` = `host`.`hostId`) AS `hostPagesTotal`'); // string: *|field names comma separated
/*
* Manifest API
*
* Application meta sharing between YGGo remote nodes
*
* When true - make this node public for distributed index sharing
*
*/
define('API_MANIFEST_ENABLED', true);

View File

@ -1,5 +1,8 @@
<?php <?php
// Current version
define('API_VERSION', 0.1);
// Load system dependencies // Load system dependencies
require_once('../config/app.php'); require_once('../config/app.php');
require_once('../library/curl.php'); require_once('../library/curl.php');
@ -17,54 +20,112 @@ if (API_ENABLED) {
// Search API // Search API
case 'search'; case 'search';
// Connect database if (API_SEARCH_ENABLED) {
$db = new MySQL(DB_HOST, DB_PORT, DB_NAME, DB_USERNAME, DB_PASSWORD);
// Connect Sphinx search server // Connect database
$sphinx = new SphinxQL(SPHINX_HOST, SPHINX_PORT); $db = new MySQL(DB_HOST, DB_PORT, DB_NAME, DB_USERNAME, DB_PASSWORD);
// Connect Sphinx search server
$sphinx = new SphinxQL(SPHINX_HOST, SPHINX_PORT);
// Filter request data // Filter request data
$query = !empty($_GET['query']) ? Filter::url($_GET['query']) : ''; $query = !empty($_GET['query']) ? Filter::url($_GET['query']) : '';
$page = !empty($_GET['page']) ? Filter::url($_GET['page']) : 1; $page = !empty($_GET['page']) ? Filter::url($_GET['page']) : 1;
// Make search request // Make search request
$sphinxResultsTotal = $sphinx->searchHostPagesTotal('"' . $query . '"'); $sphinxResultsTotal = $sphinx->searchHostPagesTotal('"' . $query . '"');
$sphinxResults = $sphinx->searchHostPages('"' . $query . '"', $page * API_SEARCH_PAGINATION_RESULTS_LIMIT - API_SEARCH_PAGINATION_RESULTS_LIMIT, API_SEARCH_PAGINATION_RESULTS_LIMIT, $sphinxResultsTotal); $sphinxResults = $sphinx->searchHostPages('"' . $query . '"', $page * API_SEARCH_PAGINATION_RESULTS_LIMIT - API_SEARCH_PAGINATION_RESULTS_LIMIT, API_SEARCH_PAGINATION_RESULTS_LIMIT, $sphinxResultsTotal);
// Generate results // Generate results
$dbResults = []; $dbResults = [];
foreach ($sphinxResults as $i => $sphinxResult) { foreach ($sphinxResults as $i => $sphinxResult) {
if ($hostPage = $db->getFoundHostPage($sphinxResult->id)) { if ($hostPage = $db->getFoundHostPage($sphinxResult->id)) {
$dbResults[$i] = $hostPage; $dbResults[$i] = $hostPage;
$dbResults[$i]->weight = $sphinxResult->weight; $dbResults[$i]->weight = $sphinxResult->weight;
}
} }
}
// Make response // Make response
$response = [ $response = [
'status' => true, 'status' => true,
'totals' => $sphinxResultsTotal, 'totals' => $sphinxResultsTotal,
'result' => $dbResults, 'result' => $dbResults,
]; ];
} else {
$response = [
'status' => false,
'result' => [],
];
}
break; break;
// Host API // Host API
case 'hosts'; case 'hosts';
// Connect database if (API_HOSTS_ENABLED) {
$db = new MySQL(DB_HOST, DB_PORT, DB_NAME, DB_USERNAME, DB_PASSWORD);
$response = [ // Connect database
'status' => true, $db = new MySQL(DB_HOST, DB_PORT, DB_NAME, DB_USERNAME, DB_PASSWORD);
'totals' => $db->getTotalHosts(),
'result' => $db->getAPIHosts(API_HOSTS_FIELDS), $response = [
]; 'status' => true,
'totals' => $db->getTotalHosts(),
'result' => $db->getAPIHosts(API_HOSTS_FIELDS),
];
} else {
$response = [
'status' => false,
'result' => [],
];
}
break;
// Manifest API
case 'manifest';
if (API_MANIFEST_ENABLED) {
$response = [
'status' => true,
'result' => [
'APPLICATION_NAME' => APPLICATION_NAME,
'WEBSITE_DOMAIN' => WEBSITE_DOMAIN,
'CRAWL_URL_REGEXP' => CRAWL_URL_REGEXP,
'CRAWL_HOST_DEFAULT_PAGES_LIMIT' => CRAWL_HOST_DEFAULT_PAGES_LIMIT,
'CRAWL_HOST_DEFAULT_STATUS' => CRAWL_HOST_DEFAULT_STATUS,
'CRAWL_HOST_DEFAULT_META_ONLY' => CRAWL_HOST_DEFAULT_META_ONLY,
'CRAWL_ROBOTS_DEFAULT_RULES' => CRAWL_ROBOTS_DEFAULT_RULES,
'CRAWL_ROBOTS_POSTFIX_RULES' => CRAWL_ROBOTS_POSTFIX_RULES,
'CLEAN_HOST_SECONDS_OFFSET' => CLEAN_HOST_SECONDS_OFFSET,
'API_VERSION' => API_VERSION,
'API_ENABLED' => API_ENABLED,
'API_SEARCH_ENABLED' => API_SEARCH_ENABLED,
'API_HOSTS_ENABLED' => API_HOSTS_ENABLED,
],
];
} else {
$response = [
'status' => false,
'result' => [],
];
}
break; break;

View File

@ -21,6 +21,9 @@ $placeholder = Filter::plural($totalPages, [sprintf(_('Over %s page or enter the
<head> <head>
<title><?php echo _('YGGo! Web Search Engine') ?></title> <title><?php echo _('YGGo! Web Search Engine') ?></title>
<meta charset="utf-8" /> <meta charset="utf-8" />
<?php if (API_MANIFEST_ENABLED) { ?>
<meta name="yggo" content="<?php echo sprintf('%s/api.php?action=manifest', WEBSITE_DOMAIN) ?>" />
<?php } ?>
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="<?php echo _('Javascript-less Open Source Web Search Engine') ?>" /> <meta name="description" content="<?php echo _('Javascript-less Open Source Web Search Engine') ?>" />
<meta name="keywords" content="<?php echo _('web, search, engine, crawler, php, pdo, mysql, sphinx, yggdrasil, js-less, open source') ?>" /> <meta name="keywords" content="<?php echo _('web, search, engine, crawler, php, pdo, mysql, sphinx, yggdrasil, js-less, open source') ?>" />