First commit
This commit is contained in:
commit
201edf2e4a
115 changed files with 29451 additions and 0 deletions
79
src/classes/Controllers/Api/Search.php
Normal file
79
src/classes/Controllers/Api/Search.php
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Directory\Controllers\Api;
|
||||
|
||||
use \Friendica\Directory\Content\Pager;
|
||||
use PDO;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
/**
|
||||
* @author Hypolite Petovan <mrpetovan@gmail.com>
|
||||
*/
|
||||
class Search
|
||||
{
|
||||
/**
|
||||
* @var \Atlas\Pdo\Connection
|
||||
*/
|
||||
private $atlas;
|
||||
/**
|
||||
* @var \Friendica\Directory\Models\Profile
|
||||
*/
|
||||
private $profileModel;
|
||||
/**
|
||||
* @var \Friendica\Directory\Content\L10n
|
||||
*/
|
||||
private $l10n;
|
||||
|
||||
public function __construct(
|
||||
\Atlas\Pdo\Connection $atlas,
|
||||
\Friendica\Directory\Models\Profile $profileModel,
|
||||
\Friendica\Directory\Content\L10n $l10n
|
||||
)
|
||||
{
|
||||
$this->atlas = $atlas;
|
||||
$this->profileModel = $profileModel;
|
||||
$this->l10n = $l10n;
|
||||
}
|
||||
|
||||
public function render(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args): \Slim\Http\Response
|
||||
{
|
||||
$pager = new Pager($this->l10n, $request, 20);
|
||||
|
||||
$originalQuery = $query = filter_input(INPUT_GET, 'q');
|
||||
|
||||
$field = filter_input(INPUT_GET, 'field', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW & FILTER_FLAG_STRIP_HIGH);
|
||||
|
||||
if ($field) {
|
||||
$query .= '%';
|
||||
$sql_where = '`' . $field . '` LIKE :query';
|
||||
} else {
|
||||
$sql_where = "MATCH (p.`name`, p.`pdesc`, p.`profile_url`, p.`locality`, p.`region`, p.`country`, p.`tags` )
|
||||
AGAINST (:query IN BOOLEAN MODE)";
|
||||
}
|
||||
|
||||
$values = ['query' => $query];
|
||||
|
||||
$account_type = $args['account_type'] ?? 'All';
|
||||
if ($account_type != 'All') {
|
||||
$sql_where .= '
|
||||
AND `account_type` = :account_type';
|
||||
$values['account_type'] = $account_type;
|
||||
}
|
||||
|
||||
$profiles = $this->profileModel->getListForDisplay($pager->getItemsPerPage(), $pager->getStart(), $sql_where, $values);
|
||||
|
||||
$count = $this->profileModel->getCountForDisplay($sql_where, $values);
|
||||
|
||||
$vars = [
|
||||
'query' => $originalQuery,
|
||||
'page' => $pager->getPage(),
|
||||
'itemsperpage' => $pager->getItemsPerPage(),
|
||||
'count' => $count,
|
||||
'profiles' => $profiles
|
||||
];
|
||||
|
||||
// Render index view
|
||||
return $response->withJson($vars);
|
||||
}
|
||||
}
|
||||
80
src/classes/Controllers/Api/Submit.php
Normal file
80
src/classes/Controllers/Api/Submit.php
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Directory\Controllers\Api;
|
||||
|
||||
use Slim\Http\Request;
|
||||
use Slim\Http\Response;
|
||||
|
||||
/**
|
||||
* @author Hypolite Petovan <mrpetovan@gmail.com>
|
||||
*/
|
||||
class Submit
|
||||
{
|
||||
/**
|
||||
* @var \Atlas\Pdo\Connection
|
||||
*/
|
||||
private $atlas;
|
||||
/**
|
||||
* @var \Friendica\Directory\Models\ProfilePollQueue
|
||||
*/
|
||||
private $profilePollQueueModel;
|
||||
/**
|
||||
* @var \Psr\Log\LoggerInterface
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
public function __construct(
|
||||
\Atlas\Pdo\Connection $atlas,
|
||||
\Friendica\Directory\Models\ProfilePollQueue $profilePollQueueModel,
|
||||
\Psr\Log\LoggerInterface $logger
|
||||
)
|
||||
{
|
||||
$this->atlas = $atlas;
|
||||
$this->profilePollQueueModel = $profilePollQueueModel;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function execute(Request $request, Response $response): Response
|
||||
{
|
||||
try {
|
||||
$hexUrl = filter_input(INPUT_GET, 'url');
|
||||
if (!$hexUrl) {
|
||||
throw new \Exception('Missing url GET parameter', 400);
|
||||
}
|
||||
|
||||
$url = strtolower(hex2bin($hexUrl));
|
||||
|
||||
$this->logger->info('Received profile URL: ' . $url);
|
||||
|
||||
$host = parse_url($url, PHP_URL_HOST);
|
||||
if (!$host) {
|
||||
$this->logger->warning('Missing hostname in received profile URL: ' . $url);
|
||||
throw new \Exception('Missing hostname', 400);
|
||||
}
|
||||
|
||||
if (!\Friendica\Directory\Utils\Network::isPublicHost($host)) {
|
||||
$this->logger->warning('Private/reserved IP in received profile URL: ' . $url);
|
||||
throw new \Exception('Private/reserved hostname', 400);
|
||||
}
|
||||
|
||||
$profileUriInfo = \Friendica\Directory\Models\Profile::extractInfoFromProfileUrl($url);
|
||||
if (!$profileUriInfo) {
|
||||
$this->logger->warning('Invalid received profile URL: ' . $url);
|
||||
throw new \Exception('Invalid Profile URL', 400);
|
||||
}
|
||||
|
||||
$this->atlas->perform(
|
||||
'INSERT INTO `server_poll_queue` SET `base_url` = :base_url ON DUPLICATE KEY UPDATE `request_count` = `request_count` + 1',
|
||||
['base_url' => $profileUriInfo['server_uri']]
|
||||
);
|
||||
|
||||
$this->profilePollQueueModel->add($url);
|
||||
|
||||
$this->logger->info('Successfully received profile URL');
|
||||
} catch (\Exception $ex) {
|
||||
$response = $response->withStatus($ex->getCode(), $ex->getMessage());
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
60
src/classes/Controllers/Api/SyncPull.php
Normal file
60
src/classes/Controllers/Api/SyncPull.php
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Directory\Controllers\Api;
|
||||
|
||||
use Slim\Http\Request;
|
||||
use Slim\Http\Response;
|
||||
|
||||
/**
|
||||
* @author Hypolite Petovan <mrpetovan@gmail.com>
|
||||
*/
|
||||
class SyncPull
|
||||
{
|
||||
/**
|
||||
* @var \Atlas\Pdo\Connection
|
||||
*/
|
||||
private $atlas;
|
||||
/**
|
||||
* @var \Psr\Log\LoggerInterface
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
public function __construct(
|
||||
\Atlas\Pdo\Connection $atlas,
|
||||
\Psr\Log\LoggerInterface $logger
|
||||
)
|
||||
{
|
||||
$this->atlas = $atlas;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function execute(Request $request, Response $response, array $args): Response
|
||||
{
|
||||
$since = $args['since'] ?? null;
|
||||
|
||||
$stmt = 'SELECT `profile_url`
|
||||
FROM `profile` p
|
||||
JOIN `server` s ON s.`id` = p.`server_id`
|
||||
WHERE p.`available`
|
||||
AND NOT p.`hidden`
|
||||
AND s.`available`
|
||||
AND NOT s.`hidden`';
|
||||
$values = [];
|
||||
|
||||
if ($since) {
|
||||
$stmt .= '
|
||||
AND p.`updated` >= FROM_UNIXTIME(:since)';
|
||||
$values['since'] = [$since, \PDO::PARAM_INT];
|
||||
}
|
||||
|
||||
$profiles = $this->atlas->fetchColumn($stmt, $values);
|
||||
|
||||
$response = $response->withJson([
|
||||
'now' => time(),
|
||||
'count' => count($profiles),
|
||||
'results' => $profiles
|
||||
]);
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
136
src/classes/Controllers/Console.php
Normal file
136
src/classes/Controllers/Console.php
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Directory\Controllers;
|
||||
|
||||
use Monolog\Logger;
|
||||
|
||||
/**
|
||||
* Description of Console
|
||||
*
|
||||
* @author Hypolite Petovan <mrpetovan@gmail.com>
|
||||
*/
|
||||
class Console extends \Asika\SimpleConsole\Console
|
||||
{
|
||||
/**
|
||||
* @var \Slim\Container
|
||||
*/
|
||||
protected $container;
|
||||
|
||||
// Disables the default help handling
|
||||
protected $helpOptions = [];
|
||||
protected $customHelpOptions = ['h', 'help', '?'];
|
||||
|
||||
protected $routes = [
|
||||
'directory-add' => \Friendica\Directory\Routes\Console\DirectoryAdd::class,
|
||||
'directory-poll' => \Friendica\Directory\Routes\Console\DirectoryPoll::class,
|
||||
'profile-hide' => \Friendica\Directory\Routes\Console\ProfileHide::class,
|
||||
'profile-poll' => \Friendica\Directory\Routes\Console\ProfilePoll::class,
|
||||
'server-hide' => \Friendica\Directory\Routes\Console\ServerHide::class,
|
||||
'server-poll' => \Friendica\Directory\Routes\Console\ServerPoll::class,
|
||||
'install' => \Friendica\Directory\Routes\Console\Install::class,
|
||||
'updatedb' => \Friendica\Directory\Routes\Console\UpdateDb::class,
|
||||
'dbupdate' => \Friendica\Directory\Routes\Console\UpdateDb::class,
|
||||
];
|
||||
|
||||
public function __construct(\Slim\Container $container, ?array $argv = null)
|
||||
{
|
||||
parent::__construct($argv);
|
||||
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
protected function getHelp()
|
||||
{
|
||||
$commandList = '';
|
||||
foreach ($this->routes as $command => $class) {
|
||||
$this->out($class);
|
||||
|
||||
$commandList .= ' ' . $command . ' ' . $class::description . "\n";
|
||||
}
|
||||
|
||||
$help = <<<HELP
|
||||
Usage: bin/console [--version] [-h|--help|-?] <command> [<args>] [-v]
|
||||
|
||||
Commands:
|
||||
$commandList
|
||||
|
||||
Options:
|
||||
-h|--help|-? Show help information
|
||||
-v Show more debug information.
|
||||
HELP;
|
||||
return $help;
|
||||
}
|
||||
|
||||
protected function doExecute()
|
||||
{
|
||||
$showHelp = false;
|
||||
$subHelp = false;
|
||||
$command = null;
|
||||
|
||||
if ($this->getOption('version')) {
|
||||
//$this->out('Friendica Console version ' . FRIENDICA_VERSION);
|
||||
|
||||
return 0;
|
||||
} elseif ((count($this->options) === 0 || $this->getOption($this->customHelpOptions) === true || $this->getOption($this->customHelpOptions) === 1) && count($this->args) === 0
|
||||
) {
|
||||
$showHelp = true;
|
||||
} elseif (count($this->args) >= 2 && $this->getArgument(0) == 'help') {
|
||||
$command = $this->getArgument(1);
|
||||
$subHelp = true;
|
||||
array_shift($this->args);
|
||||
array_shift($this->args);
|
||||
} elseif (count($this->args) >= 1) {
|
||||
$command = $this->getArgument(0);
|
||||
array_shift($this->args);
|
||||
}
|
||||
|
||||
if (is_null($command)) {
|
||||
$this->out($this->getHelp());
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Increasing the logger level if -v is provided
|
||||
if ($this->getOption('v')) {
|
||||
/** @var \Monolog\Logger $logger */
|
||||
$handler = $this->container->get('logger')->popHandler();
|
||||
|
||||
$handler->setLevel(\Monolog\Logger::DEBUG);
|
||||
|
||||
$this->container->get('logger')->pushHandler($handler);
|
||||
}
|
||||
|
||||
$console = $this->getSubConsole($command);
|
||||
|
||||
if ($subHelp) {
|
||||
$console->setOption($this->customHelpOptions, true);
|
||||
}
|
||||
|
||||
return $console->execute();
|
||||
}
|
||||
|
||||
private function getSubConsole($command): \Asika\SimpleConsole\Console
|
||||
{
|
||||
$this->container->get('logger')->debug('Command: ' . $command);
|
||||
|
||||
if (!isset($this->routes[$command])) {
|
||||
throw new \Asika\SimpleConsole\CommandArgsException('Command ' . $command . ' doesn\'t exist');
|
||||
}
|
||||
|
||||
$subargs = $this->args;
|
||||
array_unshift($subargs, $this->executable);
|
||||
|
||||
$routeClassName = $this->routes[$command];
|
||||
|
||||
$consoleRoute = new $routeClassName($this->container);
|
||||
|
||||
/** @var \Asika\SimpleConsole\Console $subconsole */
|
||||
$subconsole = $consoleRoute($subargs);
|
||||
|
||||
foreach ($this->options as $name => $value) {
|
||||
$subconsole->setOption($name, $value);
|
||||
}
|
||||
|
||||
return $subconsole;
|
||||
}
|
||||
|
||||
}
|
||||
71
src/classes/Controllers/Console/DirectoryAdd.php
Normal file
71
src/classes/Controllers/Console/DirectoryAdd.php
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Directory\Controllers\Console;
|
||||
|
||||
/**
|
||||
* @author Hypolite Petovan <mrpetovan@gmail.com>
|
||||
*/
|
||||
class DirectoryAdd extends \Asika\SimpleConsole\Console
|
||||
{
|
||||
/**
|
||||
* @var \Atlas\Pdo\Connection
|
||||
*/
|
||||
protected $atlas;
|
||||
|
||||
protected $helpOptions = ['h', 'help', '?'];
|
||||
|
||||
public function __construct(
|
||||
\Atlas\Pdo\Connection $atlas,
|
||||
?array $argv = null
|
||||
)
|
||||
{
|
||||
parent::__construct($argv);
|
||||
|
||||
$this->atlas = $atlas;
|
||||
}
|
||||
|
||||
protected function getHelp()
|
||||
{
|
||||
$help = <<<HELP
|
||||
console directory-add - Adds provided directory to queue
|
||||
Usage
|
||||
bin/console directory-add <directory_url> [-h|--help|-?] [-v]
|
||||
|
||||
Description
|
||||
Adds provided directory to queue
|
||||
|
||||
Options
|
||||
-h|--help|-? Show help information
|
||||
-v Show more debug information.
|
||||
HELP;
|
||||
return $help;
|
||||
}
|
||||
|
||||
protected function doExecute()
|
||||
{
|
||||
if (count($this->args) == 0) {
|
||||
$this->out($this->getHelp());
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (count($this->args) > 1) {
|
||||
throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
|
||||
}
|
||||
|
||||
$directory_url = $this->getArgument(0);
|
||||
|
||||
$result = $this->atlas->perform('INSERT IGNORE INTO `directory_poll_queue` SET
|
||||
`directory_url` = :directory_url',
|
||||
['directory_url' => $directory_url]
|
||||
);
|
||||
|
||||
if (!$result) {
|
||||
throw new \RuntimeException('Unable to add repository with URL: ' . $directory_url);
|
||||
}
|
||||
|
||||
$this->out('Successfully added the repository to the queue.');
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
66
src/classes/Controllers/Console/DirectoryPoll.php
Normal file
66
src/classes/Controllers/Console/DirectoryPoll.php
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Directory\Controllers\Console;
|
||||
|
||||
/**
|
||||
* @author Hypolite Petovan <mrpetovan@gmail.com>
|
||||
*/
|
||||
class DirectoryPoll extends \Asika\SimpleConsole\Console
|
||||
{
|
||||
/**
|
||||
* @var \Atlas\Pdo\Connection
|
||||
*/
|
||||
protected $atlas;
|
||||
/**
|
||||
* @var \Friendica\Directory\Pollers\Directory
|
||||
*/
|
||||
protected $pollDirectory;
|
||||
|
||||
protected $helpOptions = ['h', 'help', '?'];
|
||||
|
||||
public function __construct(
|
||||
\Atlas\Pdo\Connection $atlas,
|
||||
\Friendica\Directory\Pollers\Directory $pollDirectory,
|
||||
?array $argv = null
|
||||
)
|
||||
{
|
||||
parent::__construct($argv);
|
||||
|
||||
$this->atlas = $atlas;
|
||||
$this->pollDirectory = $pollDirectory;
|
||||
}
|
||||
|
||||
protected function getHelp()
|
||||
{
|
||||
$help = <<<HELP
|
||||
console directory-poll - Polls provided directory
|
||||
Usage
|
||||
bin/console directory-poll <directory_url> [-h|--help|-?] [-v]
|
||||
|
||||
Description
|
||||
Polls provided directory
|
||||
|
||||
Options
|
||||
-h|--help|-? Show help information
|
||||
-v Show more debug information.
|
||||
HELP;
|
||||
return $help;
|
||||
}
|
||||
|
||||
protected function doExecute()
|
||||
{
|
||||
if (count($this->args) == 0) {
|
||||
$this->out($this->getHelp());
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (count($this->args) > 1) {
|
||||
throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
|
||||
}
|
||||
|
||||
$this->pollDirectory->__invoke($this->getArgument(0));
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
158
src/classes/Controllers/Console/Install.php
Normal file
158
src/classes/Controllers/Console/Install.php
Normal file
|
|
@ -0,0 +1,158 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Directory\Controllers\Console;
|
||||
|
||||
use Atlas\Pdo\Connection;
|
||||
use Monolog\Logger;
|
||||
use Seld\CliPrompt\CliPrompt;
|
||||
|
||||
/**
|
||||
* @author Hypolite Petovan <mrpetovan@gmail.com>
|
||||
*/
|
||||
class Install extends \Asika\SimpleConsole\Console
|
||||
{
|
||||
/**
|
||||
* @var Logger
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
protected $helpOptions = ['h', 'help', '?'];
|
||||
|
||||
public function __construct(
|
||||
Logger $logger,
|
||||
?array $argv = null
|
||||
)
|
||||
{
|
||||
parent::__construct($argv);
|
||||
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
protected function getHelp()
|
||||
{
|
||||
$help = <<<HELP
|
||||
console install - Install directory
|
||||
Usage
|
||||
bin/console install <server_url> [-h|--help|-?] [-v]
|
||||
|
||||
Description
|
||||
Install directory
|
||||
|
||||
Options
|
||||
-h|--help|-? Show help information
|
||||
-v Show more debug information.
|
||||
HELP;
|
||||
return $help;
|
||||
}
|
||||
|
||||
protected function doExecute()
|
||||
{
|
||||
if (count($this->args) == 0) {
|
||||
$this->out($this->getHelp());
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (count($this->args) > 1) {
|
||||
throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
|
||||
}
|
||||
|
||||
$this->out('Friendica Directory Install Wizard');
|
||||
$this->out('==================================');
|
||||
|
||||
$config_file_path = __DIR__ . '/../../../../config/local.json';
|
||||
|
||||
if (is_file($config_file_path)) {
|
||||
throw new \RuntimeException('Local config file already exists, did you want to run "bin/console dbupdate" ?');
|
||||
}
|
||||
|
||||
if (!is_writable(dirname($config_file_path))) {
|
||||
throw new \RuntimeException('The config/ directory isn\'t writable, please check file permissions.');
|
||||
}
|
||||
|
||||
$this->out('Warning: This will override any existing database!');
|
||||
|
||||
|
||||
do {
|
||||
$this->out('Please enter your database hostname [localhost] ', false);
|
||||
|
||||
$host = CliPrompt::prompt();
|
||||
|
||||
if (!$host) {
|
||||
$host = 'localhost';
|
||||
}
|
||||
|
||||
do {
|
||||
$this->out('Please enter your database username: ', false);
|
||||
|
||||
$user = CliPrompt::prompt();
|
||||
} while (!$user);
|
||||
|
||||
$this->out('Please enter your database password: ', false);
|
||||
|
||||
$pass = CliPrompt::hiddenPrompt();
|
||||
|
||||
do {
|
||||
$this->out('Please enter your database name: ', false);
|
||||
|
||||
$base = CliPrompt::prompt();
|
||||
} while (!$base);
|
||||
|
||||
$localSettings = [
|
||||
'database' => [
|
||||
'driver' => 'mysql',
|
||||
'hostname' => $host,
|
||||
'database' => $base,
|
||||
'username' => $user,
|
||||
'password' => $pass,
|
||||
]
|
||||
];
|
||||
|
||||
try {
|
||||
$dsn = "{$localSettings['database']['driver']}:dbname={$localSettings['database']['database']};host={$localSettings['database']['hostname']}";
|
||||
|
||||
Connection::new($dsn, $localSettings['database']['username'], $localSettings['database']['password']);
|
||||
|
||||
break;
|
||||
} catch (\Exception $ex) {
|
||||
$this->logger->error($ex->getMessage());
|
||||
} catch (\Throwable $e) {
|
||||
$this->logger->error($e->getMessage());
|
||||
}
|
||||
} while (true);
|
||||
|
||||
$result = file_put_contents($config_file_path, json_encode($localSettings, JSON_PRETTY_PRINT));
|
||||
|
||||
if (!$result) {
|
||||
throw new \RuntimeException('Unable to write to config/local.json, please check writing permissions.');
|
||||
}
|
||||
|
||||
$this->out('Local config file successfully created.');
|
||||
|
||||
$this->out('Initializing database schema...');
|
||||
|
||||
$connectionUri = new \ByJG\Util\Uri("mysql://$user:$pass@$host/$base");
|
||||
|
||||
$migration = new \ByJG\DbMigration\Migration($connectionUri, __DIR__ . '/../../../sql/');
|
||||
|
||||
$migration->registerDatabase('mysql', \ByJG\DbMigration\Database\MySqlDatabase::class);
|
||||
|
||||
$migration->reset();
|
||||
|
||||
$this->out('Done.');
|
||||
|
||||
$this->out(<<<'STDOUT'
|
||||
|
||||
Note: You still need to manually set up a cronjob like the following on *nix:
|
||||
* * * * * cd /path/to/friendica-directory && bin/cron
|
||||
|
||||
======
|
||||
To populate your directory, you can either:
|
||||
- Add a new remote directory to pull from with "bin/console directory-add <directory URL>".
|
||||
- Add it as the main directory in your Friendica admin settings.
|
||||
STDOUT
|
||||
);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
79
src/classes/Controllers/Console/ProfileHide.php
Normal file
79
src/classes/Controllers/Console/ProfileHide.php
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Directory\Controllers\Console;
|
||||
|
||||
use Friendica\Directory\Models\Profile;
|
||||
|
||||
/**
|
||||
* @author Hypolite Petovan <mrpetovan@gmail.com>
|
||||
*/
|
||||
class ProfileHide extends \Asika\SimpleConsole\Console
|
||||
{
|
||||
/**
|
||||
* @var \Atlas\Pdo\Connection
|
||||
*/
|
||||
protected $atlas;
|
||||
|
||||
protected $helpOptions = ['h', 'help', '?'];
|
||||
|
||||
public function __construct(
|
||||
\Atlas\Pdo\Connection $atlas,
|
||||
?array $argv = null
|
||||
)
|
||||
{
|
||||
parent::__construct($argv);
|
||||
|
||||
$this->atlas = $atlas;
|
||||
}
|
||||
|
||||
protected function getHelp()
|
||||
{
|
||||
$help = <<<HELP
|
||||
console profile-hide - Toggle profile hidden status
|
||||
Usage
|
||||
bin/console profile-hide <profile_url> [-h|--help|-?] [-v]
|
||||
|
||||
Description
|
||||
Toggle profile hidden status
|
||||
|
||||
Options
|
||||
-h|--help|-? Show help information
|
||||
-v Show more debug information.
|
||||
HELP;
|
||||
return $help;
|
||||
}
|
||||
|
||||
protected function doExecute()
|
||||
{
|
||||
if (count($this->args) == 0) {
|
||||
$this->out($this->getHelp());
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (count($this->args) > 1) {
|
||||
throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
|
||||
}
|
||||
|
||||
$profile_url = trim($this->getArgument(0), '/');
|
||||
|
||||
$profileInfo = Profile::extractInfoFromProfileUrl($profile_url);
|
||||
if (!$profileInfo) {
|
||||
throw new \RuntimeException('Invalid profile with URL: ' . $profile_url);
|
||||
}
|
||||
|
||||
$profile = $this->atlas->fetchOne('SELECT * FROM `profile` WHERE `addr` = :addr', ['addr' => $profileInfo['addr']]);
|
||||
if (!$profile) {
|
||||
throw new \RuntimeException('Unknown profile with URL: ' . $profile_url);
|
||||
}
|
||||
|
||||
$result = $this->atlas->fetchAffected('UPDATE `profile` SET `hidden` = 1 - `hidden` WHERE `id` = :id', ['id' => [$profile['id'], \PDO::PARAM_INT]]);
|
||||
if (!$result) {
|
||||
throw new \RuntimeException('Unable to update profile with ID: ' . $profile['id']);
|
||||
}
|
||||
|
||||
$this->out('Profile successfully ' . ($profile['hidden'] ? 'visible' : 'hidden'));
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
57
src/classes/Controllers/Console/ProfilePoll.php
Normal file
57
src/classes/Controllers/Console/ProfilePoll.php
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Directory\Controllers\Console;
|
||||
|
||||
/**
|
||||
* @author Hypolite Petovan <mrpetovan@gmail.com>
|
||||
*/
|
||||
class ProfilePoll extends \Asika\SimpleConsole\Console
|
||||
{
|
||||
/**
|
||||
* @var \Friendica\Directory\Pollers\Profile
|
||||
*/
|
||||
protected $pollProfile;
|
||||
|
||||
protected $helpOptions = ['h', 'help', '?'];
|
||||
|
||||
public function __construct(\Friendica\Directory\Pollers\Profile $pollProfile, ?array $argv = null)
|
||||
{
|
||||
parent::__construct($argv);
|
||||
|
||||
$this->pollProfile = $pollProfile;
|
||||
}
|
||||
|
||||
protected function getHelp()
|
||||
{
|
||||
$help = <<<HELP
|
||||
console profile-poll - Polls provided profile
|
||||
Usage
|
||||
bin/console profile-poll <profile_url> [-h|--help|-?] [-v]
|
||||
|
||||
Description
|
||||
Polls provided profile
|
||||
|
||||
Options
|
||||
-h|--help|-? Show help information
|
||||
-v Show more debug information.
|
||||
HELP;
|
||||
return $help;
|
||||
}
|
||||
|
||||
protected function doExecute()
|
||||
{
|
||||
if (count($this->args) == 0) {
|
||||
$this->out($this->getHelp());
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (count($this->args) > 1) {
|
||||
throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
|
||||
}
|
||||
|
||||
$this->pollProfile->__invoke($this->getArgument(0));
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
81
src/classes/Controllers/Console/ServerHide.php
Normal file
81
src/classes/Controllers/Console/ServerHide.php
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Directory\Controllers\Console;
|
||||
|
||||
/**
|
||||
* @author Hypolite Petovan <mrpetovan@gmail.com>
|
||||
*/
|
||||
class ServerHide extends \Asika\SimpleConsole\Console
|
||||
{
|
||||
/**
|
||||
* @var \Atlas\Pdo\Connection
|
||||
*/
|
||||
protected $atlas;
|
||||
|
||||
/**
|
||||
* @var \Friendica\Directory\Models\Server
|
||||
*/
|
||||
protected $serverModel;
|
||||
|
||||
protected $helpOptions = ['h', 'help', '?'];
|
||||
|
||||
public function __construct(
|
||||
\Atlas\Pdo\Connection $atlas,
|
||||
\Friendica\Directory\Models\Server $serverModel,
|
||||
?array $argv = null
|
||||
)
|
||||
{
|
||||
parent::__construct($argv);
|
||||
|
||||
$this->atlas = $atlas;
|
||||
$this->serverModel = $serverModel;
|
||||
}
|
||||
|
||||
protected function getHelp()
|
||||
{
|
||||
$help = <<<HELP
|
||||
console server-hide - Toggle server hidden status
|
||||
Usage
|
||||
bin/console server-hide <server_url> [-h|--help|-?] [-v]
|
||||
|
||||
Description
|
||||
Toggle server hidden status
|
||||
|
||||
Options
|
||||
-h|--help|-? Show help information
|
||||
-v Show more debug information.
|
||||
HELP;
|
||||
return $help;
|
||||
}
|
||||
|
||||
protected function doExecute()
|
||||
{
|
||||
if (count($this->args) == 0) {
|
||||
$this->out($this->getHelp());
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (count($this->args) > 1) {
|
||||
throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
|
||||
}
|
||||
|
||||
$server_url = trim($this->getArgument(0), '/');
|
||||
|
||||
$server = $this->serverModel->getByUrlAlias($server_url);
|
||||
|
||||
if (!$server) {
|
||||
throw new \RuntimeException('Unknown server with URL: ' . $server_url);
|
||||
}
|
||||
|
||||
$result = $this->atlas->perform('UPDATE `server` SET `hidden` = 1 - `hidden` WHERE `id` = :id', ['id' => [$server['id'], \PDO::PARAM_INT]]);
|
||||
|
||||
if (!$result) {
|
||||
throw new \RuntimeException('Unable to update server with ID: ' . $server['id']);
|
||||
}
|
||||
|
||||
$this->out('Server successfully ' . ($server['hidden'] ? 'visible' : 'hidden'));
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
66
src/classes/Controllers/Console/ServerPoll.php
Normal file
66
src/classes/Controllers/Console/ServerPoll.php
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Directory\Controllers\Console;
|
||||
|
||||
/**
|
||||
* @author Hypolite Petovan <mrpetovan@gmail.com>
|
||||
*/
|
||||
class ServerPoll extends \Asika\SimpleConsole\Console
|
||||
{
|
||||
/**
|
||||
* @var \Atlas\Pdo\Connection
|
||||
*/
|
||||
protected $atlas;
|
||||
/**
|
||||
* @var \Friendica\Directory\Pollers\Server
|
||||
*/
|
||||
protected $pollServer;
|
||||
|
||||
protected $helpOptions = ['h', 'help', '?'];
|
||||
|
||||
public function __construct(
|
||||
\Atlas\Pdo\Connection $atlas,
|
||||
\Friendica\Directory\Pollers\Server $pollServer,
|
||||
?array $argv = null
|
||||
)
|
||||
{
|
||||
parent::__construct($argv);
|
||||
|
||||
$this->atlas = $atlas;
|
||||
$this->pollServer = $pollServer;
|
||||
}
|
||||
|
||||
protected function getHelp()
|
||||
{
|
||||
$help = <<<HELP
|
||||
console server-poll - Polls provided server
|
||||
Usage
|
||||
bin/console server-poll <server_url> [-h|--help|-?] [-v]
|
||||
|
||||
Description
|
||||
Polls provided server
|
||||
|
||||
Options
|
||||
-h|--help|-? Show help information
|
||||
-v Show more debug information.
|
||||
HELP;
|
||||
return $help;
|
||||
}
|
||||
|
||||
protected function doExecute()
|
||||
{
|
||||
if (count($this->args) == 0) {
|
||||
$this->out($this->getHelp());
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (count($this->args) > 1) {
|
||||
throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
|
||||
}
|
||||
|
||||
$this->pollServer->__invoke($this->getArgument(0));
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
72
src/classes/Controllers/Console/UpdateDb.php
Normal file
72
src/classes/Controllers/Console/UpdateDb.php
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Directory\Controllers\Console;
|
||||
|
||||
use Monolog\Logger;
|
||||
|
||||
/**
|
||||
* @author Hypolite Petovan <mrpetovan@gmail.com>
|
||||
*/
|
||||
class UpdateDb extends \Asika\SimpleConsole\Console
|
||||
{
|
||||
/**
|
||||
* @var Logger
|
||||
*/
|
||||
protected $logger;
|
||||
/**
|
||||
* @var \ByJG\DbMigration\Migration
|
||||
*/
|
||||
protected $migration;
|
||||
|
||||
protected $helpOptions = ['h', 'help', '?'];
|
||||
|
||||
public function __construct(
|
||||
Logger $logger,
|
||||
\ByJG\DbMigration\Migration $migration,
|
||||
?array $argv = null
|
||||
)
|
||||
{
|
||||
parent::__construct($argv);
|
||||
|
||||
$this->logger = $logger;
|
||||
$this->migration = $migration;
|
||||
}
|
||||
|
||||
protected function getHelp()
|
||||
{
|
||||
$help = <<<HELP
|
||||
console updatedb - Update database schema
|
||||
Usage
|
||||
bin/console updatedb <server_url> [-h|--help|-?] [-v]
|
||||
|
||||
Description
|
||||
Update database schema
|
||||
|
||||
Options
|
||||
-h|--help|-? Show help information
|
||||
-v Show more debug information.
|
||||
HELP;
|
||||
return $help;
|
||||
}
|
||||
|
||||
protected function doExecute()
|
||||
{
|
||||
if (count($this->args) == 0) {
|
||||
$this->out($this->getHelp());
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (count($this->args) > 1) {
|
||||
throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
|
||||
}
|
||||
|
||||
$this->out('Updating database schema to latest version...');
|
||||
|
||||
$this->migration->up();
|
||||
|
||||
$this->out('Database schema migrated to version ' . $this->migration->getCurrentVersion()['version']);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
224
src/classes/Controllers/Cron.php
Normal file
224
src/classes/Controllers/Cron.php
Normal file
|
|
@ -0,0 +1,224 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Directory\Controllers;
|
||||
|
||||
/**
|
||||
* @author Hypolite Petovan <mrpetovan@gmail.com>
|
||||
*/
|
||||
class Cron
|
||||
{
|
||||
/**
|
||||
* @var \Psr\Log\LoggerInterface
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
/**
|
||||
* @var \Friendica\Directory\Pollers\Profile
|
||||
*/
|
||||
protected $profilePoller;
|
||||
|
||||
/**
|
||||
* @var \Friendica\Directory\Pollers\Server
|
||||
*/
|
||||
protected $serverPoller;
|
||||
|
||||
/**
|
||||
* @var \Friendica\Directory\Pollers\Directory
|
||||
*/
|
||||
protected $directoryPoller;
|
||||
|
||||
/**
|
||||
* @var \Atlas\Pdo\Connection
|
||||
*/
|
||||
protected $atlas;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $settings = [
|
||||
'directory_poll_delay' => 3600, // 1 hour
|
||||
'server_poll_delay' => 24 * 3600, // 1 day
|
||||
'profile_poll_delay' => 24 * 3600, // 1 day
|
||||
|
||||
'directory_poll_retry_base_delay' => 600, // 10 minutes
|
||||
'server_poll_retry_base_delay' => 1800, // 30 minutes
|
||||
'profile_poll_retry_base_delay' => 1800, // 30 minutes
|
||||
];
|
||||
|
||||
/**
|
||||
* @var float
|
||||
*/
|
||||
private $startTime;
|
||||
|
||||
public function __construct(
|
||||
\Atlas\Pdo\Connection $atlas,
|
||||
\Friendica\Directory\Pollers\Profile $profilePoller,
|
||||
\Friendica\Directory\Pollers\Server $serverPoller,
|
||||
\Friendica\Directory\Pollers\Directory $directoryPoller,
|
||||
\Psr\Log\LoggerInterface $logger,
|
||||
array $settings = []
|
||||
)
|
||||
{
|
||||
$this->atlas = $atlas;
|
||||
$this->profilePoller = $profilePoller;
|
||||
$this->serverPoller = $serverPoller;
|
||||
$this->directoryPoller = $directoryPoller;
|
||||
$this->logger = $logger;
|
||||
$this->settings = array_merge($this->settings, $settings);
|
||||
$this->startTime = microtime(true);
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$this->logger->info('Start Cron job');
|
||||
|
||||
$this->pollDirectories(9);
|
||||
|
||||
$this->pollServers(24);
|
||||
|
||||
$this->pollProfiles(58);
|
||||
|
||||
$this->logger->info('Stop Cron job');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $time_limit
|
||||
*/
|
||||
private function pollDirectories(int $time_limit = null): void
|
||||
{
|
||||
$directories = $this->atlas->fetchAll(
|
||||
'SELECT `directory_url`, `retries_count`
|
||||
FROM `directory_poll_queue`
|
||||
WHERE `next_poll` <= NOW()
|
||||
ORDER BY ISNULL(`last_polled`) DESC'
|
||||
);
|
||||
|
||||
foreach ($directories as $directory) {
|
||||
if ($time_limit && microtime(true) - $this->startTime > $time_limit) {
|
||||
break;
|
||||
}
|
||||
|
||||
$directory_poll_result = $this->directoryPoller->__invoke($directory['directory_url']);
|
||||
|
||||
if ($directory_poll_result) {
|
||||
$new_retries_count = 0;
|
||||
$poll_delay = $this->settings['directory_poll_delay'];
|
||||
} else {
|
||||
$new_retries_count = $directory['retries_count'] + 1;
|
||||
$poll_delay = $this->settings['directory_poll_retry_base_delay'] * pow($new_retries_count, 3);
|
||||
}
|
||||
|
||||
$this->atlas->perform(
|
||||
'UPDATE `directory_poll_queue` SET
|
||||
`last_polled` = NOW(),
|
||||
`next_poll` = DATE_ADD(NOW(), INTERVAL :seconds SECOND),
|
||||
`retries_count` = :retries_count
|
||||
WHERE `directory_url` = :directory_url',
|
||||
[
|
||||
'seconds' => [$poll_delay, \PDO::PARAM_INT],
|
||||
'directory_url' => $directory['directory_url'],
|
||||
'retries_count' => [$new_retries_count, \PDO::PARAM_INT]
|
||||
]
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private function pollServers(int $time_limit = null): void
|
||||
{
|
||||
$servers = $this->atlas->fetchAll(
|
||||
'SELECT `base_url`, `retries_count`
|
||||
FROM `server_poll_queue`
|
||||
WHERE `next_poll` <= NOW()
|
||||
ORDER BY ISNULL(`last_polled`) DESC, `request_count` DESC'
|
||||
);
|
||||
|
||||
foreach ($servers as $server_queue_item) {
|
||||
if ($time_limit && microtime(true) - $this->startTime > $time_limit) {
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
$new_base_url = null;
|
||||
|
||||
$server_id = $this->serverPoller->__invoke($server_queue_item['base_url']);
|
||||
|
||||
if ($server_id) {
|
||||
$new_base_url = $this->atlas->fetchValue('SELECT `base_url` FROM `server` WHERE `id` = :id', ['id' => [$server_id, \PDO::PARAM_INT]]);
|
||||
}
|
||||
|
||||
if ($new_base_url && $new_base_url != $server_queue_item['base_url']) {
|
||||
$this->atlas->perform('INSERT IGNORE INTO `server_poll_queue` SET `base_url` = :base_url', ['base_url' => $new_base_url]);
|
||||
$this->logger->info('New base URL: ' . $server_queue_item['base_url'] . ' => ' . $new_base_url);
|
||||
}
|
||||
|
||||
if ($new_base_url == $server_queue_item['base_url']) {
|
||||
$new_retries_count = 0;
|
||||
$poll_delay = $this->settings['server_poll_delay'];
|
||||
} else {
|
||||
$new_retries_count = $server_queue_item['retries_count'] + 1;
|
||||
$poll_delay = $this->settings['server_poll_retry_base_delay'] * pow($new_retries_count, 3);
|
||||
}
|
||||
|
||||
$this->atlas->perform(
|
||||
'UPDATE `server_poll_queue` SET
|
||||
`last_polled` = NOW(),
|
||||
`next_poll` = DATE_ADD(NOW(), INTERVAL :seconds SECOND),
|
||||
`retries_count` = :retries_count,
|
||||
`request_count` = 0
|
||||
WHERE `base_url` = :base_url',
|
||||
[
|
||||
'seconds' => [$poll_delay, \PDO::PARAM_INT],
|
||||
'base_url' => $server_queue_item['base_url'],
|
||||
'retries_count' => [$new_retries_count, \PDO::PARAM_INT]
|
||||
]
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error($e->getMessage() . ': ' . $e->getTraceAsString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function pollProfiles(int $time_limit = null): void
|
||||
{
|
||||
$profiles = $this->atlas->fetchAll(
|
||||
'SELECT `profile_url`, `retries_count`
|
||||
FROM `profile_poll_queue`
|
||||
WHERE `next_poll` <= NOW()
|
||||
ORDER BY RAND() ASC'
|
||||
);
|
||||
|
||||
foreach ($profiles as $profile) {
|
||||
if ($time_limit && microtime(true) - $this->startTime > $time_limit) {
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
$profile_poll_result = $this->profilePoller->__invoke($profile['profile_url']);
|
||||
|
||||
if ($profile_poll_result) {
|
||||
$new_retries_count = 0;
|
||||
$poll_delay = $this->settings['profile_poll_delay'];
|
||||
} else {
|
||||
$new_retries_count = $profile['retries_count'] + 1;
|
||||
$poll_delay = $this->settings['profile_poll_retry_base_delay'] * pow($new_retries_count, 3);
|
||||
}
|
||||
|
||||
|
||||
$this->atlas->perform('UPDATE `profile_poll_queue` SET
|
||||
`last_polled` = NOW(),
|
||||
`next_poll` = DATE_ADD(NOW(), INTERVAL :seconds SECOND),
|
||||
`retries_count` = :retries_count
|
||||
WHERE `profile_url` = :profile_url',
|
||||
[
|
||||
'seconds' => [$poll_delay, \PDO::PARAM_INT],
|
||||
'profile_url' => $profile['profile_url'],
|
||||
'retries_count' => [$new_retries_count, \PDO::PARAM_INT]
|
||||
]
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error($e->getMessage() . ': ' . $e->getTraceAsString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
86
src/classes/Controllers/Web/Directory.php
Normal file
86
src/classes/Controllers/Web/Directory.php
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Directory\Controllers\Web;
|
||||
|
||||
use \Friendica\Directory\Content\Pager;
|
||||
use \Friendica\Directory\Views\Widget\PopularCountries;
|
||||
use \Friendica\Directory\Views\Widget\PopularTags;
|
||||
use PDO;
|
||||
use Slim\Http\Request;
|
||||
use Slim\Http\Response;
|
||||
|
||||
/**
|
||||
* @author Hypolite Petovan <mrpetovan@gmail.com>
|
||||
*/
|
||||
class Directory
|
||||
{
|
||||
/**
|
||||
* @var \Atlas\Pdo\Connection
|
||||
*/
|
||||
private $atlas;
|
||||
/**
|
||||
* @var \Friendica\Directory\Models\Profile
|
||||
*/
|
||||
private $profileModel;
|
||||
/**
|
||||
* @var \Friendica\Directory\Views\Widget\AccountTypeTabs
|
||||
*/
|
||||
private $accountTypeTabs;
|
||||
/**
|
||||
* @var \Friendica\Directory\Views\PhpRenderer
|
||||
*/
|
||||
private $renderer;
|
||||
/**
|
||||
* @var \Friendica\Directory\Content\L10n
|
||||
*/
|
||||
private $l10n;
|
||||
|
||||
public function __construct(
|
||||
\Atlas\Pdo\Connection $atlas,
|
||||
\Friendica\Directory\Models\Profile $profileModel,
|
||||
\Friendica\Directory\Views\Widget\AccountTypeTabs $accountTypeTabs,
|
||||
\Friendica\Directory\Views\PhpRenderer $renderer,
|
||||
\Friendica\Directory\Content\L10n $l10n
|
||||
)
|
||||
{
|
||||
$this->atlas = $atlas;
|
||||
$this->profileModel = $profileModel;
|
||||
$this->accountTypeTabs = $accountTypeTabs;
|
||||
$this->renderer = $renderer;
|
||||
$this->l10n = $l10n;
|
||||
}
|
||||
|
||||
public function render(Request $request, Response $response, array $args): Response
|
||||
{
|
||||
$popularTags = new PopularTags($this->atlas, $this->renderer);
|
||||
$popularCountries = new PopularCountries($this->atlas, $this->renderer);
|
||||
|
||||
$pager = new Pager($this->l10n, $request, 20);
|
||||
|
||||
$condition = '';
|
||||
$values = [];
|
||||
if (!empty($args['account_type'])) {
|
||||
$condition = '`account_type` = :account_type';
|
||||
$values = ['account_type' => $args['account_type']];
|
||||
}
|
||||
|
||||
$profiles = $this->profileModel->getListForDisplay($pager->getItemsPerPage(), $pager->getStart(), $condition, $values);
|
||||
|
||||
$count = $this->profileModel->getCountForDisplay($condition, $values);
|
||||
|
||||
$vars = [
|
||||
'title' => $this->l10n->t('People'),
|
||||
'profiles' => $profiles,
|
||||
'pager_full' => $pager->renderFull($count),
|
||||
'pager_minimal' => $pager->renderMinimal($count),
|
||||
'accountTypeTabs' => $this->accountTypeTabs->render('directory', $args['account_type'] ?? ''),
|
||||
'popularTags' => $popularTags->render(),
|
||||
'popularCountries' => $popularCountries->render(),
|
||||
];
|
||||
|
||||
$content = $this->renderer->fetch('directory.phtml', $vars);
|
||||
|
||||
// Render index view
|
||||
return $this->renderer->render($response, 'layout.phtml', ['baseUrl' => $request->getUri()->getBaseUrl(), 'content' => $content]);
|
||||
}
|
||||
}
|
||||
54
src/classes/Controllers/Web/Photo.php
Normal file
54
src/classes/Controllers/Web/Photo.php
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Directory\Controllers\Web;
|
||||
|
||||
use Slim\Http\Request;
|
||||
use Slim\Http\Response;
|
||||
|
||||
/**
|
||||
* @author Hypolite Petovan <mrpetovan@gmail.com>
|
||||
*/
|
||||
class Photo
|
||||
{
|
||||
/**
|
||||
* @var \Atlas\Pdo\Connection
|
||||
*/
|
||||
private $atlas;
|
||||
|
||||
public function __construct(
|
||||
\Atlas\Pdo\Connection $atlas
|
||||
)
|
||||
{
|
||||
$this->atlas = $atlas;
|
||||
}
|
||||
|
||||
public function render(Request $request, Response $response, array $args): Response
|
||||
{
|
||||
$data = $this->atlas->fetchValue(
|
||||
'SELECT `data` FROM `photo` WHERE `profile_id` = :profile_id',
|
||||
['profile_id' => $args['profile_id']]
|
||||
);
|
||||
|
||||
if (!$data) {
|
||||
$data = file_get_contents('public/images/default-profile-sm.jpg');
|
||||
}
|
||||
|
||||
//Try and cache our result.
|
||||
$etag = md5($data);
|
||||
|
||||
$response = $response
|
||||
->withHeader('Etag', $etag)
|
||||
->withHeader('Expires', date('D, d M Y H:i:s' . ' GMT', strtotime('now + 1 week')))
|
||||
->withHeader('Cache-Control', 'max-age=' . intval(7 * 24 * 3600))
|
||||
->withoutHeader('Pragma');
|
||||
|
||||
if ($request->getServerParam('HTTP_IF_NONE_MATCH') == $etag) {
|
||||
$response = $response->withStatus(304, 'Not Modified');
|
||||
} else {
|
||||
$response = $response->withHeader('Content-type', 'image/jpeg');
|
||||
$response->getBody()->write($data);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
94
src/classes/Controllers/Web/Search.php
Normal file
94
src/classes/Controllers/Web/Search.php
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Directory\Controllers\Web;
|
||||
|
||||
use \Friendica\Directory\Content\Pager;
|
||||
use PDO;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
/**
|
||||
* @author Hypolite Petovan <mrpetovan@gmail.com>
|
||||
*/
|
||||
class Search
|
||||
{
|
||||
/**
|
||||
* @var \Atlas\Pdo\Connection
|
||||
*/
|
||||
private $atlas;
|
||||
/**
|
||||
* @var \Friendica\Directory\Models\Profile
|
||||
*/
|
||||
private $profileModel;
|
||||
/**
|
||||
* @var \Friendica\Directory\Views\PhpRenderer
|
||||
*/
|
||||
private $renderer;
|
||||
/**
|
||||
* @var \Friendica\Directory\Views\Widget\AccountTypeTabs
|
||||
*/
|
||||
private $accountTypeTabs;
|
||||
/**
|
||||
* @var \Friendica\Directory\Content\L10n
|
||||
*/
|
||||
private $l10n;
|
||||
|
||||
public function __construct(
|
||||
\Atlas\Pdo\Connection $atlas,
|
||||
\Friendica\Directory\Models\Profile $profileModel,
|
||||
\Friendica\Directory\Views\Widget\AccountTypeTabs $accountTypeTabs,
|
||||
\Friendica\Directory\Views\PhpRenderer $renderer,
|
||||
\Friendica\Directory\Content\L10n $l10n
|
||||
)
|
||||
{
|
||||
$this->atlas = $atlas;
|
||||
$this->profileModel = $profileModel;
|
||||
$this->accountTypeTabs = $accountTypeTabs;
|
||||
$this->renderer = $renderer;
|
||||
$this->l10n = $l10n;
|
||||
}
|
||||
|
||||
public function render(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args): \Slim\Http\Response
|
||||
{
|
||||
$pager = new Pager($this->l10n, $request, 20);
|
||||
|
||||
$originalQuery = $query = filter_input(INPUT_GET, 'q');
|
||||
|
||||
$field = filter_input(INPUT_GET, 'field', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW & FILTER_FLAG_STRIP_HIGH);
|
||||
|
||||
if ($field) {
|
||||
$query .= '%';
|
||||
$sql_where = '`' . $field . '` LIKE :query';
|
||||
} else {
|
||||
$sql_where = "MATCH (p.`name`, p.`pdesc`, p.`profile_url`, p.`locality`, p.`region`, p.`country`, p.`tags` )
|
||||
AGAINST (:query IN BOOLEAN MODE)";
|
||||
}
|
||||
|
||||
$values = ['query' => $query];
|
||||
|
||||
$account_type = $args['account_type'] ?? '';
|
||||
if ($account_type) {
|
||||
$sql_where .= '
|
||||
AND `account_type` = :account_type';
|
||||
$values['account_type'] = $account_type;
|
||||
}
|
||||
|
||||
$profiles = $this->profileModel->getListForDisplay($pager->getItemsPerPage(), $pager->getStart(), $sql_where, $values);
|
||||
|
||||
$count = $this->profileModel->getCountForDisplay($sql_where, $values);
|
||||
|
||||
$vars = [
|
||||
'query' => $originalQuery,
|
||||
'count' => $count,
|
||||
'accountTypeTabs' => $this->accountTypeTabs->render('search', $account_type, ['q' => $originalQuery]),
|
||||
'profiles' => $profiles,
|
||||
'pager_full' => $pager->renderFull($count),
|
||||
'pager_minimal' => $pager->renderMinimal($count),
|
||||
];
|
||||
|
||||
$content = $this->renderer->fetch('search.phtml', $vars);
|
||||
|
||||
// Render index view
|
||||
return $this->renderer->render($response, 'layout.phtml', ['baseUrl' => $request->getUri()->getBaseUrl(), 'content' => $content, 'noNavSearch' => true]);
|
||||
}
|
||||
}
|
||||
100
src/classes/Controllers/Web/Servers.php
Normal file
100
src/classes/Controllers/Web/Servers.php
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Directory\Controllers\Web;
|
||||
|
||||
use \Friendica\Directory\Content\Pager;
|
||||
use PDO;
|
||||
use Slim\Http\Request;
|
||||
use Slim\Http\Response;
|
||||
|
||||
/**
|
||||
* @author Hypolite Petovan <mrpetovan@gmail.com>
|
||||
*/
|
||||
class Servers
|
||||
{
|
||||
/**
|
||||
* @var \Atlas\Pdo\Connection
|
||||
*/
|
||||
private $atlas;
|
||||
/**
|
||||
* @var \Friendica\Directory\Views\PhpRenderer
|
||||
*/
|
||||
private $renderer;
|
||||
/**
|
||||
* @var \Friendica\Directory\Content\L10n
|
||||
*/
|
||||
private $l10n;
|
||||
/**
|
||||
* @var \Psr\SimpleCache\CacheInterface
|
||||
*/
|
||||
private $simplecache;
|
||||
|
||||
public function __construct(
|
||||
\Atlas\Pdo\Connection $atlas,
|
||||
\Friendica\Directory\Views\PhpRenderer $renderer,
|
||||
\Friendica\Directory\Content\L10n $l10n,
|
||||
\Psr\SimpleCache\CacheInterface $simplecache
|
||||
)
|
||||
{
|
||||
$this->atlas = $atlas;
|
||||
$this->renderer = $renderer;
|
||||
$this->l10n = $l10n;
|
||||
$this->simplecache = $simplecache;
|
||||
}
|
||||
|
||||
public function render(Request $request, Response $response): Response
|
||||
{
|
||||
$stable_version = $this->simplecache->get('stable_version');
|
||||
if (!$stable_version) {
|
||||
$stable_version = trim(file_get_contents('https://git.friendi.ca/friendica/friendica/raw/branch/master/VERSION'));
|
||||
$this->simplecache->set('stable_version', $stable_version);
|
||||
}
|
||||
|
||||
$dev_version = $this->simplecache->get('dev_version');
|
||||
if (!$dev_version) {
|
||||
$dev_version = trim(file_get_contents('https://git.friendi.ca/friendica/friendica/raw/branch/develop/VERSION'));
|
||||
$this->simplecache->set('dev_version', $dev_version);
|
||||
}
|
||||
|
||||
$pager = new Pager($this->l10n, $request, 20);
|
||||
|
||||
$stmt = 'SELECT *
|
||||
FROM `server` s
|
||||
WHERE `reg_policy` = "REGISTER_OPEN"
|
||||
AND `available`
|
||||
AND NOT `hidden`
|
||||
ORDER BY `health_score` DESC, `ssl_state` DESC, `info` != "" DESC, `dt_last_probed` DESC
|
||||
LIMIT :start, :limit';
|
||||
$servers = $this->atlas->fetchAll($stmt, [
|
||||
'start' => [$pager->getStart(), PDO::PARAM_INT],
|
||||
'limit' => [$pager->getItemsPerPage(), PDO::PARAM_INT]
|
||||
]);
|
||||
|
||||
foreach ($servers as $key => $server) {
|
||||
$servers[$key]['user_count'] = $this->atlas->fetchValue(
|
||||
'SELECT COUNT(*) FROM `profile` WHERE `available` AND `server_id` = :server_id',
|
||||
['server_id' => [$server['id'], PDO::PARAM_INT]]
|
||||
);
|
||||
}
|
||||
|
||||
$stmt = 'SELECT COUNT(*)
|
||||
FROM `server` s
|
||||
WHERE `reg_policy` = "REGISTER_OPEN"
|
||||
AND `available`
|
||||
AND NOT `hidden`';
|
||||
$count = $this->atlas->fetchValue($stmt);
|
||||
|
||||
$vars = [
|
||||
'title' => $this->l10n->t('Public Servers'),
|
||||
'servers' => $servers,
|
||||
'pager' => $pager->renderFull($count),
|
||||
'stable_version' => $stable_version,
|
||||
'dev_version' => $dev_version,
|
||||
];
|
||||
|
||||
$content = $this->renderer->fetch('servers.phtml', $vars);
|
||||
|
||||
// Render index view
|
||||
return $this->renderer->render($response, 'layout.phtml', ['baseUrl' => $request->getUri()->getBaseUrl(), 'content' => $content]);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue