| 1 |
local message = {} |
| 2 |
local php |
| 3 |
|
| 4 |
local util = require 'libraryUtil' |
| 5 |
local checkType = util.checkType |
| 6 |
|
| 7 |
local valuemt = { |
| 8 |
__tostring = function ( t ) |
| 9 |
return tostring( t.raw or t.num ) |
| 10 |
end |
| 11 |
} |
| 12 |
|
| 13 |
local function checkScalar( name, argIdx, arg, level, valuemtOk ) |
| 14 |
local tp = type( arg ) |
| 15 |
|
| 16 |
|
| 17 |
if valuemtOk and tp == 'table' and getmetatable( arg ) == valuemt then |
| 18 |
return arg |
| 19 |
end |
| 20 |
|
| 21 |
|
| 22 |
if tp == 'table' and getmetatable( arg ) and getmetatable( arg ).__tostring then |
| 23 |
return tostring( arg ) |
| 24 |
end |
| 25 |
|
| 26 |
if tp ~= 'string' and tp ~= 'number' then |
| 27 |
error( string.format( |
| 28 |
"bad argument #%d to '%s' (string or number expected, got %s)", |
| 29 |
argIdx, name, tp |
| 30 |
), level + 1 ) |
| 31 |
end |
| 32 |
|
| 33 |
return arg |
| 34 |
end |
| 35 |
|
| 36 |
local function checkParams( name, valueOk, ... ) |
| 37 |
|
| 38 |
local params, nparams |
| 39 |
local first = select( 1, ... ) |
| 40 |
if type( first ) == 'table' and |
| 41 |
not ( getmetatable( first ) and getmetatable( first ).__tostring ) |
| 42 |
then |
| 43 |
if select( '#', ... ) == 1 then |
| 44 |
params = first |
| 45 |
nparams = table.maxn( params ) |
| 46 |
else |
| 47 |
error( |
| 48 |
"bad arguments to '" .. name .. "' (pass either a table of params or params as individual arguments)", |
| 49 |
3 |
| 50 |
) |
| 51 |
end |
| 52 |
else |
| 53 |
params = { ... } |
| 54 |
nparams = select( '#', ... ) |
| 55 |
end |
| 56 |
for i = 1, nparams do |
| 57 |
params[i] = checkScalar( 'params', i, params[i], 3, valueOk ) |
| 58 |
end |
| 59 |
return params |
| 60 |
end |
| 61 |
|
| 62 |
function message.setupInterface( options ) |
| 63 |
|
| 64 |
message.setupInterface = nil |
| 65 |
php = mw_interface |
| 66 |
mw_interface = nil |
| 67 |
php.options = options |
| 68 |
|
| 69 |
|
| 70 |
mw = mw or {} |
| 71 |
mw.message = message |
| 72 |
|
| 73 |
package.loaded['mw.message'] = message |
| 74 |
end |
| 75 |
|
| 76 |
local function makeMessage( options ) |
| 77 |
local obj = {} |
| 78 |
local checkSelf = util.makeCheckSelfFunction( 'mw.message', 'msg', obj, 'message object' ) |
| 79 |
|
| 80 |
local data = { |
| 81 |
keys = options.keys, |
| 82 |
rawMessage = options.rawMessage, |
| 83 |
params = {}, |
| 84 |
lang = php.options.lang, |
| 85 |
useDB = true, |
| 86 |
} |
| 87 |
|
| 88 |
function obj:params( ... ) |
| 89 |
checkSelf( self, 'params' ) |
| 90 |
local params = checkParams( 'params', true, ... ) |
| 91 |
local j = #data.params |
| 92 |
for i = 1, #params do |
| 93 |
data.params[j + i] = params[i] |
| 94 |
end |
| 95 |
return self |
| 96 |
end |
| 97 |
|
| 98 |
function obj:rawParams( ... ) |
| 99 |
checkSelf( self, 'rawParams' ) |
| 100 |
local params = checkParams( 'rawParams', false, ... ) |
| 101 |
local j = #data.params |
| 102 |
for i = 1, #params do |
| 103 |
data.params[j + i] = setmetatable( { raw = params[i] }, valuemt ) |
| 104 |
end |
| 105 |
return self |
| 106 |
end |
| 107 |
|
| 108 |
function obj:numParams( ... ) |
| 109 |
checkSelf( self, 'numParams' ) |
| 110 |
local params = checkParams( 'numParams', false, ... ) |
| 111 |
local j = #data.params |
| 112 |
for i = 1, #params do |
| 113 |
data.params[j + i] = setmetatable( { num = params[i] }, valuemt ) |
| 114 |
end |
| 115 |
return self |
| 116 |
end |
| 117 |
|
| 118 |
function obj:inLanguage( lang ) |
| 119 |
checkSelf( self, 'inLanguage' ) |
| 120 |
if type( lang ) == 'table' and lang.getCode then |
| 121 |
|
| 122 |
lang = lang:getCode() |
| 123 |
end |
| 124 |
checkType( 'inLanguage', 1, lang, 'string' ) |
| 125 |
data.lang = lang |
| 126 |
return self |
| 127 |
end |
| 128 |
|
| 129 |
function obj:useDatabase( value ) |
| 130 |
checkSelf( self, 'useDatabase' ) |
| 131 |
checkType( 'useDatabase', 1, value, 'boolean' ) |
| 132 |
data.useDB = value |
| 133 |
return self |
| 134 |
end |
| 135 |
|
| 136 |
function obj:plain() |
| 137 |
checkSelf( self, 'plain' ) |
| 138 |
return php.plain( data ) |
| 139 |
end |
| 140 |
|
| 141 |
function obj:exists() |
| 142 |
checkSelf( self, 'exists' ) |
| 143 |
return php.check( 'exists', data ) |
| 144 |
end |
| 145 |
|
| 146 |
function obj:isBlank() |
| 147 |
checkSelf( self, 'isBlank' ) |
| 148 |
return php.check( 'isBlank', data ) |
| 149 |
end |
| 150 |
|
| 151 |
function obj:isDisabled() |
| 152 |
checkSelf( self, 'isDisabled' ) |
| 153 |
return php.check( 'isDisabled', data ) |
| 154 |
end |
| 155 |
|
| 156 |
return setmetatable( obj, { |
| 157 |
__tostring = function ( t ) |
| 158 |
return t:plain() |
| 159 |
end |
| 160 |
} ) |
| 161 |
end |
| 162 |
|
| 163 |
function message.new( key, ... ) |
| 164 |
checkType( 'message.new', 1, key, 'string' ) |
| 165 |
return makeMessage{ keys = { key } }:params( ... ) |
| 166 |
end |
| 167 |
|
| 168 |
function message.newFallbackSequence( ... ) |
| 169 |
for i = 1, math.max( 1, select( '#', ... ) ) do |
| 170 |
checkType( 'message.newFallbackSequence', i, select( i, ... ), 'string' ) |
| 171 |
end |
| 172 |
return makeMessage{ keys = { ... } } |
| 173 |
end |
| 174 |
|
| 175 |
function message.newRawMessage( msg, ... ) |
| 176 |
checkType( 'message.newRawMessage', 1, msg, 'string' ) |
| 177 |
return makeMessage{ rawMessage = msg }:params( ... ) |
| 178 |
end |
| 179 |
|
| 180 |
function message.rawParam( value ) |
| 181 |
value = checkScalar( 'message.rawParam', 1, value ) |
| 182 |
return setmetatable( { raw = value }, valuemt ) |
| 183 |
end |
| 184 |
|
| 185 |
function message.numParam( value ) |
| 186 |
value = checkScalar( 'message.numParam', 1, value ) |
| 187 |
return setmetatable( { num = value }, valuemt ) |
| 188 |
end |
| 189 |
|
| 190 |
function message.getDefaultLanguage() |
| 191 |
if mw.language then |
| 192 |
return mw.language.new( php.options.lang ) |
| 193 |
else |
| 194 |
return php.options.lang |
| 195 |
end |
| 196 |
end |
| 197 |
|
| 198 |
return message |