4
0

user-info.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env php
  2. <?php
  3. declare(strict_types=1);
  4. require __DIR__ . '/_cli.php';
  5. const DATA_FORMAT = "%-7s | %-20s | %-5s | %-7s | %-25s | %-15s | %-10s | %-10s | %-10s | %-10s | %-10s | %-10s | %-5s | %-10s\n";
  6. $cliOptions = new class extends CliOptionsParser {
  7. /** @var array<int,string> $user */
  8. public array $user;
  9. public bool $header;
  10. public bool $json;
  11. public bool $humanReadable;
  12. public function __construct() {
  13. $this->addOption('user', (new CliOption('user'))->typeOfArrayOfString());
  14. $this->addOption('header', (new CliOption('header'))->withValueNone());
  15. $this->addOption('json', (new CliOption('json'))->withValueNone());
  16. $this->addOption('humanReadable', (new CliOption('human-readable', 'h'))->withValueNone());
  17. parent::__construct();
  18. }
  19. };
  20. if (!empty($cliOptions->errors)) {
  21. fail('FreshRSS error: ' . array_shift($cliOptions->errors) . "\n" . $cliOptions->usage);
  22. }
  23. $users = $cliOptions->user ?? listUsers();
  24. sort($users);
  25. $jsonOutput = [];
  26. if ($cliOptions->json) {
  27. $cliOptions->header = false;
  28. $cliOptions->humanReadable = false;
  29. }
  30. if ($cliOptions->header) {
  31. printf(
  32. DATA_FORMAT,
  33. 'default',
  34. 'user',
  35. 'admin',
  36. 'enabled',
  37. 'last user activity',
  38. 'space used',
  39. 'categories',
  40. 'feeds',
  41. 'reads',
  42. 'unreads',
  43. 'favourites',
  44. 'tags',
  45. 'lang',
  46. 'email'
  47. );
  48. }
  49. foreach ($users as $username) {
  50. $username = cliInitUser($username);
  51. $catDAO = FreshRSS_Factory::createCategoryDao($username);
  52. $feedDAO = FreshRSS_Factory::createFeedDao($username);
  53. $entryDAO = FreshRSS_Factory::createEntryDao($username);
  54. $tagDAO = FreshRSS_Factory::createTagDao($username);
  55. $databaseDAO = FreshRSS_Factory::createDatabaseDAO($username);
  56. $nbEntries = $entryDAO->countUnreadRead();
  57. $nbFavorites = $entryDAO->countUnreadReadFavorites();
  58. $feedList = $feedDAO->listFeedsIds();
  59. $data = [
  60. 'default' => $username === FreshRSS_Context::systemConf()->default_user ? '*' : '',
  61. 'user' => $username,
  62. 'admin' => FreshRSS_Context::userConf()->is_admin ? '*' : '',
  63. 'enabled' => FreshRSS_Context::userConf()->enabled ? '*' : '',
  64. 'last_user_activity' => FreshRSS_UserDAO::mtime($username),
  65. 'database_size' => $databaseDAO->size(),
  66. 'categories' => $catDAO->count(),
  67. 'feeds' => count($feedList),
  68. 'reads' => (int)$nbEntries['read'],
  69. 'unreads' => (int)$nbEntries['unread'],
  70. 'favourites' => (int)$nbFavorites['all'],
  71. 'tags' => $tagDAO->count(),
  72. 'lang' => FreshRSS_Context::userConf()->language,
  73. 'mail_login' => FreshRSS_Context::userConf()->mail_login,
  74. ];
  75. if ($cliOptions->humanReadable) { //Human format
  76. $data['last_user_activity'] = date('c', $data['last_user_activity']);
  77. $data['database_size'] = format_bytes($data['database_size']);
  78. }
  79. if ($cliOptions->json) {
  80. $data['default'] = !empty($data['default']);
  81. $data['admin'] = !empty($data['admin']);
  82. $data['enabled'] = !empty($data['enabled']);
  83. $data['last_user_activity'] = gmdate('Y-m-d\TH:i:s\Z', (int)$data['last_user_activity']);
  84. $jsonOutput[] = $data;
  85. } else {
  86. vprintf(DATA_FORMAT, $data);
  87. }
  88. }
  89. if ($cliOptions->json) {
  90. echo json_encode($jsonOutput), "\n";
  91. }
  92. done();