11 public function register() {
13 'newTitle' => [ $this,
'newTitle' ],
14 'makeTitle' => [ $this,
'makeTitle' ],
15 'getExpensiveData' => [ $this,
'getExpensiveData' ],
16 'getUrl' => [ $this,
'getUrl' ],
17 'getContent' => [ $this,
'getContent' ],
18 'getFileInfo' => [ $this,
'getFileInfo' ],
19 'protectionLevels' => [ $this,
'protectionLevels' ],
20 'cascadingProtection' => [ $this,
'cascadingProtection' ],
21 'redirectTarget' => [ $this,
'redirectTarget' ],
23 return $this->
getEngine()->registerInterface(
'mw.title.lua', $lib, [
32 if ( $arg ===
null && $default !==
null ) {
34 } elseif ( is_numeric( $arg ) ) {
38 "bad argument #$argIdx to '$name' (unrecognized namespace number '$arg')"
41 } elseif ( is_string( $arg ) ) {
43 if ( $ns ===
false ) {
45 "bad argument #$argIdx to '$name' (unrecognized namespace name '$arg')"
50 $this->
checkType( $name, $argIdx, $arg,
'namespace number or name' );
61 $ns =
$title->getNamespace();
63 'isLocal' => (bool)
$title->isLocal(),
64 'interwiki' =>
$title->getInterwiki(),
66 'nsText' =>
$title->getNsText(),
67 'text' =>
$title->getText(),
68 'fragment' =>
$title->getFragment(),
69 'thePartialUrl' =>
$title->getPartialURL(),
74 $this->
getParser()->getOutput()->addLink( $title );
76 $ret[
'exists'] = (bool)SpecialPageFactory::exists(
$title->getDBkey() );
96 $this->
checkType(
'getExpensiveData', 1, $text,
'string' );
97 $title = Title::newFromText( $text );
101 $dbKey =
$title->getPrefixedDBkey();
102 if ( isset( $this->titleCache[$dbKey] ) ) {
104 $title = $this->titleCache[$dbKey];
106 if ( !
$title->equals( $this->getTitle() ) ) {
116 $this->titleCache[$dbKey] =
$title;
117 if (
$title->getArticleID() > 0 ) {
123 'isRedirect' => (bool)
$title->isRedirect(),
124 'id' =>
$title->getArticleID(),
125 'contentModel' =>
$title->getContentModel(),
128 $ret[
'exists'] = (bool)SpecialPageFactory::exists(
$title->getDBkey() );
131 $ret[
'exists'] =
$title->exists();
148 public function newTitle( $text_or_id, $defaultNamespace =
null ) {
150 if (
$type ===
'number' ) {
151 if ( array_key_exists( $text_or_id, $this->idCache ) ) {
152 $title = $this->idCache[$text_or_id];
155 $title = Title::newFromID( $text_or_id );
156 $this->idCache[$text_or_id] =
$title;
168 } elseif (
$type ===
'string' ) {
173 $title = Title::newFromText( $text_or_id, $defaultNamespace );
179 $this->
checkType(
'title.new', 1, $text_or_id,
'number or string' );
197 public function makeTitle( $ns, $text, $fragment =
null, $interwiki =
null ) {
199 $this->
checkType(
'makeTitle', 2, $text,
'string' );
205 $title = Title::makeTitleSafe( $ns, $text, $fragment, $interwiki );
222 public function getUrl( $text, $which, $query =
null, $proto =
null ) {
230 $this->
checkType(
'getUrl', 1, $text,
'string' );
231 $this->
checkType(
'getUrl', 2, $which,
'string' );
232 if ( !in_array( $which, [
'fullUrl',
'localUrl',
'canonicalUrl' ],
true ) ) {
233 $this->
checkType(
'getUrl', 2, $which,
"'fullUrl', 'localUrl', or 'canonicalUrl'" );
238 $func =
"get" . ucfirst( $which );
240 $args = [ $query, false ];
241 if ( !is_string( $query ) && !is_array( $query ) ) {
244 if ( $which ===
'fullUrl' ) {
246 if ( !isset( $protoMap[$proto] ) ) {
247 $this->
checkType( $which, 2, $proto,
"'http', 'https', 'relative', or 'canonical'" );
249 $args[] = $protoMap[$proto];
252 $title = Title::newFromText( $text );
268 $title = Title::newFromText( $text );
274 $this->
getParser()->getOutput()->addTemplate(
280 if (
$title->equals( $this->getTitle() ) ) {
281 $parserOutput = $this->
getParser()->getOutput();
282 $parserOutput->setFlag(
'vary-revision-sha1' );
283 $parserOutput->setRevisionUsedSha1Base36( $rev ? $rev->getSha1() :
'' );
284 wfDebug( __METHOD__ .
": set vary-revision-sha1 for '$title'" );
287 return $rev ? $rev->getContent() :
null;
297 $this->
checkType(
'getContent', 1, $text,
'string' );
309 $this->
checkType(
'getFileInfo', 1, $text,
'string' );
310 $title = Title::newFromText( $text );
314 $ns =
$title->getNamespace();
322 return [ [
'exists' => false ] ];
324 $this->
getParser()->getOutput()->addImage(
327 if ( !
$file->exists() ) {
328 return [ [
'exists' => false ] ];
330 $pageCount =
$file->pageCount();
331 if ( $pageCount ===
false ) {
335 for ( $i = 1; $i <= $pageCount; ++$i ) {
337 'width' =>
$file->getWidth( $i ),
338 'height' =>
$file->getHeight( $i )
344 'width' =>
$file->getWidth(),
345 'height' =>
$file->getHeight(),
346 'mimeType' =>
$file->getMimeType(),
347 'size' =>
$file->getSize(),
353 if ( empty( $arr ) ) {
356 return array_combine( range( 1, count( $arr ) ), array_values( $arr ) );
366 $this->
checkType(
'protectionLevels', 1, $text,
'string' );
367 $title = Title::newFromText( $text );
372 if ( !
$title->areRestrictionsLoaded() ) {
376 'Scribunto_LuaTitleLibrary::makeArrayOneBased',
$title->getAllRestrictions()
387 $this->
checkType(
'cascadingProtection', 1, $text,
'string' );
388 $title = Title::newFromText( $text );
393 if ( !
$title->areCascadeProtectionSourcesLoaded() ) {
396 list( $sources, $restrictions ) =
$title->getCascadeProtectionSources();
400 return $t->getPrefixedText();
403 'restrictions' => array_map(
'Scribunto_LuaTitleLibrary::makeArrayOneBased', $restrictions )
414 $this->
checkType(
'redirectTarget', 1, $text,
'string' );
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfFindFile( $title, $options=[])
Find a file.
static exists( $index)
Returns whether the specified namespace exists.
This class provides some basic services that Lua libraries will probably need.
getLuaType( $var)
Get the Lua type corresponding to the type of the variable.
checkType( $name, $argIdx, $arg, $expectType)
Check the type of a variable.
getEngine()
Get the engine.
checkTypeOptional( $name, $argIdx, &$arg, $expectType, $default)
Check the type of a variable, with default if null.
getParser()
Get the parser.
incrementExpensiveFunctionCount()
Increment the expensive function count, and throw if limit exceeded.
newTitle( $text_or_id, $defaultNamespace=null)
Handler for title.new.
getContentInternal( $text)
Utility to get a Content object from a title.
static makeArrayOneBased( $arr)
getUrl( $text, $which, $query=null, $proto=null)
Get a URL referring to this title.
getInexpensiveTitleData(Title $title)
Extract inexpensive information from a Title object for return to Lua.
checkNamespace( $name, $argIdx, &$arg, $default=null)
getExpensiveData( $text)
Extract expensive information from a Title object for return to Lua.
makeTitle( $ns, $text, $fragment=null, $interwiki=null)
Handler for title.makeTitle.
getContent( $text)
Handler for getContent.
protectionLevels( $text)
Handler for protectionLevels.
getFileInfo( $text)
Handler for getFileInfo.
cascadingProtection( $text)
Handler for cascadingProtection.
redirectTarget( $text)
Handler for redirectTarget.
Represents a title within MediaWiki.
equals(LinkTarget $title)
Compare with another title.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.