Merge pull request #8367 from Wikia/DAT-3207

DAT-3207 add mw namespaces aliases to image filename sanitizer
This commit is contained in:
Nikodem 2015-09-08 11:28:31 +02:00
commit 0df0f91e4d
2 changed files with 28 additions and 10 deletions

View file

@ -20,6 +20,7 @@ class ImageFilenameSanitizer {
if ( is_null( self::$instance ) ) {
self::$instance = new self;
}
return self::$instance;
}
@ -28,23 +29,30 @@ class ImageFilenameSanitizer {
* Used as local cache for getting string to remove
*/
private function getFilePrefixRegex( $contLang ) {
global $wgNamespaceAliases;
$langCode = $contLang->getCode();
if ( empty( $this->filePrefixRegex[$langCode] ) ) {
$fileNamespaces = [ \MWNamespace::getCanonicalName( NS_FILE ), $contLang->getNamespaces()[NS_FILE] ];
if ( empty( $this->filePrefixRegex[ $langCode ] ) ) {
$fileNamespaces = [
\MWNamespace::getCanonicalName( NS_FILE ),
$contLang->getNamespaces()[ NS_FILE ],
];
$aliases = $contLang->getNamespaceAliases();
$aliases = array_merge( $contLang->getNamespaceAliases(), $wgNamespaceAliases );
foreach ( $aliases as $alias => $namespaceId ) {
if ( $namespaceId == NS_FILE )
if ( $namespaceId == NS_FILE ) {
$fileNamespaces [] = $alias;
}
}
$this->filePrefixRegex[$langCode] = '^(' . implode( '|', $fileNamespaces ) . '):';
$this->filePrefixRegex[ $langCode ] = '^(' . implode( '|', $fileNamespaces ) . '):';
}
return $this->filePrefixRegex[$langCode];
return $this->filePrefixRegex[ $langCode ];
}
/**
* @param $filename string
* @param $contLang \Language
*
* @return mixed
*/
public function sanitizeImageFileName( $filename, $contLang ) {
@ -54,16 +62,18 @@ class ImageFilenameSanitizer {
foreach ( $textLines as $potentialFilename ) {
$filename = $this->extractFilename( $potentialFilename, $filePrefixRegex );
if ($filename) {
if ( $filename ) {
return $filename;
}
}
return $plainText;
}
/**
* @param $filename
*
* @return string
*/
private function convertToPlainText( $filename ) {
@ -71,20 +81,22 @@ class ImageFilenameSanitizer {
$filename = strip_tags( $filename );
// replace the surrounding whitespace
$filename = trim( $filename );
return $filename;
}
/**
* @param $potentialFilename
* @param $filePrefixRegex
*
* @return string|null
*/
private function extractFilename( $potentialFilename, $filePrefixRegex ) {
$trimmedFilename = trim( $potentialFilename, "[]" );
$unprefixedFilename = mb_ereg_replace( $filePrefixRegex, "", $trimmedFilename );
$filenameParts = explode( '|', $unprefixedFilename );
if ( !empty( $filenameParts[0] ) ) {
return $filenameParts[0];
if ( !empty( $filenameParts[ 0 ] ) ) {
return $filenameParts[ 0 ];
}
return null;

View file

@ -175,7 +175,13 @@ class ImageFilenameSanitizerTest extends WikiaBaseTest {
'en',
'文件名óśłżźćńę?.jpg',
'Content of gallery with diacritics and UTF characters'
]
],
[
'Image:filename.jpg',
'en',
'filename.jpg',
'Filename with alias to namespace'
],
];
}
}