From ab424e20e29ae305226fe470388de38b2e0ec118 Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Mon, 9 Mar 2026 13:09:26 +0100 Subject: [PATCH] refactor: Deprecate url generator utils Add a new IUrlGenerator::linkToRemote and replace usage of the old API with the new one. Signed-off-by: Carl Schwan --- .../lib/OCM/CloudFederationProviderFiles.php | 5 ++++- lib/private/URLGenerator.php | 9 +++++++++ lib/public/IURLGenerator.php | 11 +++++++++++ lib/public/Util.php | 14 +++++++------- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/apps/federatedfilesharing/lib/OCM/CloudFederationProviderFiles.php b/apps/federatedfilesharing/lib/OCM/CloudFederationProviderFiles.php index 3ae6366fabe..d2761289585 100644 --- a/apps/federatedfilesharing/lib/OCM/CloudFederationProviderFiles.php +++ b/apps/federatedfilesharing/lib/OCM/CloudFederationProviderFiles.php @@ -569,7 +569,10 @@ class CloudFederationProviderFiles implements ISignedCloudFederationProvider { $file = null; } $args = Filesystem::is_dir($file) ? ['dir' => $file] : ['dir' => dirname($file), 'scrollto' => $file]; - $link = Util::linkToAbsolute('files', 'index.php', $args); + $urlGenerator = Server::get(IURLGenerator::class); + $link = $urlGenerator->getAbsoluteURL( + $urlGenerator->linkTo('files', 'index.php', $args) + ); return [$file, $link]; } diff --git a/lib/private/URLGenerator.php b/lib/private/URLGenerator.php index 2c7070f1c2d..e083886aae9 100644 --- a/lib/private/URLGenerator.php +++ b/lib/private/URLGenerator.php @@ -19,6 +19,7 @@ use OCP\IRequest; use OCP\IURLGenerator; use OCP\IUserSession; use OCP\Server; +use Override; use RuntimeException; class URLGenerator implements IURLGenerator { @@ -326,4 +327,12 @@ class URLGenerator implements IURLGenerator { public function getWebroot(): string { return \OC::$WEBROOT; } + + #[Override] + public function linkToRemote(string $service): string { + $remoteBase = $this->linkTo('', 'remote.php') . '/' . $service; + return $this->getAbsoluteURL( + $remoteBase . (($service[strlen($service) - 1] !== '/') ? '/' : '') + ); + } } diff --git a/lib/public/IURLGenerator.php b/lib/public/IURLGenerator.php index a336d18b00f..e9a54f371f5 100644 --- a/lib/public/IURLGenerator.php +++ b/lib/public/IURLGenerator.php @@ -8,10 +8,14 @@ declare(strict_types=1); */ namespace OCP; +use OCP\AppFramework\Attribute\Consumable; + /** * Class to generate URLs + * * @since 6.0.0 */ +#[Consumable(since: '6.0.0')] interface IURLGenerator { /** * Regex for matching http(s) urls @@ -115,4 +119,11 @@ interface IURLGenerator { * @since 23.0.0 */ public function getWebroot(): string; + + /** + * Return the url to the remote DAV handler. + * + * @since 34.0.0 + */ + public function linkToRemote(string $service): string; } diff --git a/lib/public/Util.php b/lib/public/Util.php index a912a7baef6..7cb22368c1e 100644 --- a/lib/public/Util.php +++ b/lib/public/Util.php @@ -236,9 +236,10 @@ class Util { * The value of $args will be urlencoded * @return string the url * @since 4.0.0 - parameter $args was added in 4.5.0 + * @deprecated 34.0.0 Use IUrlGenerator::getAbsoluteUrl and IUrlGenerator::linkTo */ public static function linkToAbsolute($app, $file, $args = []) { - $urlGenerator = \OCP\Server::get(IURLGenerator::class); + $urlGenerator = Server::get(IURLGenerator::class); return $urlGenerator->getAbsoluteURL( $urlGenerator->linkTo($app, $file, $args) ); @@ -246,16 +247,15 @@ class Util { /** * Creates an absolute url for remote use. + * * @param string $service id * @return string the url * @since 4.0.0 + * @deprecated 34.0.0 Use IURlGenerator::linkToRemote */ - public static function linkToRemote($service) { - $urlGenerator = \OCP\Server::get(IURLGenerator::class); - $remoteBase = $urlGenerator->linkTo('', 'remote.php') . '/' . $service; - return $urlGenerator->getAbsoluteURL( - $remoteBase . (($service[strlen($service) - 1] !== '/') ? '/' : '') - ); + public static function linkToRemote(string $service): string { + $urlGenerator = Server::get(IURLGenerator::class); + return $urlGenerator->linkToRemote($service); } /**