Browse Source

implement manifest API

main
ghost 2 years ago
parent
commit
a2fc14c8cf
  1. 16
      README.md
  2. 22
      config/app.php.txt
  3. 121
      public/api.php
  4. 3
      public/index.php

16
README.md

@ -51,7 +51,7 @@ Could be enabled or disabled by `API_ENABLED` option @@ -51,7 +51,7 @@ Could be enabled or disabled by `API_ENABLED` option
/api.php
```
##### Search API
##### Search
Returns search results.
@ -65,7 +65,7 @@ GET query={string} - optional, search request, empty if not provided @@ -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
```
##### Hosts distribution API
##### Hosts distribution
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 @@ -77,6 +77,18 @@ Could be enabled or disabled by `API_HOSTS_ENABLED` option
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
* [x] Web pages full text ranking search

22
config/app.php.txt

@ -5,6 +5,16 @@ ini_set('display_errors', '1'); @@ -5,6 +5,16 @@ ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
// Application
/*
* Unique project name
*
* using to ident the app in the YGGo ecosystem
*
*/
define('APPLICATION_NAME', 'My YGGo host');
// Website
/*
@ -199,4 +209,14 @@ define('API_HOSTS_FIELDS', @@ -199,4 +209,14 @@ define('API_HOSTS_FIELDS',
`host`.`robotsPostfix`,
`host`.`timeAdded`,
`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);

121
public/api.php

@ -1,5 +1,8 @@ @@ -1,5 +1,8 @@
<?php
// Current version
define('API_VERSION', 0.1);
// Load system dependencies
require_once('../config/app.php');
require_once('../library/curl.php');
@ -17,54 +20,112 @@ if (API_ENABLED) { @@ -17,54 +20,112 @@ if (API_ENABLED) {
// Search API
case 'search';
// Connect database
$db = new MySQL(DB_HOST, DB_PORT, DB_NAME, DB_USERNAME, DB_PASSWORD);
if (API_SEARCH_ENABLED) {
// Connect database
$db = new MySQL(DB_HOST, DB_PORT, DB_NAME, DB_USERNAME, DB_PASSWORD);
// Connect Sphinx search server
$sphinx = new SphinxQL(SPHINX_HOST, SPHINX_PORT);
// Connect Sphinx search server
$sphinx = new SphinxQL(SPHINX_HOST, SPHINX_PORT);
// Filter request data
$query = !empty($_GET['query']) ? Filter::url($_GET['query']) : '';
$page = !empty($_GET['page']) ? Filter::url($_GET['page']) : 1;
// Filter request data
$query = !empty($_GET['query']) ? Filter::url($_GET['query']) : '';
$page = !empty($_GET['page']) ? Filter::url($_GET['page']) : 1;
// Make search request
$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);
// Make search request
$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);
// Generate results
$dbResults = [];
// Generate results
$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
$response = [
'status' => true,
'totals' => $sphinxResultsTotal,
'result' => $dbResults,
];
// Make response
$response = [
'status' => true,
'totals' => $sphinxResultsTotal,
'result' => $dbResults,
];
} else {
$response = [
'status' => false,
'result' => [],
];
}
break;
// Host API
case 'hosts';
// Connect database
$db = new MySQL(DB_HOST, DB_PORT, DB_NAME, DB_USERNAME, DB_PASSWORD);
if (API_HOSTS_ENABLED) {
$response = [
'status' => true,
'totals' => $db->getTotalHosts(),
'result' => $db->getAPIHosts(API_HOSTS_FIELDS),
];
// Connect database
$db = new MySQL(DB_HOST, DB_PORT, DB_NAME, DB_USERNAME, DB_PASSWORD);
$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;

3
public/index.php

@ -21,6 +21,9 @@ $placeholder = Filter::plural($totalPages, [sprintf(_('Over %s page or enter the @@ -21,6 +21,9 @@ $placeholder = Filter::plural($totalPages, [sprintf(_('Over %s page or enter the
<head>
<title><?php echo _('YGGo! Web Search Engine') ?></title>
<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="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') ?>" />

Loading…
Cancel
Save