mwbot-ts - v1.2.7
    Preparing search index...

    Interface TitleStatic

    This interface defines the static members of the Title class. For instance members, see Title (defined separately due to TypeScript limitations).

    Example:

    const title = mwbot.Title.newFromText('wikipedia talk:sandbox');
    if (title) {
    console.log(title.getPrefixedDb());
    // Output: "Wikipedia_talk:Sandbox" (on enwiki)
    }

    When using the constructor directly, passing invalid titles will result in an exception. Use newFromText instead to safely handle invalid titles, returning null instead of throwing an error.

    In both the constructor and newFromText, namespace acts only as the default namespace and can be overridden by a namespace prefix in title. If you want to enforce the provided namespace, use makeTitle instead. Compare:

    new mwbot.Title('Foo', NS_TEMPLATE).getPrefixedText();
    // => 'Template:Foo'
    mwbot.Title.newFromText('Foo', NS_TEMPLATE).getPrefixedText();
    // => 'Template:Foo'
    mwbot.Title.makeTitle(NS_TEMPLATE, 'Foo').getPrefixedText();
    // => 'Template:Foo'

    new mwbot.Title('Category:Foo', NS_TEMPLATE).getPrefixedText();
    // => 'Category:Foo'
    mwbot.Title.newFromText('Category:Foo', NS_TEMPLATE).getPrefixedText();
    // => 'Category:Foo'
    mwbot.Title.makeTitle(NS_TEMPLATE, 'Category:Foo').getPrefixedText();
    // => 'Template:Category:Foo'

    In addition to the native mediawiki.Title features, mwbot.Title correctly parses interwiki prefixes. Compare:

    • mediawiki.Title
    new mw.Title('w:en:Main_page').getPrefixedDb();
    // => 'W:en:Main_page'
    // { namespace: 0, title: 'w:en:Main_page', fragment: null }
    • mwbot.Title
    new mwbot.Title('w:en:Main_page').getPrefixedDb();
    // => 'w:en:Main_page'
    // { namespace: 0, title: 'Main_page', fragment: null, colon: '', interwiki: 'w:en', local_interwiki: false }

    Here, Main_page is correctly extracted as the title in mwbot.Title. To check whether a title contains an interwiki prefix, use Title.isExternal, and to retrieve the interwiki prefixes, use Title.getInterwiki.

    Interwiki Handling and Namespace Limitations

    Titles with interwiki prefixes are always recognized as being in the main namespace, since there is no guarantee that the interwiki target site has an equivalent namespace. This also affects the casing of title, as the interwiki prefix is treated as part of the title rather than a namespace. Example:

    const title = new mwbot.Title('mw:mediawiki:sidebar');
    // { namespace: 0, title: 'mediawiki:sidebar', fragment: null, colon: '', interwiki: 'mw', local_interwiki: false }
    title.getNamespaceId();
    // => 0
    title.getPrefixedDb({ interwiki: true });
    // => 'mw:mediawiki:sidebar'

    The casing of the title depends on the input string. This ensures compatibility with case-sensitive wikis — do not assume that [[iw:foo]] will be interpreted as [[iw:Foo]] when dealing with interwiki titles. (This is how TitleParser in PHP works.)

    Leading Colons

    The handling of leading colons is improved in mwbot.Title. You can check if a title originally had a leading colon using Title.hadLeadingColon, and you can enforce its presence in the output of Title.getPrefixedDb. This makes it easier to differentiate e.g. pure links ([[:cat]]) and category links ([[cat]]).

    const title = new mwbot.Title(':Category:CSD');
    // { namespace: 14, title: 'CSD', fragment: null, colon: ':', interwiki: '', local_interwiki: false }
    title.hadLeadingColon();
    // => true
    title.getPrefixedDb({ colon: true });
    // => ':Category:CSD'
    interface TitleStatic {
        new TitleStatic(title: string, namespace?: number): Title;
        exist: {
            pages: Record<string, boolean>;
            set: (titles: string | string[], state?: boolean) => boolean;
        };
        clean(str: string, trim?: boolean): string;
        exists(title: string | Title): null | boolean;
        isTalkNamespace(namespaceId: number): boolean;
        lc(str: string): string;
        makeTitle(
            namespace: number,
            title: string,
            fragment?: string,
            interwiki?: string,
        ): null | Title;
        newFromFileName(uncleanName: string): null | Title;
        newFromText(title: string, namespace?: number): null | Title;
        newFromUserInput(
            title: string,
            defaultNamespace?: number,
            options?: { forUploading?: true },
        ): null | Title;
        normalize(title: string, options?: TitleNormalizeOptions): null | string;
        normalizeExtension(extension: string): string;
        normalizeUsername(username: string): null | string;
        phpCharToLower(chr: string): string;
        phpCharToUpper(chr: string): string;
        uc(str: string): string;
    }
    Index

    Constructors

    • Creates a new Title instance.

      Usage:

      const title = new mwbot.Title('Page title');
      

      Parameters

      • title: string

        The title of the page. If no namespace is provided, this will be analyzed to determine if it includes a namespace prefix.

      • Optionalnamespace: number

        The default namespace to use for the given title. (Default: NS_MAIN)

      Returns Title

      If the provided title is invalid.

    Properties

    exist: {
        pages: Record<string, boolean>;
        set: (titles: string | string[], state?: boolean) => boolean;
    }

    Object used by exists.

    Type declaration

    • pages: Record<string, boolean>

      Keyed by title. Boolean true value indicates page does exist.

    • set: (titles: string | string[], state?: boolean) => boolean

      The setter function. Returns a boolean.

      Example to declare existing titles:

      Title.exist.set(['User:John_Doe', ...]);
      

      Example to declare titles nonexistent:

      Title.exist.set(['File:Foo_bar.jpg', ...], false);
      

    Methods

    • Remove unicode bidirectional characters and trim a string.

      "Unicode bidirectional characters" are characters that can slip into cut-and-pasted texts, represented as red dots in WikiEditor.

      This method is exclusive to mwbot-ts.

      Parameters

      • str: string

        Input string.

      • Optionaltrim: boolean

        Whether to trim the string. Defaults to true.

      Returns string

    • Check whether this title exists on the wiki.

      Parameters

      • title: string | Title

        Prefixed DB title (string) or instance of Title.

      Returns null | boolean

      Boolean if the information is available, otherwise null.

      If title is not a string or Title.

    • Check if a given namespace is a talk namespace.

      Parameters

      • namespaceId: number

        The namespace ID.

      Returns boolean

      A boolean indicating whether the namespace is a talk namespace.

    • Converts all the alphabetic characters in a string to lowercase, as in PHP.

      This method is exclusive to mwbot-ts.

      Parameters

      • str: string

      Returns string

    • Constructor for Title objects with predefined namespace.

      Unlike newFromText or the constructor, this method doesn't allow the given namespace to be overridden by a namespace prefix in title. See the class desciprtion for details about this behavior.

      The single exception to this is when namespace is 0, indicating the main namespace. The function behaves like newFromText in that case.

      Parameters

      • namespace: number

        Namespace to use for the title.

      • title: string

        The unprefixed title.

      • Optionalfragment: string

        The link fragment (after the "#").

        This parameter is exclusive to mwbot-ts.

      • Optionalinterwiki: string

        The interwiki prefix.

        This parameter is exclusive to mwbot-ts.

      Returns null | Title

      A valid Title object or null if the title is invalid.

    • Sanitizes a file name as supplied by the user, originating in the user's file system so it is most likely a valid MediaWiki title and file name after processing.

      Parameters

      • uncleanName: string

        The unclean file name including file extension but without namespace.

      Returns null | Title

      A valid Title object or null if the title is invalid.

    • Constructor for Title objects with a null return instead of an exception for invalid titles.

      Note that namespace is the default namespace only, and can be overridden by a namespace prefix in title. If you do not want this behavior, use makeTitle. See the class desciprtion for details.

      Parameters

      • title: string
      • Optionalnamespace: number

        Default namespace. (Default: NS_MAIN)

      Returns null | Title

      A valid Title object or null if the title is invalid.

    • Constructor for Title objects from user input altering that input to produce a title that MediaWiki will accept as legal.

      Parameters

      • title: string

        The title, optionally namespace-prefixed (NOTE: interwiki prefixes are disallowed).

      • OptionaldefaultNamespace: number

        If given, used as default namespace for the given title.

      • Optionaloptions: { forUploading?: true }

        Additional options.

        • OptionalforUploading?: true

          (Default: true)

          Makes sure that a file is uploadable under the title returned. There are pages in the file namespace under which file upload is impossible. Automatically assumed if the title is created in the Media namespace.

      Returns null | Title

      A valid Title object or null if the input cannot be turned into a valid title.

    • Normalizes a title to its canonical form.

      This capitalizes the first character of the base (unprefixed) title and localizes the namespace according to the wiki’s configuration.

      This method is exclusive to mwbot-ts.

      Parameters

      • title: string

        The title to normalize.

      • Optionaloptions: TitleNormalizeOptions

        Options that control how the title is normalized.

      Returns null | string

      The normalized title as a string, or null if the input is not a valid title.

      If title is not a string.

    • Normalize a file extension to the common form, making it lowercase and checking some synonyms, and ensure it's clean. Extensions with non-alphanumeric characters will be discarded.

      Parameters

      • extension: string

        File extension (without the leading dot).

      Returns string

      File extension in canonical form.

    • Normalizes a username by capitalizing its first letter, following MediaWiki conventions. IP addresses are capitalized with all hexadecimal segments spelled out (e.g., 192.168.0.1 for IPv4 and FD12:3456:789A:1:0:0:0:0 for IPv6).

      This method is exclusive to mwbot-ts.

      Parameters

      • username: string

        The username to normalize.

      Returns null | string

      The normalized username, or null if the input contains characters that are not allowed in usernames.

    • De-capitalizes a character as in PHP.

      This method handles the difference between PHP's strtolower and JS's String.toLowerCase (see phab:T147646).

      This method is exclusive to mwbot-ts.

      Parameters

      • chr: string

        Unicode character.

      Returns string

      Unicode character, in lower case, according to the same rules as in PHP.

    • Capitalizes a character as in PHP.

      This method handles the difference between PHP's strtoupper and JS's String.toUpperCase (see phab:T147646).

      Parameters

      • chr: string

        Unicode character.

      Returns string

      Unicode character, in upper case, according to the same rules as in PHP.

    • Converts all the alphabetic characters in a string to uppercase, as in PHP.

      This method is exclusive to mwbot-ts.

      Parameters

      • str: string

      Returns string