On 1/19/15, Jeroen De Dauw jeroendedauw@gmail.com wrote:
Hey,
On my local wiki I have a page with the name "File:Blue marker.png". The following code returns false:
$title = Title::makeTitle( NS_FILE, $file ); $title->exists();
That used to return true in the past. Not sure what is broken - my wiki or MediaWiki itself.
You're sure about that? I think you're just mixing up $title->exists() and $title->isKnown().
$title = Title::makeTitle( NS_FILE, $filename ); $file = wfLocalFile( $title ); $file->getUrl();
That's generally right, but depending on the exact context, the following might be more appropriate:
$title = Title::makeTitle( NS_FILE, $filename ); $file = wfFindFile( $title ); $file->getFullUrl(); // or $file->getCanonicalUrl(); if you need the protocol.
wfLocalFile will always return a file object even if there is no file by that name (And it will take its best guess at the url even for non-existent files). If the name given would normally be a foreign file, wfLocalFile( 'Foo.jpg' )->getUrl() would return what the url would be if there was a local file of that name. On the other hand, wfFindFile will return the appropriate (possibly foreign) file for that name or false if none exists.
In almost all cases you would probably want the wfFindFile behavior and not wfLocalFile().
--bawolff