Skip to content

Commit f938e82

Browse files
authored
Merge pull request #51385 from nextcloud/fix/cleanup-template-functions
fix: Remove multiple require_once calls for template functions
2 parents a78d86e + 5ed5bef commit f938e82

8 files changed

Lines changed: 53 additions & 73 deletions

File tree

build/psalm-baseline-ocp.xml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,6 @@
5353
<code><![CDATA[\Iterator]]></code>
5454
</MissingTemplateParam>
5555
</file>
56-
<file src="lib/public/Template.php">
57-
<UndefinedFunction>
58-
<code><![CDATA[\html_select_options($options, $selected, $params)]]></code>
59-
<code><![CDATA[\human_file_size($bytes)]]></code>
60-
<code><![CDATA[\image_path($app, $image)]]></code>
61-
<code><![CDATA[\mimetype_icon($mimetype)]]></code>
62-
<code><![CDATA[\preview_icon($path)]]></code>
63-
<code><![CDATA[\publicPreview_icon($path, $token)]]></code>
64-
<code><![CDATA[\relative_modified_date($timestamp, null, $dateOnly)]]></code>
65-
</UndefinedFunction>
66-
</file>
6756
<file src="lib/public/Util.php">
6857
<UndefinedClass>
6958
<code><![CDATA[\OC]]></code>

