Module talk:File

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

Small correction needed[edit]

Hi Rillke, the function woExtension works well, thank you. Just in a very special (and seldom) case it's not so perfect as expected.

examples: Flag map of Washington DC.png, Flag of Washington, D.C. icon.svg, Flag of Washington, D.C..svg
  • Flag map of Washington DC
  • Flag of Washington, D.C. icon
  • Flag of Washington, D.C.

is fine; also

  • Flag map of Washington DC
  • Flag of Washington, D.C. icon
  • Flag of Washington, D.C.

but when the file name itself contains a period and the extension is omitted, it doesn't

  • Flag map of Washington DC ==> works well, nothing to strip
  • Flag of Washington, D.C ==> ". icon" is stripped even that it isn't an extension
  • Flag of Washington, D.C ==> last period is stripped even there isn't any extension

It is possible to perform the extension-strip even with input without extension; but only as long there is no period in the file name.

It would be fine when Module:File works also in these cases. We intend to pass an unexamined input to it and would like that it works well even in these 0.001 % of cases: It should only strip when it gets a valid dot-extension sequence allowed in wikimedia. Of course exist a very very few files like "filename.svg.png" and when the modul gets that without the ".png" it cannot recognize; but that is a problem of a vanishing small number of names and we can live with.
AFAIK 16 extensions are valid, png/svg/gif/jpg are the prevalent majority:

 ['png'] = 1, 
 ['svg'] = 1, 
 ['gif'] = 1, 
 ['jpg'] = 1, 

 ['jpeg'] = 1, 
 ['tif'] = 1, 
 ['tiff'] = 1, 
 ['xcf'] = 1, 
 ['mid'] = 1, 
 ['ogg'] = 1, 
 ['ogv'] = 1, 
 ['djvu'] = 1, 
 ['oga'] = 1, 
 ['flac'] = 1, 
 ['wav'] = 1, 
 ['webm'] = 1

In case it becomes too expensive to have this expansion in the function woExtension it would meet as well our needs to have an own function like woValidExt or so. sarang사랑 19:08, 17 February 2015 (UTC)[reply]

Users are supposed to supply a valid file title. If you show me a use case I agree with, I will tell user:CommonsMaintenanceBot to create and curate a list of valid file extensions. It won't be expensive but keeping the list up-to-date is time-consuming and dependent on motivated individuals as well as keeping my bot running :-) -- Rillke(q?) 12:10, 26 February 2015 (UTC)[reply]

workaround[edit]

