Skip to content

Commit 71dc34c

Browse files
committed
fix: Deprecate OC_Template, add proper template manager instead
Signed-off-by: Côme Chilliet <[email protected]>
1 parent b44f156 commit 71dc34c

File tree

10 files changed

+414
-343
lines changed

10 files changed

+414
-343
lines changed

build/psalm-baseline-ocp.xml

-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<files psalm-version="5.26.1@d747f6500b38ac4f7dfc5edbcae6e4b637d7add0">
3-
<file src="lib/private/legacy/OC_Template.php">
4-
<UndefinedClass>
5-
<code><![CDATA[OC]]></code>
6-
</UndefinedClass>
7-
</file>
83
<file src="lib/public/AppFramework/ApiController.php">
94
<NoInterfaceProperties>
105
<code><![CDATA[$this->request->server]]></code>

core/Controller/SetupController.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace OC\Core\Controller;
99

1010
use OC\Setup;
11+
use OCP\Template\ITemplateManager;
1112
use OCP\Util;
1213
use Psr\Log\LoggerInterface;
1314

@@ -17,6 +18,7 @@ class SetupController {
1718
public function __construct(
1819
protected Setup $setupHelper,
1920
protected LoggerInterface $logger,
21+
protected ITemplateManager $templateManager,
2022
) {
2123
$this->autoConfigFile = \OC::$configDir . 'autoconfig.php';
2224
}
@@ -57,10 +59,10 @@ public function run(array $post): void {
5759
}
5860

5961
private function displaySetupForbidden(): void {
60-
\OC_Template::printGuestPage('', 'installation_forbidden');
62+
$this->templateManager->printGuestPage('', 'installation_forbidden');
6163
}
6264

63-
public function display($post): void {
65+
public function display(array $post): void {
6466
$defaults = [
6567
'adminlogin' => '',
6668
'adminpass' => '',
@@ -80,7 +82,7 @@ public function display($post): void {
8082
Util::addScript('core', 'main');
8183
Util::addTranslations('core');
8284

83-
\OC_Template::printGuestPage('', 'installation', $parameters);
85+
$this->templateManager->printGuestPage('', 'installation', $parameters);
8486
}
8587

8688
private function finishSetup(): void {
@@ -90,7 +92,7 @@ private function finishSetup(): void {
9092
\OC::$server->getIntegrityCodeChecker()->runInstanceVerification();
9193

9294
if ($this->setupHelper->shouldRemoveCanInstallFile()) {
93-
\OC_Template::printGuestPage('', 'installation_incomplete');
95+
$this->templateManager->printGuestPage('', 'installation_incomplete');
9496
}
9597

9698
header('Location: ' . \OC::$server->getURLGenerator()->getAbsoluteURL('index.php/core/apps/recommended'));

index.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use OCP\IRequest;
1717
use OCP\Security\Bruteforce\MaxDelayReached;
1818
use OCP\Server;
19+
use OCP\Template\ITemplateManager;
1920
use Psr\Log\LoggerInterface;
2021

2122
try {
@@ -29,10 +30,10 @@
2930
]);
3031

3132
//show the user a detailed error page
32-
OC_Template::printExceptionErrorPage($ex, 503);
33+
Server::get(ITemplateManager::class)->printExceptionErrorPage($ex, 503);
3334
} catch (HintException $ex) {
3435
try {
35-
OC_Template::printErrorPage($ex->getMessage(), $ex->getHint(), 503);
36+
Server::get(ITemplateManager::class)->printErrorPage($ex->getMessage(), $ex->getHint(), 503);
3637
} catch (Exception $ex2) {
3738
try {
3839
Server::get(LoggerInterface::class)->error($ex->getMessage(), [
@@ -48,7 +49,7 @@
4849
}
4950

5051
//show the user a detailed error page
51-
OC_Template::printExceptionErrorPage($ex, 500);
52+
Server::get(ITemplateManager::class)->printExceptionErrorPage($ex, 500);
5253
}
5354
} catch (LoginException $ex) {
5455
$request = Server::get(IRequest::class);
@@ -63,7 +64,7 @@
6364
echo json_encode(['message' => $ex->getMessage()]);
6465
exit();
6566
}
66-
OC_Template::printErrorPage($ex->getMessage(), $ex->getMessage(), 401);
67+
Server::get(ITemplateManager::class)->printErrorPage($ex->getMessage(), $ex->getMessage(), 401);
6768
} catch (MaxDelayReached $ex) {
6869
$request = Server::get(IRequest::class);
6970
/**
@@ -78,15 +79,15 @@
7879
exit();
7980
}
8081
http_response_code(429);
81-
OC_Template::printGuestPage('core', '429');
82+
Server::get(ITemplateManager::class)->printGuestPage('core', '429');
8283
} catch (Exception $ex) {
8384
Server::get(LoggerInterface::class)->error($ex->getMessage(), [
8485
'app' => 'index',
8586
'exception' => $ex,
8687
]);
8788

8889
//show the user a detailed error page
89-
OC_Template::printExceptionErrorPage($ex, 500);
90+
Server::get(ITemplateManager::class)->printExceptionErrorPage($ex, 500);
9091
} catch (Error $ex) {
9192
try {
9293
Server::get(LoggerInterface::class)->error($ex->getMessage(), [
@@ -103,5 +104,5 @@
103104

104105
throw $ex;
105106
}
106-
OC_Template::printExceptionErrorPage($ex, 500);
107+
Server::get(ITemplateManager::class)->printExceptionErrorPage($ex, 500);
107108
}

lib/base.php

+10-9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use OCP\Security\Bruteforce\IThrottler;
2222
use OCP\Server;
2323
use OCP\Share;
24+
use OCP\Template\ITemplateManager;
2425
use OCP\User\Events\UserChangedEvent;
2526
use OCP\User\Events\UserDeletedEvent;
2627
use OCP\Util;
@@ -208,7 +209,7 @@ public static function checkConfig(): void {
208209
echo $l->t('See %s', [ $urlGenerator->linkToDocs('admin-config') ]) . "\n";
209210
exit;
210211
} else {
211-
OC_Template::printErrorPage(
212+
Server::get(ITemplateManager::class)->printErrorPage(
212213
$l->t('Cannot write into "config" directory!'),
213214
$l->t('This can usually be fixed by giving the web server write access to the config directory.') . ' '
214215
. $l->t('But, if you prefer to keep config.php file read only, set the option "config_is_read_only" to true in it.') . ' '
@@ -244,7 +245,7 @@ public static function checkMaintenanceMode(\OC\SystemConfig $systemConfig): voi
244245
header('Retry-After: 120');
245246

246247
// render error page
247-
$template = new OC_Template('', 'update.user', 'guest');
248+
$template = Server::get(ITemplateManager::class)->getTemplate('', 'update.user', 'guest');
248249
\OCP\Util::addScript('core', 'maintenance');
249250
\OCP\Util::addStyle('core', 'guest');
250251
$template->printPage();
@@ -300,7 +301,7 @@ private static function printUpgradePage(\OC\SystemConfig $systemConfig): void {
300301
$serverVersion = \OCP\Server::get(\OCP\ServerVersion::class);
301302

302303
// render error page
303-
$template = new OC_Template('', 'update.use-cli', 'guest');
304+
$template = Server::get(ITemplateManager::class)->getTemplate('', 'update.use-cli', 'guest');
304305
$template->assign('productName', 'nextcloud'); // for now
305306
$template->assign('version', $serverVersion->getVersionString());
306307
$template->assign('tooBig', $tooBig);
@@ -327,7 +328,7 @@ private static function printUpgradePage(\OC\SystemConfig $systemConfig): void {
327328
/** @var \OC\App\AppManager $appManager */
328329
$appManager = Server::get(\OCP\App\IAppManager::class);
329330

330-
$tmpl = new OC_Template('', 'update.admin', 'guest');
331+
$tmpl = Server::get(ITemplateManager::class)->getTemplate('', 'update.admin', 'guest');
331332
$tmpl->assign('version', \OCP\Server::get(\OCP\ServerVersion::class)->getVersionString());
332333
$tmpl->assign('isAppsOnlyUpgrade', $isAppsOnlyUpgrade);
333334

@@ -420,7 +421,7 @@ public static function initSession(): void {
420421
} catch (Exception $e) {
421422
Server::get(LoggerInterface::class)->error($e->getMessage(), ['app' => 'base','exception' => $e]);
422423
//show the user a detailed error page
423-
OC_Template::printExceptionErrorPage($e, 500);
424+
Server::get(ITemplateManager::class)->printExceptionErrorPage($e, 500);
424425
die();
425426
}
426427

@@ -659,7 +660,7 @@ public static function init(): void {
659660
if ($config->getSystemValueBool('debug', false)) {
660661
set_error_handler([$errorHandler, 'onAll'], E_ALL);
661662
if (\OC::$CLI) {
662-
$exceptionHandler = ['OC_Template', 'printExceptionErrorPage'];
663+
$exceptionHandler = [Server::get(ITemplateManager::class), 'printExceptionErrorPage'];
663664
}
664665
} else {
665666
set_error_handler([$errorHandler, 'onError']);
@@ -702,7 +703,7 @@ public static function init(): void {
702703
http_response_code(503);
703704
OC_Util::addStyle('guest');
704705
try {
705-
OC_Template::printGuestPage('', 'error', ['errors' => $errors]);
706+
Server::get(ITemplateManager::class)->printGuestPage('', 'error', ['errors' => $errors]);
706707
exit;
707708
} catch (\Exception $e) {
708709
// In case any error happens when showing the error page, we simply fall back to posting the text.
@@ -781,7 +782,7 @@ public static function init(): void {
781782
// Check whether the sample configuration has been copied
782783
if ($systemConfig->getValue('copied_sample_config', false)) {
783784
$l = Server::get(\OCP\L10N\IFactory::class)->get('lib');
784-
OC_Template::printErrorPage(
785+
Server::get(ITemplateManager::class)->printErrorPage(
785786
$l->t('Sample configuration detected'),
786787
$l->t('It has been detected that the sample configuration has been copied. This can break your installation and is unsupported. Please read the documentation before performing changes on config.php'),
787788
503
@@ -1094,7 +1095,7 @@ public static function handleRequest(): void {
10941095
logger('core')->emergency($e->getMessage(), ['exception' => $e]);
10951096
}
10961097
$l = Server::get(\OCP\L10N\IFactory::class)->get('lib');
1097-
OC_Template::printErrorPage(
1098+
Server::get(ITemplateManager::class)->printErrorPage(
10981099
'404',
10991100
$l->t('The page could not be found on the server.'),
11001101
404

lib/private/Template/Base.php

+6-21
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
class Base {
1414
private $template; // The template
15-
private $vars; // Vars
15+
private array $vars = [];
1616

1717
/** @var \OCP\IL10N */
1818
private $l10n;
@@ -59,11 +59,9 @@ protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) {
5959
}
6060

6161
/**
62-
* @param string $serverRoot
63-
* @param string $theme
6462
* @return string[]
6563
*/
66-
protected function getCoreTemplateDirs($theme, $serverRoot) {
64+
protected function getCoreTemplateDirs(string $theme, string $serverRoot): array {
6765
return [
6866
$serverRoot . '/themes/' . $theme . '/core/templates/',
6967
$serverRoot . '/core/templates/',
@@ -105,42 +103,29 @@ public function append($key, $value) {
105103

106104
/**
107105
* Prints the proceeded template
108-
* @return bool
109106
*
110107
* This function proceeds the template and prints its output.
111108
*/
112-
public function printPage() {
109+
public function printPage(): void {
113110
$data = $this->fetchPage();
114-
if ($data === false) {
115-
return false;
116-
} else {
117-
print $data;
118-
return true;
119-
}
111+
print $data;
120112
}
121113

122114
/**
123115
* Process the template
124116
*
125-
* @param array|null $additionalParams
126-
* @return string This function processes the template.
127-
*
128117
* This function processes the template.
129118
*/
130-
public function fetchPage($additionalParams = null) {
119+
public function fetchPage(?array $additionalParams = null): string {
131120
return $this->load($this->template, $additionalParams);
132121
}
133122

134123
/**
135124
* doing the actual work
136125
*
137-
* @param string $file
138-
* @param array|null $additionalParams
139-
* @return string content
140-
*
141126
* Includes the template file, fetches its output
142127
*/
143-
protected function load($file, $additionalParams = null) {
128+
protected function load(string $file, ?array $additionalParams = null): string {
144129
// Register the variables
145130
$_ = $this->vars;
146131
$l = $this->l10n;

0 commit comments

Comments
 (0)