Title normalization library for mediawiki
npm install mediawiki-titleMediawiki title normalization, that conforms to the normalization rules used in MediaWiki Core.
In general, the page title is converted to the mediawiki DB key format by trimming spaces, replacing whitespace symbols to underscores
and applying wiki-specific capitalization rules. The namespace name is converted to a localized canonical name.
ObjectInformation about a wikimedia site required to make correct
normalization. This information matches the format used by the
PHP API response,
however not all of the fields are required for library operation.
The list of required properties is documented here, others can be removed.
* Namespace
* new Namespace(id, siteInfo)
* _instance_
* .getId() ⇒ number
* .isMedia() ⇒ boolean
* .isSpecial() ⇒ boolean
* .isMain() ⇒ boolean
* .isTalk() ⇒ boolean
* .isUserTalk() ⇒ boolean
* .getNormalizedText() ⇒ string
* .equals(ns) ⇒ boolean
* _static_
* .fromText(text, siteInfo) ⇒ Namespace | undefined
* .main(siteInfo) ⇒ Namespace
| Param | Type | Description |
| --- | --- | --- |
| id | number | The namespace identifier |
| siteInfo | SiteInfo | The site metadata information. |
Kind: instance method of Namespace
MediaKind: instance method of Namespace
SpecialKind: instance method of Namespace
MainKind: instance method of Namespace
TalkKind: instance method of Namespace
UserTalkKind: instance method of Namespace
Kind: instance method of Namespace
ns,Kind: instance method of Namespace
Kind: static method of Namespace
Returns: Namespace | undefined - a namespace or undefined if it wasn't found.
| Param | Type | Description |
| --- | --- | --- |
| text | string | Namespace name text. |
| siteInfo | SiteInfo | the site information. |
Main namespace.Kind: static method of Namespace
| Param | Type | Description |
| --- | --- | --- |
| siteInfo | SiteInfo | the site information. |
* Title
* [new Title(key, namespace, siteInfo, [fragment])](#new_Title_new)
* _instance_
* .getPrefixedDBKey() ⇒ string
* .getFragment() ⇒ string | undefined
* .getNamespace() ⇒ Namespace
* .equals(title) ⇒ boolean
* _static_
* .newFromText(title, siteInfo, defaultNs) ⇒ Title
| Param | Type | Description |
| --- | --- | --- |
| key | string | The article title in a form of the dbKey. |
| namespace | Namespace | number | The article namespace. |
| siteInfo | SiteInfo | The site metadata. |
| [fragment] | string | The fragment of the title. |
Kind: instance method of Title
Kind: instance method of Title
Kind: instance method of Title
title2,Kind: instance method of Title
siteInfoKind: static method of Title
Returns: Title - The resulting title object.
| Param | Type | Description |
| --- | --- | --- |
| title | string | The page title to normalize. |
| siteInfo | SiteInfo | The site information. |
TitleError should be considered an opaque exception class,
which is thrown by the various methods in this library. It is exported
so that it can be identified if caught by the caller.
ObjectThe list of required properties is documented here, others can be removed.
Kind: global typedef
Properties
| Name | Type | Description |
| --- | --- | --- |
| general | Object | General information about the site |
| general.lang | string | Site language code. |
| general.legaltitlechars | string | A perl-like regex for characters allowed in the page title. |
| general.case | string | Whether to capitalize the first letter of the title. Could be obtained from the general section of the siteInfo php API response. |
| namespaces | Object | Site namespaces info in the same format as returned by PHP api. |
| namespacealiases | Object | Site namespace aliases in the same format as returned by PHP api. |
| specialpagealiases | Object | Site special page aliases in the same format as returned by PHP api. |
The library synchronously returns a normalized title. Wiki-specific rules should be fetched from the
MediaWiki API,
and cached outside of the library. The description of the required properties is available in the SiteInfo
object docs.
``javascripta-z~\\x80-\\xFF+",
var result = Title.newFromText('some_title', {
general: {
lang: 'en',
legaltitlechars: " %!\"$&'()*,\\-.\\/0-9:;=?@A-Z\\\\^_
case: 'first-letter'
},
namespaces: {
"0": {
id: 0,
case: "first-letter",
content: "",
"*": ""
},
}
});
console.log(result.getPrefixedDBKey());
``
For bug reporting please use Phabricator
and mark the bugs with Services` label or contact directly in IRC in the #wikimedia-services channel.