With the postprocessor-template {{Tl|Validextension}| this problem can be solved.

Flag map of Washington DC.png = Flag map of Washington DC with "png"

Flag of Washington, D.C. icon.svg = Flag of Washington, D.C. icon with "svg"

Flag of Washington, D.C..svg = File:Flag of Washington, D.C. with "svg"


Flag of Washington, D.C..svg = Flag of Washington, D.C. with "svg"

Flag of Washington, D.C..svg = Flag of Washington, D.C. with "svg"

Flag of Washington, D.C. icon = Flag of Washington, D.C with "icon"

Checkmark This section is resolved and can be archived. If you disagree, replace this template with your comment. sarang사랑 11:23, 27 February 2015 (UTC)

fileExists script errors[edit]

@Rillke: Would it be possible for the "fileExists" function to return nothing if the input is empty or an invalid page title? Currently, it returns a script error:

Because of this, Template:Vector version available/sandbox/en shows up in Category:Pages with script errors. SiBr4 (talk) 14:12, 20 February 2015 (UTC)[reply]

@SiBr4: IMHO it belongs to the invoker to ensure that no empty input will be passed. Either with some #if:, or with a construct somehow like
{{#iferror:{{#invoke:File|fileExists|file=File:{{{1<noinclude>|X.svg</noinclude>}}}}}}}
Noinclude wouldn't have worked, since the invoke is in /layout, and the call from /en wouldn't use the fallback. SiBr4 (talk) 15:50, 26 February 2015 (UTC)[reply]
@SiBr4: Use the new {{#invoke:File|fileExistsRelaxed|file=...}}. Also added a note about catching errors to the documentation. -- Rillke(q?) 12:39, 26 February 2015 (UTC)[reply]
Thanks. That leaves loading times as the only difference between this module and #ifexist. SiBr4 (talk) 15:50, 26 February 2015 (UTC)[reply]
Checkmark This section is resolved and can be archived. If you disagree, replace this template with your comment. Rillke(q?) 12:39, 26 February 2015 (UTC)

Date (done)[edit]

dateWorkCreated

Script error with "dateWorkCreated"[edit]

@Jarekt: this new error causes thousands of Pages with script errors. My knowledge does not allow to repair it. Can you please fix it? Merry Xmas -- sarang사랑 14:09, 25 December 2020 (UTC)[reply]

@Sarang: I do not understand dateWorkCreated well, as it seems to be getting HTML of Plastic-recyc-09.svg and than parses it looking for the tags associated with dates added by {{Information}} template. This is kind of crazy approach, as so many things can go wrong. I think the best solution would be not to use dateWorkCreated function at all. Why is it needed? A much easier solution could be to use {{#invoke:Information/sandbox|SDC_Date|mid=M66700555 |lang=en|icon=0}} which gives " " and relies on SDC. --Jarekt (talk) 06:07, 26 December 2020 (UTC)[reply]
Thank you Jarek, when that error is not fixed by somebody the many thousand errors will remain. Who can do it?
For my needs something other than that is requested: to calculate the difference to today the returned date should be somehow numeric; ok, I can convert from another date format.
I need not the date that is entered into the field of the description, but most preferably the last edit date of the file description. When that is too difficult or too much effort, that of the last upload. Unfortunately I do not know how to get one of these, therefore I took that dateWorkCreated for an intermediate solution.
To replace it in a template that is transcluded more often than 780K times I shall wait until a final solution exists. It is there used for the sorting within a category. -- sarang사랑 09:02, 26 December 2020 (UTC)[reply]
I will try to figure it out, but you might want to also check with people who wrote those modules on how to fix it. All this code is new to me and I think that current implementation of "dateWorkCreated" is a really bad idea and it should be avoided if possible. --Jarekt (talk) 04:00, 27 December 2020 (UTC)[reply]
@Sarang: , according to mw:Help:Magic_words {{REVISIONTIMESTAMP}} (20221026044644) gives "latest revision to current page" maybe that would work better for you. --Jarekt (talk) 03:42, 28 December 2020 (UTC)[reply]
@Sarang: I am still trying to figure out how to fix the files in Category:Pages with script errors and in Category:Pages with malformed coordinate tags, which are filled with SVG files. I do think the issue is with recent use of Module:File's "dateWorkCreated" and Module:Igenmod's getdate and wrkdate functions, which render full filepage HTML, parse it and extract info used to alter the page and HTML, leading to recursive dependencies and unstable behavior. I am planning to retire dateWorkCreated function and clean up Module:File (add sandbox and test scripts). I can also try to fix Module:Igenmod, unless you want to do it. --Jarekt (talk) 19:45, 31 December 2020 (UTC)[reply]
Thank you very much, Jarek. With the revisiondate some accesses are easier, but I still need the upload date - preferably of the last upload but any upload date will do it. Because the workdate determination in File seemed to me too complicated I tried to write it with simpler code in Igenmod but had no success and used again Module:File; when you can simplify my code you are welcome, you know that I am not at all experienced in Lua. Because both, Image generation and Igenmod are heavily used, am trying to update them not too often. With my wishes for a better 2021 -- sarang사랑 07:57, 1 January 2021 (UTC)[reply]
@Sarang: I do not know a way to get upload date for a file from Lua. I asked at en:Wikipedia_talk:Lua#file_upload_date?. Maybe someone knows the way. --Jarekt (talk) 01:22, 2 January 2021 (UTC)[reply]

require('strict')[edit]

{{Edit protected}} As per the new lua feature mentioned at m:Tech/News/2022/42, could require('Module:No globals') be replaced with require('strict') -- WOSlinker (talk) 17:21, 25 October 2022 (UTC)[reply]

✓ Done Jonesey95 (talk) 04:46, 26 October 2022 (UTC)[reply]