MediaWiki master
SpecialNamespaceInfo.php
Go to the documentation of this file.
1<?php
2
9namespace MediaWiki\Specials;
10
15
24
25 public function __construct(
26 private readonly NamespaceInfo $namespaceInfo,
27 private readonly Language $contentLanguage
28 ) {
29 parent::__construct( 'NamespaceInfo' );
30 }
31
35 public function execute( $par ) {
36 $this->setHeaders();
37 $this->outputHeader();
38
39 $out = $this->getOutput();
40
41 $idHeading = Html::element(
42 'th',
43 [],
44 $this->msg( 'namespaceinfo-heading-id' )->text()
45 );
46 $canonicalHeading = Html::element(
47 'th',
48 [],
49 $this->msg( 'namespaceinfo-heading-canonical' )->text()
50 );
51 $localHeading = Html::element(
52 'th',
53 [],
54 $this->msg( 'namespaceinfo-heading-local' )->text()
55 );
56 $aliasHeading = Html::element(
57 'th',
58 [],
59 $this->msg( 'namespaceinfo-heading-aliases' )->text()
60 );
61 $infoHeading = Html::element(
62 'th',
63 [],
64 $this->msg( 'namespaceinfo-heading-info' )->text()
65 );
66 $tableHeadingRow = Html::rawElement(
67 'tr',
68 [],
69 $idHeading . $canonicalHeading . $localHeading . $aliasHeading . $infoHeading
70 );
71
72 $allAliases = $this->contentLanguage->getNamespaceAliases();
73
74 $aliases = [];
75 foreach ( $allAliases as $alias => $ns ) {
76 $aliases[$ns][] = $alias;
77 }
78
79 $tableBodyRows = '';
80 foreach ( $this->getContentLanguage()->getFormattedNamespaces() as $ns => $localName ) {
81 $namespaceAliases = $aliases[$ns] ?? [];
82 $tableBodyRows .= $this->makeNamespaceRow( $ns, $localName, $namespaceAliases );
83 }
84
85 $table = Html::rawElement(
86 'table',
87 [ 'class' => 'wikitable' ],
88 $tableHeadingRow . $tableBodyRows
89 );
90
91 $out->addHtml( $table );
92 }
93
101 private function makeNamespaceRow( int $ns, string $localName, array $aliases = [] ): string {
102 $canonicalName = $this->namespaceInfo->getCanonicalName( $ns );
103 if ( $canonicalName ) {
104 $canonicalName = strtr( $canonicalName, '_', ' ' );
105 } else {
106 $canonicalName = '';
107 }
108
109 // Special handling for main namespace
110 if ( $ns === NS_MAIN ) {
111 $localName = $this->msg( 'blanknamespace' )->escaped();
112 $canonicalName = $this->msg( 'blanknamespace' )->inLanguage( 'en' )->escaped();
113 }
114
115 $description = $this->msg( 'namespaceinfo-description-ns' . $ns );
116 if ( $description->isDisabled() ) {
117 // Custom namespace with no message
118
119 if ( $this->namespaceInfo->isTalk( $ns ) ) {
120 $subjectNs = $this->namespaceInfo->getSubject( $ns );
121 $subjectName = strtr(
122 $this->namespaceInfo->getCanonicalName( $subjectNs ),
123 '_',
124 ' '
125 );
126 $description = $this->msg( 'namespaceinfo-description-custom-talk',
127 $subjectName,
128 $subjectNs
129 );
130 } else {
131 $description = $this->msg( 'namespaceinfo-description-custom', $localName );
132 }
133 }
134 $descriptionText = $description->parse();
135
136 $properties = [];
137 if ( $ns >= NS_MAIN ) {
138 // Don't talk about immovable namespaces for virtual NS_SPECIAL or NS_MEDIA
139 $namespaceProtection = $this->getConfig()->get( 'NamespaceProtection' );
140 if ( isset( $namespaceProtection[$ns] ) ) {
141 $rightsNeeded = $namespaceProtection[$ns];
142 if ( !is_array( $rightsNeeded ) ) {
143 $rightsNeeded = [ $rightsNeeded ];
144 }
145 foreach ( $rightsNeeded as $right ) {
146 $properties[] = $this->msg( 'namespaceinfo-namespace-protection-right', $right )
147 ->parse();
148 }
149 }
150
151 if ( !$this->namespaceInfo->isMovable( $ns ) ) {
152 $properties[] = $this->msg( 'namespaceinfo-namespace-immovable' )->parse();
153 }
154 if ( $this->namespaceInfo->isContent( $ns ) ) {
155 $properties[] = $this->msg( 'namespaceinfo-namespace-iscontent' )->parse();
156 }
157 if ( $this->namespaceInfo->hasSubpages( $ns ) ) {
158 $properties[] = $this->msg( 'namespaceinfo-namespace-subpages' )->parse();
159 }
160 if ( $this->namespaceInfo->isNonincludable( $ns ) ) {
161 $properties[] = $this->msg( 'namespaceinfo-namespace-nonincludable' )->parse();
162 }
163 $contentModel = $this->namespaceInfo->getNamespaceContentModel( $ns );
164 if ( $contentModel !== null ) {
165 $properties[] = $this->msg( 'namespaceinfo-namespace-default-contentmodel' )
166 ->params( $contentModel )
167 ->parse();
168 }
169 }
170
171 // Convert to a string
172 $namespaceProperties = '';
173 if ( $properties !== [] ) {
174 $namespaceProperties = Html::openElement( 'ul' );
175 foreach ( $properties as $propertyText ) {
176 $namespaceProperties .= Html::rawElement(
177 'li',
178 [],
179 $propertyText
180 );
181 }
182 $namespaceProperties .= Html::closeElement( 'ul' );
183 }
184
186 if ( $aliases !== [] ) {
187 if ( count( $aliases ) === 1 ) {
188 $namespaceAliases = htmlspecialchars( str_replace( '_', ' ', $aliases[0] ) );
189 } else {
190 $namespaceAliases = Html::openElement( 'ul' );
191 foreach ( $aliases as $alias ) {
192 // cleanup _ for display, since _ is only used internally and not for display
193 $alias = str_replace( "_", " ", $alias );
195 'li',
196 [],
197 $alias
198 );
199 }
200 $namespaceAliases .= Html::closeElement( 'ul' );
201 }
202
203 }
204
205 $idField = Html::rawElement( 'td', [], (string)$ns );
206 $canonicalField = Html::rawElement( 'td', [], $canonicalName );
207 $localField = Html::rawElement( 'td', [], $localName );
208 $aliasesField = Html::rawElement( 'td', [], $namespaceAliases );
209 $infoField = Html::rawElement( 'td', [], $descriptionText . $namespaceProperties );
210
211 return Html::rawElement(
212 'tr',
213 [],
214 $idField . $canonicalField . $localField . $aliasesField . $infoField
215 );
216 }
217
221 protected function getGroupName() {
222 return 'wiki';
223 }
224
225}
const NS_MAIN
Definition Defines.php:51
$namespaceAliases
This class is a collection of static functions that serve two purposes:
Definition Html.php:44
Base class for language-specific code.
Definition Language.php:65
Parent class for all special pages.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getConfig()
Shortcut to get main config object.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
getContentLanguage()
Shortcut to get content language.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages By default the message key is the canonical name of...
Show information about the different namespaces.
__construct(private readonly NamespaceInfo $namespaceInfo, private readonly Language $contentLanguage)
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
element(SerializerNode $parent, SerializerNode $node, $contents)