Index: trunk/phase3/includes/filerepo/FileRepo.php |
— | — | @@ -644,7 +644,7 @@ |
645 | 645 | * @param $title Title of image |
646 | 646 | * @return Bool |
647 | 647 | */ |
648 | | - function checkRedirect( $title ) { |
| 648 | + function checkRedirect( Title $title ) { |
649 | 649 | return false; |
650 | 650 | } |
651 | 651 | |
Index: trunk/phase3/includes/filerepo/UnregisteredLocalFile.php |
— | — | @@ -46,7 +46,7 @@ |
47 | 47 | |
48 | 48 | /** |
49 | 49 | * @throws MWException |
50 | | - * @param $title string |
| 50 | + * @param $title Title|false |
51 | 51 | * @param $repo FSRepo |
52 | 52 | * @param $path string |
53 | 53 | * @param $mime string |
— | — | @@ -55,18 +55,19 @@ |
56 | 56 | if ( !( $title && $repo ) && !$path ) { |
57 | 57 | throw new MWException( __METHOD__.': not enough parameters, must specify title and repo, or a full path' ); |
58 | 58 | } |
59 | | - if ( $title ) { |
60 | | - $this->title = $title; |
| 59 | + if ( $title instanceof Title ) { |
| 60 | + $this->title = File::normalizeTitle( $title, 'exception' ); |
61 | 61 | $this->name = $repo->getNameFromTitle( $title ); |
62 | 62 | } else { |
63 | 63 | $this->name = basename( $path ); |
64 | | - $this->title = Title::makeTitleSafe( NS_FILE, $this->name ); |
| 64 | + $this->title = File::normalizeTitle( $this->name, 'exception' ); |
65 | 65 | } |
66 | 66 | $this->repo = $repo; |
67 | 67 | if ( $path ) { |
68 | 68 | $this->path = $path; |
69 | 69 | } else { |
70 | | - $this->path = $repo->getRootDirectory() . '/' . $repo->getHashPath( $this->name ) . $this->name; |
| 70 | + $this->path = $repo->getRootDirectory() . '/' . |
| 71 | + $repo->getHashPath( $this->name ) . $this->name; |
71 | 72 | } |
72 | 73 | if ( $mime ) { |
73 | 74 | $this->mime = $mime; |
— | — | @@ -122,7 +123,8 @@ |
123 | 124 | |
124 | 125 | function getURL() { |
125 | 126 | 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 ); |
127 | 129 | } else { |
128 | 130 | return false; |
129 | 131 | } |
Index: trunk/phase3/includes/filerepo/File.php |
— | — | @@ -61,12 +61,12 @@ |
62 | 62 | */ |
63 | 63 | |
64 | 64 | /** |
65 | | - * @var LocalRepo |
| 65 | + * @var FileRepo |
66 | 66 | */ |
67 | 67 | var $repo; |
68 | 68 | |
69 | 69 | /** |
70 | | - * @var Title |
| 70 | + * @var Title|false |
71 | 71 | */ |
72 | 72 | var $title; |
73 | 73 | |
— | — | @@ -87,14 +87,44 @@ |
88 | 88 | /** |
89 | 89 | * Call this constructor from child classes |
90 | 90 | * |
91 | | - * @param $title |
92 | | - * @param $repo |
| 91 | + * @param $title Title|false |
| 92 | + * @param $repo FileRepo|false |
93 | 93 | */ |
94 | 94 | function __construct( $title, $repo ) { |
| 95 | + if ( $title !== false ) { // account for UnregisteredLocalFile et all |
| 96 | + $title = self::normalizeTitle( $title, 'exception' ); |
| 97 | + } |
95 | 98 | $this->title = $title; |
96 | 99 | $this->repo = $repo; |
97 | 100 | } |
98 | 101 | |
| 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 | + |
99 | 129 | function __get( $name ) { |
100 | 130 | $function = array( $this, 'get' . ucfirst( $name ) ); |
101 | 131 | if ( !is_callable( $function ) ) { |
Index: trunk/phase3/includes/filerepo/LocalRepo.php |
— | — | @@ -137,15 +137,10 @@ |
138 | 138 | * |
139 | 139 | * @param $title Title of file |
140 | 140 | */ |
141 | | - function checkRedirect( $title ) { |
| 141 | + function checkRedirect( Title $title ) { |
142 | 142 | global $wgMemc; |
143 | 143 | |
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' ); |
150 | 145 | |
151 | 146 | $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) ); |
152 | 147 | if ( $memcKey === false ) { |
Index: trunk/phase3/includes/filerepo/RepoGroup.php |
— | — | @@ -108,22 +108,15 @@ |
109 | 109 | if ( !$this->reposInitialised ) { |
110 | 110 | $this->initialiseRepos(); |
111 | 111 | } |
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; |
117 | 115 | } |
118 | 116 | |
119 | | - if ( $title->getNamespace() != NS_MEDIA && $title->getNamespace() != NS_FILE ) { |
120 | | - throw new MWException( __METHOD__ . ' received an Title object with incorrect namespace' ); |
121 | | - } |
122 | | - |
123 | 117 | # Check the cache |
124 | 118 | if ( empty( $options['ignoreRedirect'] ) |
125 | 119 | && empty( $options['private'] ) |
126 | | - && empty( $options['bypassCache'] ) |
127 | | - && $title->getNamespace() == NS_FILE ) |
| 120 | + && empty( $options['bypassCache'] ) ) |
128 | 121 | { |
129 | 122 | $useCache = true; |
130 | 123 | $time = isset( $options['time'] ) ? $options['time'] : ''; |
— | — | @@ -201,7 +194,7 @@ |
202 | 195 | /** |
203 | 196 | * Interface for FileRepo::checkRedirect() |
204 | 197 | */ |
205 | | - function checkRedirect( $title ) { |
| 198 | + function checkRedirect( Title $title ) { |
206 | 199 | if ( !$this->reposInitialised ) { |
207 | 200 | $this->initialiseRepos(); |
208 | 201 | } |
Index: trunk/phase3/includes/filerepo/ArchivedFile.php |
— | — | @@ -73,8 +73,8 @@ |
74 | 74 | $this->dataLoaded = false; |
75 | 75 | $this->exists = false; |
76 | 76 | |
77 | | - if( is_object( $title ) ) { |
78 | | - $this->title = $title; |
| 77 | + if( $title instanceof Title ) { |
| 78 | + $this->title = File::normalizeTitle( $title, 'exception' ); |
79 | 79 | $this->name = $title->getDBkey(); |
80 | 80 | } |
81 | 81 | |
— | — | @@ -86,7 +86,7 @@ |
87 | 87 | $this->key = $key; |
88 | 88 | } |
89 | 89 | |
90 | | - if ( !$id && !$key && !is_object( $title ) ) { |
| 90 | + if ( !$id && !$key && !( $title instanceof Title ) ) { |
91 | 91 | throw new MWException( "No specifications provided to ArchivedFile constructor." ); |
92 | 92 | } |
93 | 93 | } |