Manual:Menggunakan ruang nama buatan sendiri

From mediawiki.org
This page is a translated version of the page Manual:Using custom namespaces and the translation is 31% complete.
Outdated translations are marked like this.

Selain ruang nama bawaan, juga mungkin untuk menambahkan ruang nama buatan sendiri ke sebuah instalasi MediaWiki, untuk memperinci pemisahan konten dan membuat organisasi yang lebih logis.

Ruang nama buatan sendiri mudah dikelola menggunakan perintah konfigurasi $wgExtraNamespaces . Juga mungkin untuk mendefinisikan nama alias untuk ruang nama buatan sendiri (dan juga ruang nama bawaan), menggunakan perintah konfigurasi $wgNamespaceAliases . Some extensions make it easy for you to create custom namespaces. Examples include NamespaceManager and BlueSpiceNamespaceManager .

Disarankan untuk memastikan tidak ada pekerjaan yang sedang menunggu di antrean pekerjaan sebelum mengubah ruang nama, untuk menghindari pekerjaan tersebut gagal apabila mereka menuju halaman dari ruang nama yang Anda akan hapus atau ubah namanya. Gunakan runJobs.php untuk menjalankan semua pekerjaan yang menunggu dan membersihkan antrean sebelum mengubah konfigurasi ruang nama.

Membuat ruang nama buatan sendiri

Anda mendaftarkan ruang nama tambahan dengan menambahkannya ke variabel global $wgExtraNamespaces ke berkas "LocalSettings.php" Anda. Semua ruang nama membutuhkan sebuah indeks numerik unik di dalam larik ini. Sebagai contoh sederhana pembuatan ruang nama buatan sendiri, menambahkan baris-baris berikut ke berkas "LocalSettings.php" akan mendefinisikan sebuah ruang nama "Foo" 3000 dan ruang nama terkaitnya "Foo_talk": Note that having a talk namespace associated with your custom namespace is currently a hard requirement.

// Definisikan konstanta untuk ruang nama tambahanku.
define("NS_FOO", 3000); // Ini HARUS genap.
define("NS_FOO_TALK", 3001); // Ini HARUS bilangan ganjil berikutnya.

// Tambahkan ruang nama.
$wgExtraNamespaces[NS_FOO] = "Foo";
$wgExtraNamespaces[NS_FOO_TALK] = "Foo_talk"; // Perhatikan tanda garis bawah di nama ruang nama.
Pilih nomor yang belum dipakai
Sebagai konvensi, ruang nama yang dinomori 100-199 disimpan untuk ruang nama khusus situs, meskipun terdapat beberapa ekstensi yang tidak mengikuti konvensi ini. Penulis ekstensi menggunakan bilangan yang lebih tinggi, hingga 32767. Ketika memilih indeks Anda, Anda sebaiknya menghindari nomor yang sudah ada di ruang nama standar ekstensi, karena mungkin nanti Anda mau memasang ekstensi itu. Nomor dari 3000 hingga 4999 disimpan bagi administrator sistem untuk mendefinisikan ruang nama buatan mereka. (Juga, Anda sebaiknya menghindari nama ruang nama yang sudah ada di Ruang nama standar ekstensi.)
Genap lalu ganjil
Perhatikan bahwa indeks larik ruang namanya adalah 3000 di contoh di atas.
Buat ruang nama pembicaraan juga
Anda biasanya membuat sebuah ruang nama "Pembicaraan" beserta setiap ruang nama buatan sendiri. Dengan contoh ini, jika Anda memindahkan sebuah halaman ke ruang nama "Foo", Anda akan diberi pilihan untuk memindahkan halaman pembicaraannya, jika ada, dan apabila Anda memilih untuk melakukannya, MediaWiki akan meletakkan halaman pembicaraannya di "Foo talk".
Jangan pakai spasi
Gunakan tanda garis bawah bukannya spasi ketika mendaftarkan nama ruang nama. "Ruang Namaku" tidak valid di sini; gunakan "Ruang_Namaku".
Jangan gunakan tanda hubung

Bagian huruf kapital tidak membolehkan tanda hubung tetapi tanda hubung masih bisa ditambahkan ke judul prefiks. Contoh:

$wgExtraNamespaces[NS_FOOFOO] = "Foo-Foo";
Beri nama kepada nomor yang Anda pilih
Contoh di atas mendefinisikan konstanta untuk ID ruang nama, jadi Anda bisa menggunakannya untuk menyebut ruang nama di konfigurasi, sebagai contoh di $wgNamespaceProtection , $wgNamespacesWithSubpages , atau $wgExtraGenderNamespaces .

