Hoo man has uploaded a new change for review.

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

Change subject: Lua: Cache more stuff in memory
......................................................................

Lua: Cache more stuff in memory

Cache wikibase.entity objects and the entity id belonging to
the current page in memory, so that we don't have to get them
from PHP this often.

Change-Id: I22d4f9d2fd1f01a9bfffde77d6c9c75842e28d9e
---
M client/includes/scribunto/mw.wikibase.lua
1 file changed, 35 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/35/144935/1

diff --git a/client/includes/scribunto/mw.wikibase.lua 
b/client/includes/scribunto/mw.wikibase.lua
index d72c1e9..1d73665 100644
--- a/client/includes/scribunto/mw.wikibase.lua
+++ b/client/includes/scribunto/mw.wikibase.lua
@@ -15,21 +15,43 @@
        local php = mw_interface
        mw_interface = nil
 
-       -- Caching variable for the wikibase.entity object belonging to the 
current page
-       local entity = false
+       -- Caching variable for the wikibase.entity objects
+       local entities = {}
+       -- Caching variable for the entity id string belonging to the current 
page (nil if page is not linked to an entity)
+       local pageEntityId = false
 
+       -- Get the mw.wikibase.entity object for a given id. Cached.
        local getEntityObject = function( id )
-               local entity = php.getEntity( id, false )
-               if type( entity ) ~= 'table' then
-                       return nil
+               if entities[ id ] == nil then
+                       local entity = php.getEntity( id, false )
+
+                       if type( entity ) ~= 'table' then
+                               entities[ id ] = false
+                               return nil
+                       end
+
+                       entities[ id ] = wikibase.entity.create( entity )
                end
 
-               return wikibase.entity.create( entity )
+               if type( entities[ id ] ) == 'table' then
+                       return entities[ id ]
+               else
+                       return nil
+               end
+       end
+
+       -- Get the entity id for the current page. Cached
+       local getEntityIdForCurrentPage = function()
+               if pageEntityId == false then
+                       pageEntityId = php.getEntityId( tostring( 
mw.title.getCurrentTitle().prefixedText ) )
+               end
+
+               return pageEntityId
        end
 
        -- @DEPRECATED, uses a legacy plain Lua table holding the entity
        wikibase.getEntity = function()
-               local id = php.getEntityId( tostring( 
mw.title.getCurrentTitle().prefixedText ) )
+               local id = getEntityIdForCurrentPage()
 
                if id == nil then
                        return nil
@@ -39,12 +61,14 @@
        end
 
        -- Get the mw.wikibase.entity object for the current page
-       wikibase.getEntityObject = function()
-               if entity ~= false then
-                       return entity
+       wikibase.getEntityObject = function( id )
+               if id ~= nil and type( id ) ~= 'string' then
+                       error( 'id must be either a string or nil, ' .. type( 
id ) .. ' given' )
                end
 
-               local id = php.getEntityId( tostring( 
mw.title.getCurrentTitle().prefixedText ) )
+               if id == nil then
+                       id = getEntityIdForCurrentPage()
+               end
 
                if id == nil then
                        entity = nil

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I22d4f9d2fd1f01a9bfffde77d6c9c75842e28d9e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Hoo man <[email protected]>

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

Reply via email to