By Joe Beaudoin Jr. | Posted on August 27, 2007 at 4:29 am |
Posted in Category: Do-it-yourself, How-To, Maintenance, Mediawiki

An interesting issue presented itself a while back. As some of you might know, we have a central repository for our media files (the [[md:Main page|Battlestar Wiki Media Repository]]) where we store our images, sound files, and what not, so they can be shared across multiple language editions (English, French, German, et al).

An outstanding and somewhat irksome issue to me was the fact that I could never check to see whether or not a certain image was being used across multiple wikis, without manually fiddling with the URL in the address bar. So what I did was change “media” (as in “media.battlestarwiki.org/wiki/Image:thing.jpg”) to “en” (as in “en.battlestarwiki.org/wiki/Image:thing.jpg”), “en” to “de” and so on.  Needless to say, a tedious task to do manually, particularly when you want to delete unused or inappropriately tagged images, even with someone who can type up to 110 words a minute.

The reason for this check is to discover what pages on a wiki are using this file. Once I (or one of the admins) discovers what pages use the image, it’s a fairly simple matter to go through and remove the image from articles that are using it prior to deletion, thus avoiding a “broken image”. Which results in an unprofessional and unclean distraction from the page’s content.

So the problem that faced me was simple: How do I easily switch between the image description pages on all the Wikis?

It’s something that I’ve been trying to tackle for a while now, and I’ve rummaged through the Wikimedia Commons (a larger repository very much the “mother” of the Battlestar Wiki Media repository) in an attempt to discover inspiration.

If you take a look at this image description page on an image of [[Edward James Olmos]] on the Wikimedia Commons, you’ll see some tabs on top of the page, which read “check usage”, “find categories”, and, the last one, a small tab called “en” (the two letter code for English).

I tried (briefly) adapting the “wikitools” software that the “check usage” and “find categories” tab makes use of, only to find that it would be more of a hassle to rework, debug, and run through hundreds of lines of code and configuration swtiches. Indeed, it made the laborious task of tweaking the URL in the address boxes look like a stroll in the park.

So that was out… but the “en” tab struck me.

And so I began finding out which files in MediaWiki generated the tabs. After a brief bit of research, I found the target: a file called “SkinTemplate.php”, located in the “includes” directory of any MediaWiki installation worth its salt.

With my fair knowledge of PHP at hand, I managed to whip up basic tabs for each language (“de”, “en”, “es”, etc — including “media” for the repository), and “hide” tabs of the same language.

Quite pleased with myself, I managed to figure out that I was on the right path.

One problem. Some languages, such as German and Turkish, have a different name for the “image:” namespace. (“Bild:” and “Resim:” respectably.) While MediaWiki is apparently smart enough to discern that someone calling for:

http://de.battlestarwiki.org/wiki/Image:William_Adama_promo.jpg

would get:

http://de.battlestarwiki.org/wiki/Bild:William_Adama_promo.jpg

it’s not smart enough to do the reverse, unless you change both “de” to “media” and “Bild” to “Image:”… by hand.

So I came up with a solution of running the URL through PHP’s preg_replace().

And thus I have worked out the following bit of code, which requires only minor modifications to the SkinTemplate.php file. I’ve attached the file with the code, so it’s easier to manipulate (and, obviously, if anyone wants to take a crack at making it more efficient, you’re welcome to do so)!

Download the file here!

When you use the attached file, you will want to go through and remove the instances of “battlestarwiki.org”, in addition to renaming the file so as to strip the “.txt” extension. I had to do this since WordPress doesn’t like file attachments with the .php extension.

Comments, suggestions, and constructive criticisms welcome! Also, if you do use this on your own wikis, I would kindly ask that you credit me and Battlestar Wiki somewhere when doing so!

Also, as a side note, I plan on doing more of these “how tos” in the future, so as to share our knowledge with other webmasters and web entrepreneurs. Personally, I’ve never cared to horde knowledge, so I find teaching back to others is gratifying and, ultimately, beneficial to us all. So, if you want to know how we do something, please ask and we’ll be glad to try and answer your questions! ;-)

This entry was posted on Monday, August 27th, 2007 at 4:29 am and is filed under Do-it-yourself, How-To, Maintenance, Mediawiki. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

  • http://en.battlestarwiki.org/wiki/User:Shane Shane

    Never say never.

  • http://en.battlestarwiki.org/wiki/ Joe Beaudoin Jr.

    “Never say never.”

    Yes, I know… I know…

    Seriously, though, I don’t care to horde knowledge. Hell, you know that. ;-)

  • http://en.battlestarwiki.org/wiki/User:Shane Shane

    Aye. Going to try to modify it for our needs though.

  • Kad

    Hi, the upload link doesn’t work:
    http://blog.battlestarwiki.org/wp-content/uploads/2007/08/skintemplatephp.txt

    It just goes to your homepage.

  • http://en.battlestarwiki.org/wiki/ Joe Beaudoin Jr.

    Well, it’s no longer there.

    You can download the extension from:

    http://www.mediawiki.org/wiki/Extension:Imagetabs