Skip to content

Commit

Permalink
Merge pull request #131 from nextcloud/nextcloud/25
Browse files Browse the repository at this point in the history
πŸ†• Support Nextcloud 25 with multi-branch support
  • Loading branch information
nickvergessen authored Oct 24, 2022
2 parents ff81c19 + 0edc948 commit 6f41373
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 34 deletions.
37 changes: 26 additions & 11 deletions .github/workflows/psalm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,22 @@

name: Static analysis

on:
pull_request:
push:
branches:
- master
- main
- stable*

concurrency:
group: psalm-${{ github.head_ref || github.run_id }}
on: pull_request

concurrency:
group: lint-psalm-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
static-analysis:
runs-on: ubuntu-latest
strategy:
# do not stop on another job's failure
fail-fast: false
matrix:
ocp-version: [ 'dev-master', 'dev-stable25' ]

name: Nextcloud
name: Nextcloud ${{ matrix.ocp-version }}
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -30,10 +29,26 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
tools: composer:v1
coverage: none

- name: Install dependencies
run: composer i

- name: Install dependencies
run: composer require --dev nextcloud/ocp:${{ matrix.ocp-version }} --ignore-platform-reqs

- name: Run coding standards check
run: composer run psalm

summary:
runs-on: ubuntu-latest
needs: static-analysis

if: always()

name: static-psalm-analysis-summary

steps:
- name: Summary status
run: if ${{ needs.static-analysis.result != 'success' }}; then exit 1; fi
5 changes: 3 additions & 2 deletions .github/workflows/update-nextcloud-ocp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ jobs:
strategy:
fail-fast: false
matrix:
branches: ["master", "stable25", "stable24", "stable23"]
branches: ["master"]
target: ["stable25"]

name: update-nextcloud-ocp-${{ matrix.branches }}

Expand All @@ -38,7 +39,7 @@ jobs:
run: composer install

- name: Composer update nextcloud/ocp
run: composer require --dev nextcloud/ocp:dev-${{ matrix.branches }}
run: composer require --dev nextcloud/ocp:dev-${{ matrix.target }}
continue-on-error: true

- name: Reset checkout dirs
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# Changelog
All notable changes to this project will be documented in this file.

## 1.9.0 – 2022-10-24
### Added
- Added an option to pipe the output to a file
- Added an option to save the output as JSON

### Changed
- Require Nextcloud 25

### Fixed
- Proper error handling when a user does not exist

