Add language filter to server list #68

Merged
MrPetovan merged 3 commits from task/3-add-server-language-filter into stable 2020-09-27 21:45:42 +02:00
11 changed files with 113 additions and 15 deletions

View file

@ -16,6 +16,7 @@
"ext-curl": "*", "ext-curl": "*",
"ext-gd": "*", "ext-gd": "*",
"ext-json": "*", "ext-json": "*",
"ext-pdo": "*",
"asika/simple-console": "^1.0", "asika/simple-console": "^1.0",
"atlas/pdo": "^1.1", "atlas/pdo": "^1.1",
"boronczyk/localization-middleware": "^1.4", "boronczyk/localization-middleware": "^1.4",

23
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "32addc73d4c20158a63877c57d3654dc", "content-hash": "a8bc1750aa6136e10ea28799a0974f6b",
"packages": [ "packages": [
{ {
"name": "asika/simple-console", "name": "asika/simple-console",
@ -1773,6 +1773,7 @@
"keywords": [ "keywords": [
"tokenizer" "tokenizer"
], ],
"abandoned": true,
"time": "2017-12-04T08:55:13+00:00" "time": "2017-12-04T08:55:13+00:00"
}, },
{ {
@ -3056,6 +3057,20 @@
"Xdebug", "Xdebug",
"performance" "performance"
], ],
"funding": [
{
"url": "https://packagist.com",
"type": "custom"
},
{
"url": "https://github.com/composer",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/composer/composer",
"type": "tidelift"
}
],
"time": "2020-06-04T11:16:35+00:00" "time": "2020-06-04T11:16:35+00:00"
}, },
{ {
@ -3750,10 +3765,12 @@
"php": ">=7.1.0", "php": ">=7.1.0",
"ext-curl": "*", "ext-curl": "*",
"ext-gd": "*", "ext-gd": "*",
"ext-json": "*" "ext-json": "*",
"ext-pdo": "*"
}, },
"platform-dev": [], "platform-dev": [],
"platform-overrides": { "platform-overrides": {
"php": "7.1.0" "php": "7.1.0"
} },
"plugin-api-version": "1.1.0"
} }

View file

