| 1 |
mw = mw or {} |
| 2 |
|
| 3 |
local packageCache |
| 4 |
local packageModuleFunc |
| 5 |
local php |
| 6 |
local allowEnvFuncs = false |
| 7 |
local logBuffer = '' |
| 8 |
local loadedData = {} |
| 9 |
local loadedJsonData = {} |
| 10 |
local executeFunctionDepth = 0 |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
local function makePackageModule( env ) |
| 19 |
|
| 20 |
if env.package then |
| 21 |
env.package.loaders = nil |
| 22 |
end |
| 23 |
|
| 24 |
|
| 25 |
setfenv( packageModuleFunc, env )() |
| 26 |
|
| 27 |
|
| 28 |
local function loadPackage( modName ) |
| 29 |
local init |
| 30 |
if packageCache[modName] == 'missing' then |
| 31 |
return nil |
| 32 |
elseif packageCache[modName] == nil then |
| 33 |
local lib = php.loadPHPLibrary( modName ) |
| 34 |
if lib ~= nil then |
| 35 |
init = function () |
| 36 |
return mw.clone( lib ) |
| 37 |
end |
| 38 |
else |
| 39 |
init = php.loadPackage( modName ) |
| 40 |
if init == nil then |
| 41 |
packageCache[modName] = 'missing' |
| 42 |
return nil |
| 43 |
end |
| 44 |
end |
| 45 |
packageCache[modName] = init |
| 46 |
else |
| 47 |
init = packageCache[modName] |
| 48 |
end |
| 49 |
|
| 50 |
setfenv( init, env ) |
| 51 |
return init |
| 52 |
end |
| 53 |
|
| 54 |
table.insert( env.package.loaders, loadPackage ) |
| 55 |
end |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
function mw.setupInterface( options ) |
| 60 |
|
| 61 |
mw.setupInterface = nil |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
local old_getmetatable = getmetatable |
| 66 |
function getmetatable(obj) |
| 67 |
if type(obj) == 'table' then |
| 68 |
return old_getmetatable(obj) |
| 69 |
else |
| 70 |
return nil |
| 71 |
end |
| 72 |
end |
| 73 |
|
| 74 |
if options.allowEnvFuncs then |
| 75 |
allowEnvFuncs = true |
| 76 |
end |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
php = mw_interface |
| 86 |
mw_interface = nil |
| 87 |
|
| 88 |
packageModuleFunc = php.loadPackage( 'package' ) |
| 89 |
makePackageModule( _G ) |
| 90 |
package.loaded.mw = mw |
| 91 |
packageCache = {} |
| 92 |
end |
| 93 |
|
| 94 |
|
| 95 |
local function wrapDateTable( now ) |
| 96 |
return setmetatable( {}, { |
| 97 |
__index = function( t, k ) |
| 98 |
if k == 'sec' then |
| 99 |
php.setTTL( 1 ) |
| 100 |
elseif k == 'min' then |
| 101 |
php.setTTL( 60 - now.sec ) |
| 102 |
elseif k == 'hour' then |
| 103 |
php.setTTL( 3600 - now.min * 60 - now.sec ) |
| 104 |
elseif now[k] ~= nil then |
| 105 |
php.setTTL( 86400 - now.hour * 3600 - now.min * 60 - now.sec ) |
| 106 |
end |
| 107 |
t[k] = now[k] |
| 108 |
return now[k] |
| 109 |
end |
| 110 |
} ) |
| 111 |
end |
| 112 |
|
| 113 |
|
| 114 |
local function ttlDate( format, time ) |
| 115 |
if time == nil and ( format == nil or type( format ) == 'string' ) then |
| 116 |
local now = os.date( format and format:sub( 1, 1 ) == '!' and '!*t' or '*t' ) |
| 117 |
if format == '!*t' or format == '*t' then |
| 118 |
return wrapDateTable( now ) |
| 119 |
end |
| 120 |
local cleanedFormat = format and format:gsub( '%%%%', '' ) |
| 121 |
if not format or cleanedFormat:find( '%%[EO]?[crsSTX+]' ) then |
| 122 |
php.setTTL( 1 ) |
| 123 |
elseif cleanedFormat:find( '%%[EO]?[MR]' ) then |
| 124 |
php.setTTL( 60 - now.sec ) |
| 125 |
elseif cleanedFormat:find( '%%[EO]?[HIkl]' ) then |
| 126 |
php.setTTL( 3600 - now.min * 60 - now.sec ) |
| 127 |
elseif cleanedFormat:find( '%%[EO]?[pP]' ) then |
| 128 |
php.setTTL( 43200 - ( now.hour % 12 ) * 3600 - now.min * 60 - now.sec ) |
| 129 |
else |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
php.setTTL( 86400 - now.hour * 3600 - now.min * 60 - now.sec ) |
| 134 |
end |
| 135 |
end |
| 136 |
return os.date( format, time ) |
| 137 |
end |
| 138 |
|
| 139 |
local function ttlTime( t ) |
| 140 |
if t == nil then |
| 141 |
php.setTTL( 1 ) |
| 142 |
end |
| 143 |
return os.time( t ) |
| 144 |
end |
| 145 |
|
| 146 |
local function frameExists( frameId ) |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
return frameId == 'empty' or frameId == 'current' or php.frameExists( frameId ) |
| 151 |
end |
| 152 |
|
| 153 |
local function newFrame( frameId, ... ) |
| 154 |
if not frameExists( frameId ) then |
| 155 |
return nil |
| 156 |
end |
| 157 |
|
| 158 |
local frame = {} |
| 159 |
local parentFrameIds = { ... } |
| 160 |
local argCache = {} |
| 161 |
local argNames |
| 162 |
local args_mt = {} |
| 163 |
|
| 164 |
local function checkSelf( self, method ) |
| 165 |
if self ~= frame then |
| 166 |
error( "frame:" .. method .. ": invalid frame object. " .. |
| 167 |
"Did you call " .. method .. " with a dot instead of a colon, i.e. " .. |
| 168 |
"frame." .. method .. "() instead of frame:" .. method .. "()?", |
| 169 |
3 ) |
| 170 |
end |
| 171 |
end |
| 172 |
|
| 173 |
|
| 174 |
local function getExpandedArgument( dummy, name ) |
| 175 |
name = tostring( name ) |
| 176 |
if argCache[name] == nil then |
| 177 |
local arg = php.getExpandedArgument( frameId, name ) |
| 178 |
if arg == nil then |
| 179 |
argCache[name] = false |
| 180 |
else |
| 181 |
argCache[name] = arg |
| 182 |
end |
| 183 |
end |
| 184 |
if argCache[name] == false then |
| 185 |
return nil |
| 186 |
else |
| 187 |
return argCache[name] |
| 188 |
end |
| 189 |
end |
| 190 |
|
| 191 |
args_mt.__index = getExpandedArgument |
| 192 |
|
| 193 |
|
| 194 |
args_mt.__pairs = function () |
| 195 |
if not argNames then |
| 196 |
local arguments = php.getAllExpandedArguments( frameId ) |
| 197 |
argNames = {} |
| 198 |
for name, value in pairs( arguments ) do |
| 199 |
table.insert( argNames, name ) |
| 200 |
argCache[name] = value |
| 201 |
end |
| 202 |
end |
| 203 |
|
| 204 |
local index = 0 |
| 205 |
return function () |
| 206 |
index = index + 1 |
| 207 |
if argNames[index] then |
| 208 |
return argNames[index], argCache[argNames[index]] |
| 209 |
end |
| 210 |
end |
| 211 |
end |
| 212 |
|
| 213 |
|
| 214 |
local function argsInext( dummy, i ) |
| 215 |
local value = getExpandedArgument( dummy, i + 1 ) |
| 216 |
if value then |
| 217 |
return i + 1, value |
| 218 |
end |
| 219 |
end |
| 220 |
|
| 221 |
args_mt.__ipairs = function () return argsInext, nil, 0 end |
| 222 |
|
| 223 |
frame.args = {} |
| 224 |
setmetatable( frame.args, args_mt ) |
| 225 |
|
| 226 |
local function newCallbackParserValue( callback ) |
| 227 |
local value = {} |
| 228 |
local cache |
| 229 |
|
| 230 |
function value:expand() |
| 231 |
if not cache then |
| 232 |
cache = callback() |
| 233 |
end |
| 234 |
return cache |
| 235 |
end |
| 236 |
|
| 237 |
return value |
| 238 |
end |
| 239 |
|
| 240 |
function frame:getArgument( opt ) |
| 241 |
checkSelf( self, 'getArgument' ) |
| 242 |
|
| 243 |
local name |
| 244 |
if type( opt ) == 'table' then |
| 245 |
name = opt.name |
| 246 |
else |
| 247 |
name = opt |
| 248 |
end |
| 249 |
|
| 250 |
return newCallbackParserValue( |
| 251 |
function () |
| 252 |
return getExpandedArgument( nil, name ) |
| 253 |
end |
| 254 |
) |
| 255 |
end |
| 256 |
|
| 257 |
function frame:getParent() |
| 258 |
checkSelf( self, 'getParent' ) |
| 259 |
|
| 260 |
return newFrame( unpack( parentFrameIds ) ) |
| 261 |
end |
| 262 |
|
| 263 |
local function checkArgs( name, args ) |
| 264 |
local ret = {} |
| 265 |
for k, v in pairs( args ) do |
| 266 |
local tp = type( k ) |
| 267 |
if tp ~= 'string' and tp ~= 'number' then |
| 268 |
error( name .. ": arg keys must be strings or numbers, " .. tp .. " given", 3 ) |
| 269 |
end |
| 270 |
tp = type( v ) |
| 271 |
if tp == 'boolean' then |
| 272 |
ret[k] = v and '1' or '' |
| 273 |
elseif tp == 'string' or tp == 'number' then |
| 274 |
ret[k] = tostring( v ) |
| 275 |
else |
| 276 |
error( name .. ": invalid type " .. tp .. " for arg '" .. k .. "'", 3 ) |
| 277 |
end |
| 278 |
end |
| 279 |
return ret |
| 280 |
end |
| 281 |
|
| 282 |
function frame:newChild( opt ) |
| 283 |
checkSelf( self, 'newChild' ) |
| 284 |
|
| 285 |
if type( opt ) ~= 'table' then |
| 286 |
error( "frame:newChild: the first parameter must be a table", 2 ) |
| 287 |
end |
| 288 |
|
| 289 |
local title, args |
| 290 |
if opt.title == nil then |
| 291 |
title = false |
| 292 |
else |
| 293 |
title = tostring( opt.title ) |
| 294 |
end |
| 295 |
if opt.args == nil then |
| 296 |
args = {} |
| 297 |
elseif type( opt.args ) ~= 'table' then |
| 298 |
error( "frame:newChild: args must be a table", 2 ) |
| 299 |
else |
| 300 |
args = checkArgs( 'frame:newChild', opt.args ) |
| 301 |
end |
| 302 |
|
| 303 |
local newFrameId = php.newChildFrame( frameId, title, args ) |
| 304 |
return newFrame( newFrameId, frameId, unpack( parentFrameIds ) ) |
| 305 |
end |
| 306 |
|
| 307 |
function frame:expandTemplate( opt ) |
| 308 |
checkSelf( self, 'expandTemplate' ) |
| 309 |
|
| 310 |
local title |
| 311 |
|
| 312 |
if type( opt ) ~= 'table' then |
| 313 |
error( "frame:expandTemplate: the first parameter must be a table" ) |
| 314 |
end |
| 315 |
if opt.title == nil then |
| 316 |
error( "frame:expandTemplate: a title is required" ) |
| 317 |
else |
| 318 |
if type( opt.title ) == 'table' and opt.title.namespace == 0 then |
| 319 |
title = ':' .. tostring( opt.title ) |
| 320 |
else |
| 321 |
title = tostring( opt.title ) |
| 322 |
end |
| 323 |
end |
| 324 |
local args |
| 325 |
if opt.args == nil then |
| 326 |
args = {} |
| 327 |
elseif type( opt.args ) ~= 'table' then |
| 328 |
error( "frame:expandTemplate: args must be a table" ) |
| 329 |
else |
| 330 |
args = checkArgs( 'frame:expandTemplate', opt.args ) |
| 331 |
end |
| 332 |
|
| 333 |
return php.expandTemplate( frameId, title, args ) |
| 334 |
end |
| 335 |
|
| 336 |
function frame:callParserFunction( name, args, ... ) |
| 337 |
checkSelf( self, 'callParserFunction' ) |
| 338 |
|
| 339 |
if type( name ) == 'table' then |
| 340 |
name, args = name.name, name.args |
| 341 |
if type( args ) ~= 'table' then |
| 342 |
args = { args } |
| 343 |
end |
| 344 |
elseif type( args ) ~= 'table' then |
| 345 |
args = { args, ... } |
| 346 |
end |
| 347 |
|
| 348 |
if name == nil then |
| 349 |
error( "frame:callParserFunction: a function name is required", 2 ) |
| 350 |
elseif type( name ) == 'string' or type( name ) == 'number' then |
| 351 |
name = tostring( name ) |
| 352 |
else |
| 353 |
error( "frame:callParserFunction: function name must be a string or number", 2 ) |
| 354 |
end |
| 355 |
|
| 356 |
args = checkArgs( 'frame:callParserFunction', args ) |
| 357 |
|
| 358 |
return php.callParserFunction( frameId, name, args ) |
| 359 |
end |
| 360 |
|
| 361 |
function frame:extensionTag( name, content, args ) |
| 362 |
checkSelf( self, 'extensionTag' ) |
| 363 |
|
| 364 |
if type( name ) == 'table' then |
| 365 |
name, content, args = name.name, name.content, name.args |
| 366 |
end |
| 367 |
|
| 368 |
if name == nil then |
| 369 |
error( "frame:extensionTag: a function name is required", 2 ) |
| 370 |
elseif type( name ) == 'string' or type( name ) == 'number' then |
| 371 |
name = tostring( name ) |
| 372 |
else |
| 373 |
error( "frame:extensionTag: tag name must be a string or number", 2 ) |
| 374 |
end |
| 375 |
|
| 376 |
if content == nil then |
| 377 |
content = '' |
| 378 |
elseif type( content ) == 'string' or type( content ) == 'number' then |
| 379 |
content = tostring( content ) |
| 380 |
else |
| 381 |
error( "frame:extensionTag: content must be a string or number", 2 ) |
| 382 |
end |
| 383 |
|
| 384 |
if args == nil then |
| 385 |
args = { content } |
| 386 |
elseif type( args ) == 'string' or type( args ) == 'number' then |
| 387 |
args = { content, args } |
| 388 |
elseif type( args ) == 'table' then |
| 389 |
args = checkArgs( 'frame:extensionTag', args ) |
| 390 |
table.insert( args, 1, content ) |
| 391 |
else |
| 392 |
error( "frame:extensionTag: args must be a string, number, or table", 2 ) |
| 393 |
end |
| 394 |
|
| 395 |
return php.callParserFunction( frameId, '#tag:' .. name, args ) |
| 396 |
end |
| 397 |
|
| 398 |
function frame:preprocess( opt ) |
| 399 |
checkSelf( self, 'preprocess' ) |
| 400 |
|
| 401 |
local text |
| 402 |
if type( opt ) == 'table' then |
| 403 |
text = opt.text |
| 404 |
else |
| 405 |
text = opt |
| 406 |
end |
| 407 |
text = tostring( text ) |
| 408 |
return php.preprocess( frameId, text ) |
| 409 |
end |
| 410 |
|
| 411 |
function frame:newParserValue( opt ) |
| 412 |
checkSelf( self, 'newParserValue' ) |
| 413 |
|
| 414 |
local text |
| 415 |
if type( opt ) == 'table' then |
| 416 |
text = opt.text |
| 417 |
else |
| 418 |
text = opt |
| 419 |
end |
| 420 |
|
| 421 |
return newCallbackParserValue( |
| 422 |
function () |
| 423 |
return self:preprocess( text ) |
| 424 |
end |
| 425 |
) |
| 426 |
end |
| 427 |
|
| 428 |
function frame:newTemplateParserValue( opt ) |
| 429 |
checkSelf( self, 'newTemplateParserValue' ) |
| 430 |
|
| 431 |
if type( opt ) ~= 'table' then |
| 432 |
error( "frame:newTemplateParserValue: the first parameter must be a table" ) |
| 433 |
end |
| 434 |
if opt.title == nil then |
| 435 |
error( "frame:newTemplateParserValue: a title is required" ) |
| 436 |
end |
| 437 |
return newCallbackParserValue( |
| 438 |
function () |
| 439 |
return self:expandTemplate( opt ) |
| 440 |
end |
| 441 |
) |
| 442 |
end |
| 443 |
|
| 444 |
function frame:getTitle() |
| 445 |
checkSelf( self, 'getTitle' ) |
| 446 |
return php.getFrameTitle( frameId ) |
| 447 |
end |
| 448 |
|
| 449 |
|
| 450 |
function frame:argumentPairs() |
| 451 |
checkSelf( self, 'argumentPairs' ) |
| 452 |
return pairs( self.args ) |
| 453 |
end |
| 454 |
|
| 455 |
return frame |
| 456 |
end |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
function mw.executeModule( chunk, name, frame ) |
| 468 |
local env = mw.clone( _G ) |
| 469 |
makePackageModule( env ) |
| 470 |
|
| 471 |
|
| 472 |
env.mw.makeProtectedEnvFuncs = nil |
| 473 |
env.mw.executeModule = nil |
| 474 |
if name ~= false then |
| 475 |
env.mw.getLogBuffer = nil |
| 476 |
env.mw.clearLogBuffer = nil |
| 477 |
end |
| 478 |
|
| 479 |
if allowEnvFuncs then |
| 480 |
env.setfenv, env.getfenv = mw.makeProtectedEnvFuncs( {[_G] = true}, {} ) |
| 481 |
else |
| 482 |
env.setfenv = nil |
| 483 |
env.getfenv = nil |
| 484 |
end |
| 485 |
|
| 486 |
env.os.date = ttlDate |
| 487 |
env.os.time = ttlTime |
| 488 |
|
| 489 |
frame = frame or newFrame( 'current', 'parent' ) |
| 490 |
env.mw.getCurrentFrame = function () |
| 491 |
return frame |
| 492 |
end |
| 493 |
|
| 494 |
setfenv( chunk, env ) |
| 495 |
|
| 496 |
local res = chunk() |
| 497 |
|
| 498 |
if not name then |
| 499 |
return true, res |
| 500 |
end |
| 501 |
if type(res) ~= 'table' then |
| 502 |
return false, type(res) |
| 503 |
end |
| 504 |
return true, res[name] |
| 505 |
end |
| 506 |
|
| 507 |
function mw.executeFunction( chunk ) |
| 508 |
local getCurrentFrame = getfenv( chunk ).mw.getCurrentFrame |
| 509 |
local frame |
| 510 |
if getCurrentFrame then |
| 511 |
|
| 512 |
frame = getCurrentFrame() |
| 513 |
else |
| 514 |
|
| 515 |
|
| 516 |
frame = newFrame( 'current', 'parent' ) |
| 517 |
end |
| 518 |
|
| 519 |
if executeFunctionDepth == 0 then |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
math.randomseed( 1 ) |
| 524 |
end |
| 525 |
executeFunctionDepth = executeFunctionDepth + 1 |
| 526 |
|
| 527 |
local results = { chunk( frame ) } |
| 528 |
|
| 529 |
local stringResults = {} |
| 530 |
for i, result in ipairs( results ) do |
| 531 |
stringResults[i] = tostring( result ) |
| 532 |
end |
| 533 |
|
| 534 |
executeFunctionDepth = executeFunctionDepth - 1 |
| 535 |
|
| 536 |
return table.concat( stringResults ) |
| 537 |
end |
| 538 |
|
| 539 |
function mw.allToString( ... ) |
| 540 |
local t = { ... } |
| 541 |
for i = 1, select( '#', ... ) do |
| 542 |
t[i] = tostring( t[i] ) |
| 543 |
end |
| 544 |
return table.concat( t, '\t' ) |
| 545 |
end |
| 546 |
|
| 547 |
function mw.log( ... ) |
| 548 |
logBuffer = logBuffer .. mw.allToString( ... ) .. '\n' |
| 549 |
end |
| 550 |
|
| 551 |
function mw.dumpObject( object ) |
| 552 |
local doneTable = {} |
| 553 |
local doneObj = {} |
| 554 |
local ct = {} |
| 555 |
local function sorter( a, b ) |
| 556 |
local ta, tb = type( a ), type( b ) |
| 557 |
if ta ~= tb then |
| 558 |
return ta < tb |
| 559 |
end |
| 560 |
if ta == 'string' or ta == 'number' then |
| 561 |
return a < b |
| 562 |
end |
| 563 |
if ta == 'boolean' then |
| 564 |
return tostring( a ) < tostring( b ) |
| 565 |
end |
| 566 |
return false |
| 567 |
end |
| 568 |
local function _dumpObject( object, indent, expandTable ) |
| 569 |
local tp = type( object ) |
| 570 |
if tp == 'number' or tp == 'nil' or tp == 'boolean' then |
| 571 |
return tostring( object ) |
| 572 |
elseif tp == 'string' then |
| 573 |
return string.format( "%q", object ) |
| 574 |
elseif tp == 'table' then |
| 575 |
if not doneObj[object] then |
| 576 |
local s = tostring( object ) |
| 577 |
if s == 'table' then |
| 578 |
ct[tp] = ( ct[tp] or 0 ) + 1 |
| 579 |
doneObj[object] = 'table#' .. ct[tp] |
| 580 |
else |
| 581 |
doneObj[object] = s |
| 582 |
doneTable[object] = true |
| 583 |
end |
| 584 |
end |
| 585 |
if doneTable[object] or not expandTable then |
| 586 |
return doneObj[object] |
| 587 |
end |
| 588 |
doneTable[object] = true |
| 589 |
|
| 590 |
local ret = { doneObj[object], ' {\n' } |
| 591 |
local mt = getmetatable( object ) |
| 592 |
local indentString = " " |
| 593 |
if mt then |
| 594 |
ret[#ret + 1] = string.rep( indentString, indent + 2 ) |
| 595 |
ret[#ret + 1] = 'metatable = ' |
| 596 |
ret[#ret + 1] = _dumpObject( mt, indent + 2, false ) |
| 597 |
ret[#ret + 1] = "\n" |
| 598 |
end |
| 599 |
|
| 600 |
local doneKeys = {} |
| 601 |
for key, value in ipairs( object ) do |
| 602 |
doneKeys[key] = true |
| 603 |
ret[#ret + 1] = string.rep( indentString, indent + 2 ) |
| 604 |
ret[#ret + 1] = _dumpObject( value, indent + 2, true ) |
| 605 |
ret[#ret + 1] = ',\n' |
| 606 |
end |
| 607 |
local keys = {} |
| 608 |
for key in pairs( object ) do |
| 609 |
if not doneKeys[key] then |
| 610 |
keys[#keys + 1] = key |
| 611 |
end |
| 612 |
end |
| 613 |
table.sort( keys, sorter ) |
| 614 |
for i = 1, #keys do |
| 615 |
local key = keys[i] |
| 616 |
ret[#ret + 1] = string.rep( indentString, indent + 2 ) |
| 617 |
ret[#ret + 1] = '[' |
| 618 |
ret[#ret + 1] = _dumpObject( key, indent + 3, false ) |
| 619 |
ret[#ret + 1] = '] = ' |
| 620 |
ret[#ret + 1] = _dumpObject( object[key], indent + 2, true ) |
| 621 |
ret[#ret + 1] = ",\n" |
| 622 |
end |
| 623 |
ret[#ret + 1] = string.rep( indentString, indent ) |
| 624 |
ret[#ret + 1] = '}' |
| 625 |
return table.concat( ret ) |
| 626 |
else |
| 627 |
if not doneObj[object] then |
| 628 |
ct[tp] = ( ct[tp] or 0 ) + 1 |
| 629 |
doneObj[object] = tostring( object ) .. '#' .. ct[tp] |
| 630 |
end |
| 631 |
return doneObj[object] |
| 632 |
end |
| 633 |
end |
| 634 |
return _dumpObject( object, 0, true ) |
| 635 |
end |
| 636 |
|
| 637 |
function mw.logObject( object, prefix ) |
| 638 |
if prefix and prefix ~= '' then |
| 639 |
logBuffer = logBuffer .. prefix .. ' = ' |
| 640 |
end |
| 641 |
logBuffer = logBuffer .. mw.dumpObject( object ) .. '\n' |
| 642 |
end |
| 643 |
|
| 644 |
function mw.clearLogBuffer() |
| 645 |
logBuffer = '' |
| 646 |
end |
| 647 |
|
| 648 |
function mw.getLogBuffer() |
| 649 |
return logBuffer |
| 650 |
end |
| 651 |
|
| 652 |
function mw.isSubsting() |
| 653 |
return php.isSubsting() |
| 654 |
end |
| 655 |
|
| 656 |
function mw.incrementExpensiveFunctionCount() |
| 657 |
php.incrementExpensiveFunctionCount() |
| 658 |
end |
| 659 |
|
| 660 |
function mw.addWarning( text ) |
| 661 |
php.addWarning( text ) |
| 662 |
end |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
local function dataWrapper( data, seen, name ) |
| 673 |
local t = {} |
| 674 |
seen = seen or { [data] = t } |
| 675 |
|
| 676 |
local function pairsfunc( s, k ) |
| 677 |
k = next( data, k ) |
| 678 |
if k ~= nil then |
| 679 |
return k, t[k] |
| 680 |
end |
| 681 |
return nil |
| 682 |
end |
| 683 |
|
| 684 |
local function ipairsfunc( s, i ) |
| 685 |
i = i + 1 |
| 686 |
if data[i] ~= nil then |
| 687 |
return i, t[i] |
| 688 |
end |
| 689 |
return |
| 690 |
end |
| 691 |
|
| 692 |
local mt = { |
| 693 |
mw_loadData = true, |
| 694 |
__index = function ( tt, k ) |
| 695 |
assert( t == tt ) |
| 696 |
local v = data[k] |
| 697 |
if type( v ) == 'table' then |
| 698 |
seen[v] = seen[v] or dataWrapper( v, seen, name ) |
| 699 |
return seen[v] |
| 700 |
end |
| 701 |
return v |
| 702 |
end, |
| 703 |
__newindex = function ( t, k, v ) |
| 704 |
error( "table from " .. name .. " is read-only", 2 ) |
| 705 |
end, |
| 706 |
__pairs = function ( tt ) |
| 707 |
assert( t == tt ) |
| 708 |
return pairsfunc, t, nil |
| 709 |
end, |
| 710 |
__ipairs = function ( tt ) |
| 711 |
assert( t == tt ) |
| 712 |
return ipairsfunc, t, 0 |
| 713 |
end, |
| 714 |
} |
| 715 |
|
| 716 |
mt.__metatable = mt |
| 717 |
|
| 718 |
return setmetatable( t, mt ) |
| 719 |
end |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
local function validateData( d, seen ) |
| 729 |
seen = seen or {} |
| 730 |
local tp = type( d ) |
| 731 |
if tp == 'nil' or tp == 'boolean' or tp == 'number' or tp == 'string' then |
| 732 |
return nil |
| 733 |
elseif tp == 'table' then |
| 734 |
if seen[d] then |
| 735 |
return nil |
| 736 |
end |
| 737 |
seen[d] = true |
| 738 |
if getmetatable( d ) ~= nil then |
| 739 |
return "data for mw.loadData contains a table with a metatable" |
| 740 |
end |
| 741 |
for k, v in pairs( d ) do |
| 742 |
if type( k ) == 'table' then |
| 743 |
return "data for mw.loadData contains a table as a key" |
| 744 |
end |
| 745 |
local err = validateData( k, seen ) or validateData( v, seen ) |
| 746 |
if err then |
| 747 |
return err |
| 748 |
end |
| 749 |
end |
| 750 |
return nil |
| 751 |
else |
| 752 |
return "data for mw.loadData contains unsupported data type '" .. tp .. "'" |
| 753 |
end |
| 754 |
end |
| 755 |
|
| 756 |
function mw.loadData( module ) |
| 757 |
local data = loadedData[module] |
| 758 |
if type( data ) == 'string' then |
| 759 |
|
| 760 |
error( data, 2 ) |
| 761 |
end |
| 762 |
if not data then |
| 763 |
|
| 764 |
|
| 765 |
local l = package.loaded[module] |
| 766 |
local _ |
| 767 |
|
| 768 |
_, data = mw.executeModule( function() return require( module ) end, nil, newFrame( 'empty' ) ) |
| 769 |
|
| 770 |
package.loaded[module] = l |
| 771 |
|
| 772 |
|
| 773 |
local err |
| 774 |
if type( data ) == 'table' then |
| 775 |
err = validateData( data ) |
| 776 |
else |
| 777 |
err = module .. ' returned ' .. type( data ) .. ', table expected' |
| 778 |
end |
| 779 |
if err then |
| 780 |
loadedData[module] = err |
| 781 |
error( err, 2 ) |
| 782 |
end |
| 783 |
loadedData[module] = data |
| 784 |
end |
| 785 |
|
| 786 |
return dataWrapper( data, nil, 'mw.loadData' ) |
| 787 |
end |
| 788 |
|
| 789 |
function mw.loadJsonData( module ) |
| 790 |
if type( module ) ~= "string" then |
| 791 |
error( string.format( "bad argument #1 to 'mw.loadJsonData' (string expected, got %s)", |
| 792 |
type( module ) ), 2 ) |
| 793 |
end |
| 794 |
local data = loadedJsonData[module] |
| 795 |
if type( data ) == 'string' then |
| 796 |
|
| 797 |
error( data, 2 ) |
| 798 |
end |
| 799 |
if not data then |
| 800 |
data = php.loadJsonData( module ) |
| 801 |
if type( data ) ~= "table" then |
| 802 |
local err = module .. ' returned ' .. type( data ) .. ', table expected' |
| 803 |
loadedJsonData[module] = err |
| 804 |
error( err, 2 ) |
| 805 |
end |
| 806 |
loadedJsonData[module] = data |
| 807 |
end |
| 808 |
|
| 809 |
return dataWrapper( data, nil, 'mw.loadJsonData' ) |
| 810 |
end |
| 811 |
|
| 812 |
return mw |