https://www.mediawiki.org/wiki/Special:Code/MediaWiki/102073
Revision: 102073
Author: aaron
Date: 2011-11-04 23:49:03 +0000 (Fri, 04 Nov 2011)
Log Message:
-----------
* Added File::normalizeTitle() function and used it to consolidate file title
validity checks and sanitation. (bug 32195)
* Broke a few long lines.
Modified Paths:
--------------
trunk/phase3/includes/filerepo/ArchivedFile.php
trunk/phase3/includes/filerepo/File.php
trunk/phase3/includes/filerepo/FileRepo.php
trunk/phase3/includes/filerepo/LocalRepo.php
trunk/phase3/includes/filerepo/RepoGroup.php
trunk/phase3/includes/filerepo/UnregisteredLocalFile.php
Modified: trunk/phase3/includes/filerepo/ArchivedFile.php
===================================================================
--- trunk/phase3/includes/filerepo/ArchivedFile.php 2011-11-04 23:33:53 UTC
(rev 102072)
+++ trunk/phase3/includes/filerepo/ArchivedFile.php 2011-11-04 23:49:03 UTC
(rev 102073)
@@ -73,8 +73,8 @@
$this->dataLoaded = false;
$this->exists = false;
- if( is_object( $title ) ) {
- $this->title = $title;
+ if( $title instanceof Title ) {
+ $this->title = File::normalizeTitle( $title,
'exception' );
$this->name = $title->getDBkey();
}
@@ -86,7 +86,7 @@
$this->key = $key;
}
- if ( !$id && !$key && !is_object( $title ) ) {
+ if ( !$id && !$key && !( $title instanceof Title ) ) {
throw new MWException( "No specifications provided to
ArchivedFile constructor." );
}
}
Modified: trunk/phase3/includes/filerepo/File.php
===================================================================
--- trunk/phase3/includes/filerepo/File.php 2011-11-04 23:33:53 UTC (rev
102072)
+++ trunk/phase3/includes/filerepo/File.php 2011-11-04 23:49:03 UTC (rev
102073)
@@ -61,12 +61,12 @@
*/
/**
- * @var LocalRepo
+ * @var FileRepo
*/
var $repo;
/**
- * @var Title
+ * @var Title|false
*/
var $title;
@@ -87,14 +87,44 @@
/**
* Call this constructor from child classes
*
- * @param $title
- * @param $repo
+ * @param $title Title|false
+ * @param $repo FileRepo|false
*/
function __construct( $title, $repo ) {
+ if ( $title !== false ) { // account for UnregisteredLocalFile
et all
+ $title = self::normalizeTitle( $title, 'exception' );
+ }
$this->title = $title;
$this->repo = $repo;
}
+ /**
+ * Given a string or Title object return either a
+ * valid Title object with namespace NS_FILE or null
+ * @param $title Title|string
+ * @param $exception string|false Use 'exception' to throw an error on
bad titles
+ * @return Title|null
+ */
+ static function normalizeTitle( $title, $exception = false ) {
+ $ret = $title;
+ if ( $ret instanceof Title ) {
+ # Normalize NS_MEDIA -> NS_FILE
+ if ( $ret->getNamespace() == NS_MEDIA ) {
+ $ret = Title::makeTitleSafe( NS_FILE,
$ret->getDBkey() );
+ # Sanity check the title namespace
+ } elseif ( $ret->getNamespace() !== NS_FILE ) {
+ $ret = null;
+ }
+ } else {
+ # Convert strings to Title objects
+ $ret = Title::makeTitleSafe( NS_FILE, (string)$ret );
+ }
+ if ( !$ret && $exception !== false ) {
+ throw new MWException( "`$title` is not a valid file
title." );
+ }
+ return $ret;
+ }
+
function __get( $name ) {
$function = array( $this, 'get' . ucfirst( $name ) );
if ( !is_callable( $function ) ) {
Modified: trunk/phase3/includes/filerepo/FileRepo.php
===================================================================
--- trunk/phase3/includes/filerepo/FileRepo.php 2011-11-04 23:33:53 UTC (rev
102072)
+++ trunk/phase3/includes/filerepo/FileRepo.php 2011-11-04 23:49:03 UTC (rev
102073)
@@ -644,7 +644,7 @@
* @param $title Title of image
* @return Bool
*/
- function checkRedirect( $title ) {
+ function checkRedirect( Title $title ) {
return false;
}
Modified: trunk/phase3/includes/filerepo/LocalRepo.php
===================================================================
--- trunk/phase3/includes/filerepo/LocalRepo.php 2011-11-04 23:33:53 UTC
(rev 102072)
+++ trunk/phase3/includes/filerepo/LocalRepo.php 2011-11-04 23:49:03 UTC
(rev 102073)
@@ -137,15 +137,10 @@
*
* @param $title Title of file
*/
- function checkRedirect( $title ) {
+ function checkRedirect( Title $title ) {
global $wgMemc;
- if( is_string( $title ) ) {
- $title = Title::newFromText( $title );
- }
- if( $title instanceof Title && $title->getNamespace() ==
NS_MEDIA ) {
- $title = Title::makeTitle( NS_FILE, $title->getText() );
- }
+ $title = File::normalizeTitle( $title, 'exception' );
$memcKey = $this->getSharedCacheKey( 'image_redirect', md5(
$title->getDBkey() ) );
if ( $memcKey === false ) {
Modified: trunk/phase3/includes/filerepo/RepoGroup.php
===================================================================
--- trunk/phase3/includes/filerepo/RepoGroup.php 2011-11-04 23:33:53 UTC
(rev 102072)
+++ trunk/phase3/includes/filerepo/RepoGroup.php 2011-11-04 23:49:03 UTC
(rev 102073)
@@ -108,22 +108,15 @@
if ( !$this->reposInitialised ) {
$this->initialiseRepos();
}
- if ( !($title instanceof Title) ) {
- $title = Title::makeTitleSafe( NS_FILE, $title );
- if ( !is_object( $title ) ) {
- return false;
- }
+ $title = File::normalizeTitle( $title );
+ if ( !$title ) {
+ return false;
}
- if ( $title->getNamespace() != NS_MEDIA &&
$title->getNamespace() != NS_FILE ) {
- throw new MWException( __METHOD__ . ' received an Title
object with incorrect namespace' );
- }
-
# Check the cache
if ( empty( $options['ignoreRedirect'] )
&& empty( $options['private'] )
- && empty( $options['bypassCache'] )
- && $title->getNamespace() == NS_FILE )
+ && empty( $options['bypassCache'] ) )
{
$useCache = true;
$time = isset( $options['time'] ) ? $options['time'] :
'';
@@ -201,7 +194,7 @@
/**
* Interface for FileRepo::checkRedirect()
*/
- function checkRedirect( $title ) {
+ function checkRedirect( Title $title ) {
if ( !$this->reposInitialised ) {
$this->initialiseRepos();
}
Modified: trunk/phase3/includes/filerepo/UnregisteredLocalFile.php
===================================================================
--- trunk/phase3/includes/filerepo/UnregisteredLocalFile.php 2011-11-04
23:33:53 UTC (rev 102072)
+++ trunk/phase3/includes/filerepo/UnregisteredLocalFile.php 2011-11-04
23:49:03 UTC (rev 102073)
@@ -46,7 +46,7 @@
/**
* @throws MWException
- * @param $title string
+ * @param $title Title|false
* @param $repo FSRepo
* @param $path string
* @param $mime string
@@ -55,18 +55,19 @@
if ( !( $title && $repo ) && !$path ) {
throw new MWException( __METHOD__.': not enough
parameters, must specify title and repo, or a full path' );
}
- if ( $title ) {
- $this->title = $title;
+ if ( $title instanceof Title ) {
+ $this->title = File::normalizeTitle( $title,
'exception' );
$this->name = $repo->getNameFromTitle( $title );
} else {
$this->name = basename( $path );
- $this->title = Title::makeTitleSafe( NS_FILE,
$this->name );
+ $this->title = File::normalizeTitle( $this->name,
'exception' );
}
$this->repo = $repo;
if ( $path ) {
$this->path = $path;
} else {
- $this->path = $repo->getRootDirectory() . '/' .
$repo->getHashPath( $this->name ) . $this->name;
+ $this->path = $repo->getRootDirectory() . '/' .
+ $repo->getHashPath( $this->name ) . $this->name;
}
if ( $mime ) {
$this->mime = $mime;
@@ -122,7 +123,8 @@
function getURL() {
if ( $this->repo ) {
- return $this->repo->getZoneUrl( 'public' ) . '/' .
$this->repo->getHashPath( $this->name ) . rawurlencode( $this->name );
+ return $this->repo->getZoneUrl( 'public' ) . '/' .
+ $this->repo->getHashPath( $this->name ) .
rawurlencode( $this->name );
} else {
return false;
}
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs