Move mod/nodeinfo to src/Module/Nodeinfo

This commit is contained in:
Philipp Holzer 2019-04-22 14:00:17 +02:00
parent 9d3a131204
commit 6056081d5a
No known key found for this signature in database
GPG key ID: 517BE60E2CE5C8A5
6 changed files with 295 additions and 223 deletions

75
src/Model/Nodeinfo.php Normal file
View file

@ -0,0 +1,75 @@
<?php
namespace Friendica\Model;
use Friendica\BaseObject;
use Friendica\Core\Addon;
use Friendica\Database\DBA;
use Friendica\Util\Network;
/**
* Model interaction for the nodeinfo
*/
class Nodeinfo extends BaseObject
{
/**
* Updates the info about the current node
*
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function update()
{
$app = self::getApp();
$config = $app->getConfig();
$logger = $app->getLogger();
// If the addon 'statistics_json' is enabled then disable it and activate nodeinfo.
if (Addon::isEnabled('statistics_json')) {
$config->set('system', 'nodeinfo', true);
$addon = 'statistics_json';
$addons = $config->get('system', 'addon');
if ($addons) {
$addons_arr = explode(',', str_replace(' ', '', $addons));
$idx = array_search($addon, $addons_arr);
if ($idx !== false) {
unset($addons_arr[$idx]);
Addon::uninstall($addon);
$config->set('system', 'addon', implode(', ', $addons_arr));
}
}
}
if (empty($config->get('system', 'nodeinfo'))) {
return;
}
$logger->info('cron_start');
$userStats = User::getStatistics();
$config->set('nodeinfo', 'total_users', $userStats['total_users']);
$config->set('nodeinfo', 'active_users_halfyear', $userStats['active_users_halfyear']);
$config->set('nodeinfo', 'active_users_monthly', $userStats['active_users_monthly']);
$logger->debug('user statistics', $userStats);
$local_posts = DBA::count('thread', ["`wall` AND NOT `deleted` AND `uid` != 0"]);
$config->set('nodeinfo', 'local_posts', $local_posts);
$logger->debug('thread statistics', ['local_posts' => $local_posts]);
$local_comments = DBA::count('item', ["`origin` AND `id` != `parent` AND NOT `deleted` AND `uid` != 0"]);
$config->set('nodeinfo', 'local_comments', $local_comments);
$logger->debug('item statistics', ['local_comments' => $local_comments]);
// Now trying to register
$url = 'http://the-federation.info/register/' . $app->getHostName();
$logger->debug('Check registering url', ['url' => $url]);
$ret = Network::fetchUrl($url);
$logger->debug('Check registering answer', ['answer' => $ret]);
$logger->info('cron_end');
}
}

View file

@ -967,4 +967,51 @@ class User
return $identities;
}
/**
* Returns statistical information about the current users of this node
*
* @return array
*
* @throws Exception
*/
public static function getStatistics()
{
$statistics = [
'total_users' => 0,
'active_users_halfyear' => 0,
'active_users_monthly' => 0,
];
$userStmt = DBA::p("SELECT `user`.`uid`, `user`.`login_date`, `contact`.`last-item`
FROM `user`
INNER JOIN `profile` ON `profile`.`uid` = `user`.`uid` AND `profile`.`is-default`
INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`
WHERE (`profile`.`publish` OR `profile`.`net-publish`) AND `user`.`verified`
AND NOT `user`.`blocked` AND NOT `user`.`account_removed`
AND NOT `user`.`account_expired`");
if (!DBA::isResult($userStmt)) {
return $statistics;
}
$halfyear = time() - (180 * 24 * 60 * 60);
$month = time() - (30 * 24 * 60 * 60);
while ($user = DBA::fetch($userStmt)) {
$statistics['total_users']++;
if ((strtotime($user['login_date']) > $halfyear) ||
(strtotime($user['last-item']) > $halfyear)) {
$statistics['active_users_halfyear']++;
}
if ((strtotime($user['login_date']) > $month) ||
(strtotime($user['last-item']) > $month)) {
$statistics['active_users_monthly']++;
}
}
return $statistics;
}
}