MediaWiki  1.34.0
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 = [];
9  private $pagesInCategoryCache = [];
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 }
$filter
$filter
Definition: profileinfo.php:344
SiteStats\articles
static articles()
Definition: SiteStats.php:103
MWNamespace\isNonincludable
static isNonincludable( $index)
It is not possible to use pages from this namespace as template?
Definition: MWNamespace.php:291
SiteStats\users
static users()
Definition: SiteStats.php:121
MWNamespace\hasGenderDistinction
static hasGenderDistinction( $index)
Does the namespace (potentially) have different aliases for different genders.
Definition: MWNamespace.php:279
MWNamespace\isTalk
static isTalk( $index)
Is the given namespace a talk namespace?
Definition: MWNamespace.php:55
Scribunto_LuaError
Definition: LuaCommon.php:992
SiteStats\activeUsers
static activeUsers()
Definition: SiteStats.php:130
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
MWNamespace\getNamespaceContentModel
static getNamespaceContentModel( $index)
Get the default content model for a namespace This does not mean that all pages in that namespace hav...
Definition: MWNamespace.php:306
SiteStats\pages
static pages()
Definition: SiteStats.php:112
Scribunto_LuaSiteLibrary\pagesInNamespace
pagesInNamespace( $ns=null)
Handler for pagesInNamespace.
Definition: SiteLibrary.php:139
Scribunto_LuaSiteLibrary\$pagesInCategoryCache
$pagesInCategoryCache
Definition: SiteLibrary.php:9
SiteStats\numberingroup
static numberingroup( $group)
Find the number of users in a given user group.
Definition: SiteStats.php:150
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
Scribunto_LuaSiteLibrary\$interwikiMapCache
static $interwikiMapCache
Definition: SiteLibrary.php:8
Scribunto_LuaLibraryBase\getEngine
getEngine()
Get the engine.
Definition: LibraryBase.php:56
Scribunto_LuaSiteLibrary\pagesInCategory
pagesInCategory( $category=null, $which=null)
Handler for pagesInCategory.
Definition: SiteLibrary.php:101
SiteStats\images
static images()
Definition: SiteStats.php:139
Scribunto_LuaLibraryBase\getParser
getParser()
Get the parser.
Definition: LibraryBase.php:74
NS_MAIN
const NS_MAIN
Definition: Defines.php:60
Scribunto_LuaSiteLibrary\$namespacesCache
static $namespacesCache
Definition: SiteLibrary.php:7
Scribunto_LuaLibraryBase\checkType
checkType( $name, $argIdx, $arg, $expectType)
Check the type of a variable.
Definition: LibraryBase.php:141
Scribunto_LuaSiteLibrary\getNsIndex
getNsIndex( $name=null)
Handler for getNsIndex.
Definition: SiteLibrary.php:161
MWNamespace\isContent
static isContent( $index)
Does this namespace contain content, for the purposes of calculating statistics, etc?
Definition: MWNamespace.php:198
MWNamespace\hasSubpages
static hasSubpages( $index)
Does the namespace allow subpages?
Definition: MWNamespace.php:229
$title
$title
Definition: testCompression.php:34
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:74
MWNamespace\isSubject
static isSubject( $index)
Is the given namespace is a subject (non-talk) namespace?
Definition: MWNamespace.php:45
Scribunto_LuaLibraryBase\checkTypeOptional
checkTypeOptional( $name, $argIdx, &$arg, $expectType, $default)
Check the type of a variable, with default if null.
Definition: LibraryBase.php:164
Category\newFromTitle
static newFromTitle( $title)
Factory function.
Definition: Category.php:146
SpecialVersion\getVersion
static getVersion( $flags='', $lang=null)
Return a string of the MediaWiki version with Git revision if available.
Definition: SpecialVersion.php:295
Scribunto_LuaLibraryBase
This class provides some basic services that Lua libraries will probably need.
Definition: LibraryBase.php:27
$wgNamespaceAliases
$wgNamespaceAliases
Namespace aliases.
Definition: DefaultSettings.php:3892
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:613
SiteStats\pagesInNs
static pagesInNs( $ns)
Definition: SiteStats.php:201
$wgLocalInterwikis
$wgLocalInterwikis
Array for multiple $wgLocalInterwiki values, in case there are several interwiki prefixes that point ...
Definition: DefaultSettings.php:3935
PROTO_RELATIVE
const PROTO_RELATIVE
Definition: Defines.php:201
Scribunto_LuaSiteLibrary\$namespacesCacheLang
static $namespacesCacheLang
Definition: SiteLibrary.php:6
MWNamespace\isMovable
static isMovable( $index)
Can pages in the given namespace be moved?
Definition: MWNamespace.php:34
$wgExtraInterlanguageLinkPrefixes
$wgExtraInterlanguageLinkPrefixes
List of additional interwiki prefixes that should be treated as interlanguage links (i....
Definition: DefaultSettings.php:2990
Scribunto_LuaSiteLibrary\interwikiMap
interwikiMap( $filter=null)
Handler for interwikiMap.
Definition: SiteLibrary.php:175
Scribunto_LuaLibraryBase\incrementExpensiveFunctionCount
incrementExpensiveFunctionCount()
Increment the expensive function count, and throw if limit exceeded.
Definition: LibraryBase.php:177
Scribunto_LuaSiteLibrary
Definition: SiteLibrary.php:5
MWNamespace\isCapitalized
static isCapitalized( $index)
Is the namespace first-letter capitalized?
Definition: MWNamespace.php:267
Scribunto_LuaSiteLibrary\usersInGroup
usersInGroup( $group=null)
Handler for usersInGroup.
Definition: SiteLibrary.php:150
MWNamespace\getTalk
static getTalk( $index)
Get the talk namespace index for a given namespace.
Definition: MWNamespace.php:65
MWNamespace\getSubject
static getSubject( $index)
Get the subject namespace index for a given namespace Special namespaces (NS_MEDIA,...
Definition: MWNamespace.php:76
$wgContLang
$wgContLang
Definition: Setup.php:801
MWNamespace\getAssociated
static getAssociated( $index)
Get the associated namespace.
Definition: MWNamespace.php:88
MWNamespace\getCanonicalName
static getCanonicalName( $index)
Returns the canonical (English) name for a given index.
Definition: MWNamespace.php:155
SiteStats\edits
static edits()
Definition: SiteStats.php:94
$GLOBALS
$GLOBALS['IP']
Definition: ComposerHookHandler.php:6
wfExpandUrl
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Definition: GlobalFunctions.php:491