Anda bisa mengonfigurasi pengaturan tambahan untuk ruang nama baru Anda.

$wgNamespaceProtection[NS_FOO] = [ 'editfoo' ]; // izin "editfoo" diperlukan untuk menyunting ruang nama foo
$wgNamespacesWithSubpages[NS_FOO] = true;            // subhalaman dinyalakan untuk ruang nama foo
$wgGroupPermissions['sysop']['editfoo'] = true;      // izin "editfoo" diberikan kepada pengguna di kelompok "sysop"
Lakukan sejak awal
Pengubahan $wgExtraNamespaces perlu diselesaikan ketika inisialisasi MediaWiki, jika ekstensi seharusnya bekerja dengan ruang nama yang baru dibuat, pastikan Anda mendefinisikan dan menamainya sebelum memanggil ekstensinya. Sebagai contoh pengaturan tersebut tidak bisa diubah di dalam hook pascainisialisasi seperti $wgExtensionFunctions .
Hati-hati terhadap tabrakan dengan protokol URL
Kode tautan MediaWiki mengetahui tentang beberapa protokol URL, didefinisikan dalam variabel $wgUrlProtocols . Jika nama ruang nama Anda sama dengan salah satu protokol tersebut, Anda akan kesulitan membuat [[pranala wiki]] ke halaman di ruang nama buatan Anda. Masalah ini biasanya paling sering muncul ketika seseorang mencoba membuat ruang nama "News", karena news: adalah sebuah protokol URL untuk grup berita NNTP.
Untuk menghindari masalah ini, Anda bisa menghapus protokol URL yang relevan dari daftar dengan menambahkan kode berikut ke berkas LocalSettings.php (mengganti news dengan nama huruf kecil dari protokol yang Anda ingin hapus):
$wgUrlProtocols = array_diff( $wgUrlProtocols, array( 'news:' ) );

In extensions

Extensions often add their own namespaces, such as the Flow extension's "Topic" namespace. An extension can unconditionally add to $wgExtraNamespaces as described above, or if its namespace registration is conditional (for example EventLogging only defines its "Schema" namespace on the wiki where it stores schemas), then it can add a handler function for the CanonicalNamespaces hook that decides what to do.

The timing of registering extensions is subtle. Functions that extensions register with $wgExtensionFunctions are executed too late to register additional namespaces (task T47031). So extensions should bind the CanonicalNamespaces hook at file scope (in MyExtension.php) and check there whether the wiki should activate the extra namespace or not. Extensions can configure namespace permissions and content handlers unconditionally at file scope since they do not require the namespace to actually be created.

Versi MediaWiki:
1.25
Gerrit change 166705

The extension.json registration system has a namespaces key for an extension to list its namespaces that should always exist. From the Gadgets extension:

"namespaces": [
		{
			"id": 2300,
			"constant": "NS_GADGET",
			"name": "Gadget",
			"protection": "gadgets-edit"
		},
		{
			"id": 2301,
			"constant": "NS_GADGET_TALK",
			"name": "Gadget_talk"
		},
]

It also supports the CanonicalNamespaces hook. Extensions should prefer extension registration over CanonicalNamespaces. Note that adding an extension to LocalSettings.php does not necessarily make relevant namespace constants available as globals for other extensions.

Content namespaces

When building the site statistics page (see Special:Statistics), MediaWiki uses values stored in the database to calculate certain totals. One particular total is the "number of articles" or "number of content pages" figure.

For a page to be considered an article, or proper content, it must:

When creating custom namespaces to hold additional content, it is a good idea to indicate this in the configuration. This is done via the $wgContentNamespaces configuration directive.

To extend the example above, one might add the following to the "LocalSettings.php" file:

$wgContentNamespaces[] = 3000;
or
$wgContentNamespaces[] = NS_FOO;

MediaWiki will now consider pages in the "Foo" namespace to be articles, if they meet the remaining criteria, and will include them when updating the site statistics counters.

Running maintenance scripts

  • When adjusting the value of configuration parameter $wgContentNamespaces, it is a good idea to run either the "path/to/maintenance/updateArticleCount.php or "path/to/maintenance/initSiteStats.php" script to update the internal statistics cache (see Manual:Maintenance scripts ).

