Merge "(bug 45774) Add title.fileExists, improve title.exists"

This commit is contained in:
jenkins-bot 2013-03-08 23:38:31 +00:00 committed by Gerrit Code Review
commit df568c6a32
2 changed files with 64 additions and 4 deletions

View file

@ -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() );
}
}

View file

@ -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 ()