Manual:Title.php
MediaWiki file: Title.php | |
---|---|
Location: | includes/title/ |
Source code: | master • 1.42.3 • 1.41.4 • 1.39.10 |
Classes: | MediaWiki\Title\Title |
Before 1.40:
MediaWiki file: Title.php | |
---|---|
Location: | includes/ |
Source code: | master • 1.42.3 • 1.41.4 • 1.39.10 |
Classes: | Title |
The Title
class represents page titles.
Title
implements the following interfaces: LinkTarget , PageIdentity- It can be instantiated with one of the factory methods below.
- Once instantiated, the title can be retrieved in other formats or queried for its attributes. Various non-static accessor methods are available for this, such as
getText()
,getDBKey()
,getNamespace()
, etc. Many of these are used by Manual:CoreParserFunctions.php to generate magic words. - It is intended to be an immutable "value" class, so there are no mutator functions.[1][2]
Retrieval
editMethods for accessing an instance of Title include :
Public methods
editInstantiation methods
editTo instantiate Title
, call one of the static factory methods:
Title::newFromDBKey( $key )
Title::newFromID( ($id, $flags=0) )
. Note that an alternative method,Title::newFromIDs( $ids )
, is deprecated in favour of using a PageStore QueryBuilder.Title::newFromText( $text, $defaultNamespace=NS_MAIN )
Title::newFromURL( $url )
- This is not the function you want. Use Title::newFromText().Title::makeTitleSafe( $ns, $title, $fragment= '', $interwiki= '')
- Create a new Title from a namespace index and a DB key. Returns null on an error.Title::newFromRow( $row )
Title::newMainPage()
- create a new Title for the Main Page.
Example
editTo check and see if a given page already exists:
$titleObject = Title::newFromText( 'Talk:Your desired title here' );
if ( !$titleObject->exists() ) echo "There is no page with that name.";
Create a new Title from text, such as what one would find in a link. Decodes any HTML entities in the text. Spaces, prefixes, and an initial ':' indicating the main namespace are accepted. Note that if the page does not exist, this will not create it. For that, see Manual:Article.php .
Relating to page titles
edit- getDBKey() - get the main part (i.e. the title excluding namespace) with underscores.
- getText() - get the text form (spaces not underscores) of the main part, i.e. the title excluding namespace, but including slashes and everything after. See magic word {{PAGENAME }}.
- Prefixed with namespace and interwiki prefixes, if any:
- getPrefixedDBkey() - get the prefixed database key form with underscores.
- getPrefixedText() - get the prefixed title with spaces. This is the form usually used for display. See magic word {{FULLPAGENAME }}.
- __toString() - return a string representation of the current title.
- getFullText() - get the prefixed title with spaces, plus any fragment (i.e. the part beginning with '#').
- getRootText() - get the root page name text without a namespace, i.e. the leftmost part before any slashes.
- getRootTitle() - get the root page name title, i.e. the leftmost part before any slashes.
- getBaseText() - get the base page name, i.e. the leftmost part excluding namespace and before any slashes (before the subpage name), without converting spaces to underscores. See magic words {{BASEPAGENAME }} and {{BASEPAGENAMEE }}.
- getBaseTitle() - get the base page name title, i.e. the part before the subpage name.
Relating to page titles (URL-encoded)
editURL-encoded titles, not actual URLs:
- getPrefixedUrl() - get a URL-encoded title, including interwiki. See magic word {{FULLPAGENAMEE }}.
- getPartialURL() - get the URL-encoded form of the main part. See magic word {{PAGENAMEE }}.
- getSubpageUrlForm() - get a URL-encoded form of the subpage text. See magic word {{SUBPAGENAMEE }}.
Relating to page URLs
edit- getFullURL() - get a real URL referring to this title, with interwiki link and fragment.
- getFullUrlForRedirect()
- getLocalURL() - get a URL with no fragment or server name.
- getLinkURL()
- getInternalURL()
- getCanonicalURL() - get the URL for a canonical link, for use in things like IRC and e-mail notifications. Uses $wgCanonicalServer and the GetCanonicalURL hook.
- getEditURL() - get the edit URL (with action=edit) for this Title
Checking page titles
edit- equals() - compare by checking if the namespace prefix, title text and interwiki prefix are the same.
- isSameLinkAs() - similar to equals(), but additionally compares the fragment part. See also LinkTarget::isSameLinkAs() .
Relating to page title fragments
edit- getFragment() - get the Title fragment (i.e. the bit after the #) in text form.
- hasFragment()
- getFragmentForURL()
- setFragment()
- createFragmentTarget() - creates a new Title for a different fragment of the same page.
Relating to namespaces
edit- getNamespace() - get the namespace index. See {{NAMESPACENUMBER }}.
- getNamespaceKey() - generate strings used for xml
id
names in monobook tags. - getNsText() - get the namespace text of the page. See magic words {{NAMESPACE }} and {{NAMESPACEE }}.
For other options, see NamespaceInfo instead.
Relating to talk pages
edit- getTalkPageIfDefined() - get a Title object associated with the talk page of the current article if such a talk page can exist. See {{TALKPAGENAME }} and {{TALKPAGENAMEE }}. getTalkPage() is deprecated since 1.34 (in favour of getTalkPageIfDefined).
- isTalkPage() - check current page
- getTalkNsText() - get the namespace text of the talk page. See magic words {{TALKSPACE }} and {{TALKSPACEE }}.
- getSubjectNsText() - get the namespace text of the subject (rather than talk) page. See magic words {{SUBJECTSPACE }} and {{SUBJECTSPACEE }}.
- getSubjectPage() - used to get a title object associated with the subject page of this talk page - is deprecated since 1.34 in favour of NamespaceInfo::getSubjectPage . See also the magic word {{SUBJECTPAGENAME }}.
Relating to categories
edit- getParentCategories() - get categories to which the current Title belongs.
- getParentCategoryTree() - get a tree of parent categories.
- getCategorySortkey() - returns the raw sort key to be used for categories, with the specified prefix. This will be fed to Collation::getSortKey() to get a binary sortkey that can be used for actual sorting.
- canUseNoindex() - whether the magic words __INDEX__ and __NOINDEX__ function for this page.
- isRedirect() - check if the article is a redirect page
- getRedirectsHere() - get all extant redirects to the current Title.
- isValidRedirectTarget() - check if the current Title is a valid redirect target.
- isSingleRevRedirect() - locks the page row and check if this page is single revision redirect.
- getSubpage() - get the title for a subpage of the current page.
- getSubpageText() - get the lowest-level subpage name, i.e. the rightmost part after any slashes. See magic word {{SUBPAGENAME }}.
- hasSubpages() - check if the current page has subpages.
- getSubpages() - get all subpages of this page.
- isSubpageOf() - check if the current title is a subpage of another title.
- See also: getSubpageUrlForm().
Relating to incoming and outgoing links
editHandle with care:
- getLinksTo
- getTemplateLinksTo()
- getLinksFrom()
- getTemplateLinksFrom()
- getBrokenLinksFrom()
Relating to page language
edit- getPageLanguage() - get the language in which the content of this page is written in wikitext. Defaults to content language, but in certain cases it can be e.g. $wgLang (such as special pages, which are in the user language).
- getPageViewLanguage() - get the language in which the content of this page is written when viewed by user. Defaults to content language, but in certain cases it can be e.g. $wgLang (such as special pages, which are in the user language). Will be deprecated in 1.42 in favour of ParserOutput::getLanguage and see also OutputPage::getContLangForJS .
Relating to page revisions , etc.
edit- exists() - check if page exists
- isNewPage() - check if the current page is new.
- purgeExpiredRestrictions()
- estimateRevisionCount() - get the approximate revision count of the current page.
- getDeletedEditsCount - count archived revisions.
- hasDeletedEdits() - check if there is a version of the current page in the deletion archive.
- getEditNotices() - get a list of rendered edit notices for this page.
Relating to page_touched
/timestamps :
- invalidateCache() - updates
page_touched
for the current page. It is called from LinksUpdate.php . - touchLinks() - update
page_touched
timestamps and send CDN purge messages for pages linking to this title. May be sent to the job queue depending on the number of links. Typically called on create and delete. - getTouched() - get the latest touched timestamp
Relating to viewability
edit- isAlwaysKnown() - semi-deprecated for public use
- isKnown() - check if the current Title refers to a page that can (or might) be meaningfully viewed?
- hasSourceText() - check if the current page has any source text.
- getDefaultMessageText() - Get the default (plain) message contents for an page that overrides an interface message key.
Link cache, page IDs, etc.
edit- getArticleID() - get the article ID for this Title from the link cache, adding it if necessary. For "internal" articles, it should return the "page_id" field if the article exists, else it returns 0. For all external articles it returns 0. All of the IDs for all instances of Title created during a request are cached, so they can be looked up quickly while rendering wikitext with lots of internal links.
- resetArticleID() - inject a page ID, reset DB-loaded fields, and clear the link cache for the current Title.
- clearCaches() - clear link cache as well as Title cache.
- getLatestRevID() - get the
page_latest
field. - getLength() - get page length.
- pageCond() - get an associative array for selecting the current Title from the page table.
- getWikiId()
- getId() - get the page ID.
- toPageIdentity() - return the page represented by the current Title as a ProperPageIdentity .
- toPageRecord() - return the page represented by the current Title as a ProperPageRecord .
Using the Title object
editIn order to create objects or access data, many methods in other MediaWiki PHP classes depend on the availability of a Title object.
For example, if you have a Title object, you can get a:
- Article object, using
Article::newFromTitle ( $title, IContextSource $context )
- WikiPage object, using WikiPageFactory
This class can fetch various kinds of data from the database; however, it does so inefficiently. Consider using a TitleValue object instead. TitleValue is more lightweight and does not rely on global state or the database.
See also
edit- Manual:$wgLegalTitleChars
- Manual:TitleValue.php
- BacklinkCacheFactory::getBacklinkCache()
- Manual:Modeling pages
- Help:Bad title
- Category:MediaWiki hooks included in Title.php
References
edit- ↑ Mutable is a key word of the norm saying that even a const object can modify this variable. A mutable attribute can be private or public. It can always be modified even when the object is in a constant context.
- ↑ Mutator is the general name of a member function which can modify one or more object private variables from outside. It is useless to set it private.