Skip to content

Commit

Permalink
MDL-78884 files: Deprecate size parameter for icons
Browse files Browse the repository at this point in the history
The parameter $size of the following functions has been deprecated and is not used any more:
  - file_extension_icon
  - file_file_icon
  - file_folder_icon
  - file_mimetype_icon
  - mimeinfo_from_type
  - url_guess_icon

That way, the sized icons (xxxxxxx-yyy.png) can be removed and replaced by SVG, to make it easier
to keep them updated because once they are replaced, there will only be one single file for each
MIME icon.
  • Loading branch information
sarjona committed Aug 23, 2023
1 parent cccc009 commit 7430208
Show file tree
Hide file tree
Showing 33 changed files with 135 additions and 133 deletions.
2 changes: 1 addition & 1 deletion course/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ protected function course_overview_files(core_course_list_element $course): stri
html_writer::empty_tag('img', ['src' => $url, 'alt' => '']),
['class' => 'courseimage']);
} else {
$image = $this->output->pix_icon(file_file_icon($file, 24), $file->get_filename(), 'moodle');
$image = $this->output->pix_icon(file_file_icon($file), $file->get_filename(), 'moodle');
$filename = html_writer::tag('span', $image, ['class' => 'fp-icon']).
html_writer::tag('span', $file->get_filename(), ['class' => 'fp-filename']);
$contentfiles .= html_writer::tag('span',
Expand Down
2 changes: 1 addition & 1 deletion files/classes/external/stored_file_exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ protected function get_other_values(renderer_base $output) {
$filenameshort .= substr($filename, -4);
}

$icon = $this->file->is_directory() ? file_folder_icon(128) : file_file_icon($this->file, 128);
$icon = $this->file->is_directory() ? file_folder_icon() : file_file_icon($this->file);

$url = moodle_url::make_pluginfile_url(
$this->file->get_contextid(),
Expand Down
4 changes: 2 additions & 2 deletions files/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ public function render_files_tree_viewer(files_tree_viewer $tree) {
if ($file['filesize']) {
$filesize = display_size($file['filesize']);
}
$fileicon = file_file_icon($file, 24);
$fileicon = file_file_icon($file);
$filetype = get_mimetype_description($file);
} else {
$fileicon = file_folder_icon(24);
$fileicon = file_folder_icon();
}
$table->data[] = array(
html_writer::link($file['url'], $this->output->pix_icon($fileicon, get_string('icon')) . ' ' . $file['filename']),
Expand Down
98 changes: 45 additions & 53 deletions lib/filelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -798,11 +798,11 @@ function file_get_drafarea_files($draftitemid, $filepath = '/') {

if ($file->is_directory()) {
$item->filesize = 0;
$item->icon = $OUTPUT->image_url(file_folder_icon(24))->out(false);
$item->icon = $OUTPUT->image_url(file_folder_icon())->out(false);
$item->type = 'folder';
$foldername = explode('/', trim($item->filepath, '/'));
$item->fullname = trim(array_pop($foldername), '/');
$item->thumbnail = $OUTPUT->image_url(file_folder_icon(90))->out(false);
$item->thumbnail = $OUTPUT->image_url(file_folder_icon())->out(false);
} else {
// do NOT use file browser here!
$item->mimetype = get_mimetype_description($file);
Expand All @@ -813,8 +813,8 @@ function file_get_drafarea_files($draftitemid, $filepath = '/') {
}
$itemurl = moodle_url::make_draftfile_url($draftitemid, $item->filepath, $item->filename);
$item->url = $itemurl->out();
$item->icon = $OUTPUT->image_url(file_file_icon($file, 24))->out(false);
$item->thumbnail = $OUTPUT->image_url(file_file_icon($file, 90))->out(false);
$item->icon = $OUTPUT->image_url(file_file_icon($file))->out(false);
$item->thumbnail = $OUTPUT->image_url(file_file_icon($file))->out(false);

// The call to $file->get_imageinfo() fails with an exception if the file can't be read on the file system.
// We still want to add such files to the list, so the owner can view and delete them if needed. So, we only call
Expand Down Expand Up @@ -1811,7 +1811,7 @@ function mimeinfo($element, $filename) {
* the other way around.
*
* @category files
* @param string $element Desired information ('extension', 'icon', 'icon-24', etc.)
* @param string $element Desired information ('extension', 'icon', etc.)
* @param string $mimetype MIME type we're looking up
* @return string Requested piece of information from array
*/
Expand Down Expand Up @@ -1860,10 +1860,14 @@ function mimeinfo_from_type($element, $mimetype) {
*
* @param stored_file|file_info|stdClass|array $file (in case of object attributes $file->filename
* and $file->mimetype are expected)
* @param int $size The size of the icon. Defaults to 16 can also be 24, 32, 64, 128, 256
* @param null $unused This parameter has been deprecated since 4.3 and should not be used anymore.
* @return string
*/
function file_file_icon($file, $size = null) {
function file_file_icon($file, $unused = null) {
if ($unused !== null) {
debugging('Deprecated argument passed to ' . __FUNCTION__, DEBUG_DEVELOPER);
}

if (!is_object($file)) {
$file = (object)$file;
}
Expand All @@ -1888,14 +1892,14 @@ function file_file_icon($file, $size = null) {
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
if ($extension && !empty($mimetypes[$extension])) {
// if file name has known extension, return icon for this extension
return file_extension_icon($filename, $size);
return file_extension_icon($filename);
}
}
return file_mimetype_icon($mimetype, $size);
return file_mimetype_icon($mimetype);
}

/**
* Return the relative icon path for a folder image
* Return the relative icon path for a folder image.
*
* Usage:
* <code>
Expand All @@ -1904,28 +1908,20 @@ function file_file_icon($file, $size = null) {
* </code>
* or
* <code>
* echo $OUTPUT->pix_icon(file_folder_icon(32), '');
* echo $OUTPUT->pix_icon(file_folder_icon(), '');
* </code>
*
* @param int $iconsize The size of the icon. Defaults to 16 can also be 24, 32, 48, 64, 72, 80, 96, 128, 256
* @param null $unused This parameter has been deprecated since 4.3 and should not be used anymore.
* @return string
*/
function file_folder_icon($iconsize = null) {
function file_folder_icon($unused = null) {
global $CFG;
static $iconpostfixes = array(256=>'-256', 128=>'-128', 96=>'-96', 80=>'-80', 72=>'-72', 64=>'-64', 48=>'-48', 32=>'-32', 24=>'-24', 16=>'');
static $cached = array();
$iconsize = max(array(16, (int)$iconsize));
if (!array_key_exists($iconsize, $cached)) {
foreach ($iconpostfixes as $size => $postfix) {
$fullname = $CFG->dirroot.'/pix/f/folder'.$postfix;
if ($iconsize >= $size &&
(file_exists($fullname.'.svg') || file_exists($fullname.'.png') || file_exists($fullname.'.gif'))) {
$cached[$iconsize] = 'f/folder'.$postfix;
break;
}
}

if ($unused !== null) {
debugging('Deprecated argument passed to ' . __FUNCTION__, DEBUG_DEVELOPER);
}
return $cached[$iconsize];

return 'f/folder';
}

/**
Expand All @@ -1944,11 +1940,11 @@ function file_folder_icon($iconsize = null) {
* @todo MDL-31074 When an $OUTPUT->icon method is available this function should be altered
* to conform with that.
* @param string $mimetype The mimetype to fetch an icon for
* @param int $size The size of the icon. Defaults to 16 can also be 24, 32, 64, 128, 256
* @param null $unused This parameter has been deprecated since 4.3 and should not be used anymore.
* @return string The relative path to the icon
*/
function file_mimetype_icon($mimetype, $size = NULL) {
return 'f/'.mimeinfo_from_type('icon'.$size, $mimetype);
function file_mimetype_icon($mimetype, $unused = null) {
return 'f/'.mimeinfo_from_type('icon', $mimetype);
}

/**
Expand All @@ -1968,11 +1964,14 @@ function file_mimetype_icon($mimetype, $size = NULL) {
* @todo MDL-31074 Implement $size
* @category files
* @param string $filename The filename to get the icon for
* @param int $size The size of the icon. Defaults to 16 can also be 24, 32, 64, 128, 256
* @param null $unused This parameter has been deprecated since 4.3 and should not be used anymore.
* @return string
*/
function file_extension_icon($filename, $size = NULL) {
return 'f/'.mimeinfo('icon'.$size, $filename);
function file_extension_icon($filename, $unused = null) {
if ($unused !== null) {
debugging('Deprecated argument passed to ' . __FUNCTION__, DEBUG_DEVELOPER);
}
return 'f/'.mimeinfo('icon', $filename);
}

/**
Expand Down Expand Up @@ -2675,14 +2674,14 @@ function send_file($path, $filename, $lifetime = null , $filter=0, $pathisstring
* Note: it's up to the consumer to set it properly i.e. when serving a "versioned" URL.
*
* @category files
* @param stored_file $stored_file local file object
* @param stored_file $storedfile local file object
* @param int $lifetime Number of seconds before the file should expire from caches (null means $CFG->filelifetime)
* @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
* @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
* @param array $options additional options affecting the file serving
* @return null script execution stopped unless $options['dontdie'] is true
*/
function send_stored_file($stored_file, $lifetime=null, $filter=0, $forcedownload=false, array $options=array()) {
function send_stored_file($storedfile, $lifetime=null, $filter=0, $forcedownload=false, array $options=array()) {
global $CFG, $COURSE;

static $recursion = 0;
Expand All @@ -2706,61 +2705,54 @@ function send_stored_file($stored_file, $lifetime=null, $filter=0, $forcedownloa
if (!empty($options['preview'])) {
// replace the file with its preview
$fs = get_file_storage();
$preview_file = $fs->get_file_preview($stored_file, $options['preview']);
if (!$preview_file) {
// unable to create a preview of the file, send its default mime icon instead
if ($options['preview'] === 'tinyicon') {
$size = 24;
} else if ($options['preview'] === 'thumb') {
$size = 90;
} else {
$size = 256;
}
$fileicon = file_file_icon($stored_file, $size);
$previewfile = $fs->get_file_preview($storedfile, $options['preview']);
if (!$previewfile) {
// Unable to create a preview of the file, send its default mime icon instead.
$fileicon = file_file_icon($storedfile);
send_file($CFG->dirroot.'/pix/'.$fileicon.'.png', basename($fileicon).'.png');
} else {
// preview images have fixed cache lifetime and they ignore forced download
// (they are generated by GD and therefore they are considered reasonably safe).
$stored_file = $preview_file;
$storedfile = $previewfile;
$lifetime = DAYSECS;
$filter = 0;
$forcedownload = false;
}
}

// handle external resource
if ($stored_file && $stored_file->is_external_file() && !isset($options['sendcachedexternalfile'])) {
if ($storedfile && $storedfile->is_external_file() && !isset($options['sendcachedexternalfile'])) {

// Have we been here before?
$recursion++;
if ($recursion > 10) {
throw new coding_exception('Recursive file serving detected');
}

$stored_file->send_file($lifetime, $filter, $forcedownload, $options);
$storedfile->send_file($lifetime, $filter, $forcedownload, $options);
die;
}

if (!$stored_file or $stored_file->is_directory()) {
// nothing to serve
if (!$storedfile || $storedfile->is_directory()) {
// Nothing to serve.
if ($dontdie) {
return;
}
die;
}

$filename = is_null($filename) ? $stored_file->get_filename() : $filename;
$filename = is_null($filename) ? $storedfile->get_filename() : $filename;

// Use given MIME type if specified.
$mimetype = $stored_file->get_mimetype();
$mimetype = $storedfile->get_mimetype();

// Allow cross-origin requests only for Web Services.
// This allow to receive requests done by Web Workers or webapps in different domains.
if (WS_SERVER) {
header('Access-Control-Allow-Origin: *');
}

send_file($stored_file, $filename, $lifetime, $filter, false, $forcedownload, $mimetype, $dontdie, $options);
send_file($storedfile, $filename, $lifetime, $filter, false, $forcedownload, $mimetype, $dontdie, $options);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/googleapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function get_file_list($search = '') {
'url' => "{$gdoc->link[0]->attributes()->href}",
'source' => $source,
'date' => strtotime($gdoc->updated),
'thumbnail' => (string) $OUTPUT->image_url(file_extension_icon($title, 32))
'thumbnail' => (string) $OUTPUT->image_url(file_extension_icon($title))
);
}
core_date::set_default_server_timezone();
Expand Down
7 changes: 7 additions & 0 deletions lib/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ information provided here is intended especially for developers.
to ensure in some test that the block drawer is closed. This helps with random failures due to the block drawer
being forced open in all behat tests.
* The core_useragent::get_device_type_list() function has been deprecated. Use core_useragent::devicetypes instead as a replacement.
* The parameter $size of the following functions has been deprecated and is not used any more:
- file_extension_icon
- file_file_icon
- file_folder_icon
- file_mimetype_icon
- mimeinfo_from_type
- url_guess_icon

=== 4.2 ===

Expand Down
2 changes: 1 addition & 1 deletion mod/bigbluebuttonbn/classes/local/helpers/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public static function get_presentation(context $context, string $presentation,
);
return [
'icondesc' => get_mimetype_description($file),
'iconname' => file_file_icon($file, 24),
'iconname' => file_file_icon($file),
'name' => $file->get_filename(),
'url' => $url->out(false),
];
Expand Down
2 changes: 1 addition & 1 deletion mod/folder/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ function folder_get_recent_mod_activity(&$activities, &$index, $timestart, $cour
$image = $url->out(false, array('preview' => 'tinyicon', 'oid' => $file->get_timemodified()));
$image = html_writer::empty_tag('img', array('src' => $image));
} else {
$image = $OUTPUT->pix_icon(file_file_icon($file, 24), $file->get_filename(), 'moodle');
$image = $OUTPUT->pix_icon(file_file_icon($file), $file->get_filename(), 'moodle');
}

$tmpactivity->content = (object) [
Expand Down
4 changes: 2 additions & 2 deletions mod/folder/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected function htmllize_tree($tree, $dir) {
}
$result = '<ul>';
foreach ($dir['subdirs'] as $subdir) {
$image = $this->output->pix_icon(file_folder_icon(24), $subdir['dirname'], 'moodle');
$image = $this->output->pix_icon(file_folder_icon(), $subdir['dirname'], 'moodle');
$filename = html_writer::tag('span', $image, array('class' => 'fp-icon')).
html_writer::tag('span', s($subdir['dirname']), array('class' => 'fp-filename'));
$filename = html_writer::tag('div', $filename, array('class' => 'fp-filename-icon'));
Expand All @@ -136,7 +136,7 @@ protected function htmllize_tree($tree, $dir) {
$image = $url->out(false, array('preview' => 'tinyicon', 'oid' => $file->get_timemodified()));
$image = html_writer::empty_tag('img', array('src' => $image));
} else {
$image = $this->output->pix_icon(file_file_icon($file, 24), $filenamedisplay, 'moodle');
$image = $this->output->pix_icon(file_file_icon($file), $filenamedisplay, 'moodle');
}
$filename = html_writer::tag('span', $image, array('class' => 'fp-icon')).
html_writer::tag('span', $filenamedisplay, array('class' => 'fp-filename'));
Expand Down
6 changes: 3 additions & 3 deletions mod/forum/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public function tearDown(): void {
/**
* Get the expected attachment.
*
* @param stored_file $file
* @param \stored_file $file
* @param array $values
* @param moodle_url|null $url
* @param \moodle_url|null $url
* @return array
*/
protected function get_expected_attachment(\stored_file $file, array $values = [], ?\moodle_url $url = null): array {
Expand Down Expand Up @@ -94,7 +94,7 @@ protected function get_expected_attachment(\stored_file $file, array $values =
'license' => $file->get_license(),
'filenameshort' => $file->get_filename(),
'filesizeformatted' => display_size((int) $file->get_filesize()),
'icon' => $file->is_directory() ? file_folder_icon(128) : file_file_icon($file, 128),
'icon' => $file->is_directory() ? file_folder_icon() : file_file_icon($file),
'timecreatedformatted' => userdate($file->get_timecreated()),
'timemodifiedformatted' => userdate($file->get_timemodified()),
'url' => $url->out(),
Expand Down
4 changes: 2 additions & 2 deletions mod/url/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ function url_get_coursemodule_info($coursemodule) {
$info = new cached_cm_info();
$info->name = $url->name;

//note: there should be a way to differentiate links from normal resources
$info->icon = url_guess_icon($url->externalurl, 24);
// Note: there should be a way to differentiate links from normal resources.
$info->icon = url_guess_icon($url->externalurl);

$display = url_get_final_display_type($url);

Expand Down
16 changes: 10 additions & 6 deletions mod/url/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,13 +528,17 @@ function url_get_encrypted_parameter($url, $config) {
/**
* Optimised mimetype detection from general URL
* @param $fullurl
* @param int $size of the icon.
* @param null $unused This parameter has been deprecated since 4.3 and should not be used anymore.
* @return string|null mimetype or null when the filetype is not relevant.
*/
function url_guess_icon($fullurl, $size = null) {
function url_guess_icon($fullurl, $unused = null) {
global $CFG;
require_once("$CFG->libdir/filelib.php");

if ($unused !== null) {
debugging('Deprecated argument passed to ' . __FUNCTION__, DEBUG_DEVELOPER);
}

if (substr_count($fullurl, '/') < 3 or substr($fullurl, -1) === '/') {
// Most probably default directory - index.php, index.html, etc. Return null because
// we want to use the default module icon instead of the HTML file icon.
Expand All @@ -551,10 +555,10 @@ function url_guess_icon($fullurl, $size = null) {
return null;
}

$icon = file_extension_icon($fullurl, $size);
$htmlicon = file_extension_icon('.htm', $size);
$unknownicon = file_extension_icon('', $size);
$phpicon = file_extension_icon('.php', $size); // Exception for php files.
$icon = file_extension_icon($fullurl);
$htmlicon = file_extension_icon('.htm');
$unknownicon = file_extension_icon('');
$phpicon = file_extension_icon('.php'); // Exception for php files.

// We do not want to return those icon types, the module icon is more appropriate.
if ($icon === $unknownicon || $icon === $htmlicon || $icon === $phpicon) {
Expand Down
Loading

0 comments on commit 7430208

Please sign in to comment.