Anomie has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/52697


Change subject: Add title.fileExists
......................................................................

Add title.fileExists

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.

Change-Id: I019adc89858a1d32995a38d5e8eef32577fd32d6
---
M engines/LuaCommon/TitleLibrary.php
M engines/LuaCommon/lualib/mw.title.lua
2 files changed, 45 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Scribunto 
refs/changes/97/52697/1

diff --git a/engines/LuaCommon/TitleLibrary.php 
b/engines/LuaCommon/TitleLibrary.php
index c48390f..a820048 100644
--- a/engines/LuaCommon/TitleLibrary.php
+++ b/engines/LuaCommon/TitleLibrary.php
@@ -14,9 +14,11 @@
                        '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 @@
                        $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,10 @@
                        'contentModel' => $title->getContentModel(),
                        'thePartialUrl' => $title->getPartialURL(),
                );
+               if ( $ns !== NS_FILE && $ns !== NS_MEDIA ) {
+                       $ret['fileExists'] = false;
+               }
+               return $ret;
        }
 
        /**
@@ -198,6 +205,7 @@
        }
 
        function getContent( $text ) {
+               $this->checkType( 'getContent', 1, $text, 'string' );
                $title = Title::newFromText( $text );
                if ( !$title ) {
                        return array( null );
@@ -218,4 +226,26 @@
                }
                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() );
+       }
 }
diff --git a/engines/LuaCommon/lualib/mw.title.lua 
b/engines/LuaCommon/lualib/mw.title.lua
index afbbdd1..37d2b2e 100644
--- a/engines/LuaCommon/lualib/mw.title.lua
+++ b/engines/LuaCommon/lualib/mw.title.lua
@@ -1,5 +1,6 @@
 local title = {}
 local php
+local NS_MEDIA = -2
 
 local util = require 'libraryUtil'
 local checkType = util.checkType
@@ -158,6 +159,10 @@
                __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 +194,12 @@
                                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 +207,7 @@
                        if k == 'fragment' then
                                checkTypeForIndex( k, v, 'string' )
                                data[k] = v
-                       elseif data[k] then
+                       elseif data[k] ~= nil or k == 'fileExists' then
                                error( "index '" .. k .. "' is read only", 2 )
                        else
                                rawset( t, k, v )
@@ -213,6 +224,7 @@
        title.setupInterface = nil
        php = mw_interface
        mw_interface = nil
+       NS_MEDIA = options.NS_MEDIA
 
        -- Set current title
        title.getCurrentTitle = function ()

-- 
To view, visit https://gerrit.wikimedia.org/r/52697
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I019adc89858a1d32995a38d5e8eef32577fd32d6
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Scribunto
Gerrit-Branch: master
Gerrit-Owner: Anomie <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to