mirror of
https://github.com/Universal-Omega/PortableInfobox.git
synced 2024-11-15 11:59:56 +00:00
Merge pull request #8367 from Wikia/DAT-3207
DAT-3207 add mw namespaces aliases to image filename sanitizer
This commit is contained in:
commit
0df0f91e4d
|
@ -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;
|
||||
|
|
|
@ -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'
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue