r102073 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r102072‎ | r102073 | r102074 >
Date:23:49, 4 November 2011
Author:aaron
Status:ok (Comments)
Tags:filebackend 
Comment:
* 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 (modified) (history)
  • /trunk/phase3/includes/filerepo/File.php (modified) (history)
  • /trunk/phase3/includes/filerepo/FileRepo.php (modified) (history)
  • /trunk/phase3/includes/filerepo/LocalRepo.php (modified) (history)
  • /trunk/phase3/includes/filerepo/RepoGroup.php (modified) (history)
  • /trunk/phase3/includes/filerepo/UnregisteredLocalFile.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/filerepo/FileRepo.php
@@ -644,7 +644,7 @@
645645 * @param $title Title of image
646646 * @return Bool
647647 */
648 - function checkRedirect( $title ) {
 648+ function checkRedirect( Title $title ) {
649649 return false;
650650 }
651651
Index: trunk/phase3/includes/filerepo/UnregisteredLocalFile.php
@@ -46,7 +46,7 @@
4747
4848 /**
4949 * @throws MWException
50 - * @param $title string
 50+ * @param $title Title|false
5151 * @param $repo FSRepo
5252 * @param $path string
5353 * @param $mime string
@@ -55,18 +55,19 @@
5656 if ( !( $title && $repo ) && !$path ) {
5757 throw new MWException( __METHOD__.': not enough parameters, must specify title and repo, or a full path' );
5858 }
59 - if ( $title ) {
60 - $this->title = $title;
 59+ if ( $title instanceof Title ) {
 60+ $this->title = File::normalizeTitle( $title, 'exception' );
6161 $this->name = $repo->getNameFromTitle( $title );
6262 } else {
6363 $this->name = basename( $path );
64 - $this->title = Title::makeTitleSafe( NS_FILE, $this->name );
 64+ $this->title = File::normalizeTitle( $this->name, 'exception' );
6565 }
6666 $this->repo = $repo;
6767 if ( $path ) {
6868 $this->path = $path;
6969 } else {
70 - $this->path = $repo->getRootDirectory() . '/' . $repo->getHashPath( $this->name ) . $this->name;
 70+ $this->path = $repo->getRootDirectory() . '/' .
 71+ $repo->getHashPath( $this->name ) . $this->name;
7172 }
7273 if ( $mime ) {
7374 $this->mime = $mime;
@@ -122,7 +123,8 @@
123124
124125 function getURL() {
125126 if ( $this->repo ) {
126 - return $this->repo->getZoneUrl( 'public' ) . '/' . $this->repo->getHashPath( $this->name ) . rawurlencode( $this->name );
 127+ return $this->repo->getZoneUrl( 'public' ) . '/' .
 128+ $this->repo->getHashPath( $this->name ) . rawurlencode( $this->name );
127129 } else {
128130 return false;
129131 }
Index: trunk/phase3/includes/filerepo/File.php
@@ -61,12 +61,12 @@
6262 */
6363
6464 /**
65 - * @var LocalRepo
 65+ * @var FileRepo
6666 */
6767 var $repo;
6868
6969 /**
70 - * @var Title
 70+ * @var Title|false
7171 */
7272 var $title;
7373
@@ -87,14 +87,44 @@
8888 /**
8989 * Call this constructor from child classes
9090 *
91 - * @param $title
92 - * @param $repo
 91+ * @param $title Title|false
 92+ * @param $repo FileRepo|false
9393 */
9494 function __construct( $title, $repo ) {
 95+ if ( $title !== false ) { // account for UnregisteredLocalFile et all
 96+ $title = self::normalizeTitle( $title, 'exception' );
 97+ }
9598 $this->title = $title;
9699 $this->repo = $repo;
97100 }
98101
 102+ /**
 103+ * Given a string or Title object return either a
 104+ * valid Title object with namespace NS_FILE or null
 105+ * @param $title Title|string
 106+ * @param $exception string|false Use 'exception' to throw an error on bad titles
 107+ * @return Title|null
 108+ */
 109+ static function normalizeTitle( $title, $exception = false ) {
 110+ $ret = $title;
 111+ if ( $ret instanceof Title ) {
 112+ # Normalize NS_MEDIA -> NS_FILE
 113+ if ( $ret->getNamespace() == NS_MEDIA ) {
 114+ $ret = Title::makeTitleSafe( NS_FILE, $ret->getDBkey() );
 115+ # Sanity check the title namespace
 116+ } elseif ( $ret->getNamespace() !== NS_FILE ) {
 117+ $ret = null;
 118+ }
 119+ } else {
 120+ # Convert strings to Title objects
 121+ $ret = Title::makeTitleSafe( NS_FILE, (string)$ret );
 122+ }
 123+ if ( !$ret && $exception !== false ) {
 124+ throw new MWException( "`$title` is not a valid file title." );
 125+ }
 126+ return $ret;
 127+ }
 128+
99129 function __get( $name ) {
100130 $function = array( $this, 'get' . ucfirst( $name ) );
101131 if ( !is_callable( $function ) ) {
Index: trunk/phase3/includes/filerepo/LocalRepo.php
@@ -137,15 +137,10 @@
138138 *
139139 * @param $title Title of file
140140 */
141 - function checkRedirect( $title ) {
 141+ function checkRedirect( Title $title ) {
142142 global $wgMemc;
143143
144 - if( is_string( $title ) ) {
145 - $title = Title::newFromText( $title );
146 - }
147 - if( $title instanceof Title && $title->getNamespace() == NS_MEDIA ) {
148 - $title = Title::makeTitle( NS_FILE, $title->getText() );
149 - }
 144+ $title = File::normalizeTitle( $title, 'exception' );
150145
151146 $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
152147 if ( $memcKey === false ) {
Index: trunk/phase3/includes/filerepo/RepoGroup.php
@@ -108,22 +108,15 @@
109109 if ( !$this->reposInitialised ) {
110110 $this->initialiseRepos();
111111 }
112 - if ( !($title instanceof Title) ) {
113 - $title = Title::makeTitleSafe( NS_FILE, $title );
114 - if ( !is_object( $title ) ) {
115 - return false;
116 - }
 112+ $title = File::normalizeTitle( $title );
 113+ if ( !$title ) {
 114+ return false;
117115 }
118116
119 - if ( $title->getNamespace() != NS_MEDIA && $title->getNamespace() != NS_FILE ) {
120 - throw new MWException( __METHOD__ . ' received an Title object with incorrect namespace' );
121 - }
122 -
123117 # Check the cache
124118 if ( empty( $options['ignoreRedirect'] )
125119 && empty( $options['private'] )
126 - && empty( $options['bypassCache'] )
127 - && $title->getNamespace() == NS_FILE )
 120+ && empty( $options['bypassCache'] ) )
128121 {
129122 $useCache = true;
130123 $time = isset( $options['time'] ) ? $options['time'] : '';
@@ -201,7 +194,7 @@
202195 /**
203196 * Interface for FileRepo::checkRedirect()
204197 */
205 - function checkRedirect( $title ) {
 198+ function checkRedirect( Title $title ) {
206199 if ( !$this->reposInitialised ) {
207200 $this->initialiseRepos();
208201 }
Index: trunk/phase3/includes/filerepo/ArchivedFile.php
@@ -73,8 +73,8 @@
7474 $this->dataLoaded = false;
7575 $this->exists = false;
7676
77 - if( is_object( $title ) ) {
78 - $this->title = $title;
 77+ if( $title instanceof Title ) {
 78+ $this->title = File::normalizeTitle( $title, 'exception' );
7979 $this->name = $title->getDBkey();
8080 }
8181
@@ -86,7 +86,7 @@
8787 $this->key = $key;
8888 }
8989
90 - if ( !$id && !$key && !is_object( $title ) ) {
 90+ if ( !$id && !$key && !( $title instanceof Title ) ) {
9191 throw new MWException( "No specifications provided to ArchivedFile constructor." );
9292 }
9393 }

Sign-offs

UserFlagDate
😂inspected21:09, 24 January 2012

Follow-up revisions

RevisionCommit summaryAuthorDate
r102331FU r102073:...aaron21:54, 7 November 2011
r102818FU r102073:...aaron22:14, 11 November 2011

Comments

#Comment by Nikerabbit (talk | contribs)   14:51, 5 November 2011

!$title instanceof Title works too, but apparently is non-obvious.

#Comment by Aaron Schulz (talk | contribs)   02:11, 8 November 2011

It looks unholy.

Status & tagging log