| 1 |
local title = {} |
| 2 |
local php |
| 3 |
local NS_MEDIA = -2 |
| 4 |
|
| 5 |
local util = require 'libraryUtil' |
| 6 |
local checkType = util.checkType |
| 7 |
local checkTypeForIndex = util.checkTypeForIndex |
| 8 |
|
| 9 |
local function checkNamespace( name, argIdx, arg ) |
| 10 |
if type( arg ) == 'string' and tostring( tonumber( arg ) ) == arg then |
| 11 |
arg = tonumber( arg ) |
| 12 |
end |
| 13 |
if type( arg ) == 'number' then |
| 14 |
arg = math.floor( arg + 0.5 ) |
| 15 |
if not mw.site.namespaces[arg] then |
| 16 |
local msg = string.format( "bad argument #%d to '%s' (unrecognized namespace number '%s')", |
| 17 |
argIdx, name, arg |
| 18 |
) |
| 19 |
error( msg, 3 ) |
| 20 |
end |
| 21 |
elseif type( arg ) == 'string' then |
| 22 |
local ns = mw.site.namespaces[arg] |
| 23 |
if not ns then |
| 24 |
local msg = string.format( "bad argument #%d to '%s' (unrecognized namespace name '%s')", |
| 25 |
argIdx, name, arg |
| 26 |
) |
| 27 |
error( msg, 3 ) |
| 28 |
end |
| 29 |
arg = ns.id |
| 30 |
else |
| 31 |
local msg = string.format( "bad argument #%d to '%s' (string or number expected, got %s)", |
| 32 |
argIdx, name, type( arg ) |
| 33 |
) |
| 34 |
error( msg, 3 ) |
| 35 |
end |
| 36 |
return arg |
| 37 |
end |
| 38 |
|
| 39 |
|
| 40 |
local function lt( a, b ) |
| 41 |
if a.interwiki ~= b.interwiki then |
| 42 |
return a.interwiki < b.interwiki |
| 43 |
end |
| 44 |
if a.namespace ~= b.namespace then |
| 45 |
return a.namespace < b.namespace |
| 46 |
end |
| 47 |
return a.text < b.text |
| 48 |
end |
| 49 |
|
| 50 |
local function makeTitleObject( data ) |
| 51 |
if not data then |
| 52 |
return nil |
| 53 |
end |
| 54 |
|
| 55 |
local obj = {} |
| 56 |
local checkSelf = util.makeCheckSelfFunction( 'mw.title', 'title', obj, 'title object' ); |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
local ns = mw.site.namespaces[data.namespace] |
| 61 |
|
| 62 |
local isCurrentTitle = data.isCurrentTitle |
| 63 |
data.isCurrentTitle = nil |
| 64 |
|
| 65 |
data.isContentPage = ns.isContent |
| 66 |
data.isExternal = data.interwiki ~= '' |
| 67 |
data.isSpecialPage = data.namespace == mw.site.namespaces.Special.id |
| 68 |
data.isTalkPage = ns.isTalk |
| 69 |
data.subjectNsText = ns.subject.name |
| 70 |
|
| 71 |
if ns.talk ~= nil then |
| 72 |
data.canTalk = true |
| 73 |
data.talkNsText = ns.talk.name |
| 74 |
else |
| 75 |
data.canTalk = false |
| 76 |
end |
| 77 |
|
| 78 |
if data.isExternal then |
| 79 |
|
| 80 |
|
| 81 |
data.isContentPage = nil |
| 82 |
data.isSpecialPage = nil |
| 83 |
data.isTalkPage = nil |
| 84 |
data.canTalk = nil |
| 85 |
data.subjectNsText = nil |
| 86 |
data.talkNsText = nil |
| 87 |
end |
| 88 |
|
| 89 |
data.prefixedText = data.text |
| 90 |
if data.nsText ~= '' then |
| 91 |
data.prefixedText = string.gsub( data.nsText .. ':' .. data.prefixedText, '_', ' ' ) |
| 92 |
end |
| 93 |
if data.interwiki ~= '' then |
| 94 |
data.prefixedText = data.interwiki .. ':' .. data.prefixedText |
| 95 |
end |
| 96 |
|
| 97 |
local firstSlash, lastSlash |
| 98 |
if ns.hasSubpages then |
| 99 |
firstSlash, lastSlash = string.match( data.text, '^[^/]*().*()/[^/]*$' ) |
| 100 |
end |
| 101 |
if firstSlash then |
| 102 |
data.isSubpage = true |
| 103 |
data.rootText = string.sub( data.text, 1, firstSlash - 1 ) |
| 104 |
data.baseText = string.sub( data.text, 1, lastSlash - 1 ) |
| 105 |
data.subpageText = string.sub( data.text, lastSlash + 1 ) |
| 106 |
else |
| 107 |
data.isSubpage = false |
| 108 |
data.rootText = data.text |
| 109 |
data.baseText = data.text |
| 110 |
data.subpageText = data.text |
| 111 |
end |
| 112 |
|
| 113 |
function data:inNamespace( ns ) |
| 114 |
checkSelf( self, 'inNamespace' ) |
| 115 |
ns = checkNamespace( 'inNamespace', 1, ns ) |
| 116 |
return ns == self.namespace |
| 117 |
end |
| 118 |
|
| 119 |
function data:inNamespaces( ... ) |
| 120 |
checkSelf( self, 'inNamespaces' ) |
| 121 |
for i = 1, select( '#', ... ) do |
| 122 |
local ns = checkNamespace( 'inNamespaces', i, select( i, ... ) ) |
| 123 |
if ns == self.namespace then |
| 124 |
return true |
| 125 |
end |
| 126 |
end |
| 127 |
return false |
| 128 |
end |
| 129 |
|
| 130 |
function data:hasSubjectNamespace( ns ) |
| 131 |
checkSelf( self, 'hasSubjectNamespace' ) |
| 132 |
ns = checkNamespace( 'hasSubjectNamespace', 1, ns ) |
| 133 |
return ns == mw.site.namespaces[self.namespace].subject.id |
| 134 |
end |
| 135 |
|
| 136 |
function data:isSubpageOf( title ) |
| 137 |
checkSelf( self, 'isSubpageOf' ) |
| 138 |
checkType( 'isSubpageOf', 1, title, 'table' ) |
| 139 |
return self.interwiki == title.interwiki and |
| 140 |
self.namespace == title.namespace and |
| 141 |
title.text .. '/' == string.sub( self.text, 1, #title.text + 1 ) |
| 142 |
end |
| 143 |
|
| 144 |
function data:subPageTitle( text ) |
| 145 |
checkSelf( self, 'subpageTitle' ) |
| 146 |
checkType( 'subpageTitle', 1, text, 'string' ) |
| 147 |
return title.makeTitle( data.namespace, data.text .. '/' .. text ) |
| 148 |
end |
| 149 |
|
| 150 |
function data:partialUrl() |
| 151 |
checkSelf( self, 'partialUrl' ) |
| 152 |
return data.thePartialUrl |
| 153 |
end |
| 154 |
|
| 155 |
function data:fullUrl( query, proto ) |
| 156 |
checkSelf( self, 'fullUrl' ) |
| 157 |
return php.getUrl( self.fullText, 'fullUrl', query, proto ) |
| 158 |
end |
| 159 |
|
| 160 |
function data:localUrl( query ) |
| 161 |
checkSelf( self, 'localUrl' ) |
| 162 |
return php.getUrl( self.fullText, 'localUrl', query ) |
| 163 |
end |
| 164 |
|
| 165 |
function data:canonicalUrl( query ) |
| 166 |
checkSelf( self, 'canonicalUrl' ) |
| 167 |
return php.getUrl( self.fullText, 'canonicalUrl', query ) |
| 168 |
end |
| 169 |
|
| 170 |
function data:getContent() |
| 171 |
|
| 172 |
checkSelf( self, 'getContent' ) |
| 173 |
local content = php.getContent( self.fullText ) |
| 174 |
data.getContent = function ( self ) |
| 175 |
checkSelf( self, 'getContent' ) |
| 176 |
return content |
| 177 |
end |
| 178 |
return content |
| 179 |
end |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
local readOnlyFields = { |
| 186 |
fragment = false, |
| 187 |
fullText = true, |
| 188 |
rootPageTitle = true, |
| 189 |
basePageTitle = true, |
| 190 |
talkPageTitle = true, |
| 191 |
subjectPageTitle = true, |
| 192 |
fileExists = true, |
| 193 |
file = true, |
| 194 |
protectionLevels = true, |
| 195 |
cascadingProtection = true, |
| 196 |
exists = 'e', |
| 197 |
isRedirect = 'e', |
| 198 |
contentModel = 'e', |
| 199 |
id = 'e', |
| 200 |
redirectTarget = true, |
| 201 |
} |
| 202 |
for k in pairs( data ) do |
| 203 |
readOnlyFields[k] = true |
| 204 |
end |
| 205 |
|
| 206 |
local function pairsfunc( t, k ) |
| 207 |
local v |
| 208 |
repeat |
| 209 |
k = next( readOnlyFields, k ) |
| 210 |
if k == nil then |
| 211 |
return nil |
| 212 |
end |
| 213 |
v = t[k] |
| 214 |
until v ~= nil |
| 215 |
return k, v |
| 216 |
end |
| 217 |
|
| 218 |
return setmetatable( obj, { |
| 219 |
__eq = title.equals, |
| 220 |
__lt = lt, |
| 221 |
__pairs = function ( t ) |
| 222 |
return pairsfunc, t, nil |
| 223 |
end, |
| 224 |
__index = function ( t, k ) |
| 225 |
if k == 'exists' and data.namespace == NS_MEDIA then |
| 226 |
k = 'fileExists' |
| 227 |
end |
| 228 |
|
| 229 |
if readOnlyFields[k] == 'e' and data[k] == nil then |
| 230 |
for k,v in pairs( php.getExpensiveData( t.fullText ) ) do |
| 231 |
data[k] = v |
| 232 |
end |
| 233 |
end |
| 234 |
|
| 235 |
if k == 'id' and isCurrentTitle then |
| 236 |
php.recordVaryFlag( t.fullText, 'vary-page-id' ) |
| 237 |
end |
| 238 |
|
| 239 |
if k == 'fullText' then |
| 240 |
if data.fragment ~= '' then |
| 241 |
return data.prefixedText .. '#' .. data.fragment |
| 242 |
else |
| 243 |
return data.prefixedText |
| 244 |
end |
| 245 |
end |
| 246 |
|
| 247 |
if k == 'rootPageTitle' then |
| 248 |
return title.makeTitle( data.namespace, data.rootText ) |
| 249 |
end |
| 250 |
if k == 'basePageTitle' then |
| 251 |
return title.makeTitle( data.namespace, data.baseText ) |
| 252 |
end |
| 253 |
if k == 'talkPageTitle' then |
| 254 |
local ns = mw.site.namespaces[data.namespace].talk |
| 255 |
if not ns then |
| 256 |
return nil |
| 257 |
end |
| 258 |
if ns.id == data.namespace then |
| 259 |
return obj |
| 260 |
end |
| 261 |
return title.makeTitle( ns.id, data.text ) |
| 262 |
end |
| 263 |
if k == 'subjectPageTitle' then |
| 264 |
local ns = mw.site.namespaces[data.namespace].subject |
| 265 |
if ns.id == data.namespace then |
| 266 |
return obj |
| 267 |
end |
| 268 |
return title.makeTitle( ns.id, data.text ) |
| 269 |
end |
| 270 |
if k == 'pageLang' then |
| 271 |
if data.pageLang == nil then |
| 272 |
data.pageLang = mw.language.new( |
| 273 |
php.getPageLangCode( data.prefixedText ) |
| 274 |
); |
| 275 |
end |
| 276 |
return data.pageLang |
| 277 |
end |
| 278 |
if k == 'file' then |
| 279 |
if data.file == nil then |
| 280 |
data.file = php.getFileInfo( data.prefixedText ) |
| 281 |
end |
| 282 |
return data.file or nil |
| 283 |
end |
| 284 |
if k == 'fileExists' then |
| 285 |
return t.file and t.file.exists |
| 286 |
end |
| 287 |
if k == 'protectionLevels' then |
| 288 |
if data.protectionLevels == nil then |
| 289 |
data.protectionLevels = php.protectionLevels( data.prefixedText ) |
| 290 |
end |
| 291 |
return data.protectionLevels |
| 292 |
end |
| 293 |
if k == 'cascadingProtection' then |
| 294 |
if data.cascadingProtection == nil then |
| 295 |
data.cascadingProtection = php.cascadingProtection( data.prefixedText ) |
| 296 |
end |
| 297 |
return data.cascadingProtection |
| 298 |
end |
| 299 |
if k == 'redirectTarget' then |
| 300 |
if data.redirectTarget == nil then |
| 301 |
data.redirectTarget = makeTitleObject( php.redirectTarget( data.prefixedText ) ) or false |
| 302 |
end |
| 303 |
return data.redirectTarget |
| 304 |
end |
| 305 |
if k == 'content' then |
| 306 |
if data.content == nil then |
| 307 |
data.content = php.getContent( data.prefixedText ) or false |
| 308 |
end |
| 309 |
return data.content |
| 310 |
end |
| 311 |
if k == 'categories' then |
| 312 |
if data.categories == nil then |
| 313 |
data.categories = php.getCategories( data.prefixedText ) |
| 314 |
end |
| 315 |
return data.categories |
| 316 |
end |
| 317 |
|
| 318 |
return data[k] |
| 319 |
end, |
| 320 |
__newindex = function ( t, k, v ) |
| 321 |
if k == 'fragment' then |
| 322 |
checkTypeForIndex( k, v, 'string' ) |
| 323 |
v = string.gsub( v, '[%s_]+', ' ' ) |
| 324 |
v = string.gsub( v, '^(.-) ?$', '%1' ) |
| 325 |
data[k] = v |
| 326 |
elseif readOnlyFields[k] then |
| 327 |
error( "index '" .. k .. "' is read only", 2 ) |
| 328 |
else |
| 329 |
readOnlyFields[k] = v and false |
| 330 |
rawset( t, k, v ) |
| 331 |
end |
| 332 |
end, |
| 333 |
__tostring = function ( t ) |
| 334 |
return t.prefixedText |
| 335 |
end |
| 336 |
} ) |
| 337 |
end |
| 338 |
|
| 339 |
function title.setupInterface( options ) |
| 340 |
|
| 341 |
title.setupInterface = nil |
| 342 |
php = mw_interface |
| 343 |
mw_interface = nil |
| 344 |
NS_MEDIA = options.NS_MEDIA |
| 345 |
|
| 346 |
|
| 347 |
title.getCurrentTitle = function () |
| 348 |
return makeTitleObject( mw.clone( options.thisTitle ) ) |
| 349 |
end |
| 350 |
|
| 351 |
|
| 352 |
mw = mw or {} |
| 353 |
mw.title = title |
| 354 |
|
| 355 |
package.loaded['mw.title'] = title |
| 356 |
end |
| 357 |
|
| 358 |
function title.new( text_or_id, defaultNamespace ) |
| 359 |
return makeTitleObject( php.newTitle( text_or_id, defaultNamespace ) ) |
| 360 |
end |
| 361 |
|
| 362 |
function title.makeTitle( ns, title, fragment, interwiki ) |
| 363 |
return makeTitleObject( php.makeTitle( ns, title, fragment, interwiki ) ) |
| 364 |
end |
| 365 |
|
| 366 |
function title.equals( a, b ) |
| 367 |
return a.interwiki == b.interwiki and |
| 368 |
a.namespace == b.namespace and |
| 369 |
a.text == b.text |
| 370 |
end |
| 371 |
|
| 372 |
function title.compare( a, b ) |
| 373 |
if a.interwiki ~= b.interwiki then |
| 374 |
return a.interwiki < b.interwiki and -1 or 1 |
| 375 |
end |
| 376 |
if a.namespace ~= b.namespace then |
| 377 |
return a.namespace < b.namespace and -1 or 1 |
| 378 |
end |
| 379 |
if a.text ~= b.text then |
| 380 |
return a.text < b.text and -1 or 1 |
| 381 |
end |
| 382 |
return 0 |
| 383 |
end |
| 384 |
|
| 385 |
return title |