Page MenuHomePhabricator

[Task] Allow formatting references by a callback in Wikibase's infobox functionality
Closed, InvalidPublic1 Story Points

Description

A callback should be an optional setting when creating the capiunto infobox called referenceRenderer which is called to render references in the addStatement method. If no callback is provided, all snaks of a reference will be rendered in the order they are stored in the statement.

The callback will be defined in its own module on client wikis like Wikipedia because references might be rendered differently in different languages. They can create a module like Module:Reference which contains a method that can render the list of snaks a reference provides according to the local standards. This method should be passed to capiunto when initializing the module calling capiunto.create.

Code example

Module:Infobox
local infobox = require 'capiunto-wikibase'
local reference = require 'Module:Reference'

local p = {}

function p.run()
	return infobox.create( {
		title = 'Title of the Infobox',
		referenceRenderer = reference.render
	} )
	:addStatement( 'Some wikidata property with references' )
end

return p

Details

Related Gerrit Patches:
mediawiki/extensions/Wikibase : masterSupport an optional callback to render references

Event Timeline

Bene created this task.May 30 2015, 4:00 PM
Bene raised the priority of this task from to Needs Triage.
Bene updated the task description. (Show Details)
Bene set Security to None.
Bene added subscribers: Lydia_Pintscher, hoo, Bene, Aklapper.
Bene removed subscribers: hoo, Lydia_Pintscher.
Bene added a comment.May 30 2015, 4:03 PM

P707 also includes this feature already.

1local capiunto = require 'capiunto'
2local util = require 'libraryUtil'
3
4local referenceRenderer
5
6function addStatement( t, property )
7 local value = ''
8 local frame = mw.getCurrentFrame()
9
10 if frame.args[property] then
11 value = frame.args[property]
12 else
13 local entity = mw.wikibase.getEntity()
14 local propertyId = mw.wikibase.resolvePropertyId( property )
15
16 if propertyId ~= nil then
17 value = renderStatements( entity:getStatements( propertyId ) )
18 end
19 end
20
21 if value ~= '' then
22 t:addRow( property, value )
23 end
24
25 return t
26end
27
28function renderStatements( statements )
29 local value = ''
30 local frame = mw.getCurrentFrame()
31
32 for k, statement in pairs( statements ) do
33 if value ~= '' then
34 value = value .. tostring( mw.message.new( 'comma-separator' ) )
35 end
36
37 value = value .. mw.wikibase.renderSnak( statement.mainsnak )
38
39 if statement.references then
40 value = value .. renderReferences( statement.references )
41 end
42 end
43
44 return value
45end
46
47function renderReferences( references )
48 local value = ''
49 local frame = mw.getCurrentFrame()
50
51 for k, reference in pairs( references ) do
52 value = value .. frame:preprocess(
53 '<ref name="' .. reference.hash .. '">' .. referenceRenderer( reference.snaks ) .. '</ref>'
54 )
55 end
56
57 return value
58end
59
60function defaultReferenceRenderer( snaks )
61 return mw.wikibase.renderSnaks( snaks )
62end
63
64local create = capiunto.create
65
66capiunto.create = function( options )
67 local infobox = create( options )
68 infobox.addStatement = addStatement
69
70 referenceRenderer = options.referenceRenderer or defaultReferenceRenderer
71 util.checkType( 'referenceRenderer', 1, referenceRenderer, 'function' )
72
73 return infobox
74end
75
76return capiunto

Bene added a subscriber: hoo.May 30 2015, 5:56 PM
hoo added a comment.May 30 2015, 6:09 PM

I'm not sure about the scope of this bug, it mixes many things... First of all, this has hardly anything to do with Capiunto as this only concerns Wikibase's Capiunto decorator.

Also I think we should first of all think about reference formatting and how to make it convenient some more (doubt we have a bug for that), before taking a stab at this.

hoo renamed this task from An optional callback should be passed to capiunto to render references to Allow formatting references by a callback in Wikibase's infobox functionality.May 30 2015, 6:10 PM
Bene claimed this task.Jun 12 2015, 10:51 AM
Lydia_Pintscher triaged this task as Normal priority.Jun 12 2015, 1:14 PM

Change 217898 had a related patch set uploaded (by Bene):
Support an optional callback to render references

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

Bene edited a custom field.Jun 16 2015, 7:42 PM
Jonas renamed this task from Allow formatting references by a callback in Wikibase's infobox functionality to [Taks] Allow formatting references by a callback in Wikibase's infobox functionality.Aug 13 2015, 3:29 PM
Lydia_Pintscher renamed this task from [Taks] Allow formatting references by a callback in Wikibase's infobox functionality to [Task] Allow formatting references by a callback in Wikibase's infobox functionality.Aug 13 2015, 8:03 PM

Change 217898 abandoned by Addshore:
Support an optional callback to render references

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

@hoo, I feel this ticket and the patch https://gerrit.wikimedia.org/r/217898 that got abandoned might be relevant for what you are currently working on for T182147: more convenience functions for Lua and https://phabricator.wikimedia.org/tag/wikibase-lua/ in general. I'm not referring to the "render references" part – I agree this is not needed – but to the idea of supporting custom render callbacks in the Lua library. This is a concept @Tpt's well-crafted Lua modules also follow.