User talk:CharlesC/Template for document links showing file icons

From Meta, a Wikimedia project coordination wiki

Is there any way to modify this template to open the links in a new window?[edit]

Just curious...


I don't think there is (using standard wiki code and templates). You would have to change some MediaWiki scripts to get that effect. - CharlesC 20:14, 18 August 2006 (UTC)[reply]

403 forbidden[edit]

Great instructions, but when I get to the "type 'template:pdf' at the end of the site's URL", I get a 403 Forbidden error and the page displays "You don't have permission to access /wiki/template:php on this server".

Is there a step I'm missing?

Probably to do with how your Apache webserver is setup, but can easily get around this. Just go to your user page or some other page and add a link like this:
Then when you've saved the page you can click on the hyperlink which will take you to the new template page where you can add the wiki text for the template. --CharlesC 00:31, 17 July 2006 (UTC)[reply]
I also had this problem, but the solution suggested by Charles gave the same error. However, using the following URL does work
My machine changed .../template:pdf to .../Template:Pdf, but it still worked.
Like the user above, I had problems, but the solution for me was
I have altered the instruction slightly that should be able to take into account varying wiki site URL formats. CharlesC 18:48, 16 March 2007 (UTC)[reply]


When I use this template, I don't see the icons, only hyperlinks:

"http://mywiki/images/2/2a/PowerPoint.gif [[media:{{{1}}}|{{{2}}}]] http://mywiki/images/d/da/Info_circle.png"

It may be because you haven't uploaded some relevant icon images as described in the 'Creating a document type template' section. -- 11:39, 4 August 2006 (UTC)[reply]
The solution is the same as explained later in here just set $wgAllowExternalImages = true;

.Doc Format[edit]

I've successfully made pdf files. But when I did the same, but .doc file, I received ".doc" is not a recommended image file format.

Change in you LocalSettings.php
$wgFileExtensions = array( 'png', 'gif', 'jpg', 'jpeg', 'pdf', 'doc', 'xls' );
to allow 'other' file types. 14:04, 8 August 2006 (UTC)[reply]

Generic File Icon Template[edit]

Thanks for the head start on this template CharlesC!

I made a little change to it which allows you to have a single template that can be used for multiple file types. Instead of uploading the icons individually, I put them into a folder under Images named Icons so I'd know exactly where they were and I called the macro File and added a parameter for the icon type you wanted to show. The key to making it generic is knowing where the icon images are stored and that's why I elected to put them in their own subdirectory of images.

http://mysite/wiki/images/icons/{{{1}}}.png [[media:{{{2}}}|{{{3}}}]] <span class="plainlinks">[{{SERVER}}{{localurl:image:{{{2}}}}} http://mysite/wiki/images/icons/info.png]</span>

To use this template, the syntax is the same as your example except for the addition of the file type:

For pdf icon you would do this:

{{file|pdf|document.pdf|My pdf document}}

If you want to do it for a xls file it would be:

{{file|xls|sheet.xls|My xls document}}

Generic File Template Revisited:[edit]

Another spin on the Generic File Template which makes things a bit easier for users, but is a bit more work for the admin, and may be a bit complicated to follow is the following:

Note: You require ParserFunctions StringFunctions for this to work. Basically I took the solution above, and rewrote it to parse the end of the filename, to look for various extensions. I use that to lookup the appropriate icon. The downside is you need an if statement for each doc type you wish to represent.

This works for mixes of upper and lower case extensions.

http://mysite/wiki/images/icons/{{#ifexpr:{{#rpos:{{lc:{{{1}}}}}|.zip}} > 0 |zip.png|}}{{#ifexpr:{{#rpos:{{lc:{{{1}}}}}|.doc}} > 0 |doc.png|}}{{#ifexpr:{{#rpos:{{lc:{{{1}}}}}|.xls}} > 0 |xls.png|}}{{#ifexpr:{{#rpos:{{lc:{{{1}}}}}|.ppt}} > 0 |ppt.png|}}{{#ifexpr:{{#rpos:{{lc:{{{1}}}}}|.pdf}} > 0 |pdf.png|}} [[media:{{{1}}}|{{{2}}}]] <span class="plainlinks">[{{SERVER}}{{localurl:image:{{{1}}}}} http://mysite/wiki/images/icons/Click_for_more_info.png]</span>

