r56925 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r56924‎ | r56925 | r56926 >
Date:18:24, 25 September 2009
Author:rarohde
Status:ok (Comments)
Tags:
Comment:
New Extension: FilePageMasking

Allows file description page URLs to be automatically rewritten by Mediawiki so that they don't appear to end in a file extension. This avoids confusing systems and search engines that use file extensions on URLs to determine behavior.

By default, it converts ".xxx" to "_xxx" for calls to file description pages.
Modified paths:
  • /trunk/extensions/FilePageMasking (added) (history)
  • /trunk/extensions/FilePageMasking/FilePageMasking.i18n.php (added) (history)
  • /trunk/extensions/FilePageMasking/FilePageMasking.php (added) (history)

Diff [purge]

Index: trunk/extensions/FilePageMasking/FilePageMasking.i18n.php
@@ -0,0 +1,12 @@
 2+<?php
 3+/**
 4+ * Internationalisation file for FilePageMasking extension.
 5+ *
 6+ * @addtogroup Extensions
 7+*/
 8+
 9+$messages = array();
 10+
 11+$messages['en'] = array(
 12+ 'file_masking_desc' => 'Rewrites ".xxx" into "_xxx" in image description page links',
 13+);
Property changes on: trunk/extensions/FilePageMasking/FilePageMasking.i18n.php
___________________________________________________________________
Name: svn:eol-style
114 + native
Index: trunk/extensions/FilePageMasking/FilePageMasking.php
@@ -0,0 +1,87 @@
 2+<?php
 3+
 4+/*
 5+
 6+ This extension masks local links for file description pages by modifying the file extension
 7+ at the end of the description page URL so it does not also look like a file. This makes it
 8+ easier for search engines and other programs to recognize the file description page as HTML
 9+ rather than assuming it is also a file based on its extension.
 10+
 11+ By default, extensions of the form ".xxx" are translated to "_xxx". This only occurs
 12+ for internal links that should target the file description page. When a file description page
 13+ of the form "_xxx" is requested, this extension automatically translates it back to ".xxx"
 14+ so that Mediawiki will retrieve the correct page. The choice of masking character can be
 15+ set by changing $wgFilePageMaskingCharacter, which defaults to "_". The setting of this
 16+ variable must be a single character, and must be valid in a URL.
 17+
 18+ This extension relies on $wgFileExtensions to determine the list of case insensitive file
 19+ extensions to translate. On sites where $wgStrictFileExtensions is set to false, the user may
 20+ upload files with extensions not considered by $wgFileExtensions. If this is the case, those
 21+ file extensions will not be masked.
 22+
 23+*/
 24+
 25+if ( ! defined( 'MEDIAWIKI' ) )
 26+ die();
 27+
 28+$wgExtensionCredits['parserhook'][] = array(
 29+ 'name' => 'FilePageMasking',
 30+ 'author' => 'Robert Rohde',
 31+ 'version' => '1.0',
 32+ 'description' => 'Converts file extensions ".xxx" to "_xxx" on description page links', // kept for b/c
 33+ 'descriptionmsg' => 'file_masking_desc',
 34+ 'path' => __FILE__,
 35+ 'url' => 'http://www.mediawiki.org/wiki/Extension:FilePageMasking',
 36+);
 37+
 38+$wgFilePageMaskingCharacter = '_';
 39+
 40+$wgExtensionMessagesFiles['FilePageMasking'] = dirname( __FILE__ ) . "/FilePageMasking.i18n.php";
 41+
 42+$wgHooks['GetLocalURL'][] = 'wfFilePageMasking_Mask';
 43+$wgHooks['ArticleFromTitle'][] = 'wfFilePageMasking_Unmask';
 44+
 45+
 46+/**
 47+ * This hook applies masking to file page links.
 48+ **/
 49+function wfFilePageMasking_Mask( &$title, &$url, $query ) {
 50+ global $wgFileExtensions, $wgFilePageMaskingCharacter;
 51+
 52+ if( $title->getNamespace() == NS_FILE ) {
 53+ $lUrl = strlen($url);
 54+ foreach( $wgFileExtensions as $ext ) {
 55+ $lExt = strlen($ext);
 56+ $p = stripos( $url, "." . $ext, $lUrl - $lExt - 1 );
 57+ if( $p == $lUrl - $lExt - 1 ) {
 58+ $url[$p] = $wgFilePageMaskingCharacter;
 59+ break;
 60+ }
 61+ }
 62+ }
 63+ return true;
 64+}
 65+
 66+
 67+/**
 68+ * This hook removes masking to file page requests.
 69+ **/
 70+function wfFilePageMasking_Unmask( &$title, &$article ) {
 71+ global $wgFileExtensions, $wgFilePageMaskingCharacter;
 72+
 73+ if( $title->getNamespace() == NS_FILE ) {
 74+ $name = $title->getDBkey();
 75+ $lName = strlen($name);
 76+ foreach( $wgFileExtensions as $ext ) {
 77+ $lExt = strlen($ext);
 78+ $p = stripos( $name, $wgFilePageMaskingCharacter . $ext, $lName - $lExt - 1 );
 79+ if( $p == $lName - $lExt - 1 ) {
 80+ $name[$p] = ".";
 81+ $title = Title::newFromText( $name, NS_FILE );
 82+ break;
 83+ }
 84+ }
 85+ }
 86+ return true;
 87+}
 88+
Property changes on: trunk/extensions/FilePageMasking/FilePageMasking.php
___________________________________________________________________
Name: svn:eol-style
189 + native

Comments

#Comment by Siebrand (talk | contribs)   18:29, 25 September 2009

Interesting! How will this handle "Test.jpg" and "Test.png"?

#Comment by Siebrand (talk | contribs)   18:31, 25 September 2009

Lemme answer that myself: it will rewrite to "Test_jpg" and "Test_png". Duh.

#Comment by 😂 (talk | contribs)   18:52, 25 September 2009

Curious, shouldn't this be in core?

Would suggest doing it in core and hide behind an off-by-default var until it's perfect :) We don't want bug 4421 depending on an extension.

#Comment by Dragons flight (talk | contribs)   19:09, 25 September 2009

The last discussion of issues related to 4421 (on wikitech-l) mostly wanted to move in the direction of suppressing file extensions entirely. In other words, just having File:Foo for description pages with file type information preserved in the database. Such a change would be a much bigger detail and require strategies for conflict resolution, but would have the advantage that one could change a file's type without using a whole new page.

Compared to that approach, this is much quicker, and something I've been using on one of my sites for some time. Obviously the logic here could be ported into the core, but I didn't because people seemed to want to take the core in a different direction.

Status & tagging log