lib/private/Template/Base.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ protected function load(string $file, ?array $additionalParams = null): string {
136136
// Include
137137
ob_start();
138138
try {
139+
require_once __DIR__ . '/functions.php';
139140
include $file;
140141
$data = ob_get_contents();
141142
} catch (\Exception $e) {

lib/private/Template/Template.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
use OCP\Template\TemplateNotFoundException;
2222
use OCP\Util;
2323

24-
require_once __DIR__ . '/../legacy/template/functions.php';
25-
2624
class Template extends Base implements ITemplate {
2725
private string $path;
2826
private array $headers = [];
Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,23 @@
66
* SPDX-License-Identifier: AGPL-3.0-only
77
*/
88

9+
use OC\Security\CSP\ContentSecurityPolicyNonceManager;
10+
use OCP\Files\IMimeTypeDetector;
911
use OCP\IDateTimeFormatter;
12+
use OCP\IURLGenerator;
13+
use OCP\Server;
1014
use OCP\Util;
1115

12-
function p($string) {
13-
print(\OCP\Util::sanitizeHTML($string));
16+
function p(string $string): void {
17+
print(Util::sanitizeHTML($string));
1418
}
1519

16-
1720
/**
1821
* Prints a <link> tag for loading css
1922
* @param string $href the source URL, ignored when empty
2023
* @param string $opts, additional optional options
2124
*/
22-
function emit_css_tag($href, $opts = '') {
25+
function emit_css_tag($href, $opts = ''): void {
2326
$s = '<link rel="stylesheet"';
2427
if (!empty($href)) {
2528
$s .= ' href="' . $href . '"';
@@ -34,7 +37,7 @@ function emit_css_tag($href, $opts = '') {
3437
* Prints all tags for CSS loading
3538
* @param array $obj all the script information from template
3639
*/
37-
function emit_css_loading_tags($obj) {
40+
function emit_css_loading_tags($obj): void {
3841
foreach ($obj['cssfiles'] as $css) {
3942
emit_css_tag($css);
4043
}
@@ -49,8 +52,8 @@ function emit_css_loading_tags($obj) {
4952
* @param string $script_content the inline script content, ignored when empty
5053
* @param string $content_type the type of the source (e.g. 'module')
5154
*/
52-
function emit_script_tag(string $src, string $script_content = '', string $content_type = '') {
53-
$nonceManager = \OC::$server->get(\OC\Security\CSP\ContentSecurityPolicyNonceManager::class);
55+
function emit_script_tag(string $src, string $script_content = '', string $content_type = ''): void {
56+
$nonceManager = Server::get(ContentSecurityPolicyNonceManager::class);
5457

5558
$defer_str = ' defer';
5659
$type = $content_type !== '' ? ' type="' . $content_type . '"' : '';
@@ -74,7 +77,7 @@ function emit_script_tag(string $src, string $script_content = '', string $conte
7477
* Print all <script> tags for loading JS
7578
* @param array $obj all the script information from template
7679
*/
77-
function emit_script_loading_tags($obj) {
80+
function emit_script_loading_tags($obj): void {
7881
foreach ($obj['jsfiles'] as $jsfile) {
7982
$fileName = explode('?', $jsfile, 2)[0];
8083
$type = str_ends_with($fileName, '.mjs') ? 'module' : '';
@@ -88,9 +91,9 @@ function emit_script_loading_tags($obj) {
8891
/**
8992
* Prints an unsanitized string - usage of this function may result into XSS.
9093
* Consider using p() instead.
91-
* @param string|array $string the string which will be printed as it is
94+
* @param string $string the string which will be printed as it is
9295
*/
93-
function print_unescaped($string) {
96+
function print_unescaped($string): void {
9497
print($string);
9598
}
9699

@@ -106,7 +109,7 @@ function print_unescaped($string) {
106109
* @param string|string[] $file the filename,
107110
* if an array is given it will add all scripts
108111
*/
109-
function script($app, $file = null) {
112+
function script($app, $file = null): void {
110113
if (is_array($file)) {
111114
foreach ($file as $script) {
112115
Util::addScript($app, $script, 'core');
@@ -122,7 +125,7 @@ function script($app, $file = null) {
122125
* @param string|string[] $file the filename,
123126
* if an array is given it will add all scripts
124127
*/
125-
function vendor_script($app, $file = null) {
128+
function vendor_script($app, $file = null): void {
126129
if (is_array($file)) {
127130
foreach ($file as $f) {
128131
OC_Util::addVendorScript($app, $f);
@@ -138,7 +141,7 @@ function vendor_script($app, $file = null) {
138141
* @param string|string[] $file the filename,
139142
* if an array is given it will add all styles
140143
*/
141-
function style($app, $file = null) {
144+
function style($app, $file = null): void {
142145
if (is_array($file)) {
143146
foreach ($file as $f) {
144147
OC_Util::addStyle($app, $f);
@@ -154,7 +157,7 @@ function style($app, $file = null) {
154157
* @param string|string[] $file the filename,
155158
* if an array is given it will add all styles
156159
*/
157-
function vendor_style($app, $file = null) {
160+
function vendor_style($app, $file = null): void {
158161
if (is_array($file)) {
159162
foreach ($file as $f) {
160163
OC_Util::addVendorStyle($app, $f);
@@ -169,28 +172,10 @@ function vendor_style($app, $file = null) {
169172
* @param string $app the appname
170173
* if an array is given it will add all styles
171174
*/
172-
function translation($app) {
175+
function translation($app): void {
173176
OC_Util::addTranslations($app);
174177
}
175178

176-
/**
177-
* Shortcut for HTML imports
178-
* @param string $app the appname
179-
* @param string|string[] $file the path relative to the app's component folder,
180-
* if an array is given it will add all components
181-
*/
182-
function component($app, $file) {
183-
if (is_array($file)) {
184-
foreach ($file as $f) {
185-
$url = link_to($app, 'component/' . $f . '.html');
186-
OC_Util::addHeader('link', ['rel' => 'import', 'href' => $url]);
187-
}
188-
} else {
189-
$url = link_to($app, 'component/' . $file . '.html');
190-
OC_Util::addHeader('link', ['rel' => 'import', 'href' => $url]);
191-
}
192-
}
193-
194179
/**
195180
* make \OCP\IURLGenerator::linkTo available as a simple function
196181
* @param string $app app
@@ -201,15 +186,15 @@ function component($app, $file) {
201186
* For further information have a look at \OCP\IURLGenerator::linkTo
202187
*/
203188
function link_to($app, $file, $args = []) {
204-
return \OC::$server->getURLGenerator()->linkTo($app, $file, $args);
189+
return Server::get(IURLGenerator::class)->linkTo($app, $file, $args);
205190
}
206191

207192
/**
208-
* @param $key
193+
* @param string $key
209194
* @return string url to the online documentation
210195
*/
211196
function link_to_docs($key) {
212-
return \OC::$server->getURLGenerator()->linkToDocs($key);
197+
return Server::get(IURLGenerator::class)->linkToDocs($key);
213198
}
214199

215200
/**
@@ -221,16 +206,16 @@ function link_to_docs($key) {
221206
* For further information have a look at \OCP\IURLGenerator::imagePath
222207
*/
223208
function image_path($app, $image) {
224-
return \OC::$server->getURLGenerator()->imagePath($app, $image);
209+
return Server::get(IURLGenerator::class)->imagePath($app, $image);
225210
}
226211

227212
/**
228-
* make OC_Helper::mimetypeIcon available as a simple function
213+
* make mimetypeIcon available as a simple function
229214
* @param string $mimetype mimetype
230215
* @return string link to the image
231216
*/
232217
function mimetype_icon($mimetype) {
233-
return \OC::$server->getMimeTypeDetector()->mimeTypeIcon($mimetype);
218+
return Server::get(IMimeTypeDetector::class)->mimeTypeIcon($mimetype);
234219
}
235220

236221
/**
@@ -240,7 +225,7 @@ function mimetype_icon($mimetype) {
240225
* @return string link to the preview
241226
*/
242227
function preview_icon($path) {
243-
return \OC::$server->getURLGenerator()->linkToRoute('core.Preview.getPreview', ['x' => 32, 'y' => 32, 'file' => $path]);
228+
return Server::get(IURLGenerator::class)->linkToRoute('core.Preview.getPreview', ['x' => 32, 'y' => 32, 'file' => $path]);
244229
}
245230

246231
/**
@@ -249,18 +234,19 @@ function preview_icon($path) {
249234
* @return string
250235
*/
251236
function publicPreview_icon($path, $token) {
252-
return \OC::$server->getURLGenerator()->linkToRoute('files_sharing.PublicPreview.getPreview', ['x' => 32, 'y' => 32, 'file' => $path, 'token' => $token]);
237+
return Server::get(IURLGenerator::class)->linkToRoute('files_sharing.PublicPreview.getPreview', ['x' => 32, 'y' => 32, 'file' => $path, 'token' => $token]);
253238
}
254239

255240
/**
256-
* make OC_Helper::humanFileSize available as a simple function
241+
* make Util::humanFileSize available as a simple function
257242
* @param int $bytes size in bytes
258243
* @return string size as string
244+
* @deprecated use Util::humanFileSize instead
259245
*
260-
* For further information have a look at OC_Helper::humanFileSize
246+
* For further information have a look at Util::humanFileSize
261247
*/
262248
function human_file_size($bytes) {
263-
return OC_Helper::humanFileSize($bytes);
249+
return Util::humanFileSize($bytes);
264250
}
265251

266252
/**
@@ -283,15 +269,20 @@ function strip_time($timestamp) {
283269
* @return string timestamp
284270
*/
285271
function relative_modified_date($timestamp, $fromTime = null, $dateOnly = false): string {
286-
$formatter = \OCP\Server::get(IDateTimeFormatter::class);
272+
$formatter = Server::get(IDateTimeFormatter::class);
287273

288274
if ($dateOnly) {
289275
return $formatter->formatDateSpan($timestamp, $fromTime);
290276
}
291277
return $formatter->formatTimeSpan($timestamp, $fromTime);
292278
}
293279

294-
function html_select_options($options, $selected, $params = []) {
280+
/**
281+
* @param array $options
282+
* @param string[]|string $selected
283+
* @param array $params
284+
*/
285+
function html_select_options($options, $selected, $params = []): string {
295286
if (!is_array($selected)) {
296287
$selected = [$selected];
297288
}
@@ -314,7 +305,7 @@ function html_select_options($options, $selected, $params = []) {
314305
$label = $label[$label_name];
315306
}
316307
$select = in_array($value, $selected) ? ' selected="selected"' : '';
317-
$html .= '<option value="' . \OCP\Util::sanitizeHTML($value) . '"' . $select . '>' . \OCP\Util::sanitizeHTML($label) . '</option>' . "\n";
308+
$html .= '<option value="' . Util::sanitizeHTML($value) . '"' . $select . '>' . Util::sanitizeHTML($label) . '</option>' . "\n";
318309
}
319310
return $html;
320311
}

lib/private/legacy/OC_Template.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
use OCP\Server;
99
use OCP\Template\ITemplateManager;
1010

11-
require_once __DIR__ . '/template/functions.php';
12-
1311
/**
1412
* This class provides the templates for ownCloud.
1513
* @deprecated 32.0.0 Use \OCP\Template\ITemplateManager instead

lib/public/Template.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
*/
88
namespace OCP;
99

10+
/*
11+
* We have to require the functions file because this class contains aliases to the functions
12+
*/
13+
require_once __DIR__ . '/../private/Template/functions.php';
14+
1015
/**
1116
* This class provides the template system for owncloud. You can use it to load
1217
* specific templates, add data and generate the html code
@@ -24,7 +29,7 @@ class Template extends \OC_Template {
2429
* @param string $image
2530
* @return string to the image
2631
* @since 8.0.0
27-
* @suppress PhanDeprecatedFunction
32+
* @deprecated 32.0.0 Use the function directly instead
2833
*/
2934
public static function image_path($app, $image) {
3035
return \image_path($app, $image);
@@ -37,7 +42,7 @@ public static function image_path($app, $image) {
3742
* @param string $mimetype
3843
* @return string to the image of this file type.
3944
* @since 8.0.0
40-
* @suppress PhanDeprecatedFunction
45+
* @deprecated 32.0.0 Use the function directly instead
4146
*/
4247
public static function mimetype_icon($mimetype) {
4348
return \mimetype_icon($mimetype);
@@ -49,7 +54,7 @@ public static function mimetype_icon($mimetype) {
4954
* @param string $path path to file
5055
* @return string to the preview of the image
5156
* @since 8.0.0
52-
* @suppress PhanDeprecatedFunction
57+
* @deprecated 32.0.0 Use the function directly instead
5358
*/
5459
public static function preview_icon($path) {
5560
return \preview_icon($path);
@@ -63,7 +68,7 @@ public static function preview_icon($path) {
6368
* @param string $token
6469
* @return string link to the preview
6570
* @since 8.0.0
66-
* @suppress PhanDeprecatedFunction
71+
* @deprecated 32.0.0 Use the function directly instead
6772
*/
6873
public static function publicPreview_icon($path, $token) {
6974
return \publicPreview_icon($path, $token);
@@ -76,10 +81,10 @@ public static function publicPreview_icon($path, $token) {
7681
* @param int $bytes in bytes
7782
* @return string size as string
7883
* @since 8.0.0
79-
* @suppress PhanDeprecatedFunction
84+
* @deprecated 32.0.0 Use \OCP\Util::humanFileSize instead
8085
*/
8186
public static function human_file_size($bytes) {
82-
return \human_file_size($bytes);
87+
return Util::humanFileSize($bytes);
8388
}
8489

8590
/**
@@ -89,8 +94,8 @@ public static function human_file_size($bytes) {
8994
* @param boolean $dateOnly
9095
* @return string human readable interpretation of the timestamp
9196
* @since 8.0.0
92-
* @suppress PhanDeprecatedFunction
9397
* @suppress PhanTypeMismatchArgument
98+
* @deprecated 32.0.0 Use the function directly instead
9499
*/
95100
public static function relative_modified_date($timestamp, $dateOnly = false) {
96101
return \relative_modified_date($timestamp, null, $dateOnly);
@@ -104,7 +109,7 @@ public static function relative_modified_date($timestamp, $dateOnly = false) {
104109
* @param array $params the parameters
105110
* @return string html options
106111
* @since 8.0.0
107-
* @suppress PhanDeprecatedFunction
112+
* @deprecated 32.0.0 Use the function directly instead
108113
*/
109114
public static function html_select_options($options, $selected, $params = []) {
110115
return \html_select_options($options, $selected, $params);

tests/lib/TemplateFunctionsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class TemplateFunctionsTest extends \Test\TestCase {
1111
protected function setUp(): void {
1212
parent::setUp();
1313

14-
require_once \OC::$SERVERROOT . '/lib/private/legacy/OC_Template.php';
14+
require_once \OC::$SERVERROOT . '/lib/private/Template/functions.php';
1515
}
1616

1717
public function testPJavaScript(): void {

tests/lib/TestCase.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,6 @@ protected function IsDatabaseAccessAllowed() {
549549
* @param array $vars
550550
*/
551551
protected function assertTemplate($expectedHtml, $template, $vars = []) {
552-
require_once __DIR__ . '/../../lib/private/legacy/template/functions.php';
553-
554552
$requestToken = 12345;
555553
/** @var Defaults|\PHPUnit\Framework\MockObject\MockObject $l10n */
556554
$theme = $this->getMockBuilder('\OCP\Defaults')

0 commit comments

Comments
 (0)