@ -4,7 +4,7 @@ namespace Friendica\Directory\Controllers\Web;
use Friendica\Directory\Content\Pager; use Friendica\Directory\Content\Pager;
use Friendica\Directory\Views\Widget\PopularCountries; use Friendica\Directory\Views\Widget\PopularCountries;
use Friendica\Directory\Views\Widget\PopularLanguages; use Friendica\Directory\Views\Widget\PopularProfileLanguages;
use Friendica\Directory\Views\Widget\PopularTags; use Friendica\Directory\Views\Widget\PopularTags;
use Slim\Http\Request; use Slim\Http\Request;
use Slim\Http\Response; use Slim\Http\Response;
@ -54,7 +54,7 @@ class Directory extends BaseController
{ {
$popularTags = new PopularTags($this->atlas, $this->renderer); $popularTags = new PopularTags($this->atlas, $this->renderer);
$popularCountries = new PopularCountries($this->atlas, $this->renderer); $popularCountries = new PopularCountries($this->atlas, $this->renderer);
$popularLanguages = new PopularLanguages($this->atlas, $this->renderer); $popularLanguages = new PopularProfileLanguages($this->atlas, $this->renderer);
$pager = new Pager($this->l10n, $request, 20); $pager = new Pager($this->l10n, $request, 20);

View file

@ -2,7 +2,8 @@
namespace Friendica\Directory\Controllers\Web; namespace Friendica\Directory\Controllers\Web;
use \Friendica\Directory\Content\Pager; use Friendica\Directory\Content\Pager;
use Friendica\Directory\Views\Widget\PopularServerLanguages;
use PDO; use PDO;
use Slim\Http\Request; use Slim\Http\Request;
use Slim\Http\Response; use Slim\Http\Response;
@ -58,19 +59,32 @@ class Servers extends BaseController
$rc_version = str_replace('-dev', '-rc', $dev_version); $rc_version = str_replace('-dev', '-rc', $dev_version);
$popularLanguages = new PopularServerLanguages($this->atlas, $this->renderer);
$pager = new Pager($this->l10n, $request, 20); $pager = new Pager($this->l10n, $request, 20);
$sql_where = '';
$values = [];
if ($args['language']) {
$sql_where .= '
AND LEFT(`language`, 2) = LEFT(:language, 2)';
$values['language'] = $args['language'];
}
$stmt = 'SELECT * $stmt = 'SELECT *
FROM `server` s FROM `server` s
WHERE `reg_policy` != "REGISTER_CLOSED" WHERE `reg_policy` != "REGISTER_CLOSED"
AND `available` AND `available`
AND NOT `hidden` AND NOT `hidden`
' . $sql_where . '
ORDER BY `health_score` DESC, `ssl_state` DESC, `info` != "" DESC, `last_seen` DESC ORDER BY `health_score` DESC, `ssl_state` DESC, `info` != "" DESC, `last_seen` DESC
LIMIT :start, :limit'; LIMIT :start, :limit';
$servers = $this->atlas->fetchAll($stmt, [ $listValues = array_merge($values, [
'start' => [$pager->getStart(), PDO::PARAM_INT], 'start' => [$pager->getStart(), PDO::PARAM_INT],
'limit' => [$pager->getItemsPerPage(), PDO::PARAM_INT] 'limit' => [$pager->getItemsPerPage(), PDO::PARAM_INT]
]); ]);
$servers = $this->atlas->fetchAll($stmt, $listValues);
foreach ($servers as $key => $server) { foreach ($servers as $key => $server) {
$servers[$key]['user_count'] = $this->atlas->fetchValue( $servers[$key]['user_count'] = $this->atlas->fetchValue(
@ -83,16 +97,20 @@ LIMIT :start, :limit';
FROM `server` s FROM `server` s
WHERE `reg_policy` != "REGISTER_CLOSED" WHERE `reg_policy` != "REGISTER_CLOSED"
AND `available` AND `available`
AND NOT `hidden`'; AND NOT `hidden`
$count = $this->atlas->fetchValue($stmt); ' . $sql_where;
$count = $this->atlas->fetchValue($stmt, $values);
$vars = [ $vars = [
'title' => $this->l10n->gettext('Public Servers'), 'title' => $this->l10n->gettext('Public Servers'),
'total' => $count,
'language' => $args['language'],
'servers' => $servers, 'servers' => $servers,
'pager' => $pager->renderFull($count), 'pager' => $pager->renderFull($count),
'stable_version' => $stable_version, 'stable_version' => $stable_version,
'rc_version' => $rc_version, 'rc_version' => $rc_version,
'dev_version' => $dev_version, 'dev_version' => $dev_version,
'popularLanguages' => $popularLanguages->render(),
]; ];
$content = $this->renderer->fetch('servers.phtml', $vars); $content = $this->renderer->fetch('servers.phtml', $vars);

View file

@ -5,7 +5,7 @@ namespace Friendica\Directory\Views\Widget;
/** /**
* @author Hypolite Petovan <hypolite@mrpetovan.com> * @author Hypolite Petovan <hypolite@mrpetovan.com>
*/ */
class PopularLanguages class PopularProfileLanguages
{ {
/** /**
* @var \Atlas\Pdo\Connection * @var \Atlas\Pdo\Connection
@ -36,6 +36,6 @@ class PopularLanguages
'languages' => $languages 'languages' => $languages
]; ];
return $this->renderer->fetch('widget/popularlanguages.phtml', $vars); return $this->renderer->fetch('widget/popularprofilelanguages.phtml', $vars);
} }
} }

View file

@ -0,0 +1,44 @@
<?php
namespace Friendica\Directory\Views\Widget;
/**
* @author Hypolite Petovan <hypolite@mrpetovan.com>
*/
class PopularServerLanguages
{
/**
* @var \Atlas\Pdo\Connection
*/
private $connection;
/**
* @var \Friendica\Directory\Views\PhpRenderer
*/
private $renderer;
public function __construct(\Atlas\Pdo\Connection $connection, \Friendica\Directory\Views\PhpRenderer $renderer)
{
$this->connection = $connection;
$this->renderer = $renderer;
}
public function render(): string
{
$stmt = 'SELECT LEFT(`language`, 2) AS `language`, COUNT(*) AS `total`
FROM `server`
WHERE `reg_policy` != "REGISTER_CLOSED"
AND `available`
AND NOT `hidden`
AND `language` IS NOT NULL
GROUP BY LEFT(`language`, 2)
ORDER BY `total` DESC
LIMIT 10';
$languages = $this->connection->fetchAll($stmt);
$vars = [
'languages' => $languages
];
return $this->renderer->fetch('widget/popularserverlanguages.phtml', $vars);
}
}

View file

@ -10,7 +10,7 @@ use Slim\Http\Response;
*/ */
$app->get('/servers/surprise', \Friendica\Directory\Routes\Http\Surprise::class); $app->get('/servers/surprise', \Friendica\Directory\Routes\Http\Surprise::class);
$app->get('/servers', \Friendica\Directory\Routes\Web\Servers::class)->setName('servers'); $app->get('/servers[/{language}]', \Friendica\Directory\Routes\Web\Servers::class)->setName('servers');
$app->get('/search[/{account_type}]', function (Request $request, Response $response, $args) { $app->get('/search[/{account_type}]', function (Request $request, Response $response, $args) {
if ($request->getAttribute('negotiation')->getMediaType() == 'application/json') { if ($request->getAttribute('negotiation')->getMediaType() == 'application/json') {

View file

@ -1,4 +1,9 @@
<h1><?php echo $this->e($title) ?></h1> <h1><?php echo $this->e($title) ?> (<?php echo $this->e($total)?>)</h1>
<?php if ($language):?>
<p><?php echo $this->__('Filtered by language:')?> <span class="badge badge-secondary"><?php echo $this->e(\Friendica\Directory\Utils\L10n::localeToLanguageString($language)) ?></span> <a href="<?php echo $this->r('servers')?>"><?php echo $this->__('Clear language filter')?></a></p>
<?php else:?>
<?php echo $popularLanguages?>
<?php endif;?>
<nav aria-label="<?php echo $this->__('Top servers pagination')?>"> <nav aria-label="<?php echo $this->__('Top servers pagination')?>">
<?php echo $this->fetch('sub/pager_full.phtml', $pager) ?> <?php echo $this->fetch('sub/pager_full.phtml', $pager) ?>
</nav> </nav>

View file

@ -41,10 +41,10 @@ $base_url_display = substr($base_url, strpos($base_url, '/') + 2);
<i class="fa fa-heartbeat"></i> <?php echo $server['health_score'] ?> <i class="fa fa-heartbeat"></i> <?php echo $server['health_score'] ?>
</span> </span>
<?php if ($server['language']):?> <?php if ($server['language']):?>
<span class="badge badge-secondary" title="<?php echo $this->__('Default Language')?>"> <a href="<?php echo $this->r('servers', ['language' => $server['language']])?>" class="badge badge-secondary" title="<?php echo $this->__('Default Language')?>">
<i class="fa fa-language" alt="<?php echo $this->__('Default Language')?>"></i> <i class="fa fa-language" alt="<?php echo $this->__('Default Language')?>"></i>
<?php echo $this->e(Friendica\Directory\Utils\L10n::localeToLanguageString($server['language'])) ?> <?php echo $this->e(Friendica\Directory\Utils\L10n::localeToLanguageString($server['language'])) ?>
</span> </a>
<?php endif;?> <?php endif;?>
<span class="badge badge-secondary" title="<?php echo $this->__('Known Users')?>"> <span class="badge badge-secondary" title="<?php echo $this->__('Known Users')?>">
<i class="fa fa-user"></i> <i class="fa fa-user"></i>

View file

@ -0,0 +1,13 @@
<div>
<h3><?php echo $this->__('Filter by language')?></h3>
<ul>
<?php foreach ($languages as $language): ?>
<li>
<a href="<?php echo $this->escapeHtmlAttr($this->r('servers', ['language' => $language['language']])) ?>">
<?php echo $this->e(Friendica\Directory\Utils\L10n::localeToLanguageString($language['language'])) ?>
(<?php echo $this->e($language['total'])?>)
</a>
</li>
<?php endforeach; ?>
</ul>
</div>