[TikiWiki-commits] [Git][tikiwiki/tiki][master] [NEW][DB][MOD] fgal: Add preference to control display name generation
Benoit Grégoire (@benoitg) via TikiWiki-cvs <[email protected]>
| Newsgroups | gmane.comp.cms.tiki.cvs |
|---|---|
| Message-ID | <68957d4112b33_2c566f584934@gitlab-sidekiq-low-urgency-cpu-bound-v2-54bd675fbf-cl2w9.mail> |
Benoit Grégoire pushed to branch master at Tiki Wiki CMS Groupware / Tiki
Commits:
c944bb08 by MAGENE Sem Joel at 2025-08-08T04:22:07+00:00
[NEW][DB][MOD] fgal: Add preference to control display name generation
---
* [NEW][DB][MOD] Fgal: Add preference to control display name generation
See merge request tikiwiki/tiki!8015
- - - - -
9 changed files:
- db/tiki.sql
- + installer/schema/20250719_add_display_name_generation_to_fgals_tiki.sql
- lib/Filegals/FileGalLib.php
- lib/core/Tiki/FileGallery/Manipulator/MetadataExtractor.php
- lib/prefs/fgal.php
- lib/test/Core/Tiki/FileGallery/Manipulator/MetadataExtractorTest.php
- templates/admin/include_fgal.tpl
- templates/edit_file_gallery.tpl
- tiki-list_file_gallery.php
Changes:
=====================================
db/tiki.sql
=====================================
@@ -918,6 +918,7 @@ CREATE TABLE `tiki_file_galleries` (
`direct` text,
`template` int(10) default NULL,
`description` text,
+ `display_name_generation` VARCHAR(20) DEFAULT NULL,
`created` int(14) default NULL,
`visible` char(1) default NULL,
`lastModif` int(14) default NULL,
=====================================
installer/schema/20250719_add_display_name_generation_to_fgals_tiki.sql
=====================================
@@ -0,0 +1 @@
+ALTER TABLE `tiki_file_galleries` ADD COLUMN `display_name_generation` VARCHAR(20) DEFAULT NULL;
\ No newline at end of file
=====================================
lib/Filegals/FileGalLib.php
=====================================
@@ -32,6 +32,9 @@ use WikiParser_PluginMatcher;
class FileGalLib extends TikiLib
{
+ public const DISPLAY_NAME_PRESERVE = 'preserve';
+ public const DISPLAY_NAME_TITLECASE = 'titlecase';
+ public const DISPLAY_NAME_SPACE = 'space';
private $wikiupMoved = [];
protected static $getGalleriesParentIdsCache = null;
@@ -3421,7 +3424,7 @@ class FileGalLib extends TikiLib
}
if (empty($params['name'][$key])) {
- $params['name'][$key] = $this->getTitleFromFilename($name);
+ $params['name'][$key] = $this->generateDisplayNameFromFilename($name, $galleryId);
}
if (empty($params['deleteAfter'][$key]) || empty($params['deleteAfter_unit'][$key])) {
@@ -3646,6 +3649,54 @@ class FileGalLib extends TikiLib
$this->table('tiki_files')->update(['deleteAfter' => $deleteAfter], ['fileId' => $fileId]);
}
+ /**
+ * Generates a file display name based on the original filename, respecting system preferences.
+ * This is the centralized logic to replace getTitleFromFilename().
+ *
+ * @param string $filename The original filename (e.g., "my-file_v1.txt").
+ * @param int $galleryId The ID of the gallery the file is being
+ * uploaded to.
+ * @return string The processed display name.
+ */
+ public function generateDisplayNameFromFilename(string $filename, int $galleryId)
+ {
+ global $prefs;
+
+ $gallery_info = $this->get_file_gallery_info($galleryId);
+ $behavior = $gallery_info['display_name_generation'] ?? $prefs['fgal_filename_to_display_name'];
+
+ // Use the new preference to decide the behavior.
+ switch ($behavior) {
+ case self::DISPLAY_NAME_TITLECASE:
+ // For 'titlecase', we still pass the original filename to the legacy function.
+ return self::getTitleFromFilename($filename);
+ case self::DISPLAY_NAME_SPACE:
+ // Strips the string after the last dot (i.e., the file extension).
+ $title = preg_replace('/\.[^\.]*$/', '', $filename);
+ // Unify separators by replacing any sequence of hyphens or underscores with a single space.
+ $title = preg_replace('/[\-_]+/', ' ', $title);
+ break;
+ case self::DISPLAY_NAME_PRESERVE:
+ default:
+ // Strips the string after the last dot (i.e., the file extension).
+ $title = preg_replace('/\.[^\.]*$/', '', $filename);
+ break;
+ }
+
+ // As a final safeguard, ensure the name isn't too long for the database.
+ if (mb_strlen($title) > 200) {
+ $title = mb_substr($title, 0, 200);
+ }
+
+ return $title;
+ }
+
+ /**
+ * Applies opinionated cosmetic changes to a filename to create a display
+ * title.
+ * @deprecated Use generateDisplayNameFromFilename() instead. This
+ * function's logic is preserved for legacy compatibility.
+ */
public static function getTitleFromFilename($title)
{
if (strpos($title, '.zip') !== strlen($title) - 4) {
=====================================
lib/core/Tiki/FileGallery/Manipulator/MetadataExtractor.php
=====================================
@@ -21,7 +21,7 @@ class MetadataExtractor extends Manipulator
$file->setParam('metadata', $metadata);
if ($file->name === $file->filename && ! $file->galleryDefinition()->isDirect()) {
- $name = TikiLib::lib('filegal')::getTitleFromFilename($file->name);
+ $name = $filegallib->generateDisplayNameFromFilename($file->name, $file->galleryId);
} else {
$name = $file->name;
}
=====================================
lib/prefs/fgal.php
=====================================
@@ -5,6 +5,7 @@
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
use Tiki\Package\VendorHelper;
+use Tiki\Lib\Filegals\FileGalLib;
function prefs_fgal_list()
{
@@ -161,6 +162,19 @@ When the limit is reached, no more files can be uploaded. The user will see an e
'default' => 'n',
'tags' => ['basic'],
],
+ 'fgal_filename_to_display_name' => [
+ 'name' => tra('Default Display Name Generation'),
+ 'type' => 'radio',
+ 'default' => FileGalLib::DISPLAY_NAME_PRESERVE,
+ 'description' => tra("Controls how Tiki generates a file's default 'Display Name' from its original filename upon upload."),
+ 'options' => [
+ FileGalLib::DISPLAY_NAME_PRESERVE => tra('Keep original filename (Recommended)'),
+ FileGalLib::DISPLAY_NAME_TITLECASE => tra('Convert to Title Case (Legacy Behavior)'),
+ FileGalLib::DISPLAY_NAME_SPACE => tra('Replace underscores and hyphens with spaces only'),
+ ],
+ 'note' => tra("This affects the title shown to users in lists and pages. It is separate from the 'Preserve filenames' option, which controls the physical filename on the server disk."),
+ 'tags' => ['basic'],
+ ],
'fgal_search_in_content' => [
'name' => tra('Searchable file gallery content'),
'description' => tra('Include the search form on the current gallery page just after "Find"'),
=====================================
lib/test/Core/Tiki/FileGallery/Manipulator/MetadataExtractorTest.php
=====================================
@@ -63,10 +63,12 @@ class Tiki_FileGallery_Manipulator_MetadataExtractorTest extends TikiTestCase
public function testNameExtractionFromFilename()
{
+ global $prefs;
+ $prefs['fgal_filename_to_display_name'] = 'preserve';
$this->file->setParam('name', 'test-data.png');
$this->file->setParam('filename', 'test-data.png');
(new MetadataExtractor($this->file))->run();
- $this->assertEquals('Test Data', $this->file->name);
+ $this->assertEquals('test-data', $this->file->name);
}
public function testCreatedSoon()
@@ -97,4 +99,24 @@ class Tiki_FileGallery_Manipulator_MetadataExtractorTest extends TikiTestCase
(new MetadataExtractor($this->file))->run();
$this->assertEquals('image/png', $this->file->filetype);
}
+
+ public function testNameExtractionWithTitleCaseSetting()
+ {
+ global $prefs;
+ $prefs['fgal_filename_to_display_name'] = 'titlecase';
+ $this->file->setParam('name', 'test-data.png');
+ $this->file->setParam('filename', 'test-data.png');
+ (new MetadataExtractor($this->file))->run();
+ $this->assertEquals('Test Data', $this->file->name);
+ }
+
+ public function testNameExtractionWithSpaceSetting()
+ {
+ global $prefs;
+ $prefs['fgal_filename_to_display_name'] = 'space';
+ $this->file->setParam('name', 'test-data_file.png');
+ $this->file->setParam('filename', 'test-data_file.png');
+ (new MetadataExtractor($this->file))->run();
+ $this->assertEquals('test data file', $this->file->name);
+ }
}
=====================================
templates/admin/include_fgal.tpl
=====================================
@@ -34,6 +34,9 @@
<div class="mb-sm-3">
{preference name='fgal_preserve_filenames'}
</div>
+ <div class="mb-sm-3">
+ {preference name='fgal_filename_to_display_name'}
+ </div>
<div class="mb-sm-3">
{preference name='fgal_use_dir'}
<button role="button" type="submit" class="btn btn-primary" name="move" value="to_fs">
=====================================
templates/edit_file_gallery.tpl
=====================================
@@ -223,6 +223,21 @@ if ($(this).val() != '') {
<span class="form-text">{tr}Required for podcasts{/tr}.</span>
</div>
</div>
+ <div class="tiki-form-group row">
+ <label class="col-sm-4 col-form-label">{tr}Default Display Name Generation{/tr}</label>
+ <div class="col-sm-8">
+ <div class="form-check">
+ <input class="form-check-input" type="radio" name="display_name_generation" id="dng_default" value="" {if $gal_info.display_name_generation|default:'' eq ''}checked{/if}>
+ <label class="form-check-label" for="dng_default">{tr}Use site-wide default{/tr} ({$prefs.fgal_filename_to_display_name|default:'preserve'})</label>
+ </div>
+ {foreach from=$displayNameGenerationOptions key=value item=label}
+ <div class="form-check">
+ <input class="form-check-input" type="radio" name="display_name_generation" id="dng_{$value}" value="{$value}" {if $gal_info.display_name_generation eq $value}checked{/if}>
+ <label class="form-check-label" for="dng_{$value}">{$label}</label>
+ </div>
+ {/foreach}
+ </div>
+ </div>
<div class="tiki-form-group row">
<label for="visible" class="col-sm-4">{tr}Gallery is visible to non-admin users{/tr}</label>
<div class="col-sm-8">
=====================================
tiki-list_file_gallery.php
=====================================
@@ -614,6 +614,11 @@ $smarty->assign('url', $tikilib->httpPrefix() . parse_url($_SERVER['REQUEST_URI'
if (isset($_REQUEST['edit_mode']) and $_REQUEST['edit_mode']) {
$smarty->assign('edit_mode', 'y');
$smarty->assign('edited', 'y');
+
+ $prefslib = TikiLib::lib('prefs');
+ $displayNamePrefInfo = $prefslib->getPreference('fgal_filename_to_display_name');
+ $smarty->assign('displayNameGenerationOptions', $displayNamePrefInfo['options']);
+
if ($prefs['feature_categories'] == 'y') {
$cat_type = 'file gallery';
$cat_objid = $galleryId;
@@ -813,6 +818,7 @@ if (isset($_REQUEST['edit']) && $access->checkCsrf()) {
$gal_info = [
'galleryId' => $galleryId,
'name' => $_REQUEST['name'],
+ 'display_name_generation' => empty($_REQUEST['display_name_generation']) ? null : $_REQUEST['display_name_generation'],
'description' => $_REQUEST['description'],
'user' => $_REQUEST['user'],
'maxRows' => $_REQUEST['maxRows'],
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/c944bb08b98b7b3cff0ce9c1bd82d3ec8b8ac93b
--
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/c944bb08b98b7b3cff0ce9c1bd82d3ec8b8ac93b
You're receiving this email because of your account on gitlab.com.
_______________________________________________
TikiWiki-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-cvs