mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Scribunto
synced 2024-11-24 00:05:00 +00:00
(bug 45774) Add title.fileExists, improve title.exists
People sometimes want to know if the actual file exists, not just the file description page. Support that. Also alias .exists to .fileExists for the Media namespace, since that's what the Media namespace is for. And while we're at it, fix .exists for special pages too. Bug: 45774 Change-Id: I019adc89858a1d32995a38d5e8eef32577fd32d6
This commit is contained in:
parent
ec86b161cd
commit
466ba8668e
|
@ -14,9 +14,11 @@ class Scribunto_LuaTitleLibrary extends Scribunto_LuaLibraryBase {
|
|||
'makeTitle' => array( $this, 'makeTitle' ),
|
||||
'getUrl' => array( $this, 'getUrl' ),
|
||||
'getContent' => array( $this, 'getContent' ),
|
||||
'fileExists' => array( $this, 'fileExists' ),
|
||||
);
|
||||
$this->getEngine()->registerInterface( 'mw.title.lua', $lib, array(
|
||||
'thisTitle' => $this->returnTitleToLua( $this->getTitle() ),
|
||||
'NS_MEDIA' => NS_MEDIA,
|
||||
) );
|
||||
}
|
||||
|
||||
|
@ -69,11 +71,12 @@ class Scribunto_LuaTitleLibrary extends Scribunto_LuaLibraryBase {
|
|||
$this->getParser()->getOutput()->addLink( $title );
|
||||
}
|
||||
|
||||
return array(
|
||||
$ns = $title->getNamespace();
|
||||
$ret = array(
|
||||
'isLocal' => (bool)$title->isLocal(),
|
||||
'isRedirect' => (bool)$title->isRedirect(),
|
||||
'interwiki' => $title->getInterwiki(),
|
||||
'namespace' => $title->getNamespace(),
|
||||
'namespace' => $ns,
|
||||
'nsText' => $title->getNsText(),
|
||||
'text' => $title->getText(),
|
||||
'id' => $title->getArticleID(),
|
||||
|
@ -81,6 +84,15 @@ class Scribunto_LuaTitleLibrary extends Scribunto_LuaLibraryBase {
|
|||
'contentModel' => $title->getContentModel(),
|
||||
'thePartialUrl' => $title->getPartialURL(),
|
||||
);
|
||||
if ( $ns === NS_SPECIAL ) {
|
||||
$ret['exists'] = (bool)SpecialPageFactory::exists( $title->getDBkey() );
|
||||
} else {
|
||||
$ret['exists'] = $ret['id'] > 0;
|
||||
}
|
||||
if ( $ns !== NS_FILE && $ns !== NS_MEDIA ) {
|
||||
$ret['fileExists'] = false;
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -198,6 +210,7 @@ class Scribunto_LuaTitleLibrary extends Scribunto_LuaLibraryBase {
|
|||
}
|
||||
|
||||
function getContent( $text ) {
|
||||
$this->checkType( 'getContent', 1, $text, 'string' );
|
||||
$title = Title::newFromText( $text );
|
||||
if ( !$title ) {
|
||||
return array( null );
|
||||
|
@ -218,4 +231,26 @@ class Scribunto_LuaTitleLibrary extends Scribunto_LuaLibraryBase {
|
|||
}
|
||||
return array( $content->serialize() );
|
||||
}
|
||||
|
||||
function fileExists( $text ) {
|
||||
$this->checkType( 'fileExists', 1, $text, 'string' );
|
||||
$title = Title::newFromText( $text );
|
||||
if ( !$title ) {
|
||||
return array( false );
|
||||
}
|
||||
$ns = $title->getNamespace();
|
||||
if ( $ns !== NS_FILE && $ns !== NS_MEDIA ) {
|
||||
return array( false );
|
||||
}
|
||||
|
||||
$this->incrementExpensiveFunctionCount();
|
||||
$file = wfFindFile( $title );
|
||||
if ( !$file ) {
|
||||
return array( false );
|
||||
}
|
||||
$this->getParser()->getOutput()->addImage(
|
||||
$file->getName(), $file->getTimestamp(), $file->getSha1()
|
||||
);
|
||||
return array( (bool)$file->exists() );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
local title = {}
|
||||
local php
|
||||
local NS_MEDIA = -2
|
||||
|
||||
local util = require 'libraryUtil'
|
||||
local checkType = util.checkType
|
||||
|
@ -59,7 +60,6 @@ local function makeTitleObject( data )
|
|||
data.isExternal = data.interwiki ~= ''
|
||||
data.isSpecialPage = data.namespace == mw.site.namespaces.Special.id
|
||||
data.isTalkPage = ns.isTalk
|
||||
data.exists = data.id ~= 0
|
||||
data.subjectNsText = ns.subject.name
|
||||
data.canTalk = ns.talk ~= nil
|
||||
|
||||
|
@ -154,10 +154,28 @@ local function makeTitleObject( data )
|
|||
return content
|
||||
end
|
||||
|
||||
-- Read-only fields, both those defined above and any dynamically handled
|
||||
-- in __index.
|
||||
local readOnlyFields = {
|
||||
fullText = true,
|
||||
rootPageTitle = true,
|
||||
basePageTitle = true,
|
||||
talkPageTitle = true,
|
||||
subjectPageTitle = true,
|
||||
fileExists = true,
|
||||
}
|
||||
for k in pairs( data ) do
|
||||
readOnlyFields[k] = true
|
||||
end
|
||||
|
||||
return setmetatable( obj, {
|
||||
__eq = title.equals,
|
||||
__lt = lt,
|
||||
__index = function ( t, k )
|
||||
if k == 'exists' and data.namespace == NS_MEDIA then
|
||||
k = 'fileExists'
|
||||
end
|
||||
|
||||
if k == 'fullText' then
|
||||
if data.fragment ~= '' then
|
||||
return data.prefixedText .. '#' .. data.fragment
|
||||
|
@ -189,6 +207,12 @@ local function makeTitleObject( data )
|
|||
end
|
||||
return title.makeTitle( ns.id, data.text )
|
||||
end
|
||||
if k == 'fileExists' then
|
||||
if data.fileExists == nil then
|
||||
data.fileExists = php.fileExists( data.prefixedText )
|
||||
end
|
||||
return data.fileExists
|
||||
end
|
||||
|
||||
return data[k]
|
||||
end,
|
||||
|
@ -196,7 +220,7 @@ local function makeTitleObject( data )
|
|||
if k == 'fragment' then
|
||||
checkTypeForIndex( k, v, 'string' )
|
||||
data[k] = v
|
||||
elseif data[k] then
|
||||
elseif readOnlyFields[k] then
|
||||
error( "index '" .. k .. "' is read only", 2 )
|
||||
else
|
||||
rawset( t, k, v )
|
||||
|
@ -213,6 +237,7 @@ function title.setupInterface( options )
|
|||
title.setupInterface = nil
|
||||
php = mw_interface
|
||||
mw_interface = nil
|
||||
NS_MEDIA = options.NS_MEDIA
|
||||
|
||||
-- Set current title
|
||||
title.getCurrentTitle = function ()
|
||||
|
|
Loading…
Reference in a new issue