Why you would want a custom namespace

There are several reasons you might want this:

  • A custom namespace can be used to hold content that should not be shown on the search results page, for example pages that are used only for transclusion.
  • Certain namespaces require additional privilege(s) for editing.
  • You want certain namespaces not to be subjected to certain limitations or default settings ($wgNoFollowNsExceptions for example)
  • A uniform prefix for specific content(s), which is searchable for that namespace only

Dealing with existing pages

When storing page records, MediaWiki uses a namespace's numerical index, along with the remaining title text. Thus, when a page is created in a namespace that doesn't exist, e.g. "Bar:Some page", it is treated as being in the main namespace.

This can cause problems if adding a custom namespace definition for "Bar" at a later date, as MediaWiki will look for a page indexed via the proper namespace, but won't be able to find it, thus making the content inaccessible.

To correct this problem, there are three main approaches.

Move conflicting pages

If the number of pages affected is small (e.g. "Bar" held five pages created before the namespace was defined in the site configuration), then the following approach might be suitable:

  1. Comment out the namespace definition in the configuration file
  1. Access each affected page, and move it out of the pseudo-namespace, e.g. move "Bar:Some page" to "Bar2:Some page"
  1. Un-comment the namespace definition
  1. Move the affected pages back into the new namespace

Use a maintenance script

Within the maintenance directory, there is a maintenance script which performs the above operation more effectively for a large number of pages: NamespaceDupes.php It is simple to use, but as with all MediaWiki maintenance scripts, consult the available usage information first (use --help as an option).

Use a database query

To move all pages "Bar:Some page" into namespace 3000, make the following database query:

UPDATE page SET
page_title = REPLACE(page_title, 'Bar:', ''),
page_namespace = 3000
WHERE page_title LIKE 'Bar:%' AND page_namespace=0

To handle discussion pages:

UPDATE page SET
page_title = REPLACE(page_title, 'Bar_talk:', ''),
page_namespace = 3001
WHERE page_title LIKE 'Bar_talk:%' AND page_namespace=1

After such fiddling, run the refreshLinks.php script and the updateSearchIndex.php script to update internal links and search results in your wiki. Note that external search engines like Google will take some time to update their index.

Removing custom namespaces

The problem addressed above also occurs when a custom namespace definition is removed; MediaWiki is no longer aware of the numerical index for the namespace, and attempts to search the main namespace for the desired pages, leading to inaccessible content. This is a rare occurrence, since most sites will not need namespaces removed, but it is a problem. (See mailing list discussion).

Example on how to remove Flow and the Topic namespace:

  • Uninstall Flow
  • Temporarily add $wgExtraNamespaces[2600] = 'Topic'; to the config
  • Use deleteBatch.php to delete all pages in the Topic namespace
  • Remove the $wgExtraNamespaces config

Renaming custom namespaces

Suppose that you need to rename custom namespace "Foo" to "New" without performing a mass move of pages. The easiest way to achieve this is to preserve the namespace ID (here "3000") as well as the namespace constant (here "NS_FOO"), modify the (visible) namespace title and add the old one as an alias.

change
define("NS_FOO", 3000);
$wgExtraNamespaces[NS_FOO] = "Foo";
to
define("NS_FOO", 3000);
$wgExtraNamespaces[NS_FOO] = "New";
$wgNamespaceAliases['Foo'] = NS_FOO;

Avoid namespace conflicts

In order for you to avoid namespace conflicts e.g. your namespace has the same number as a namespace defined by an extension, the extension namespace list shows you which numbers to avoid to prevent conflicts.

Defining $wgNamespacesToBeSearchedDefault , $wgNamespacesWithSubpages , $wgContentNamespaces or $wgNamespaceAliases for an ID not associated to any existing namespace in $wgExtraNamespaces doesn't break the wiki; MediaWiki gracefully ignores such configurations.

Styling namespaces

For example, to set the background color of pages in a particular namespace (and its associated talk namespace) you can add the following code to your common.css:

.ns-3000 #content, .ns-3001 #content { background-color: #f3f3ff; }
.ns-3000 div.thumb, .ns-3001 div.thumb { border-color: #f3f3ff; }

where 3000 is the namespace's index and #f3f3ff is the color you want as its background color.

You might also want to change the name of the tab from its default (the namespace's name). This is located in your system messages at MediaWiki:nstab-namespace.

See also

Site administration

Extensions

For extension developers