## 1.8.0 – 2022-04-11
### Changed
- Nextcloud 24 support
Expand Down
4 changes: 2 additions & 2 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ $ sudo -u www-data ./occ usage-report:generate --verbose admin
```
]]></description>

<version>1.10.0</version>
<version>1.9.0</version>
<licence>agpl</licence>
<author>Joas Schilling</author>

Expand All @@ -39,7 +39,7 @@ $ sudo -u www-data ./occ usage-report:generate --verbose admin
<screenshot>https://raw.githubusercontent.com/nextcloud/user_usage_report/master/docs/screenshot.png</screenshot>

<dependencies>
<nextcloud min-version="26" max-version="26" />
<nextcloud min-version="25" max-version="25" />
</dependencies>

<commands>
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"phpunit/phpunit": "^9.5",
"nextcloud/coding-standard": "^1.0.0",
"vimeo/psalm": "^4.3.2",
"nextcloud/ocp": "dev-master"
"nextcloud/ocp": "dev-stable25"
},
"config": {
"optimize-autoloader": true,
Expand Down
15 changes: 7 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 51 additions & 4 deletions lib/Command/Generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\StreamOutput;

class Generate extends Command {

Expand Down Expand Up @@ -75,7 +76,7 @@ protected function configure(): void {
'field-separator',
'',
InputOption::VALUE_REQUIRED,
'Separator for the fields in the list',
'Separator for the fields in the list (Only used when output format is CSV)',
','
)
->addOption(
Expand All @@ -97,6 +98,19 @@ protected function configure(): void {
InputOption::VALUE_NONE,
'Should the display name be included in the report'
)
->addOption(
'output',
'',
InputOption::VALUE_REQUIRED,
'Output format (csv or json)',
'csv'
)
->addOption(
'output-file',
'O',
InputOption::VALUE_REQUIRED,
'Output file for stdout'
)
;
}

Expand All @@ -106,30 +120,63 @@ protected function configure(): void {
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$outputFile = $input->getOption('output-file');

if ($outputFile) {
$stream = fopen($outputFile, 'w');
$streamOutput = new StreamOutput($stream, $output->getVerbosity(), null, $output->getFormatter());
} else {
$streamOutput = $output;
}

$userId = $input->getArgument('user-id');
if ($userId) {
if (!$this->userManager->userExists($userId)) {
$output->writeln('<error>User with ID "' . $userId . '" could not be found.</error>');
return 1;
}
}

if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
$separator = $input->getOption('field-separator');

$header = [];
$header['user_id'] = '';
$data = '"user-id"'. $separator;
if ($input->getOption('display-name')) {
$header['display_name'] = '';
$data .= '"display name"'. $separator;
}
$header['date'] = '';
$data .= '"date as \'' . $input->getOption('date-format') . '\'"'. $separator;
if ($input->getOption('last-login')) {
$header['login'] = '';
$data .= '"last login date as \'' . $input->getOption('date-format') . '\'"'. $separator;
}
$header['quota'] = '';
$data .= '"assigned quota (5 GB)"' . $separator;
$header['used'] = '';
$data .= '"used quota (500 MB)"' . $separator;
$header['files'] = 0;
$data .= 'number of files' . $separator;
$header['shares'] = 0;
$data .= 'number of shares' . $separator;
$header['uploads'] = 0;
$data .= 'number of uploads' . $separator;
$header['downloads'] = 0;
$data .= 'number of downloads';
$output->writeln($data);

if ($input->getOption('output') === 'csv') {
$streamOutput->writeln($data);
} else {
$streamOutput->writeln(json_encode($header));
}
}

if ($input->getArgument('user-id')) {
$this->single->printReport($input, $output, $input->getArgument('user-id'));
$this->single->printReport($input, $streamOutput, $userId);
} else {
$this->all->printReport($input, $output);
$this->all->printReport($input, $output, $streamOutput);
}

return 0;
Expand Down
23 changes: 19 additions & 4 deletions lib/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,46 @@ protected function printRecord(InputInterface $input, OutputInterface $output, $
$this->timestamp = date($input->getOption('date-format'));
}

$jsonArray = ['user_id' => $userId];
$data = '"'. $userId . '"'. $separator;
if ($input->getOption('display-name')) {
$jsonArray['display_name'] = $report['display_name'];
$data .= '"' . $report['display_name'] . '"' . $separator;
}
$jsonArray['date'] = $this->timestamp;
$data .= '"'. $this->timestamp . '"'. $separator;
if ($input->getOption('last-login')) {
$data .= '"'. date($input->getOption('date-format'), $report['login']) . '"'. $separator;
$report['login'] = date($input->getOption('date-format'), $report['login']);
$jsonArray['login'] = $report['login'];
$data .= '"'. $report['login'] . '"'. $separator;
}

// ensure all fields we trying to print are set
// ensure all fields we are trying to print are set
$fields = ['quota', 'used', 'files', 'shares', 'uploads', 'downloads'];
foreach ($fields as $field) {
if (!isset($report[$field])) {
$report[$field] = '';
$report[$field] = 0;
}
}

$data .= (!is_numeric($report['quota']) ? '"'. $report['quota'] . '"' : $report['quota']). $separator;
$jsonArray['quota'] = $report['quota'];
$data .= (!is_numeric($report['used']) ? '"'. $report['used'] . '"' : $report['used']). $separator;
$jsonArray['used'] = $report['used'];
$data .= $report['files'] . $separator;
$jsonArray['files'] = $report['files'];
$data .= $report['shares'] . $separator;
$jsonArray['shares'] = $report['shares'];
$data .= $report['uploads'] . $separator;
$jsonArray['uploads'] = $report['uploads'];
$data .= $report['downloads'];
$jsonArray['downloads'] = $report['downloads'];

$output->writeln($data);
if ($input->getOption('output') === 'csv') {
$output->writeln($data);
} else {
$output->writeln(json_encode($jsonArray));
}
}

public function humanFileSize($bytes) {
Expand Down
5 changes: 3 additions & 2 deletions lib/Reports/AllUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ public function __construct(IDBConnection $connection, IConfig $config, IUserMan
/**
* @param InputInterface $input
* @param OutputInterface $output
* @param OutputInterface $streamOutput
*/
public function printReport(InputInterface $input, OutputInterface $output): void {
public function printReport(InputInterface $input, OutputInterface $output, OutputInterface $streamOutput): void {
$this->createQueries();

$default = [
Expand Down Expand Up @@ -126,7 +127,7 @@ public function printReport(InputInterface $input, OutputInterface $output): voi
$progress->clear();

foreach ($this->reports as $userId => $report) {
$this->printRecord($input, $output, $userId, $report);
$this->printRecord($input, $streamOutput, $userId, $report);
}
}

Expand Down
3 changes: 3 additions & 0 deletions lib/Reports/SingleUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public function printReport(InputInterface $input, OutputInterface $output, stri
);

$report['quota'] = $this->getUserQuota($userId);
if (is_numeric($report['quota'])) {
$report['quota'] = (int) $report['quota'];
}
if ($input->getOption('last-login')) {
$report['login'] = $this->getUserLastLogin($userId);
}
Expand Down

0 comments on commit 6f41373

Please sign in to comment.