blob: c362833feebeb53757ee5a47d39b953ea5201afa (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
Getting Namespaces
==================
The Name Space Getter allows you to search for namespaces and their aliases and to list all namespaces of a wiki.
To use it, first get a new NamespaceGetter object from the factory:
.. code-block:: php
$api = new \Mediawiki\Api\MediawikiApi( 'http://localhost/w/api.php' );
$services = new \Mediawiki\Api\MediawikiFactory( $api );
$namespaceGetter = $services->newNamespaceGetter();
Looking for a namespace
-----------------------
If you've got a page name like ``File:awesome_cats.jpg`` and want to know its namespace ID and possible localized names
and aliases, use the following code:
.. code-block:: php
$fileNamespace = $namespaceGetter->getNamespaceByName( 'File' );
printf( "Name in local language: %s\n", $fileNamespace->getLocalName() );
printf( "Possible aliases: %s\n", implode( ', ', $fileNamespace->getAliases() ) );
// ... etc
``getNamespaceByName`` accepts the canonical name, the local name and aliases. If you want to match only the canonical
name, use ``getNamespaceByCanonicalName`` instead.
Getting a namespaced page
-------------------------
If you have a page title that is not in the default namespace, you can't pass the page name string ``PageGetter`` but
must construct a ``Title`` object instead:
.. code-block:: php
$pageName = 'User:MalReynolds';
$nameParts = explode( ':', $pageName, 2 );
$namespace = $namespaceGetter->getNamespaceByName( $nameParts[0] );
$title = new \Mediawiki\DataModel\Title( $nameParts[1], $namespace->getId() );
$page = $services->newPageGetter()->getFromTitle( $title );
Listing all namespaces
----------------------
.. code-block:: php
foreach( $namespaceGetter->getNamespaces() as $namespace ) {
echo $namespace->getLocalName() . "\n";
}
|