Files
nextcloud-server-mirror/apps/dav/lib/Search/TasksSearchProvider.php
Git'Fellow 4920bd1725 refactor: add PHP 8.3 types to BC-safe class constants
PHP 8.3 allows class constants to declare a type. Adopt it where doing so
cannot break third-party apps.

Typing an inheritable constant is a hard BC break: a subclass that
redeclares it untyped fails to load with "Type of C::FOO must be
compatible with P::FOO of type string". Changes are therefore limited to
constants that cannot be redeclared by a subclass:

  - private const (not inherited)
  - final public/protected const
  - constants declared in a final class or an enum

Interface constants, trait constants, and public/protected constants in
non-final (including abstract) classes are left untyped, as is all of
lib/public (OCP) and lib/unstable (NCU).

One review-requested exception: the public OBJECT_PREFIX/OBJECT_SUFFIX
constants of the app-internal CalDAV import helpers (TextImporter,
XmlImporter) are typed as well; these classes are not public API and
have no subclasses.

Only string, int and array are used. float is avoided because it would
silently coerce an int literal and change === comparisons. No constant
name or value is modified: every changed line adds only the type token,
so runtime behaviour is unchanged.

Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com>
2026-08-11 13:52:52 +01:00

182 lines
4.9 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\DAV\Search;
use OCA\DAV\CalDAV\CalDavBackend;
use OCP\IUser;
use OCP\Search\ISearchQuery;
use OCP\Search\SearchResult;
use OCP\Search\SearchResultEntry;
use Sabre\VObject\Component;
use Sabre\VObject\Component\VCalendar;
use Sabre\VObject\InvalidDataException;
use Sabre\VObject\ParseException;
use Sabre\VObject\Reader;
/**
* Class TasksSearchProvider
*
* @package OCA\DAV\Search
*/
class TasksSearchProvider extends ACalendarSearchProvider {
/**
* @var string[]
*/
private const array SEARCH_PROPERTIES = [
'SUMMARY',
'DESCRIPTION',
'CATEGORIES',
];
/**
* @var string[]
*/
private const array SEARCH_PARAMETERS = [];
/**
* @var string
*/
private const string COMPONENT_TYPE = 'VTODO';
/**
* @inheritDoc
*/
#[\Override]
public function getId(): string {
return 'tasks';
}
/**
* @inheritDoc
*/
#[\Override]
public function getName(): string {
return $this->l10n->t('Tasks');
}
/**
* @inheritDoc
*/
#[\Override]
public function getOrder(string $route, array $routeParameters): ?int {
if ($this->appManager->isEnabledForUser('tasks')) {
return $route === 'tasks.Page.index' ? -1 : 35;
}
return null;
}
/**
* @inheritDoc
*/
#[\Override]
public function search(
IUser $user,
ISearchQuery $query,
): SearchResult {
if (!$this->appManager->isEnabledForUser('tasks', $user)) {
return SearchResult::complete($this->getName(), []);
}
$principalUri = 'principals/users/' . $user->getUID();
$calendarsById = $this->getSortedCalendars($principalUri);
$subscriptionsById = $this->getSortedSubscriptions($principalUri);
$searchResults = $this->backend->searchPrincipalUri(
$principalUri,
$query->getFilter('term')?->get() ?? '',
[self::COMPONENT_TYPE],
self::SEARCH_PROPERTIES,
self::SEARCH_PARAMETERS,
[
'limit' => $query->getLimit(),
'offset' => $query->getCursor(),
'since' => $query->getFilter('since'),
'until' => $query->getFilter('until'),
]
);
$formattedResults = [];
foreach ($searchResults as $taskRow) {
try {
$vCalendar = Reader::read($taskRow['calendardata'], Reader::OPTION_FORGIVING);
} catch (ParseException|InvalidDataException) {
continue;
}
if (!$vCalendar instanceof VCalendar) {
continue;
}
$component = $this->getPrimaryComponent($vCalendar, self::COMPONENT_TYPE);
if ($component === null) {
continue;
}
$title = (string)($component->SUMMARY ?? $this->l10n->t('Untitled task'));
if ($taskRow['calendartype'] === CalDavBackend::CALENDAR_TYPE_CALENDAR) {
$calendar = $calendarsById[$taskRow['calendarid']];
} else {
$calendar = $subscriptionsById[$taskRow['calendarid']];
}
$subline = $this->generateSubline($component, $calendar);
$resourceUrl = $this->getDeepLinkToTasksApp($calendar['uri'], $taskRow['uri']);
$formattedResults[] = new SearchResultEntry('', $title, $subline, $resourceUrl, 'icon-checkmark', false);
}
// Advance the cursor by the number of backend rows consumed, not by
// the number of formatted results, so a page where every row gets
// dropped (invalid data) still moves the cursor forward instead of
// stalling pagination.
return SearchResult::paginated(
$this->getName(),
$formattedResults,
$query->getCursor() + count($searchResults)
);
}
protected function getDeepLinkToTasksApp(
string $calendarUri,
string $taskUri,
): string {
return $this->urlGenerator->getAbsoluteURL(
$this->urlGenerator->linkToRoute('tasks.page.index')
. 'calendars/'
. $calendarUri
. '/tasks/'
. $taskUri
);
}
protected function generateSubline(Component $taskComponent, array $calendarInfo): string {
if ($taskComponent->COMPLETED) {
$completedDateTime = new \DateTime($taskComponent->COMPLETED->getDateTime()->format(\DateTimeInterface::ATOM));
$formattedDate = $this->l10n->l('date', $completedDateTime, ['width' => 'medium']);
$formattedSubline = $this->l10n->t('Completed on %s', [$formattedDate]);
} elseif ($taskComponent->DUE) {
$dueDateTime = new \DateTime($taskComponent->DUE->getDateTime()->format(\DateTimeInterface::ATOM));
$formattedDate = $this->l10n->l('date', $dueDateTime, ['width' => 'medium']);
if ($taskComponent->DUE->hasTime()) {
$formattedTime = $this->l10n->l('time', $dueDateTime, ['width' => 'short']);
$formattedSubline = $this->l10n->t('Due on %s by %s', [$formattedDate, $formattedTime]);
} else {
$formattedSubline = $this->l10n->t('Due on %s', [$formattedDate]);
}
} else {
$formattedSubline = '';
}
if (isset($calendarInfo['{DAV:}displayname']) && !empty($calendarInfo['{DAV:}displayname'])) {
$formattedSubline = $formattedSubline . (!empty($formattedSubline) ? ' ' : '') . "({$calendarInfo['{DAV:}displayname']})";
}
return $formattedSubline;
}
}