Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add named metadata event #41601

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions apps/files/lib/Command/Scan.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ protected function configure(): void {
->addOption(
'generate-metadata',
null,
InputOption::VALUE_NONE,
'Generate metadata for all scanned files'
InputOption::VALUE_OPTIONAL,
'Generate metadata for all scanned files; if specified only generate for named value',
''
)
->addOption(
'all',
Expand All @@ -122,7 +123,7 @@ protected function configure(): void {
);
}

protected function scanFiles(string $user, string $path, bool $scanMetadata, OutputInterface $output, bool $backgroundScan = false, bool $recursive = true, bool $homeOnly = false): void {
protected function scanFiles(string $user, string $path, ?string $scanMetadata, OutputInterface $output, bool $backgroundScan = false, bool $recursive = true, bool $homeOnly = false): void {
$connection = $this->reconnectToDatabase($output);
$scanner = new \OC\Files\Utils\Scanner(
$user,
Expand All @@ -136,11 +137,12 @@ protected function scanFiles(string $user, string $path, bool $scanMetadata, Out
$output->writeln("\tFile\t<info>$path</info>", OutputInterface::VERBOSITY_VERBOSE);
++$this->filesCounter;
$this->abortIfInterrupted();
if ($scanMetadata) {
if ($scanMetadata !== null) {
$node = $this->rootFolder->get($path);
$this->filesMetadataManager->refreshMetadata(
$node,
IFilesMetadataManager::PROCESS_LIVE | IFilesMetadataManager::PROCESS_BACKGROUND
($scanMetadata !== '') ? IFilesMetadataManager::PROCESS_NAMED : IFilesMetadataManager::PROCESS_LIVE | IFilesMetadataManager::PROCESS_BACKGROUND,
$scanMetadata
ArtificialOwl marked this conversation as resolved.
Show resolved Hide resolved
);
}
});
Expand Down Expand Up @@ -221,6 +223,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$this->initTools($output);

// getOption() logic on VALUE_OPTIONAL
$metadata = null; // null if --generate-metadata is not set, empty if option have no value, value if set
if ($input->getOption('generate-metadata') !== '') {
$metadata = $input->getOption('generate-metadata') ?? '';
}

$user_count = 0;
foreach ($users as $user) {
if (is_object($user)) {
Expand All @@ -230,7 +238,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
++$user_count;
if ($this->userManager->userExists($user)) {
$output->writeln("Starting scan for user $user_count out of $users_total ($user)");
$this->scanFiles($user, $path, $input->getOption('generate-metadata'), $output, $input->getOption('unscanned'), !$input->getOption('shallow'), $input->getOption('home-only'));
$this->scanFiles($user, $path, $metadata, $output, $input->getOption('unscanned'), !$input->getOption('shallow'), $input->getOption('home-only'));
$output->writeln('', OutputInterface::VERBOSITY_VERBOSE);
} else {
$output->writeln("<error>Unknown user $user_count $user</error>");
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@
'OCP\\FilesMetadata\\AMetadataEvent' => $baseDir . '/lib/public/FilesMetadata/AMetadataEvent.php',
'OCP\\FilesMetadata\\Event\\MetadataBackgroundEvent' => $baseDir . '/lib/public/FilesMetadata/Event/MetadataBackgroundEvent.php',
'OCP\\FilesMetadata\\Event\\MetadataLiveEvent' => $baseDir . '/lib/public/FilesMetadata/Event/MetadataLiveEvent.php',
'OCP\\FilesMetadata\\Event\\MetadataNamedEvent' => $baseDir . '/lib/public/FilesMetadata/Event/MetadataNamedEvent.php',
'OCP\\FilesMetadata\\Exceptions\\FilesMetadataException' => $baseDir . '/lib/public/FilesMetadata/Exceptions/FilesMetadataException.php',
'OCP\\FilesMetadata\\Exceptions\\FilesMetadataKeyFormatException' => $baseDir . '/lib/public/FilesMetadata/Exceptions/FilesMetadataKeyFormatException.php',
'OCP\\FilesMetadata\\Exceptions\\FilesMetadataNotFoundException' => $baseDir . '/lib/public/FilesMetadata/Exceptions/FilesMetadataNotFoundException.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\FilesMetadata\\AMetadataEvent' => __DIR__ . '/../../..' . '/lib/public/FilesMetadata/AMetadataEvent.php',
'OCP\\FilesMetadata\\Event\\MetadataBackgroundEvent' => __DIR__ . '/../../..' . '/lib/public/FilesMetadata/Event/MetadataBackgroundEvent.php',
'OCP\\FilesMetadata\\Event\\MetadataLiveEvent' => __DIR__ . '/../../..' . '/lib/public/FilesMetadata/Event/MetadataLiveEvent.php',
'OCP\\FilesMetadata\\Event\\MetadataNamedEvent' => __DIR__ . '/../../..' . '/lib/public/FilesMetadata/Event/MetadataNamedEvent.php',
'OCP\\FilesMetadata\\Exceptions\\FilesMetadataException' => __DIR__ . '/../../..' . '/lib/public/FilesMetadata/Exceptions/FilesMetadataException.php',
'OCP\\FilesMetadata\\Exceptions\\FilesMetadataKeyFormatException' => __DIR__ . '/../../..' . '/lib/public/FilesMetadata/Exceptions/FilesMetadataKeyFormatException.php',
'OCP\\FilesMetadata\\Exceptions\\FilesMetadataNotFoundException' => __DIR__ . '/../../..' . '/lib/public/FilesMetadata/Exceptions/FilesMetadataNotFoundException.php',
Expand Down
8 changes: 7 additions & 1 deletion lib/private/FilesMetadata/FilesMetadataManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
use OCP\Files\NotFoundException;
use OCP\FilesMetadata\Event\MetadataBackgroundEvent;
use OCP\FilesMetadata\Event\MetadataLiveEvent;
use OCP\FilesMetadata\Event\MetadataNamedEvent;
use OCP\FilesMetadata\Exceptions\FilesMetadataException;
use OCP\FilesMetadata\Exceptions\FilesMetadataNotFoundException;
use OCP\FilesMetadata\IFilesMetadataManager;
Expand Down Expand Up @@ -89,7 +90,8 @@ public function __construct(
*/
public function refreshMetadata(
Node $node,
int $process = self::PROCESS_LIVE
int $process = self::PROCESS_LIVE,
string $namedEvent = ''
): IFilesMetadata {
try {
$metadata = $this->metadataRequestService->getMetadataFromFileId($node->getId());
Expand All @@ -98,8 +100,12 @@ public function refreshMetadata(
}

// if $process is LIVE, we enforce LIVE
// if $process is NAMED, we go NAMED
// else BACKGROUND
if ((self::PROCESS_LIVE & $process) !== 0) {
$event = new MetadataLiveEvent($node, $metadata);
} elseif ((self::PROCESS_NAMED & $process) !== 0) {
$event = new MetadataNamedEvent($node, $metadata, $namedEvent);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So named event is always live, or always backgrounded?
Why the name is not an optional parameter for both MetadataLiveEvent and MetadataBackgroundEvent?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it would require a check condition on every Live/Background listeners

} else {
$event = new MetadataBackgroundEvent($node, $metadata);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/public/FilesMetadata/AMetadataEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ abstract class AMetadataEvent extends Event {
* @since 28.0.0
*/
public function __construct(
private Node $node,
private IFilesMetadata $metadata
protected Node $node,
protected IFilesMetadata $metadata
) {
parent::__construct();
}
Expand Down
74 changes: 74 additions & 0 deletions lib/public/FilesMetadata/Event/MetadataNamedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);
/**
* @copyright 2023 Maxence Lange <maxence@artificial-owl.com>
*
* @author Maxence Lange <maxence@artificial-owl.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCP\FilesMetadata\Event;

use OCP\Files\Node;
use OCP\FilesMetadata\AMetadataEvent;
use OCP\FilesMetadata\Model\IFilesMetadata;

/**
* MetadataNamedEvent is an event similar to MetadataBackgroundEvent completed with a target name,
* used to limit the refresh of metadata only listeners capable of filtering themselves out.
*
* Meaning that when using this event, your app must implement a filter on the event's registered
* name returned by getName()
*
* This event is mostly triggered when a registered name is added to the files scan
* i.e. ./occ files:scan --generate-metadata [name]
*
* @see AMetadataEvent::getMetadata()
* @see AMetadataEvent::getNode()
* @see MetadataNamedEvent::getName()
* @since 28.0.0
*/
class MetadataNamedEvent extends AMetadataEvent {
/**
* @param Node $node
* @param IFilesMetadata $metadata
* @param string $name name assigned to the event
*
* @since 28.0.0
*/
public function __construct(
Node $node,
IFilesMetadata $metadata,
private string $name = ''
) {
parent::__construct($node, $metadata);
}

/**
* get the assigned name for the event.
* This is used to know if your app is the called one when running the
* ./occ files:scan --generate-metadata [name]
*
* @return string
* @since 28.0.0
*/
public function getName(): string {
return $this->name;
}
}
7 changes: 6 additions & 1 deletion lib/public/FilesMetadata/IFilesMetadataManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ interface IFilesMetadataManager {
public const PROCESS_LIVE = 1;
/** @since 28.0.0 */
public const PROCESS_BACKGROUND = 2;
/** @since 28.0.0 */
public const PROCESS_NAMED = 4;

/**
* initiate the process of refreshing the metadata in relation to a node
Expand All @@ -54,15 +56,18 @@ interface IFilesMetadataManager {
*
* @param Node $node related node
* @param int $process type of process
* @param string $namedEvent limit process to a named event
*
* @return IFilesMetadata
* @see self::PROCESS_BACKGROUND
* @see self::PROCESS_LIVE
* @see self::PROCESS_NAMED
* @since 28.0.0
*/
public function refreshMetadata(
Node $node,
int $process = self::PROCESS_LIVE
int $process = self::PROCESS_LIVE,
string $namedEvent = ''
): IFilesMetadata;

/**
Expand Down
Loading