So to use this you just need to use:

 {{file|sheet.xls|My xls document}}

MarkEldridge 22:46, 28 November 2006 (UTC)[reply]

Generic File Template Revisited And A Bit More Enhanced[edit]

What this version gives you above that described earlier?

  • easier add of new extension and image pairs
  • simplier edit and better readability
  • simplier handling of case when one image belongs to more than one extension
  • possibility of handling extensions of type like .zip.001 or .7z.001
{{SERVER}}/skins/common/images/icons/fileicon-{{#switch: {{ #sub:{{lc:{{{1}}}}} | {{ #ifexpr: {{#rpos:{{lc:{{{1}}}}}|.}} > 0 | {{#rpos:{{lc:{{{1}}}}}|.}} | {{#len:{{{1}}}}} }} }} 
{{#switch: {{#sub: {{#sub:{{lc:{{{1}}}}} | 0 | {{#rpos:{{lc:{{{1}}}}}|.}} }} | {{#rpos: {{#sub:{{lc:{{{1}}}}} | 0 | {{#rpos:{{lc:{{{1}}}}}|.}} }}|.}} }} 
| #default = ''N/A'' 
| #default = ''N/A'' 
}} [[media:{{{1}}} | {{#if: {{{2|}}} | {{{2}}} | {{{1}}} }}]] <span class="plainlinks">[{{SERVER}}{{localurl:Image:{{{1}}}}} {{SERVER}}/skins/common/images/icons/fileinfo.png]</span>

Tested in MediaWiki 1.8.2 --Hofi 00:52, 17 December 2006 (UTC)[reply]

Generic templates pros and cons[edit]

Yes I have played around with using a single template too. It is a good idea! The reasons I settled on keeping the templates separate was for simplicity for the user (it makes the templates shorter) and the flexibility of being able to slightly tweak the code for individual filetypes.

For instance in one of my wikis I have a template for video links that can take an extra parameter for an image file (which also acts as a link) representing the file which makes the links a bit more visual.

Another example might be for an obscure file type that people may not know what to do with it, so could put an extra icon next to the link that has instructions for viewing it perhaps. But anyway I quite agree, some people may well prefer the single template. Thanks for the suggestions. - CharlesC 19:16, 16 March 2007 (UTC)[reply]

Can there be an issue in Mediawiki 1.7 ?[edit]

I used this great template on an earlier MW version without any problems. Now, on 1.7.1 I am consistently getting the following output in my test page:

http://localhost/wiki/images/8/83/Pdf.gif Example PDF document http://localhost/wiki/images/d/da/Info_circle.png

The three hyperlinks work fine. The first one opens the pdf icon, the second one opens the PDF and the third one opens the image info page. Why can' I see the icons? Is there a setting I have forgotten or overlooked?

Sorry to bother, I found it already. Set the following parameter and it works like a dream: $wgAllowExternalImages = true;

Another question: The PDF opens in the same window as MediaWiki. Is there a possibility to set the target to open a new page?

Thanks for your help


Great, well you're ahead of me, my wikis are still using 1.5. This actually applies to any version of MW I think - so I have now mentioned this as part of the instructions.
As far as opening a pdf in another window - I don't think that is possible with wiki code, although I'm sure it's possible to use tags and create a MW extension to do the trick. The simple answer is to shift-click the link in IE, or ctrl-click in Firefox. - CharlesC 16:45, 30 August 2006 (UTC)[reply]

Linked File[edit]

Instead of puting the uploaded document in the center section below, is there a way to have a linked file from our server there?

Something like:

  {{pdf|file://servername/documentname.pdf|Document Name}}
I'm not sure about linking to a local file system or network drive as I think many browsers have issues with this. But if the files are being served on an ftp or http server then yes, that is even simpler. The template would have to be different though, and it wouldn't need the info icon (as the file presumably wouldn't have an associated wiki page). Easiest thing to do would be to write:
{{pdf icon}} [http://servername/documentname.pdf Document Name]
The 'pdf icon' template would simply display the icon.
Actually thinking about it, in this instance you can adjust the CSS on the site to make external links to PDF files have a pdf icon after the hyperlink by default. I have seen notes on doing this I think in the Meta page about file uploads. - CharlesC 19:12, 16 March 2007 (UTC)[reply]

Icons to use[edit]

Multiple File Download?[edit]

Very nice template! A semi-related question. I'm working on a private wiki for my club.

I'd like to be able to store several common forms (PDFs, DOCs and XLS files) on the wiki, and be able to set up some "packets" of the forms, where a user can download all the forms in the packet at one swipe.

I'm not interested in using a ZIP file, because if one of the forms changes and the form is used in multiple packets, I don't want to have to rebuild all the packets. Oh great CharlesC, any ideas? Thanks in advance, --Shelton1234 21:07, 9 March 2007 (UTC)

I think you either:
  1. Group the filetype variants of each form on the page together as individual links and the user decides which format they need to download - then you can update files individually OR
  2. You do the zip method. If all the files in each zip file are different formats of the same form it sounds like if the form changes than all the filetypes will need to be changed and re-zipped anyway. This is the only way of downloading all the files in one go. Will need to allow the zip filetype in your wikis 'localSettings.php' though. See instruction here. - CharlesC 19:25, 16 March 2007 (UTC)[reply]

Improving Template with ParserFunctions[edit]

If you have the [ParserFunctions] extension, you can improve the template by making the title optional. This template uses the filename if no title is given:

{{SERVER}}/wiki/images/pdf_icon.png [[media:{{{1}}}|{{#if: {{{2}}} | {{{2}}} | {{{1}}} }} ]] <span class="plainlinks">[{{SERVER}}{{localurl:image:{{{1}}}}} {{SERVER}}/wiki/images/3/34/Info_circle.png]</span>

Dbu 08:42, 25 January 2008 (UTC)[reply]

Force download[edit]

Thanks for the instructions - very helpful. Do you know of a way to force a download? We are using a file type that cannot be opened in a browser (FileMaker Pro), and I'd like clicking on the link to start a download if possible.

Thanks, Aion

I am afraid I don't - I think this depends on the browser type (and settings). --CharlesC 12:28, 2 June 2008 (UTC)[reply]


Is it possible, that the phrase "media" needs a conlon before: :media:

My installation based on Mediawiki 1.7 only works with this additional colon.

See also (Linking directly to an image without displaying it)

FCKeditor integration[edit]

I think this could be integrated into FCKeditor.

I have made a quick ugly hack, certainly somebody with more knowledge of mediawiki and fckeditor could give a better version.

in mediawiki/extensions/FCKeditor/plugins/mediawiki

edit the file fckplugin.js

modify the 'img' case into something like this

case 'img' :
var imgHeight   = htmlNode.getAttribute( '_fck_mw_height' ) || '' ;
if (imgName.EndsWith('.doc') || imgName.EndsWith('.xls') || imgName.EndsWith('.pdf')){
        stringBuilder.push( '{{file|' ) ;
        stringBuilder.push( imgName ) ;
        if ( imgCaption.length > 0 )
        stringBuilder.push( '|' + imgCaption ) ;
        stringBuilder.push( '}}' ) ;

when you use the 'insert image' button of FCKeditor it will use the generic file template (as explained above) instead of the image tag.

the nice thing is that the 'insert image' button does a search also in other file types.

Hyperlink not working[edit]

Even though the hyperlink is pointing to the correct file , still on clicking the hyperlink the pdf file does not open, Any pointers on where am i going wrong ? Only on saying "Copy Shortcut" on right click of the link and pasting the shortcut in the explorer bar the file opens in the browser.

How to include page with the template in the file history page?[edit]

I've played around with the various suggestions for rendering a useful friendly link to "file:" resources, and got it working well. My only concern/question is that when using the template for a particular file on an article, the file history page for that file, in the "File usage" section that says:

<quote>The following 3 pages link to this file: </quote>

does not include the article where I use the template. I am new to templates, and I'm sure it is probably some simple solution. Any suggestions? MeekMark (talk) 14:51, 9 April 2013 (UTC)[reply]