MediaWiki REL1_34
SiteLibrary.php
Go to the documentation of this file.
1<?php
2
4
6 private static $namespacesCacheLang = null;
7 private static $namespacesCache = null;
8 private static $interwikiMapCache = [];
10
11 public function register() {
13
14 $lib = [
15 'getNsIndex' => [ $this, 'getNsIndex' ],
16 'pagesInCategory' => [ $this, 'pagesInCategory' ],
17 'pagesInNamespace' => [ $this, 'pagesInNamespace' ],
18 'usersInGroup' => [ $this, 'usersInGroup' ],
19 'interwikiMap' => [ $this, 'interwikiMap' ],
20 ];
21 $parser = $this->getParser();
22 $info = [
23 'siteName' => $GLOBALS['wgSitename'],
24 'server' => $GLOBALS['wgServer'],
25 'scriptPath' => $GLOBALS['wgScriptPath'],
26 'stylePath' => $GLOBALS['wgStylePath'],
27 'currentVersion' => SpecialVersion::getVersion(
28 '', $parser ? $parser->getTargetLanguage() : $wgContLang
29 ),
30 ];
31
32 if ( !self::$namespacesCache || self::$namespacesCacheLang !== $wgContLang->getCode() ) {
33 $namespaces = [];
34 $namespacesByName = [];
35 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
36 $canonical = MWNamespace::getCanonicalName( $ns );
37 $namespaces[$ns] = [
38 'id' => $ns,
39 'name' => $title,
40 'canonicalName' => strtr( $canonical, '_', ' ' ),
41 'hasSubpages' => MWNamespace::hasSubpages( $ns ),
42 'hasGenderDistinction' => MWNamespace::hasGenderDistinction( $ns ),
43 'isCapitalized' => MWNamespace::isCapitalized( $ns ),
44 'isContent' => MWNamespace::isContent( $ns ),
45 'isIncludable' => !MWNamespace::isNonincludable( $ns ),
46 'isMovable' => MWNamespace::isMovable( $ns ),
47 'isSubject' => MWNamespace::isSubject( $ns ),
48 'isTalk' => MWNamespace::isTalk( $ns ),
49 'defaultContentModel' => MWNamespace::getNamespaceContentModel( $ns ),
50 'aliases' => [],
51 ];
52 if ( $ns >= NS_MAIN ) {
53 $namespaces[$ns]['subject'] = MWNamespace::getSubject( $ns );
54 $namespaces[$ns]['talk'] = MWNamespace::getTalk( $ns );
55 $namespaces[$ns]['associated'] = MWNamespace::getAssociated( $ns );
56 } else {
57 $namespaces[$ns]['subject'] = $ns;
58 }
59 $namespacesByName[strtr( $title, ' ', '_' )] = $ns;
60 if ( $canonical ) {
61 $namespacesByName[$canonical] = $ns;
62 }
63 }
64
65 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
66 foreach ( $aliases as $title => $ns ) {
67 if ( !isset( $namespacesByName[$title] ) && isset( $namespaces[$ns] ) ) {
68 $ct = count( $namespaces[$ns]['aliases'] );
69 $namespaces[$ns]['aliases'][$ct + 1] = $title;
70 $namespacesByName[$title] = $ns;
71 }
72 }
73
74 $namespaces[NS_MAIN]['displayName'] = wfMessage( 'blanknamespace' )->inContentLanguage()->text();
75
76 self::$namespacesCache = $namespaces;
77 self::$namespacesCacheLang = $wgContLang->getCode();
78 }
79 $info['namespaces'] = self::$namespacesCache;
80
81 $info['stats'] = [
82 'pages' => (int)SiteStats::pages(),
83 'articles' => (int)SiteStats::articles(),
84 'files' => (int)SiteStats::images(),
85 'edits' => (int)SiteStats::edits(),
86 'users' => (int)SiteStats::users(),
87 'activeUsers' => (int)SiteStats::activeUsers(),
88 'admins' => (int)SiteStats::numberingroup( 'sysop' ),
89 ];
90
91 return $this->getEngine()->registerInterface( 'mw.site.lua', $lib, $info );
92 }
93
101 public function pagesInCategory( $category = null, $which = null ) {
102 $this->checkType( 'pagesInCategory', 1, $category, 'string' );
103 $this->checkTypeOptional( 'pagesInCategory', 2, $which, 'string', 'all' );
104
105 $title = Title::makeTitleSafe( NS_CATEGORY, $category );
106 if ( !$title ) {
107 return [ 0 ];
108 }
109 $cacheKey = $title->getDBkey();
110
111 if ( !isset( $this->pagesInCategoryCache[$cacheKey] ) ) {
113 $category = Category::newFromTitle( $title );
114 $counts = [
115 'all' => (int)$category->getPageCount(),
116 'subcats' => (int)$category->getSubcatCount(),
117 'files' => (int)$category->getFileCount(),
118 ];
119 $counts['pages'] = $counts['all'] - $counts['subcats'] - $counts['files'];
120 $this->pagesInCategoryCache[$cacheKey] = $counts;
121 }
122 if ( $which === '*' ) {
123 return [ $this->pagesInCategoryCache[$cacheKey] ];
124 }
125 if ( !isset( $this->pagesInCategoryCache[$cacheKey][$which] ) ) {
126 $this->checkType(
127 'pagesInCategory', 2, $which, "one of '*', 'all', 'pages', 'subcats', or 'files'"
128 );
129 }
130 return [ $this->pagesInCategoryCache[$cacheKey][$which] ];
131 }
132
139 public function pagesInNamespace( $ns = null ) {
140 $this->checkType( 'pagesInNamespace', 1, $ns, 'number' );
141 return [ (int)SiteStats::pagesInNs( intval( $ns ) ) ];
142 }
143
150 public function usersInGroup( $group = null ) {
151 $this->checkType( 'usersInGroup', 1, $group, 'string' );
152 return [ (int)SiteStats::numberingroup( strtolower( $group ) ) ];
153 }
154
161 public function getNsIndex( $name = null ) {
162 global $wgContLang;
163 $this->checkType( 'getNsIndex', 1, $name, 'string' );
164 // PHP call is case-insensitive but chokes on non-standard spaces/underscores.
165 $name = trim( preg_replace( '/[\s_]+/', '_', $name ), '_' );
166 return [ $wgContLang->getNsIndex( $name ) ];
167 }
168
175 public function interwikiMap( $filter = null ) {
177 $this->checkTypeOptional( 'interwikiMap', 1, $filter, 'string', null );
178 $local = null;
179 if ( $filter === 'local' ) {
180 $local = 1;
181 } elseif ( $filter === '!local' ) {
182 $local = 0;
183 } elseif ( $filter !== null ) {
184 throw new Scribunto_LuaError(
185 "bad argument #1 to 'interwikiMap' (unknown filter '$filter')"
186 );
187 }
188 $cacheKey = $filter === null ? 'null' : $filter;
189 if ( !isset( self::$interwikiMapCache[$cacheKey] ) ) {
190 // Not expensive because we can have a max of three cache misses in the
191 // entire page parse.
192 $interwikiMap = [];
193 $lookup = MediaWikiServices::getInstance()->getInterwikiLookup();
194 $prefixes = $lookup->getAllPrefixes( $local );
195 foreach ( $prefixes as $row ) {
196 $prefix = $row['iw_prefix'];
197 $val = [
198 'prefix' => $prefix,
199 'url' => wfExpandUrl( $row['iw_url'], PROTO_RELATIVE ),
200 'isProtocolRelative' => substr( $row['iw_url'], 0, 2 ) === '//',
201 'isLocal' => isset( $row['iw_local'] ) && $row['iw_local'] == '1',
202 'isTranscludable' => isset( $row['iw_trans'] ) && $row['iw_trans'] == '1',
203 'isCurrentWiki' => in_array( $prefix, $wgLocalInterwikis ),
204 'isExtraLanguageLink' => in_array( $prefix, $wgExtraInterlanguageLinkPrefixes ),
205 ];
206 if ( $val['isExtraLanguageLink'] ) {
207 $displayText = wfMessage( "interlanguage-link-$prefix" );
208 if ( !$displayText->isDisabled() ) {
209 $val['displayText'] = $displayText->text();
210 }
211 $tooltip = wfMessage( "interlanguage-link-sitename-$prefix" );
212 if ( !$tooltip->isDisabled() ) {
213 $val['tooltip'] = $tooltip->text();
214 }
215 }
216 $interwikiMap[$prefix] = $val;
217 }
218 self::$interwikiMapCache[$cacheKey] = $interwikiMap;
219 }
220 return [ self::$interwikiMapCache[$cacheKey] ];
221 }
222}
$GLOBALS['IP']
$wgExtraInterlanguageLinkPrefixes
List of additional interwiki prefixes that should be treated as interlanguage links (i....
$wgLocalInterwikis
Array for multiple $wgLocalInterwiki values, in case there are several interwiki prefixes that point ...
$wgNamespaceAliases
Namespace aliases.
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
$wgContLang
Definition Setup.php:800
static getSubject( $index)
Get the subject namespace index for a given namespace Special namespaces (NS_MEDIA,...
static isSubject( $index)
Is the given namespace is a subject (non-talk) namespace?
static getCanonicalName( $index)
Returns the canonical (English) name for a given index.
static isCapitalized( $index)
Is the namespace first-letter capitalized?
static hasSubpages( $index)
Does the namespace allow subpages?
static isNonincludable( $index)
It is not possible to use pages from this namespace as template?
static hasGenderDistinction( $index)
Does the namespace (potentially) have different aliases for different genders.
static isTalk( $index)
Is the given namespace a talk namespace?
static getTalk( $index)
Get the talk namespace index for a given namespace.
static isMovable( $index)
Can pages in the given namespace be moved?
static isContent( $index)
Does this namespace contain content, for the purposes of calculating statistics, etc?
static getAssociated( $index)
Get the associated namespace.
static getNamespaceContentModel( $index)
Get the default content model for a namespace This does not mean that all pages in that namespace hav...
MediaWikiServices is the service locator for the application scope of MediaWiki.
This class provides some basic services that Lua libraries will probably need.
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.
pagesInCategory( $category=null, $which=null)
Handler for pagesInCategory.
usersInGroup( $group=null)
Handler for usersInGroup.
pagesInNamespace( $ns=null)
Handler for pagesInNamespace.
getNsIndex( $name=null)
Handler for getNsIndex.
interwikiMap( $filter=null)
Handler for interwikiMap.
static articles()
static pagesInNs( $ns)
static images()
static edits()
Definition SiteStats.php:94
static users()
static pages()
static numberingroup( $group)
Find the number of users in a given user group.
static activeUsers()
static getVersion( $flags='', $lang=null)
Return a string of the MediaWiki version with Git revision if available.
const NS_MAIN
Definition Defines.php:69
const PROTO_RELATIVE
Definition Defines.php:210
const NS_CATEGORY
Definition Defines.php:83
$filter