3 * Representation of a title within %MediaWiki.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
24 use MediaWiki\Linker\LinkTarget
;
25 use MediaWiki\MediaWikiServices
;
28 * Represents a title within MediaWiki.
29 * Optionally may contain an interwiki designation or namespace.
30 * @note This class can fetch various kinds of data from the database;
31 * however, it does so inefficiently.
32 * @note Consider using a TitleValue object instead. TitleValue is more lightweight
33 * and does not rely on global state or the database.
35 class Title
implements LinkTarget
{
36 /** @var HashBagOStuff */
37 static private $titleCache = null;
40 * Title::newFromText maintains a cache to avoid expensive re-normalization of
41 * commonly used titles. On a batch operation this can become a memory leak
42 * if not bounded. After hitting this many titles reset the cache.
44 const CACHE_MAX
= 1000;
47 * Used to be GAID_FOR_UPDATE define. Used with getArticleID() and friends
48 * to use the master DB
50 const GAID_FOR_UPDATE
= 1;
53 * @name Private member variables
54 * Please use the accessor functions instead.
59 /** @var string Text form (spaces not underscores) of the main part */
60 public $mTextform = '';
62 /** @var string URL-encoded form of the main part */
63 public $mUrlform = '';
65 /** @var string Main part with underscores */
66 public $mDbkeyform = '';
68 /** @var string Database key with the initial letter in the case specified by the user */
69 protected $mUserCaseDBKey;
71 /** @var int Namespace index, i.e. one of the NS_xxxx constants */
72 public $mNamespace = NS_MAIN
;
74 /** @var string Interwiki prefix */
75 public $mInterwiki = '';
77 /** @var bool Was this Title created from a string with a local interwiki prefix? */
78 private $mLocalInterwiki = false;
80 /** @var string Title fragment (i.e. the bit after the #) */
81 public $mFragment = '';
83 /** @var int Article ID, fetched from the link cache on demand */
84 public $mArticleID = -1;
86 /** @var bool|int ID of most recent revision */
87 protected $mLatestID = false;
90 * @var bool|string ID of the page's content model, i.e. one of the
91 * CONTENT_MODEL_XXX constants
93 public $mContentModel = false;
95 /** @var int Estimated number of revisions; null of not loaded */
96 private $mEstimateRevisions;
98 /** @var array Array of groups allowed to edit this article */
99 public $mRestrictions = [];
101 /** @var string|bool */
102 protected $mOldRestrictions = false;
104 /** @var bool Cascade restrictions on this page to included templates and images? */
105 public $mCascadeRestriction;
107 /** Caching the results of getCascadeProtectionSources */
108 public $mCascadingRestrictions;
110 /** @var array When do the restrictions on this page expire? */
111 protected $mRestrictionsExpiry = [];
113 /** @var bool Are cascading restrictions in effect on this page? */
114 protected $mHasCascadingRestrictions;
116 /** @var array Where are the cascading restrictions coming from on this page? */
117 public $mCascadeSources;
119 /** @var bool Boolean for initialisation on demand */
120 public $mRestrictionsLoaded = false;
122 /** @var string Text form including namespace/interwiki, initialised on demand */
123 protected $mPrefixedText = null;
125 /** @var mixed Cached value for getTitleProtection (create protection) */
126 public $mTitleProtection;
129 * @var int Namespace index when there is no namespace. Don't change the
130 * following default, NS_MAIN is hardcoded in several places. See bug 696.
131 * Zero except in {{transclusion}} tags.
133 public $mDefaultNamespace = NS_MAIN
;
135 /** @var int The page length, 0 for special pages */
136 protected $mLength = -1;
138 /** @var null Is the article at this title a redirect? */
139 public $mRedirect = null;
141 /** @var array Associative array of user ID -> timestamp/false */
142 private $mNotificationTimestamp = [];
144 /** @var bool Whether a page has any subpages */
145 private $mHasSubpages;
147 /** @var bool The (string) language code of the page's language and content code. */
148 private $mPageLanguage = false;
150 /** @var string|bool|null The page language code from the database, null if not saved in
151 * the database or false if not loaded, yet. */
152 private $mDbPageLanguage = false;
154 /** @var TitleValue A corresponding TitleValue object */
155 private $mTitleValue = null;
157 /** @var bool Would deleting this page be a big deletion? */
158 private $mIsBigDeletion = null;
162 * B/C kludge: provide a TitleParser for use by Title.
163 * Ideally, Title would have no methods that need this.
164 * Avoid usage of this singleton by using TitleValue
165 * and the associated services when possible.
167 * @return TitleFormatter
169 private static function getTitleFormatter() {
170 return MediaWikiServices
::getInstance()->getTitleFormatter();
176 function __construct() {
180 * Create a new Title from a prefixed DB key
182 * @param string $key The database key, which has underscores
183 * instead of spaces, possibly including namespace and
185 * @return Title|null Title, or null on an error
187 public static function newFromDBkey( $key ) {
189 $t->mDbkeyform
= $key;
192 $t->secureAndSplit();
194 } catch ( MalformedTitleException
$ex ) {
200 * Create a new Title from a TitleValue
202 * @param TitleValue $titleValue Assumed to be safe.
206 public static function newFromTitleValue( TitleValue
$titleValue ) {
207 return self
::newFromLinkTarget( $titleValue );
211 * Create a new Title from a LinkTarget
213 * @param LinkTarget $linkTarget Assumed to be safe.
217 public static function newFromLinkTarget( LinkTarget
$linkTarget ) {
218 if ( $linkTarget instanceof Title
) {
219 // Special case if it's already a Title object
222 return self
::makeTitle(
223 $linkTarget->getNamespace(),
224 $linkTarget->getText(),
225 $linkTarget->getFragment(),
226 $linkTarget->getInterwiki()
231 * Create a new Title from text, such as what one would find in a link. De-
232 * codes any HTML entities in the text.
234 * @param string|int|null $text The link text; spaces, prefixes, and an
235 * initial ':' indicating the main namespace are accepted.
236 * @param int $defaultNamespace The namespace to use if none is specified
237 * by a prefix. If you want to force a specific namespace even if
238 * $text might begin with a namespace prefix, use makeTitle() or
240 * @throws InvalidArgumentException
241 * @return Title|null Title or null on an error.
243 public static function newFromText( $text, $defaultNamespace = NS_MAIN
) {
244 // DWIM: Integers can be passed in here when page titles are used as array keys.
245 if ( $text !== null && !is_string( $text ) && !is_int( $text ) ) {
246 throw new InvalidArgumentException( '$text must be a string.' );
248 if ( $text === null ) {
253 return Title
::newFromTextThrow( strval( $text ), $defaultNamespace );
254 } catch ( MalformedTitleException
$ex ) {
260 * Like Title::newFromText(), but throws MalformedTitleException when the title is invalid,
261 * rather than returning null.
263 * The exception subclasses encode detailed information about why the title is invalid.
265 * @see Title::newFromText
268 * @param string $text Title text to check
269 * @param int $defaultNamespace
270 * @throws MalformedTitleException If the title is invalid
273 public static function newFromTextThrow( $text, $defaultNamespace = NS_MAIN
) {
274 if ( is_object( $text ) ) {
275 throw new MWException( '$text must be a string, given an object' );
278 $titleCache = self
::getTitleCache();
280 // Wiki pages often contain multiple links to the same page.
281 // Title normalization and parsing can become expensive on pages with many
282 // links, so we can save a little time by caching them.
283 // In theory these are value objects and won't get changed...
284 if ( $defaultNamespace == NS_MAIN
) {
285 $t = $titleCache->get( $text );
291 // Convert things like é ā or 〗 into normalized (bug 14952) text
292 $filteredText = Sanitizer
::decodeCharReferencesAndNormalize( $text );
295 $t->mDbkeyform
= strtr( $filteredText, ' ', '_' );
296 $t->mDefaultNamespace
= intval( $defaultNamespace );
298 $t->secureAndSplit();
299 if ( $defaultNamespace == NS_MAIN
) {
300 $titleCache->set( $text, $t );
306 * THIS IS NOT THE FUNCTION YOU WANT. Use Title::newFromText().
308 * Example of wrong and broken code:
309 * $title = Title::newFromURL( $wgRequest->getVal( 'title' ) );
311 * Example of right code:
312 * $title = Title::newFromText( $wgRequest->getVal( 'title' ) );
314 * Create a new Title from URL-encoded text. Ensures that
315 * the given title's length does not exceed the maximum.
317 * @param string $url The title, as might be taken from a URL
318 * @return Title|null The new object, or null on an error
320 public static function newFromURL( $url ) {
323 # For compatibility with old buggy URLs. "+" is usually not valid in titles,
324 # but some URLs used it as a space replacement and they still come
325 # from some external search tools.
326 if ( strpos( self
::legalChars(), '+' ) === false ) {
327 $url = strtr( $url, '+', ' ' );
330 $t->mDbkeyform
= strtr( $url, ' ', '_' );
333 $t->secureAndSplit();
335 } catch ( MalformedTitleException
$ex ) {
341 * @return HashBagOStuff
343 private static function getTitleCache() {
344 if ( self
::$titleCache == null ) {
345 self
::$titleCache = new HashBagOStuff( [ 'maxKeys' => self
::CACHE_MAX
] );
347 return self
::$titleCache;
351 * Returns a list of fields that are to be selected for initializing Title
352 * objects or LinkCache entries. Uses $wgContentHandlerUseDB to determine
353 * whether to include page_content_model.
357 protected static function getSelectFields() {
358 global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
361 'page_namespace', 'page_title', 'page_id',
362 'page_len', 'page_is_redirect', 'page_latest',
365 if ( $wgContentHandlerUseDB ) {
366 $fields[] = 'page_content_model';
369 if ( $wgPageLanguageUseDB ) {
370 $fields[] = 'page_lang';
377 * Create a new Title from an article ID
379 * @param int $id The page_id corresponding to the Title to create
380 * @param int $flags Use Title::GAID_FOR_UPDATE to use master
381 * @return Title|null The new object, or null on an error
383 public static function newFromID( $id, $flags = 0 ) {
384 $db = ( $flags & self
::GAID_FOR_UPDATE
) ?
wfGetDB( DB_MASTER
) : wfGetDB( DB_SLAVE
);
385 $row = $db->selectRow(
387 self
::getSelectFields(),
388 [ 'page_id' => $id ],
391 if ( $row !== false ) {
392 $title = Title
::newFromRow( $row );
400 * Make an array of titles from an array of IDs
402 * @param int[] $ids Array of IDs
403 * @return Title[] Array of Titles
405 public static function newFromIDs( $ids ) {
406 if ( !count( $ids ) ) {
409 $dbr = wfGetDB( DB_SLAVE
);
413 self
::getSelectFields(),
414 [ 'page_id' => $ids ],
419 foreach ( $res as $row ) {
420 $titles[] = Title
::newFromRow( $row );
426 * Make a Title object from a DB row
428 * @param stdClass $row Object database row (needs at least page_title,page_namespace)
429 * @return Title Corresponding Title
431 public static function newFromRow( $row ) {
432 $t = self
::makeTitle( $row->page_namespace
, $row->page_title
);
433 $t->loadFromRow( $row );
438 * Load Title object fields from a DB row.
439 * If false is given, the title will be treated as non-existing.
441 * @param stdClass|bool $row Database row
443 public function loadFromRow( $row ) {
444 if ( $row ) { // page found
445 if ( isset( $row->page_id
) ) {
446 $this->mArticleID
= (int)$row->page_id
;
448 if ( isset( $row->page_len
) ) {
449 $this->mLength
= (int)$row->page_len
;
451 if ( isset( $row->page_is_redirect
) ) {
452 $this->mRedirect
= (bool)$row->page_is_redirect
;
454 if ( isset( $row->page_latest
) ) {
455 $this->mLatestID
= (int)$row->page_latest
;
457 if ( isset( $row->page_content_model
) ) {
458 $this->mContentModel
= strval( $row->page_content_model
);
460 $this->mContentModel
= false; # initialized lazily in getContentModel()
462 if ( isset( $row->page_lang
) ) {
463 $this->mDbPageLanguage
= (string)$row->page_lang
;
465 if ( isset( $row->page_restrictions
) ) {
466 $this->mOldRestrictions
= $row->page_restrictions
;
468 } else { // page not found
469 $this->mArticleID
= 0;
471 $this->mRedirect
= false;
472 $this->mLatestID
= 0;
473 $this->mContentModel
= false; # initialized lazily in getContentModel()
478 * Create a new Title from a namespace index and a DB key.
479 * It's assumed that $ns and $title are *valid*, for instance when
480 * they came directly from the database or a special page name.
481 * For convenience, spaces are converted to underscores so that
482 * eg user_text fields can be used directly.
484 * @param int $ns The namespace of the article
485 * @param string $title The unprefixed database key form
486 * @param string $fragment The link fragment (after the "#")
487 * @param string $interwiki The interwiki prefix
488 * @return Title The new object
490 public static function &makeTitle( $ns, $title, $fragment = '', $interwiki = '' ) {
492 $t->mInterwiki
= $interwiki;
493 $t->mFragment
= $fragment;
494 $t->mNamespace
= $ns = intval( $ns );
495 $t->mDbkeyform
= strtr( $title, ' ', '_' );
496 $t->mArticleID
= ( $ns >= 0 ) ?
-1 : 0;
497 $t->mUrlform
= wfUrlencode( $t->mDbkeyform
);
498 $t->mTextform
= strtr( $title, '_', ' ' );
499 $t->mContentModel
= false; # initialized lazily in getContentModel()
504 * Create a new Title from a namespace index and a DB key.
505 * The parameters will be checked for validity, which is a bit slower
506 * than makeTitle() but safer for user-provided data.
508 * @param int $ns The namespace of the article
509 * @param string $title Database key form
510 * @param string $fragment The link fragment (after the "#")
511 * @param string $interwiki Interwiki prefix
512 * @return Title|null The new object, or null on an error
514 public static function makeTitleSafe( $ns, $title, $fragment = '', $interwiki = '' ) {
515 if ( !MWNamespace
::exists( $ns ) ) {
520 $t->mDbkeyform
= Title
::makeName( $ns, $title, $fragment, $interwiki, true );
523 $t->secureAndSplit();
525 } catch ( MalformedTitleException
$ex ) {
531 * Create a new Title for the Main Page
533 * @return Title The new object
535 public static function newMainPage() {
536 $title = Title
::newFromText( wfMessage( 'mainpage' )->inContentLanguage()->text() );
537 // Don't give fatal errors if the message is broken
539 $title = Title
::newFromText( 'Main Page' );
545 * Get the prefixed DB key associated with an ID
547 * @param int $id The page_id of the article
548 * @return Title|null An object representing the article, or null if no such article was found
550 public static function nameOf( $id ) {
551 $dbr = wfGetDB( DB_SLAVE
);
553 $s = $dbr->selectRow(
555 [ 'page_namespace', 'page_title' ],
556 [ 'page_id' => $id ],
559 if ( $s === false ) {
563 $n = self
::makeName( $s->page_namespace
, $s->page_title
);
568 * Get a regex character class describing the legal characters in a link
570 * @return string The list of characters, not delimited
572 public static function legalChars() {
573 global $wgLegalTitleChars;
574 return $wgLegalTitleChars;
578 * Returns a simple regex that will match on characters and sequences invalid in titles.
579 * Note that this doesn't pick up many things that could be wrong with titles, but that
580 * replacing this regex with something valid will make many titles valid.
582 * @deprecated since 1.25, use MediaWikiTitleCodec::getTitleInvalidRegex() instead
584 * @return string Regex string
586 static function getTitleInvalidRegex() {
587 wfDeprecated( __METHOD__
, '1.25' );
588 return MediaWikiTitleCodec
::getTitleInvalidRegex();
592 * Utility method for converting a character sequence from bytes to Unicode.
594 * Primary usecase being converting $wgLegalTitleChars to a sequence usable in
595 * javascript, as PHP uses UTF-8 bytes where javascript uses Unicode code units.
597 * @param string $byteClass
600 public static function convertByteClassToUnicodeClass( $byteClass ) {
601 $length = strlen( $byteClass );
603 $x0 = $x1 = $x2 = '';
605 $d0 = $d1 = $d2 = '';
606 // Decoded integer codepoints
607 $ord0 = $ord1 = $ord2 = 0;
609 $r0 = $r1 = $r2 = '';
613 $allowUnicode = false;
614 for ( $pos = 0; $pos < $length; $pos++
) {
615 // Shift the queues down
624 // Load the current input token and decoded values
625 $inChar = $byteClass[$pos];
626 if ( $inChar == '\\' ) {
627 if ( preg_match( '/x([0-9a-fA-F]{2})/A', $byteClass, $m, 0, $pos +
1 ) ) {
628 $x0 = $inChar . $m[0];
629 $d0 = chr( hexdec( $m[1] ) );
630 $pos +
= strlen( $m[0] );
631 } elseif ( preg_match( '/[0-7]{3}/A', $byteClass, $m, 0, $pos +
1 ) ) {
632 $x0 = $inChar . $m[0];
633 $d0 = chr( octdec( $m[0] ) );
634 $pos +
= strlen( $m[0] );
635 } elseif ( $pos +
1 >= $length ) {
638 $d0 = $byteClass[$pos +
1];
646 // Load the current re-encoded value
647 if ( $ord0 < 32 ||
$ord0 == 0x7f ) {
648 $r0 = sprintf( '\x%02x', $ord0 );
649 } elseif ( $ord0 >= 0x80 ) {
650 // Allow unicode if a single high-bit character appears
651 $r0 = sprintf( '\x%02x', $ord0 );
652 $allowUnicode = true;
653 } elseif ( strpos( '-\\[]^', $d0 ) !== false ) {
659 if ( $x0 !== '' && $x1 === '-' && $x2 !== '' ) {
661 if ( $ord2 > $ord0 ) {
663 } elseif ( $ord0 >= 0x80 ) {
665 $allowUnicode = true;
666 if ( $ord2 < 0x80 ) {
667 // Keep the non-unicode section of the range
674 // Reset state to the initial value
675 $x0 = $x1 = $d0 = $d1 = $r0 = $r1 = '';
676 } elseif ( $ord2 < 0x80 ) {
681 if ( $ord1 < 0x80 ) {
684 if ( $ord0 < 0x80 ) {
687 if ( $allowUnicode ) {
688 $out .= '\u0080-\uFFFF';
694 * Make a prefixed DB key from a DB key and a namespace index
696 * @param int $ns Numerical representation of the namespace
697 * @param string $title The DB key form the title
698 * @param string $fragment The link fragment (after the "#")
699 * @param string $interwiki The interwiki prefix
700 * @param bool $canonicalNamespace If true, use the canonical name for
701 * $ns instead of the localized version.
702 * @return string The prefixed form of the title
704 public static function makeName( $ns, $title, $fragment = '', $interwiki = '',
705 $canonicalNamespace = false
709 if ( $canonicalNamespace ) {
710 $namespace = MWNamespace
::getCanonicalName( $ns );
712 $namespace = $wgContLang->getNsText( $ns );
714 $name = $namespace == '' ?
$title : "$namespace:$title";
715 if ( strval( $interwiki ) != '' ) {
716 $name = "$interwiki:$name";
718 if ( strval( $fragment ) != '' ) {
719 $name .= '#' . $fragment;
725 * Escape a text fragment, say from a link, for a URL
727 * @param string $fragment Containing a URL or link fragment (after the "#")
728 * @return string Escaped string
730 static function escapeFragmentForURL( $fragment ) {
731 # Note that we don't urlencode the fragment. urlencoded Unicode
732 # fragments appear not to work in IE (at least up to 7) or in at least
733 # one version of Opera 9.x. The W3C validator, for one, doesn't seem
734 # to care if they aren't encoded.
735 return Sanitizer
::escapeId( $fragment, 'noninitial' );
739 * Callback for usort() to do title sorts by (namespace, title)
744 * @return int Result of string comparison, or namespace comparison
746 public static function compare( $a, $b ) {
747 if ( $a->getNamespace() == $b->getNamespace() ) {
748 return strcmp( $a->getText(), $b->getText() );
750 return $a->getNamespace() - $b->getNamespace();
755 * Determine whether the object refers to a page within
756 * this project (either this wiki or a wiki with a local
757 * interwiki, see https://www.mediawiki.org/wiki/Manual:Interwiki_table#iw_local )
759 * @return bool True if this is an in-project interwiki link or a wikilink, false otherwise
761 public function isLocal() {
762 if ( $this->isExternal() ) {
763 $iw = Interwiki
::fetch( $this->mInterwiki
);
765 return $iw->isLocal();
772 * Is this Title interwiki?
776 public function isExternal() {
777 return $this->mInterwiki
!== '';
781 * Get the interwiki prefix
783 * Use Title::isExternal to check if a interwiki is set
785 * @return string Interwiki prefix
787 public function getInterwiki() {
788 return $this->mInterwiki
;
792 * Was this a local interwiki link?
796 public function wasLocalInterwiki() {
797 return $this->mLocalInterwiki
;
801 * Determine whether the object refers to a page within
802 * this project and is transcludable.
804 * @return bool True if this is transcludable
806 public function isTrans() {
807 if ( !$this->isExternal() ) {
811 return Interwiki
::fetch( $this->mInterwiki
)->isTranscludable();
815 * Returns the DB name of the distant wiki which owns the object.
817 * @return string The DB name
819 public function getTransWikiID() {
820 if ( !$this->isExternal() ) {
824 return Interwiki
::fetch( $this->mInterwiki
)->getWikiID();
828 * Get a TitleValue object representing this Title.
830 * @note Not all valid Titles have a corresponding valid TitleValue
831 * (e.g. TitleValues cannot represent page-local links that have a
832 * fragment but no title text).
834 * @return TitleValue|null
836 public function getTitleValue() {
837 if ( $this->mTitleValue
=== null ) {
839 $this->mTitleValue
= new TitleValue(
840 $this->getNamespace(),
842 $this->getFragment(),
843 $this->getInterwiki()
845 } catch ( InvalidArgumentException
$ex ) {
846 wfDebug( __METHOD__
. ': Can\'t create a TitleValue for [[' .
847 $this->getPrefixedText() . ']]: ' . $ex->getMessage() . "\n" );
851 return $this->mTitleValue
;
855 * Get the text form (spaces not underscores) of the main part
857 * @return string Main part of the title
859 public function getText() {
860 return $this->mTextform
;
864 * Get the URL-encoded form of the main part
866 * @return string Main part of the title, URL-encoded
868 public function getPartialURL() {
869 return $this->mUrlform
;
873 * Get the main part with underscores
875 * @return string Main part of the title, with underscores
877 public function getDBkey() {
878 return $this->mDbkeyform
;
882 * Get the DB key with the initial letter case as specified by the user
884 * @return string DB key
886 function getUserCaseDBKey() {
887 if ( !is_null( $this->mUserCaseDBKey
) ) {
888 return $this->mUserCaseDBKey
;
890 // If created via makeTitle(), $this->mUserCaseDBKey is not set.
891 return $this->mDbkeyform
;
896 * Get the namespace index, i.e. one of the NS_xxxx constants.
898 * @return int Namespace index
900 public function getNamespace() {
901 return $this->mNamespace
;
905 * Get the page's content model id, see the CONTENT_MODEL_XXX constants.
907 * @param int $flags A bit field; may be Title::GAID_FOR_UPDATE to select for update
908 * @return string Content model id
910 public function getContentModel( $flags = 0 ) {
911 if ( ( !$this->mContentModel ||
$flags === Title
::GAID_FOR_UPDATE
) &&
912 $this->getArticleID( $flags )
914 $linkCache = LinkCache
::singleton();
915 $linkCache->addLinkObj( $this ); # in case we already had an article ID
916 $this->mContentModel
= $linkCache->getGoodLinkFieldObj( $this, 'model' );
919 if ( !$this->mContentModel
) {
920 $this->mContentModel
= ContentHandler
::getDefaultModelFor( $this );
923 return $this->mContentModel
;
927 * Convenience method for checking a title's content model name
929 * @param string $id The content model ID (use the CONTENT_MODEL_XXX constants).
930 * @return bool True if $this->getContentModel() == $id
932 public function hasContentModel( $id ) {
933 return $this->getContentModel() == $id;
937 * Get the namespace text
939 * @return string Namespace text
941 public function getNsText() {
942 if ( $this->isExternal() ) {
943 // This probably shouldn't even happen,
944 // but for interwiki transclusion it sometimes does.
945 // Use the canonical namespaces if possible to try to
946 // resolve a foreign namespace.
947 if ( MWNamespace
::exists( $this->mNamespace
) ) {
948 return MWNamespace
::getCanonicalName( $this->mNamespace
);
953 $formatter = self
::getTitleFormatter();
954 return $formatter->getNamespaceName( $this->mNamespace
, $this->mDbkeyform
);
955 } catch ( InvalidArgumentException
$ex ) {
956 wfDebug( __METHOD__
. ': ' . $ex->getMessage() . "\n" );
962 * Get the namespace text of the subject (rather than talk) page
964 * @return string Namespace text
966 public function getSubjectNsText() {
968 return $wgContLang->getNsText( MWNamespace
::getSubject( $this->mNamespace
) );
972 * Get the namespace text of the talk page
974 * @return string Namespace text
976 public function getTalkNsText() {
978 return $wgContLang->getNsText( MWNamespace
::getTalk( $this->mNamespace
) );
982 * Could this title have a corresponding talk page?
986 public function canTalk() {
987 return MWNamespace
::canTalk( $this->mNamespace
);
991 * Is this in a namespace that allows actual pages?
995 public function canExist() {
996 return $this->mNamespace
>= NS_MAIN
;
1000 * Can this title be added to a user's watchlist?
1004 public function isWatchable() {
1005 return !$this->isExternal() && MWNamespace
::isWatchable( $this->getNamespace() );
1009 * Returns true if this is a special page.
1013 public function isSpecialPage() {
1014 return $this->getNamespace() == NS_SPECIAL
;
1018 * Returns true if this title resolves to the named special page
1020 * @param string $name The special page name
1023 public function isSpecial( $name ) {
1024 if ( $this->isSpecialPage() ) {
1025 list( $thisName, /* $subpage */ ) = SpecialPageFactory
::resolveAlias( $this->getDBkey() );
1026 if ( $name == $thisName ) {
1034 * If the Title refers to a special page alias which is not the local default, resolve
1035 * the alias, and localise the name as necessary. Otherwise, return $this
1039 public function fixSpecialName() {
1040 if ( $this->isSpecialPage() ) {
1041 list( $canonicalName, $par ) = SpecialPageFactory
::resolveAlias( $this->mDbkeyform
);
1042 if ( $canonicalName ) {
1043 $localName = SpecialPageFactory
::getLocalNameFor( $canonicalName, $par );
1044 if ( $localName != $this->mDbkeyform
) {
1045 return Title
::makeTitle( NS_SPECIAL
, $localName );
1053 * Returns true if the title is inside the specified namespace.
1055 * Please make use of this instead of comparing to getNamespace()
1056 * This function is much more resistant to changes we may make
1057 * to namespaces than code that makes direct comparisons.
1058 * @param int $ns The namespace
1062 public function inNamespace( $ns ) {
1063 return MWNamespace
::equals( $this->getNamespace(), $ns );
1067 * Returns true if the title is inside one of the specified namespaces.
1069 * @param int $namespaces,... The namespaces to check for
1073 public function inNamespaces( /* ... */ ) {
1074 $namespaces = func_get_args();
1075 if ( count( $namespaces ) > 0 && is_array( $namespaces[0] ) ) {
1076 $namespaces = $namespaces[0];
1079 foreach ( $namespaces as $ns ) {
1080 if ( $this->inNamespace( $ns ) ) {
1089 * Returns true if the title has the same subject namespace as the
1090 * namespace specified.
1091 * For example this method will take NS_USER and return true if namespace
1092 * is either NS_USER or NS_USER_TALK since both of them have NS_USER
1093 * as their subject namespace.
1095 * This is MUCH simpler than individually testing for equivalence
1096 * against both NS_USER and NS_USER_TALK, and is also forward compatible.
1101 public function hasSubjectNamespace( $ns ) {
1102 return MWNamespace
::subjectEquals( $this->getNamespace(), $ns );
1106 * Is this Title in a namespace which contains content?
1107 * In other words, is this a content page, for the purposes of calculating
1112 public function isContentPage() {
1113 return MWNamespace
::isContent( $this->getNamespace() );
1117 * Would anybody with sufficient privileges be able to move this page?
1118 * Some pages just aren't movable.
1122 public function isMovable() {
1123 if ( !MWNamespace
::isMovable( $this->getNamespace() ) ||
$this->isExternal() ) {
1124 // Interwiki title or immovable namespace. Hooks don't get to override here
1129 Hooks
::run( 'TitleIsMovable', [ $this, &$result ] );
1134 * Is this the mainpage?
1135 * @note Title::newFromText seems to be sufficiently optimized by the title
1136 * cache that we don't need to over-optimize by doing direct comparisons and
1137 * accidentally creating new bugs where $title->equals( Title::newFromText() )
1138 * ends up reporting something differently than $title->isMainPage();
1143 public function isMainPage() {
1144 return $this->equals( Title
::newMainPage() );
1148 * Is this a subpage?
1152 public function isSubpage() {
1153 return MWNamespace
::hasSubpages( $this->mNamespace
)
1154 ?
strpos( $this->getText(), '/' ) !== false
1159 * Is this a conversion table for the LanguageConverter?
1163 public function isConversionTable() {
1164 // @todo ConversionTable should become a separate content model.
1166 return $this->getNamespace() == NS_MEDIAWIKI
&&
1167 strpos( $this->getText(), 'Conversiontable/' ) === 0;
1171 * Does that page contain wikitext, or it is JS, CSS or whatever?
1175 public function isWikitextPage() {
1176 return $this->hasContentModel( CONTENT_MODEL_WIKITEXT
);
1180 * Could this page contain custom CSS or JavaScript for the global UI.
1181 * This is generally true for pages in the MediaWiki namespace having CONTENT_MODEL_CSS
1182 * or CONTENT_MODEL_JAVASCRIPT.
1184 * This method does *not* return true for per-user JS/CSS. Use isCssJsSubpage()
1187 * Note that this method should not return true for pages that contain and
1188 * show "inactive" CSS or JS.
1191 * @todo FIXME: Rename to isSiteConfigPage() and remove deprecated hook
1193 public function isCssOrJsPage() {
1194 $isCssOrJsPage = NS_MEDIAWIKI
== $this->mNamespace
1195 && ( $this->hasContentModel( CONTENT_MODEL_CSS
)
1196 ||
$this->hasContentModel( CONTENT_MODEL_JAVASCRIPT
) );
1198 # @note This hook is also called in ContentHandler::getDefaultModel.
1199 # It's called here again to make sure hook functions can force this
1200 # method to return true even outside the MediaWiki namespace.
1202 Hooks
::run( 'TitleIsCssOrJsPage', [ $this, &$isCssOrJsPage ], '1.25' );
1204 return $isCssOrJsPage;
1208 * Is this a .css or .js subpage of a user page?
1210 * @todo FIXME: Rename to isUserConfigPage()
1212 public function isCssJsSubpage() {
1213 return ( NS_USER
== $this->mNamespace
&& $this->isSubpage()
1214 && ( $this->hasContentModel( CONTENT_MODEL_CSS
)
1215 ||
$this->hasContentModel( CONTENT_MODEL_JAVASCRIPT
) ) );
1219 * Trim down a .css or .js subpage title to get the corresponding skin name
1221 * @return string Containing skin name from .css or .js subpage title
1223 public function getSkinFromCssJsSubpage() {
1224 $subpage = explode( '/', $this->mTextform
);
1225 $subpage = $subpage[count( $subpage ) - 1];
1226 $lastdot = strrpos( $subpage, '.' );
1227 if ( $lastdot === false ) {
1228 return $subpage; # Never happens: only called for names ending in '.css' or '.js'
1230 return substr( $subpage, 0, $lastdot );
1234 * Is this a .css subpage of a user page?
1238 public function isCssSubpage() {
1239 return ( NS_USER
== $this->mNamespace
&& $this->isSubpage()
1240 && $this->hasContentModel( CONTENT_MODEL_CSS
) );
1244 * Is this a .js subpage of a user page?
1248 public function isJsSubpage() {
1249 return ( NS_USER
== $this->mNamespace
&& $this->isSubpage()
1250 && $this->hasContentModel( CONTENT_MODEL_JAVASCRIPT
) );
1254 * Is this a talk page of some sort?
1258 public function isTalkPage() {
1259 return MWNamespace
::isTalk( $this->getNamespace() );
1263 * Get a Title object associated with the talk page of this article
1265 * @return Title The object for the talk page
1267 public function getTalkPage() {
1268 return Title
::makeTitle( MWNamespace
::getTalk( $this->getNamespace() ), $this->getDBkey() );
1272 * Get a title object associated with the subject page of this
1275 * @return Title The object for the subject page
1277 public function getSubjectPage() {
1278 // Is this the same title?
1279 $subjectNS = MWNamespace
::getSubject( $this->getNamespace() );
1280 if ( $this->getNamespace() == $subjectNS ) {
1283 return Title
::makeTitle( $subjectNS, $this->getDBkey() );
1287 * Get the other title for this page, if this is a subject page
1288 * get the talk page, if it is a subject page get the talk page
1291 * @throws MWException
1294 public function getOtherPage() {
1295 if ( $this->isSpecialPage() ) {
1296 throw new MWException( 'Special pages cannot have other pages' );
1298 if ( $this->isTalkPage() ) {
1299 return $this->getSubjectPage();
1301 return $this->getTalkPage();
1306 * Get the default namespace index, for when there is no namespace
1308 * @return int Default namespace index
1310 public function getDefaultNamespace() {
1311 return $this->mDefaultNamespace
;
1315 * Get the Title fragment (i.e.\ the bit after the #) in text form
1317 * Use Title::hasFragment to check for a fragment
1319 * @return string Title fragment
1321 public function getFragment() {
1322 return $this->mFragment
;
1326 * Check if a Title fragment is set
1331 public function hasFragment() {
1332 return $this->mFragment
!== '';
1336 * Get the fragment in URL form, including the "#" character if there is one
1337 * @return string Fragment in URL form
1339 public function getFragmentForURL() {
1340 if ( !$this->hasFragment() ) {
1343 return '#' . Title
::escapeFragmentForURL( $this->getFragment() );
1348 * Set the fragment for this title. Removes the first character from the
1349 * specified fragment before setting, so it assumes you're passing it with
1352 * Deprecated for public use, use Title::makeTitle() with fragment parameter,
1353 * or Title::createFragmentTarget().
1354 * Still in active use privately.
1357 * @param string $fragment Text
1359 public function setFragment( $fragment ) {
1360 $this->mFragment
= strtr( substr( $fragment, 1 ), '_', ' ' );
1364 * Creates a new Title for a different fragment of the same page.
1367 * @param string $fragment
1370 public function createFragmentTarget( $fragment ) {
1371 return self
::makeTitle(
1372 $this->getNamespace(),
1375 $this->getInterwiki()
1381 * Prefix some arbitrary text with the namespace or interwiki prefix
1384 * @param string $name The text
1385 * @return string The prefixed text
1387 private function prefix( $name ) {
1389 if ( $this->isExternal() ) {
1390 $p = $this->mInterwiki
. ':';
1393 if ( 0 != $this->mNamespace
) {
1394 $p .= $this->getNsText() . ':';
1400 * Get the prefixed database key form
1402 * @return string The prefixed title, with underscores and
1403 * any interwiki and namespace prefixes
1405 public function getPrefixedDBkey() {
1406 $s = $this->prefix( $this->mDbkeyform
);
1407 $s = strtr( $s, ' ', '_' );
1412 * Get the prefixed title with spaces.
1413 * This is the form usually used for display
1415 * @return string The prefixed title, with spaces
1417 public function getPrefixedText() {
1418 if ( $this->mPrefixedText
=== null ) {
1419 $s = $this->prefix( $this->mTextform
);
1420 $s = strtr( $s, '_', ' ' );
1421 $this->mPrefixedText
= $s;
1423 return $this->mPrefixedText
;
1427 * Return a string representation of this title
1429 * @return string Representation of this title
1431 public function __toString() {
1432 return $this->getPrefixedText();
1436 * Get the prefixed title with spaces, plus any fragment
1437 * (part beginning with '#')
1439 * @return string The prefixed title, with spaces and the fragment, including '#'
1441 public function getFullText() {
1442 $text = $this->getPrefixedText();
1443 if ( $this->hasFragment() ) {
1444 $text .= '#' . $this->getFragment();
1450 * Get the root page name text without a namespace, i.e. the leftmost part before any slashes
1454 * Title::newFromText('User:Foo/Bar/Baz')->getRootText();
1458 * @return string Root name
1461 public function getRootText() {
1462 if ( !MWNamespace
::hasSubpages( $this->mNamespace
) ) {
1463 return $this->getText();
1466 return strtok( $this->getText(), '/' );
1470 * Get the root page name title, i.e. the leftmost part before any slashes
1474 * Title::newFromText('User:Foo/Bar/Baz')->getRootTitle();
1475 * # returns: Title{User:Foo}
1478 * @return Title Root title
1481 public function getRootTitle() {
1482 return Title
::makeTitle( $this->getNamespace(), $this->getRootText() );
1486 * Get the base page name without a namespace, i.e. the part before the subpage name
1490 * Title::newFromText('User:Foo/Bar/Baz')->getBaseText();
1491 * # returns: 'Foo/Bar'
1494 * @return string Base name
1496 public function getBaseText() {
1497 if ( !MWNamespace
::hasSubpages( $this->mNamespace
) ) {
1498 return $this->getText();
1501 $parts = explode( '/', $this->getText() );
1502 # Don't discard the real title if there's no subpage involved
1503 if ( count( $parts ) > 1 ) {
1504 unset( $parts[count( $parts ) - 1] );
1506 return implode( '/', $parts );
1510 * Get the base page name title, i.e. the part before the subpage name
1514 * Title::newFromText('User:Foo/Bar/Baz')->getBaseTitle();
1515 * # returns: Title{User:Foo/Bar}
1518 * @return Title Base title
1521 public function getBaseTitle() {
1522 return Title
::makeTitle( $this->getNamespace(), $this->getBaseText() );
1526 * Get the lowest-level subpage name, i.e. the rightmost part after any slashes
1530 * Title::newFromText('User:Foo/Bar/Baz')->getSubpageText();
1534 * @return string Subpage name
1536 public function getSubpageText() {
1537 if ( !MWNamespace
::hasSubpages( $this->mNamespace
) ) {
1538 return $this->mTextform
;
1540 $parts = explode( '/', $this->mTextform
);
1541 return $parts[count( $parts ) - 1];
1545 * Get the title for a subpage of the current page
1549 * Title::newFromText('User:Foo/Bar/Baz')->getSubpage("Asdf");
1550 * # returns: Title{User:Foo/Bar/Baz/Asdf}
1553 * @param string $text The subpage name to add to the title
1554 * @return Title Subpage title
1557 public function getSubpage( $text ) {
1558 return Title
::makeTitleSafe( $this->getNamespace(), $this->getText() . '/' . $text );
1562 * Get a URL-encoded form of the subpage text
1564 * @return string URL-encoded subpage name
1566 public function getSubpageUrlForm() {
1567 $text = $this->getSubpageText();
1568 $text = wfUrlencode( strtr( $text, ' ', '_' ) );
1573 * Get a URL-encoded title (not an actual URL) including interwiki
1575 * @return string The URL-encoded form
1577 public function getPrefixedURL() {
1578 $s = $this->prefix( $this->mDbkeyform
);
1579 $s = wfUrlencode( strtr( $s, ' ', '_' ) );
1584 * Helper to fix up the get{Canonical,Full,Link,Local,Internal}URL args
1585 * get{Canonical,Full,Link,Local,Internal}URL methods accepted an optional
1586 * second argument named variant. This was deprecated in favor
1587 * of passing an array of option with a "variant" key
1588 * Once $query2 is removed for good, this helper can be dropped
1589 * and the wfArrayToCgi moved to getLocalURL();
1591 * @since 1.19 (r105919)
1592 * @param array|string $query
1593 * @param bool $query2
1596 private static function fixUrlQueryArgs( $query, $query2 = false ) {
1597 if ( $query2 !== false ) {
1598 wfDeprecated( "Title::get{Canonical,Full,Link,Local,Internal}URL " .
1599 "method called with a second parameter is deprecated. Add your " .
1600 "parameter to an array passed as the first parameter.", "1.19" );
1602 if ( is_array( $query ) ) {
1603 $query = wfArrayToCgi( $query );
1606 if ( is_string( $query2 ) ) {
1607 // $query2 is a string, we will consider this to be
1608 // a deprecated $variant argument and add it to the query
1609 $query2 = wfArrayToCgi( [ 'variant' => $query2 ] );
1611 $query2 = wfArrayToCgi( $query2 );
1613 // If we have $query content add a & to it first
1617 // Now append the queries together
1624 * Get a real URL referring to this title, with interwiki link and
1627 * @see self::getLocalURL for the arguments.
1629 * @param array|string $query
1630 * @param bool $query2
1631 * @param string $proto Protocol type to use in URL
1632 * @return string The URL
1634 public function getFullURL( $query = '', $query2 = false, $proto = PROTO_RELATIVE
) {
1635 $query = self
::fixUrlQueryArgs( $query, $query2 );
1637 # Hand off all the decisions on urls to getLocalURL
1638 $url = $this->getLocalURL( $query );
1640 # Expand the url to make it a full url. Note that getLocalURL has the
1641 # potential to output full urls for a variety of reasons, so we use
1642 # wfExpandUrl instead of simply prepending $wgServer
1643 $url = wfExpandUrl( $url, $proto );
1645 # Finally, add the fragment.
1646 $url .= $this->getFragmentForURL();
1648 Hooks
::run( 'GetFullURL', [ &$this, &$url, $query ] );
1653 * Get a URL with no fragment or server name (relative URL) from a Title object.
1654 * If this page is generated with action=render, however,
1655 * $wgServer is prepended to make an absolute URL.
1657 * @see self::getFullURL to always get an absolute URL.
1658 * @see self::getLinkURL to always get a URL that's the simplest URL that will be
1659 * valid to link, locally, to the current Title.
1660 * @see self::newFromText to produce a Title object.
1662 * @param string|array $query An optional query string,
1663 * not used for interwiki links. Can be specified as an associative array as well,
1664 * e.g., array( 'action' => 'edit' ) (keys and values will be URL-escaped).
1665 * Some query patterns will trigger various shorturl path replacements.
1666 * @param array $query2 An optional secondary query array. This one MUST
1667 * be an array. If a string is passed it will be interpreted as a deprecated
1668 * variant argument and urlencoded into a variant= argument.
1669 * This second query argument will be added to the $query
1670 * The second parameter is deprecated since 1.19. Pass it as a key,value
1671 * pair in the first parameter array instead.
1673 * @return string String of the URL.
1675 public function getLocalURL( $query = '', $query2 = false ) {
1676 global $wgArticlePath, $wgScript, $wgServer, $wgRequest;
1678 $query = self
::fixUrlQueryArgs( $query, $query2 );
1680 $interwiki = Interwiki
::fetch( $this->mInterwiki
);
1682 $namespace = $this->getNsText();
1683 if ( $namespace != '' ) {
1684 # Can this actually happen? Interwikis shouldn't be parsed.
1685 # Yes! It can in interwiki transclusion. But... it probably shouldn't.
1688 $url = $interwiki->getURL( $namespace . $this->getDBkey() );
1689 $url = wfAppendQuery( $url, $query );
1691 $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
1692 if ( $query == '' ) {
1693 $url = str_replace( '$1', $dbkey, $wgArticlePath );
1694 Hooks
::run( 'GetLocalURL::Article', [ &$this, &$url ] );
1696 global $wgVariantArticlePath, $wgActionPaths, $wgContLang;
1700 if ( !empty( $wgActionPaths )
1701 && preg_match( '/^(.*&|)action=([^&]*)(&(.*)|)$/', $query, $matches )
1703 $action = urldecode( $matches[2] );
1704 if ( isset( $wgActionPaths[$action] ) ) {
1705 $query = $matches[1];
1706 if ( isset( $matches[4] ) ) {
1707 $query .= $matches[4];
1709 $url = str_replace( '$1', $dbkey, $wgActionPaths[$action] );
1710 if ( $query != '' ) {
1711 $url = wfAppendQuery( $url, $query );
1717 && $wgVariantArticlePath
1718 && preg_match( '/^variant=([^&]*)$/', $query, $matches )
1719 && $wgContLang->getCode() === $this->getPageLanguage()->getCode()
1720 && $this->getPageLanguage()->hasVariants()
1722 $variant = urldecode( $matches[1] );
1723 if ( $this->getPageLanguage()->hasVariant( $variant ) ) {
1724 // Only do the variant replacement if the given variant is a valid
1725 // variant for the page's language.
1726 $url = str_replace( '$2', urlencode( $variant ), $wgVariantArticlePath );
1727 $url = str_replace( '$1', $dbkey, $url );
1731 if ( $url === false ) {
1732 if ( $query == '-' ) {
1735 $url = "{$wgScript}?title={$dbkey}&{$query}";
1739 Hooks
::run( 'GetLocalURL::Internal', [ &$this, &$url, $query ] );
1741 // @todo FIXME: This causes breakage in various places when we
1742 // actually expected a local URL and end up with dupe prefixes.
1743 if ( $wgRequest->getVal( 'action' ) == 'render' ) {
1744 $url = $wgServer . $url;
1747 Hooks
::run( 'GetLocalURL', [ &$this, &$url, $query ] );
1752 * Get a URL that's the simplest URL that will be valid to link, locally,
1753 * to the current Title. It includes the fragment, but does not include
1754 * the server unless action=render is used (or the link is external). If
1755 * there's a fragment but the prefixed text is empty, we just return a link
1758 * The result obviously should not be URL-escaped, but does need to be
1759 * HTML-escaped if it's being output in HTML.
1761 * @param array $query
1762 * @param bool $query2
1763 * @param string $proto Protocol to use; setting this will cause a full URL to be used
1764 * @see self::getLocalURL for the arguments.
1765 * @return string The URL
1767 public function getLinkURL( $query = '', $query2 = false, $proto = PROTO_RELATIVE
) {
1768 if ( $this->isExternal() ||
$proto !== PROTO_RELATIVE
) {
1769 $ret = $this->getFullURL( $query, $query2, $proto );
1770 } elseif ( $this->getPrefixedText() === '' && $this->hasFragment() ) {
1771 $ret = $this->getFragmentForURL();
1773 $ret = $this->getLocalURL( $query, $query2 ) . $this->getFragmentForURL();
1779 * Get the URL form for an internal link.
1780 * - Used in various CDN-related code, in case we have a different
1781 * internal hostname for the server from the exposed one.
1783 * This uses $wgInternalServer to qualify the path, or $wgServer
1784 * if $wgInternalServer is not set. If the server variable used is
1785 * protocol-relative, the URL will be expanded to http://
1787 * @see self::getLocalURL for the arguments.
1788 * @return string The URL
1790 public function getInternalURL( $query = '', $query2 = false ) {
1791 global $wgInternalServer, $wgServer;
1792 $query = self
::fixUrlQueryArgs( $query, $query2 );
1793 $server = $wgInternalServer !== false ?
$wgInternalServer : $wgServer;
1794 $url = wfExpandUrl( $server . $this->getLocalURL( $query ), PROTO_HTTP
);
1795 Hooks
::run( 'GetInternalURL', [ &$this, &$url, $query ] );
1800 * Get the URL for a canonical link, for use in things like IRC and
1801 * e-mail notifications. Uses $wgCanonicalServer and the
1802 * GetCanonicalURL hook.
1804 * NOTE: Unlike getInternalURL(), the canonical URL includes the fragment
1806 * @see self::getLocalURL for the arguments.
1807 * @return string The URL
1810 public function getCanonicalURL( $query = '', $query2 = false ) {
1811 $query = self
::fixUrlQueryArgs( $query, $query2 );
1812 $url = wfExpandUrl( $this->getLocalURL( $query ) . $this->getFragmentForURL(), PROTO_CANONICAL
);
1813 Hooks
::run( 'GetCanonicalURL', [ &$this, &$url, $query ] );
1818 * Get the edit URL for this Title
1820 * @return string The URL, or a null string if this is an interwiki link
1822 public function getEditURL() {
1823 if ( $this->isExternal() ) {
1826 $s = $this->getLocalURL( 'action=edit' );
1832 * Can $user perform $action on this page?
1833 * This skips potentially expensive cascading permission checks
1834 * as well as avoids expensive error formatting
1836 * Suitable for use for nonessential UI controls in common cases, but
1837 * _not_ for functional access control.
1839 * May provide false positives, but should never provide a false negative.
1841 * @param string $action Action that permission needs to be checked for
1842 * @param User $user User to check (since 1.19); $wgUser will be used if not provided.
1845 public function quickUserCan( $action, $user = null ) {
1846 return $this->userCan( $action, $user, false );
1850 * Can $user perform $action on this page?
1852 * @param string $action Action that permission needs to be checked for
1853 * @param User $user User to check (since 1.19); $wgUser will be used if not
1855 * @param string $rigor Same format as Title::getUserPermissionsErrors()
1858 public function userCan( $action, $user = null, $rigor = 'secure' ) {
1859 if ( !$user instanceof User
) {
1864 return !count( $this->getUserPermissionsErrorsInternal( $action, $user, $rigor, true ) );
1868 * Can $user perform $action on this page?
1870 * @todo FIXME: This *does not* check throttles (User::pingLimiter()).
1872 * @param string $action Action that permission needs to be checked for
1873 * @param User $user User to check
1874 * @param string $rigor One of (quick,full,secure)
1875 * - quick : does cheap permission checks from slaves (usable for GUI creation)
1876 * - full : does cheap and expensive checks possibly from a slave
1877 * - secure : does cheap and expensive checks, using the master as needed
1878 * @param array $ignoreErrors Array of Strings Set this to a list of message keys
1879 * whose corresponding errors may be ignored.
1880 * @return array Array of arrays of the arguments to wfMessage to explain permissions problems.
1882 public function getUserPermissionsErrors(
1883 $action, $user, $rigor = 'secure', $ignoreErrors = []
1885 $errors = $this->getUserPermissionsErrorsInternal( $action, $user, $rigor );
1887 // Remove the errors being ignored.
1888 foreach ( $errors as $index => $error ) {
1889 $errKey = is_array( $error ) ?
$error[0] : $error;
1891 if ( in_array( $errKey, $ignoreErrors ) ) {
1892 unset( $errors[$index] );
1894 if ( $errKey instanceof MessageSpecifier
&& in_array( $errKey->getKey(), $ignoreErrors ) ) {
1895 unset( $errors[$index] );
1903 * Permissions checks that fail most often, and which are easiest to test.
1905 * @param string $action The action to check
1906 * @param User $user User to check
1907 * @param array $errors List of current errors
1908 * @param string $rigor Same format as Title::getUserPermissionsErrors()
1909 * @param bool $short Short circuit on first error
1911 * @return array List of errors
1913 private function checkQuickPermissions( $action, $user, $errors, $rigor, $short ) {
1914 if ( !Hooks
::run( 'TitleQuickPermissions',
1915 [ $this, $user, $action, &$errors, ( $rigor !== 'quick' ), $short ] )
1920 if ( $action == 'create' ) {
1922 ( $this->isTalkPage() && !$user->isAllowed( 'createtalk' ) ) ||
1923 ( !$this->isTalkPage() && !$user->isAllowed( 'createpage' ) )
1925 $errors[] = $user->isAnon() ?
[ 'nocreatetext' ] : [ 'nocreate-loggedin' ];
1927 } elseif ( $action == 'move' ) {
1928 if ( !$user->isAllowed( 'move-rootuserpages' )
1929 && $this->mNamespace
== NS_USER
&& !$this->isSubpage() ) {
1930 // Show user page-specific message only if the user can move other pages
1931 $errors[] = [ 'cant-move-user-page' ];
1934 // Check if user is allowed to move files if it's a file
1935 if ( $this->mNamespace
== NS_FILE
&& !$user->isAllowed( 'movefile' ) ) {
1936 $errors[] = [ 'movenotallowedfile' ];
1939 // Check if user is allowed to move category pages if it's a category page
1940 if ( $this->mNamespace
== NS_CATEGORY
&& !$user->isAllowed( 'move-categorypages' ) ) {
1941 $errors[] = [ 'cant-move-category-page' ];
1944 if ( !$user->isAllowed( 'move' ) ) {
1945 // User can't move anything
1946 $userCanMove = User
::groupHasPermission( 'user', 'move' );
1947 $autoconfirmedCanMove = User
::groupHasPermission( 'autoconfirmed', 'move' );
1948 if ( $user->isAnon() && ( $userCanMove ||
$autoconfirmedCanMove ) ) {
1949 // custom message if logged-in users without any special rights can move
1950 $errors[] = [ 'movenologintext' ];
1952 $errors[] = [ 'movenotallowed' ];
1955 } elseif ( $action == 'move-target' ) {
1956 if ( !$user->isAllowed( 'move' ) ) {
1957 // User can't move anything
1958 $errors[] = [ 'movenotallowed' ];
1959 } elseif ( !$user->isAllowed( 'move-rootuserpages' )
1960 && $this->mNamespace
== NS_USER
&& !$this->isSubpage() ) {
1961 // Show user page-specific message only if the user can move other pages
1962 $errors[] = [ 'cant-move-to-user-page' ];
1963 } elseif ( !$user->isAllowed( 'move-categorypages' )
1964 && $this->mNamespace
== NS_CATEGORY
) {
1965 // Show category page-specific message only if the user can move other pages
1966 $errors[] = [ 'cant-move-to-category-page' ];
1968 } elseif ( !$user->isAllowed( $action ) ) {
1969 $errors[] = $this->missingPermissionError( $action, $short );
1976 * Add the resulting error code to the errors array
1978 * @param array $errors List of current errors
1979 * @param array $result Result of errors
1981 * @return array List of errors
1983 private function resultToError( $errors, $result ) {
1984 if ( is_array( $result ) && count( $result ) && !is_array( $result[0] ) ) {
1985 // A single array representing an error
1986 $errors[] = $result;
1987 } elseif ( is_array( $result ) && is_array( $result[0] ) ) {
1988 // A nested array representing multiple errors
1989 $errors = array_merge( $errors, $result );
1990 } elseif ( $result !== '' && is_string( $result ) ) {
1991 // A string representing a message-id
1992 $errors[] = [ $result ];
1993 } elseif ( $result instanceof MessageSpecifier
) {
1994 // A message specifier representing an error
1995 $errors[] = [ $result ];
1996 } elseif ( $result === false ) {
1997 // a generic "We don't want them to do that"
1998 $errors[] = [ 'badaccess-group0' ];
2004 * Check various permission hooks
2006 * @param string $action The action to check
2007 * @param User $user User to check
2008 * @param array $errors List of current errors
2009 * @param string $rigor Same format as Title::getUserPermissionsErrors()
2010 * @param bool $short Short circuit on first error
2012 * @return array List of errors
2014 private function checkPermissionHooks( $action, $user, $errors, $rigor, $short ) {
2015 // Use getUserPermissionsErrors instead
2017 if ( !Hooks
::run( 'userCan', [ &$this, &$user, $action, &$result ] ) ) {
2018 return $result ?
[] : [ [ 'badaccess-group0' ] ];
2020 // Check getUserPermissionsErrors hook
2021 if ( !Hooks
::run( 'getUserPermissionsErrors', [ &$this, &$user, $action, &$result ] ) ) {
2022 $errors = $this->resultToError( $errors, $result );
2024 // Check getUserPermissionsErrorsExpensive hook
2027 && !( $short && count( $errors ) > 0 )
2028 && !Hooks
::run( 'getUserPermissionsErrorsExpensive', [ &$this, &$user, $action, &$result ] )
2030 $errors = $this->resultToError( $errors, $result );
2037 * Check permissions on special pages & namespaces
2039 * @param string $action The action to check
2040 * @param User $user User to check
2041 * @param array $errors List of current errors
2042 * @param string $rigor Same format as Title::getUserPermissionsErrors()
2043 * @param bool $short Short circuit on first error
2045 * @return array List of errors
2047 private function checkSpecialsAndNSPermissions( $action, $user, $errors, $rigor, $short ) {
2048 # Only 'createaccount' can be performed on special pages,
2049 # which don't actually exist in the DB.
2050 if ( NS_SPECIAL
== $this->mNamespace
&& $action !== 'createaccount' ) {
2051 $errors[] = [ 'ns-specialprotected' ];
2054 # Check $wgNamespaceProtection for restricted namespaces
2055 if ( $this->isNamespaceProtected( $user ) ) {
2056 $ns = $this->mNamespace
== NS_MAIN ?
2057 wfMessage( 'nstab-main' )->text() : $this->getNsText();
2058 $errors[] = $this->mNamespace
== NS_MEDIAWIKI ?
2059 [ 'protectedinterface', $action ] : [ 'namespaceprotected', $ns, $action ];
2066 * Check CSS/JS sub-page permissions
2068 * @param string $action The action to check
2069 * @param User $user User to check
2070 * @param array $errors List of current errors
2071 * @param string $rigor Same format as Title::getUserPermissionsErrors()
2072 * @param bool $short Short circuit on first error
2074 * @return array List of errors
2076 private function checkCSSandJSPermissions( $action, $user, $errors, $rigor, $short ) {
2077 # Protect css/js subpages of user pages
2078 # XXX: this might be better using restrictions
2079 # XXX: right 'editusercssjs' is deprecated, for backward compatibility only
2080 if ( $action != 'patrol' && !$user->isAllowed( 'editusercssjs' ) ) {
2081 if ( preg_match( '/^' . preg_quote( $user->getName(), '/' ) . '\//', $this->mTextform
) ) {
2082 if ( $this->isCssSubpage() && !$user->isAllowedAny( 'editmyusercss', 'editusercss' ) ) {
2083 $errors[] = [ 'mycustomcssprotected', $action ];
2084 } elseif ( $this->isJsSubpage() && !$user->isAllowedAny( 'editmyuserjs', 'edituserjs' ) ) {
2085 $errors[] = [ 'mycustomjsprotected', $action ];
2088 if ( $this->isCssSubpage() && !$user->isAllowed( 'editusercss' ) ) {
2089 $errors[] = [ 'customcssprotected', $action ];
2090 } elseif ( $this->isJsSubpage() && !$user->isAllowed( 'edituserjs' ) ) {
2091 $errors[] = [ 'customjsprotected', $action ];
2100 * Check against page_restrictions table requirements on this
2101 * page. The user must possess all required rights for this
2104 * @param string $action The action to check
2105 * @param User $user User to check
2106 * @param array $errors List of current errors
2107 * @param string $rigor Same format as Title::getUserPermissionsErrors()
2108 * @param bool $short Short circuit on first error
2110 * @return array List of errors
2112 private function checkPageRestrictions( $action, $user, $errors, $rigor, $short ) {
2113 foreach ( $this->getRestrictions( $action ) as $right ) {
2114 // Backwards compatibility, rewrite sysop -> editprotected
2115 if ( $right == 'sysop' ) {
2116 $right = 'editprotected';
2118 // Backwards compatibility, rewrite autoconfirmed -> editsemiprotected
2119 if ( $right == 'autoconfirmed' ) {
2120 $right = 'editsemiprotected';
2122 if ( $right == '' ) {
2125 if ( !$user->isAllowed( $right ) ) {
2126 $errors[] = [ 'protectedpagetext', $right, $action ];
2127 } elseif ( $this->mCascadeRestriction
&& !$user->isAllowed( 'protect' ) ) {
2128 $errors[] = [ 'protectedpagetext', 'protect', $action ];
2136 * Check restrictions on cascading pages.
2138 * @param string $action The action to check
2139 * @param User $user User to check
2140 * @param array $errors List of current errors
2141 * @param string $rigor Same format as Title::getUserPermissionsErrors()
2142 * @param bool $short Short circuit on first error
2144 * @return array List of errors
2146 private function checkCascadingSourcesRestrictions( $action, $user, $errors, $rigor, $short ) {
2147 if ( $rigor !== 'quick' && !$this->isCssJsSubpage() ) {
2148 # We /could/ use the protection level on the source page, but it's
2149 # fairly ugly as we have to establish a precedence hierarchy for pages
2150 # included by multiple cascade-protected pages. So just restrict
2151 # it to people with 'protect' permission, as they could remove the
2152 # protection anyway.
2153 list( $cascadingSources, $restrictions ) = $this->getCascadeProtectionSources();
2154 # Cascading protection depends on more than this page...
2155 # Several cascading protected pages may include this page...
2156 # Check each cascading level
2157 # This is only for protection restrictions, not for all actions
2158 if ( isset( $restrictions[$action] ) ) {
2159 foreach ( $restrictions[$action] as $right ) {
2160 // Backwards compatibility, rewrite sysop -> editprotected
2161 if ( $right == 'sysop' ) {
2162 $right = 'editprotected';
2164 // Backwards compatibility, rewrite autoconfirmed -> editsemiprotected
2165 if ( $right == 'autoconfirmed' ) {
2166 $right = 'editsemiprotected';
2168 if ( $right != '' && !$user->isAllowedAll( 'protect', $right ) ) {
2170 foreach ( $cascadingSources as $page ) {
2171 $pages .= '* [[:' . $page->getPrefixedText() . "]]\n";
2173 $errors[] = [ 'cascadeprotected', count( $cascadingSources ), $pages, $action ];
2183 * Check action permissions not already checked in checkQuickPermissions
2185 * @param string $action The action to check
2186 * @param User $user User to check
2187 * @param array $errors List of current errors
2188 * @param string $rigor Same format as Title::getUserPermissionsErrors()
2189 * @param bool $short Short circuit on first error
2191 * @return array List of errors
2193 private function checkActionPermissions( $action, $user, $errors, $rigor, $short ) {
2194 global $wgDeleteRevisionsLimit, $wgLang;
2196 if ( $action == 'protect' ) {
2197 if ( count( $this->getUserPermissionsErrorsInternal( 'edit', $user, $rigor, true ) ) ) {
2198 // If they can't edit, they shouldn't protect.
2199 $errors[] = [ 'protect-cantedit' ];
2201 } elseif ( $action == 'create' ) {
2202 $title_protection = $this->getTitleProtection();
2203 if ( $title_protection ) {
2204 if ( $title_protection['permission'] == ''
2205 ||
!$user->isAllowed( $title_protection['permission'] )
2209 User
::whoIs( $title_protection['user'] ),
2210 $title_protection['reason']
2214 } elseif ( $action == 'move' ) {
2215 // Check for immobile pages
2216 if ( !MWNamespace
::isMovable( $this->mNamespace
) ) {
2217 // Specific message for this case
2218 $errors[] = [ 'immobile-source-namespace', $this->getNsText() ];
2219 } elseif ( !$this->isMovable() ) {
2220 // Less specific message for rarer cases
2221 $errors[] = [ 'immobile-source-page' ];
2223 } elseif ( $action == 'move-target' ) {
2224 if ( !MWNamespace
::isMovable( $this->mNamespace
) ) {
2225 $errors[] = [ 'immobile-target-namespace', $this->getNsText() ];
2226 } elseif ( !$this->isMovable() ) {
2227 $errors[] = [ 'immobile-target-page' ];
2229 } elseif ( $action == 'delete' ) {
2230 $tempErrors = $this->checkPageRestrictions( 'edit', $user, [], $rigor, true );
2231 if ( !$tempErrors ) {
2232 $tempErrors = $this->checkCascadingSourcesRestrictions( 'edit',
2233 $user, $tempErrors, $rigor, true );
2235 if ( $tempErrors ) {
2236 // If protection keeps them from editing, they shouldn't be able to delete.
2237 $errors[] = [ 'deleteprotected' ];
2239 if ( $rigor !== 'quick' && $wgDeleteRevisionsLimit
2240 && !$this->userCan( 'bigdelete', $user ) && $this->isBigDeletion()
2242 $errors[] = [ 'delete-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ];
2249 * Check that the user isn't blocked from editing.
2251 * @param string $action The action to check
2252 * @param User $user User to check
2253 * @param array $errors List of current errors
2254 * @param string $rigor Same format as Title::getUserPermissionsErrors()
2255 * @param bool $short Short circuit on first error
2257 * @return array List of errors
2259 private function checkUserBlock( $action, $user, $errors, $rigor, $short ) {
2260 // Account creation blocks handled at userlogin.
2261 // Unblocking handled in SpecialUnblock
2262 if ( $rigor === 'quick' ||
in_array( $action, [ 'createaccount', 'unblock' ] ) ) {
2266 global $wgEmailConfirmToEdit;
2268 if ( $wgEmailConfirmToEdit && !$user->isEmailConfirmed() ) {
2269 $errors[] = [ 'confirmedittext' ];
2272 $useSlave = ( $rigor !== 'secure' );
2273 if ( ( $action == 'edit' ||
$action == 'create' )
2274 && !$user->isBlockedFrom( $this, $useSlave )
2276 // Don't block the user from editing their own talk page unless they've been
2277 // explicitly blocked from that too.
2278 } elseif ( $user->isBlocked() && $user->getBlock()->prevents( $action ) !== false ) {
2279 // @todo FIXME: Pass the relevant context into this function.
2280 $errors[] = $user->getBlock()->getPermissionsError( RequestContext
::getMain() );
2287 * Check that the user is allowed to read this page.
2289 * @param string $action The action to check
2290 * @param User $user User to check
2291 * @param array $errors List of current errors
2292 * @param string $rigor Same format as Title::getUserPermissionsErrors()
2293 * @param bool $short Short circuit on first error
2295 * @return array List of errors
2297 private function checkReadPermissions( $action, $user, $errors, $rigor, $short ) {
2298 global $wgWhitelistRead, $wgWhitelistReadRegexp;
2300 $whitelisted = false;
2301 if ( User
::isEveryoneAllowed( 'read' ) ) {
2302 # Shortcut for public wikis, allows skipping quite a bit of code
2303 $whitelisted = true;
2304 } elseif ( $user->isAllowed( 'read' ) ) {
2305 # If the user is allowed to read pages, he is allowed to read all pages
2306 $whitelisted = true;
2307 } elseif ( $this->isSpecial( 'Userlogin' )
2308 ||
$this->isSpecial( 'ChangePassword' )
2309 ||
$this->isSpecial( 'PasswordReset' )
2311 # Always grant access to the login page.
2312 # Even anons need to be able to log in.
2313 $whitelisted = true;
2314 } elseif ( is_array( $wgWhitelistRead ) && count( $wgWhitelistRead ) ) {
2315 # Time to check the whitelist
2316 # Only do these checks is there's something to check against
2317 $name = $this->getPrefixedText();
2318 $dbName = $this->getPrefixedDBkey();
2320 // Check for explicit whitelisting with and without underscores
2321 if ( in_array( $name, $wgWhitelistRead, true ) ||
in_array( $dbName, $wgWhitelistRead, true ) ) {
2322 $whitelisted = true;
2323 } elseif ( $this->getNamespace() == NS_MAIN
) {
2324 # Old settings might have the title prefixed with
2325 # a colon for main-namespace pages
2326 if ( in_array( ':' . $name, $wgWhitelistRead ) ) {
2327 $whitelisted = true;
2329 } elseif ( $this->isSpecialPage() ) {
2330 # If it's a special page, ditch the subpage bit and check again
2331 $name = $this->getDBkey();
2332 list( $name, /* $subpage */ ) = SpecialPageFactory
::resolveAlias( $name );
2334 $pure = SpecialPage
::getTitleFor( $name )->getPrefixedText();
2335 if ( in_array( $pure, $wgWhitelistRead, true ) ) {
2336 $whitelisted = true;
2342 if ( !$whitelisted && is_array( $wgWhitelistReadRegexp ) && !empty( $wgWhitelistReadRegexp ) ) {
2343 $name = $this->getPrefixedText();
2344 // Check for regex whitelisting
2345 foreach ( $wgWhitelistReadRegexp as $listItem ) {
2346 if ( preg_match( $listItem, $name ) ) {
2347 $whitelisted = true;
2353 if ( !$whitelisted ) {
2354 # If the title is not whitelisted, give extensions a chance to do so...
2355 Hooks
::run( 'TitleReadWhitelist', [ $this, $user, &$whitelisted ] );
2356 if ( !$whitelisted ) {
2357 $errors[] = $this->missingPermissionError( $action, $short );
2365 * Get a description array when the user doesn't have the right to perform
2366 * $action (i.e. when User::isAllowed() returns false)
2368 * @param string $action The action to check
2369 * @param bool $short Short circuit on first error
2370 * @return array List of errors
2372 private function missingPermissionError( $action, $short ) {
2373 // We avoid expensive display logic for quickUserCan's and such
2375 return [ 'badaccess-group0' ];
2378 $groups = array_map( [ 'User', 'makeGroupLinkWiki' ],
2379 User
::getGroupsWithPermission( $action ) );
2381 if ( count( $groups ) ) {
2385 $wgLang->commaList( $groups ),
2389 return [ 'badaccess-group0' ];
2394 * Can $user perform $action on this page? This is an internal function,
2395 * with multiple levels of checks depending on performance needs; see $rigor below.
2396 * It does not check wfReadOnly().
2398 * @param string $action Action that permission needs to be checked for
2399 * @param User $user User to check
2400 * @param string $rigor One of (quick,full,secure)
2401 * - quick : does cheap permission checks from slaves (usable for GUI creation)
2402 * - full : does cheap and expensive checks possibly from a slave
2403 * - secure : does cheap and expensive checks, using the master as needed
2404 * @param bool $short Set this to true to stop after the first permission error.
2405 * @return array Array of arrays of the arguments to wfMessage to explain permissions problems.
2407 protected function getUserPermissionsErrorsInternal(
2408 $action, $user, $rigor = 'secure', $short = false
2410 if ( $rigor === true ) {
2411 $rigor = 'secure'; // b/c
2412 } elseif ( $rigor === false ) {
2413 $rigor = 'quick'; // b/c
2414 } elseif ( !in_array( $rigor, [ 'quick', 'full', 'secure' ] ) ) {
2415 throw new Exception( "Invalid rigor parameter '$rigor'." );
2418 # Read has special handling
2419 if ( $action == 'read' ) {
2421 'checkPermissionHooks',
2422 'checkReadPermissions',
2424 # Don't call checkSpecialsAndNSPermissions or checkCSSandJSPermissions
2425 # here as it will lead to duplicate error messages. This is okay to do
2426 # since anywhere that checks for create will also check for edit, and
2427 # those checks are called for edit.
2428 } elseif ( $action == 'create' ) {
2430 'checkQuickPermissions',
2431 'checkPermissionHooks',
2432 'checkPageRestrictions',
2433 'checkCascadingSourcesRestrictions',
2434 'checkActionPermissions',
2439 'checkQuickPermissions',
2440 'checkPermissionHooks',
2441 'checkSpecialsAndNSPermissions',
2442 'checkCSSandJSPermissions',
2443 'checkPageRestrictions',
2444 'checkCascadingSourcesRestrictions',
2445 'checkActionPermissions',
2451 while ( count( $checks ) > 0 &&
2452 !( $short && count( $errors ) > 0 ) ) {
2453 $method = array_shift( $checks );
2454 $errors = $this->$method( $action, $user, $errors, $rigor, $short );
2461 * Get a filtered list of all restriction types supported by this wiki.
2462 * @param bool $exists True to get all restriction types that apply to
2463 * titles that do exist, False for all restriction types that apply to
2464 * titles that do not exist
2467 public static function getFilteredRestrictionTypes( $exists = true ) {
2468 global $wgRestrictionTypes;
2469 $types = $wgRestrictionTypes;
2471 # Remove the create restriction for existing titles
2472 $types = array_diff( $types, [ 'create' ] );
2474 # Only the create and upload restrictions apply to non-existing titles
2475 $types = array_intersect( $types, [ 'create', 'upload' ] );
2481 * Returns restriction types for the current Title
2483 * @return array Applicable restriction types
2485 public function getRestrictionTypes() {
2486 if ( $this->isSpecialPage() ) {
2490 $types = self
::getFilteredRestrictionTypes( $this->exists() );
2492 if ( $this->getNamespace() != NS_FILE
) {
2493 # Remove the upload restriction for non-file titles
2494 $types = array_diff( $types, [ 'upload' ] );
2497 Hooks
::run( 'TitleGetRestrictionTypes', [ $this, &$types ] );
2499 wfDebug( __METHOD__
. ': applicable restrictions to [[' .
2500 $this->getPrefixedText() . ']] are {' . implode( ',', $types ) . "}\n" );
2506 * Is this title subject to title protection?
2507 * Title protection is the one applied against creation of such title.
2509 * @return array|bool An associative array representing any existent title
2510 * protection, or false if there's none.
2512 public function getTitleProtection() {
2513 // Can't protect pages in special namespaces
2514 if ( $this->getNamespace() < 0 ) {
2518 // Can't protect pages that exist.
2519 if ( $this->exists() ) {
2523 if ( $this->mTitleProtection
=== null ) {
2524 $dbr = wfGetDB( DB_SLAVE
);
2525 $res = $dbr->select(
2528 'user' => 'pt_user',
2529 'reason' => 'pt_reason',
2530 'expiry' => 'pt_expiry',
2531 'permission' => 'pt_create_perm'
2533 [ 'pt_namespace' => $this->getNamespace(), 'pt_title' => $this->getDBkey() ],
2537 // fetchRow returns false if there are no rows.
2538 $row = $dbr->fetchRow( $res );
2540 if ( $row['permission'] == 'sysop' ) {
2541 $row['permission'] = 'editprotected'; // B/C
2543 if ( $row['permission'] == 'autoconfirmed' ) {
2544 $row['permission'] = 'editsemiprotected'; // B/C
2546 $row['expiry'] = $dbr->decodeExpiry( $row['expiry'] );
2548 $this->mTitleProtection
= $row;
2550 return $this->mTitleProtection
;
2554 * Remove any title protection due to page existing
2556 public function deleteTitleProtection() {
2557 $dbw = wfGetDB( DB_MASTER
);
2561 [ 'pt_namespace' => $this->getNamespace(), 'pt_title' => $this->getDBkey() ],
2564 $this->mTitleProtection
= false;
2568 * Is this page "semi-protected" - the *only* protection levels are listed
2569 * in $wgSemiprotectedRestrictionLevels?
2571 * @param string $action Action to check (default: edit)
2574 public function isSemiProtected( $action = 'edit' ) {
2575 global $wgSemiprotectedRestrictionLevels;
2577 $restrictions = $this->getRestrictions( $action );
2578 $semi = $wgSemiprotectedRestrictionLevels;
2579 if ( !$restrictions ||
!$semi ) {
2580 // Not protected, or all protection is full protection
2584 // Remap autoconfirmed to editsemiprotected for BC
2585 foreach ( array_keys( $semi, 'autoconfirmed' ) as $key ) {
2586 $semi[$key] = 'editsemiprotected';
2588 foreach ( array_keys( $restrictions, 'autoconfirmed' ) as $key ) {
2589 $restrictions[$key] = 'editsemiprotected';
2592 return !array_diff( $restrictions, $semi );
2596 * Does the title correspond to a protected article?
2598 * @param string $action The action the page is protected from,
2599 * by default checks all actions.
2602 public function isProtected( $action = '' ) {
2603 global $wgRestrictionLevels;
2605 $restrictionTypes = $this->getRestrictionTypes();
2607 # Special pages have inherent protection
2608 if ( $this->isSpecialPage() ) {
2612 # Check regular protection levels
2613 foreach ( $restrictionTypes as $type ) {
2614 if ( $action == $type ||
$action == '' ) {
2615 $r = $this->getRestrictions( $type );
2616 foreach ( $wgRestrictionLevels as $level ) {
2617 if ( in_array( $level, $r ) && $level != '' ) {
2628 * Determines if $user is unable to edit this page because it has been protected
2629 * by $wgNamespaceProtection.
2631 * @param User $user User object to check permissions
2634 public function isNamespaceProtected( User
$user ) {
2635 global $wgNamespaceProtection;
2637 if ( isset( $wgNamespaceProtection[$this->mNamespace
] ) ) {
2638 foreach ( (array)$wgNamespaceProtection[$this->mNamespace
] as $right ) {
2639 if ( $right != '' && !$user->isAllowed( $right ) ) {
2648 * Cascading protection: Return true if cascading restrictions apply to this page, false if not.
2650 * @return bool If the page is subject to cascading restrictions.
2652 public function isCascadeProtected() {
2653 list( $sources, /* $restrictions */ ) = $this->getCascadeProtectionSources( false );
2654 return ( $sources > 0 );
2658 * Determines whether cascading protection sources have already been loaded from
2661 * @param bool $getPages True to check if the pages are loaded, or false to check
2662 * if the status is loaded.
2663 * @return bool Whether or not the specified information has been loaded
2666 public function areCascadeProtectionSourcesLoaded( $getPages = true ) {
2667 return $getPages ?
$this->mCascadeSources
!== null : $this->mHasCascadingRestrictions
!== null;
2671 * Cascading protection: Get the source of any cascading restrictions on this page.
2673 * @param bool $getPages Whether or not to retrieve the actual pages
2674 * that the restrictions have come from and the actual restrictions
2676 * @return array Two elements: First is an array of Title objects of the
2677 * pages from which cascading restrictions have come, false for
2678 * none, or true if such restrictions exist but $getPages was not
2679 * set. Second is an array like that returned by
2680 * Title::getAllRestrictions(), or an empty array if $getPages is
2683 public function getCascadeProtectionSources( $getPages = true ) {
2684 $pagerestrictions = [];
2686 if ( $this->mCascadeSources
!== null && $getPages ) {
2687 return [ $this->mCascadeSources
, $this->mCascadingRestrictions
];
2688 } elseif ( $this->mHasCascadingRestrictions
!== null && !$getPages ) {
2689 return [ $this->mHasCascadingRestrictions
, $pagerestrictions ];
2692 $dbr = wfGetDB( DB_SLAVE
);
2694 if ( $this->getNamespace() == NS_FILE
) {
2695 $tables = [ 'imagelinks', 'page_restrictions' ];
2697 'il_to' => $this->getDBkey(),
2702 $tables = [ 'templatelinks', 'page_restrictions' ];
2704 'tl_namespace' => $this->getNamespace(),
2705 'tl_title' => $this->getDBkey(),
2712 $cols = [ 'pr_page', 'page_namespace', 'page_title',
2713 'pr_expiry', 'pr_type', 'pr_level' ];
2714 $where_clauses[] = 'page_id=pr_page';
2717 $cols = [ 'pr_expiry' ];
2720 $res = $dbr->select( $tables, $cols, $where_clauses, __METHOD__
);
2722 $sources = $getPages ?
[] : false;
2723 $now = wfTimestampNow();
2725 foreach ( $res as $row ) {
2726 $expiry = $dbr->decodeExpiry( $row->pr_expiry
);
2727 if ( $expiry > $now ) {
2729 $page_id = $row->pr_page
;
2730 $page_ns = $row->page_namespace
;
2731 $page_title = $row->page_title
;
2732 $sources[$page_id] = Title
::makeTitle( $page_ns, $page_title );
2733 # Add groups needed for each restriction type if its not already there
2734 # Make sure this restriction type still exists
2736 if ( !isset( $pagerestrictions[$row->pr_type
] ) ) {
2737 $pagerestrictions[$row->pr_type
] = [];
2741 isset( $pagerestrictions[$row->pr_type
] )
2742 && !in_array( $row->pr_level
, $pagerestrictions[$row->pr_type
] )
2744 $pagerestrictions[$row->pr_type
][] = $row->pr_level
;
2753 $this->mCascadeSources
= $sources;
2754 $this->mCascadingRestrictions
= $pagerestrictions;
2756 $this->mHasCascadingRestrictions
= $sources;
2759 return [ $sources, $pagerestrictions ];
2763 * Accessor for mRestrictionsLoaded
2765 * @return bool Whether or not the page's restrictions have already been
2766 * loaded from the database
2769 public function areRestrictionsLoaded() {
2770 return $this->mRestrictionsLoaded
;
2774 * Accessor/initialisation for mRestrictions
2776 * @param string $action Action that permission needs to be checked for
2777 * @return array Restriction levels needed to take the action. All levels are
2778 * required. Note that restriction levels are normally user rights, but 'sysop'
2779 * and 'autoconfirmed' are also allowed for backwards compatibility. These should
2780 * be mapped to 'editprotected' and 'editsemiprotected' respectively.
2782 public function getRestrictions( $action ) {
2783 if ( !$this->mRestrictionsLoaded
) {
2784 $this->loadRestrictions();
2786 return isset( $this->mRestrictions
[$action] )
2787 ?
$this->mRestrictions
[$action]
2792 * Accessor/initialisation for mRestrictions
2794 * @return array Keys are actions, values are arrays as returned by
2795 * Title::getRestrictions()
2798 public function getAllRestrictions() {
2799 if ( !$this->mRestrictionsLoaded
) {
2800 $this->loadRestrictions();
2802 return $this->mRestrictions
;
2806 * Get the expiry time for the restriction against a given action
2808 * @param string $action
2809 * @return string|bool 14-char timestamp, or 'infinity' if the page is protected forever
2810 * or not protected at all, or false if the action is not recognised.
2812 public function getRestrictionExpiry( $action ) {
2813 if ( !$this->mRestrictionsLoaded
) {
2814 $this->loadRestrictions();
2816 return isset( $this->mRestrictionsExpiry
[$action] ) ?
$this->mRestrictionsExpiry
[$action] : false;
2820 * Returns cascading restrictions for the current article
2824 function areRestrictionsCascading() {
2825 if ( !$this->mRestrictionsLoaded
) {
2826 $this->loadRestrictions();
2829 return $this->mCascadeRestriction
;
2833 * Loads a string into mRestrictions array
2835 * @param ResultWrapper $res Resource restrictions as an SQL result.
2836 * @param string $oldFashionedRestrictions Comma-separated list of page
2837 * restrictions from page table (pre 1.10)
2839 private function loadRestrictionsFromResultWrapper( $res, $oldFashionedRestrictions = null ) {
2842 foreach ( $res as $row ) {
2846 $this->loadRestrictionsFromRows( $rows, $oldFashionedRestrictions );
2850 * Compiles list of active page restrictions from both page table (pre 1.10)
2851 * and page_restrictions table for this existing page.
2852 * Public for usage by LiquidThreads.
2854 * @param array $rows Array of db result objects
2855 * @param string $oldFashionedRestrictions Comma-separated list of page
2856 * restrictions from page table (pre 1.10)
2858 public function loadRestrictionsFromRows( $rows, $oldFashionedRestrictions = null ) {
2859 $dbr = wfGetDB( DB_SLAVE
);
2861 $restrictionTypes = $this->getRestrictionTypes();
2863 foreach ( $restrictionTypes as $type ) {
2864 $this->mRestrictions
[$type] = [];
2865 $this->mRestrictionsExpiry
[$type] = 'infinity';
2868 $this->mCascadeRestriction
= false;
2870 # Backwards-compatibility: also load the restrictions from the page record (old format).
2871 if ( $oldFashionedRestrictions !== null ) {
2872 $this->mOldRestrictions
= $oldFashionedRestrictions;
2875 if ( $this->mOldRestrictions
=== false ) {
2876 $this->mOldRestrictions
= $dbr->selectField( 'page', 'page_restrictions',
2877 [ 'page_id' => $this->getArticleID() ], __METHOD__
);
2880 if ( $this->mOldRestrictions
!= '' ) {
2881 foreach ( explode( ':', trim( $this->mOldRestrictions
) ) as $restrict ) {
2882 $temp = explode( '=', trim( $restrict ) );
2883 if ( count( $temp ) == 1 ) {
2884 // old old format should be treated as edit/move restriction
2885 $this->mRestrictions
['edit'] = explode( ',', trim( $temp[0] ) );
2886 $this->mRestrictions
['move'] = explode( ',', trim( $temp[0] ) );
2888 $restriction = trim( $temp[1] );
2889 if ( $restriction != '' ) { // some old entries are empty
2890 $this->mRestrictions
[$temp[0]] = explode( ',', $restriction );
2896 if ( count( $rows ) ) {
2897 # Current system - load second to make them override.
2898 $now = wfTimestampNow();
2900 # Cycle through all the restrictions.
2901 foreach ( $rows as $row ) {
2903 // Don't take care of restrictions types that aren't allowed
2904 if ( !in_array( $row->pr_type
, $restrictionTypes ) ) {
2908 // This code should be refactored, now that it's being used more generally,
2909 // But I don't really see any harm in leaving it in Block for now -werdna
2910 $expiry = $dbr->decodeExpiry( $row->pr_expiry
);
2912 // Only apply the restrictions if they haven't expired!
2913 if ( !$expiry ||
$expiry > $now ) {
2914 $this->mRestrictionsExpiry
[$row->pr_type
] = $expiry;
2915 $this->mRestrictions
[$row->pr_type
] = explode( ',', trim( $row->pr_level
) );
2917 $this->mCascadeRestriction |
= $row->pr_cascade
;
2922 $this->mRestrictionsLoaded
= true;
2926 * Load restrictions from the page_restrictions table
2928 * @param string $oldFashionedRestrictions Comma-separated list of page
2929 * restrictions from page table (pre 1.10)
2931 public function loadRestrictions( $oldFashionedRestrictions = null ) {
2932 if ( !$this->mRestrictionsLoaded
) {
2933 $dbr = wfGetDB( DB_SLAVE
);
2934 if ( $this->exists() ) {
2935 $res = $dbr->select(
2936 'page_restrictions',
2937 [ 'pr_type', 'pr_expiry', 'pr_level', 'pr_cascade' ],
2938 [ 'pr_page' => $this->getArticleID() ],
2942 $this->loadRestrictionsFromResultWrapper( $res, $oldFashionedRestrictions );
2944 $title_protection = $this->getTitleProtection();
2946 if ( $title_protection ) {
2947 $now = wfTimestampNow();
2948 $expiry = $dbr->decodeExpiry( $title_protection['expiry'] );
2950 if ( !$expiry ||
$expiry > $now ) {
2951 // Apply the restrictions
2952 $this->mRestrictionsExpiry
['create'] = $expiry;
2953 $this->mRestrictions
['create'] = explode( ',', trim( $title_protection['permission'] ) );
2954 } else { // Get rid of the old restrictions
2955 $this->mTitleProtection
= false;
2958 $this->mRestrictionsExpiry
['create'] = 'infinity';
2960 $this->mRestrictionsLoaded
= true;
2966 * Flush the protection cache in this object and force reload from the database.
2967 * This is used when updating protection from WikiPage::doUpdateRestrictions().
2969 public function flushRestrictions() {
2970 $this->mRestrictionsLoaded
= false;
2971 $this->mTitleProtection
= null;
2975 * Purge expired restrictions from the page_restrictions table
2977 * This will purge no more than $wgUpdateRowsPerQuery page_restrictions rows
2979 static function purgeExpiredRestrictions() {
2980 if ( wfReadOnly() ) {
2984 DeferredUpdates
::addUpdate( new AtomicSectionUpdate(
2985 wfGetDB( DB_MASTER
),
2987 function ( IDatabase
$dbw, $fname ) {
2988 $config = MediaWikiServices
::getInstance()->getMainConfig();
2989 $ids = $dbw->selectFieldValues(
2990 'page_restrictions',
2992 [ 'pr_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ],
2994 [ 'LIMIT' => $config->get( 'UpdateRowsPerQuery' ) ] // T135470
2997 $dbw->delete( 'page_restrictions', [ 'pr_id' => $ids ], $fname );
3002 DeferredUpdates
::addUpdate( new AtomicSectionUpdate(
3003 wfGetDB( DB_MASTER
),
3005 function ( IDatabase
$dbw, $fname ) {
3008 [ 'pt_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ],
3016 * Does this have subpages? (Warning, usually requires an extra DB query.)
3020 public function hasSubpages() {
3021 if ( !MWNamespace
::hasSubpages( $this->mNamespace
) ) {
3026 # We dynamically add a member variable for the purpose of this method
3027 # alone to cache the result. There's no point in having it hanging
3028 # around uninitialized in every Title object; therefore we only add it
3029 # if needed and don't declare it statically.
3030 if ( $this->mHasSubpages
=== null ) {
3031 $this->mHasSubpages
= false;
3032 $subpages = $this->getSubpages( 1 );
3033 if ( $subpages instanceof TitleArray
) {
3034 $this->mHasSubpages
= (bool)$subpages->count();
3038 return $this->mHasSubpages
;
3042 * Get all subpages of this page.
3044 * @param int $limit Maximum number of subpages to fetch; -1 for no limit
3045 * @return TitleArray|array TitleArray, or empty array if this page's namespace
3046 * doesn't allow subpages
3048 public function getSubpages( $limit = -1 ) {
3049 if ( !MWNamespace
::hasSubpages( $this->getNamespace() ) ) {
3053 $dbr = wfGetDB( DB_SLAVE
);
3054 $conds['page_namespace'] = $this->getNamespace();
3055 $conds[] = 'page_title ' . $dbr->buildLike( $this->getDBkey() . '/', $dbr->anyString() );
3057 if ( $limit > -1 ) {
3058 $options['LIMIT'] = $limit;
3060 $this->mSubpages
= TitleArray
::newFromResult(
3061 $dbr->select( 'page',
3062 [ 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' ],
3068 return $this->mSubpages
;
3072 * Is there a version of this page in the deletion archive?
3074 * @return int The number of archived revisions
3076 public function isDeleted() {
3077 if ( $this->getNamespace() < 0 ) {
3080 $dbr = wfGetDB( DB_SLAVE
);
3082 $n = $dbr->selectField( 'archive', 'COUNT(*)',
3083 [ 'ar_namespace' => $this->getNamespace(), 'ar_title' => $this->getDBkey() ],
3086 if ( $this->getNamespace() == NS_FILE
) {
3087 $n +
= $dbr->selectField( 'filearchive', 'COUNT(*)',
3088 [ 'fa_name' => $this->getDBkey() ],
3097 * Is there a version of this page in the deletion archive?
3101 public function isDeletedQuick() {
3102 if ( $this->getNamespace() < 0 ) {
3105 $dbr = wfGetDB( DB_SLAVE
);
3106 $deleted = (bool)$dbr->selectField( 'archive', '1',
3107 [ 'ar_namespace' => $this->getNamespace(), 'ar_title' => $this->getDBkey() ],
3110 if ( !$deleted && $this->getNamespace() == NS_FILE
) {
3111 $deleted = (bool)$dbr->selectField( 'filearchive', '1',
3112 [ 'fa_name' => $this->getDBkey() ],
3120 * Get the article ID for this Title from the link cache,
3121 * adding it if necessary
3123 * @param int $flags A bit field; may be Title::GAID_FOR_UPDATE to select
3125 * @return int The ID
3127 public function getArticleID( $flags = 0 ) {
3128 if ( $this->getNamespace() < 0 ) {
3129 $this->mArticleID
= 0;
3130 return $this->mArticleID
;
3132 $linkCache = LinkCache
::singleton();
3133 if ( $flags & self
::GAID_FOR_UPDATE
) {
3134 $oldUpdate = $linkCache->forUpdate( true );
3135 $linkCache->clearLink( $this );
3136 $this->mArticleID
= $linkCache->addLinkObj( $this );
3137 $linkCache->forUpdate( $oldUpdate );
3139 if ( -1 == $this->mArticleID
) {
3140 $this->mArticleID
= $linkCache->addLinkObj( $this );
3143 return $this->mArticleID
;
3147 * Is this an article that is a redirect page?
3148 * Uses link cache, adding it if necessary
3150 * @param int $flags A bit field; may be Title::GAID_FOR_UPDATE to select for update
3153 public function isRedirect( $flags = 0 ) {
3154 if ( !is_null( $this->mRedirect
) ) {
3155 return $this->mRedirect
;
3157 if ( !$this->getArticleID( $flags ) ) {
3158 $this->mRedirect
= false;
3159 return $this->mRedirect
;
3162 $linkCache = LinkCache
::singleton();
3163 $linkCache->addLinkObj( $this ); # in case we already had an article ID
3164 $cached = $linkCache->getGoodLinkFieldObj( $this, 'redirect' );
3165 if ( $cached === null ) {
3166 # Trust LinkCache's state over our own
3167 # LinkCache is telling us that the page doesn't exist, despite there being cached
3168 # data relating to an existing page in $this->mArticleID. Updaters should clear
3169 # LinkCache as appropriate, or use $flags = Title::GAID_FOR_UPDATE. If that flag is
3170 # set, then LinkCache will definitely be up to date here, since getArticleID() forces
3171 # LinkCache to refresh its data from the master.
3172 $this->mRedirect
= false;
3173 return $this->mRedirect
;
3176 $this->mRedirect
= (bool)$cached;
3178 return $this->mRedirect
;
3182 * What is the length of this page?
3183 * Uses link cache, adding it if necessary
3185 * @param int $flags A bit field; may be Title::GAID_FOR_UPDATE to select for update
3188 public function getLength( $flags = 0 ) {
3189 if ( $this->mLength
!= -1 ) {
3190 return $this->mLength
;
3192 if ( !$this->getArticleID( $flags ) ) {
3194 return $this->mLength
;
3196 $linkCache = LinkCache
::singleton();
3197 $linkCache->addLinkObj( $this ); # in case we already had an article ID
3198 $cached = $linkCache->getGoodLinkFieldObj( $this, 'length' );
3199 if ( $cached === null ) {
3200 # Trust LinkCache's state over our own, as for isRedirect()
3202 return $this->mLength
;
3205 $this->mLength
= intval( $cached );
3207 return $this->mLength
;
3211 * What is the page_latest field for this page?
3213 * @param int $flags A bit field; may be Title::GAID_FOR_UPDATE to select for update
3214 * @return int Int or 0 if the page doesn't exist
3216 public function getLatestRevID( $flags = 0 ) {
3217 if ( !( $flags & Title
::GAID_FOR_UPDATE
) && $this->mLatestID
!== false ) {
3218 return intval( $this->mLatestID
);
3220 if ( !$this->getArticleID( $flags ) ) {
3221 $this->mLatestID
= 0;
3222 return $this->mLatestID
;
3224 $linkCache = LinkCache
::singleton();
3225 $linkCache->addLinkObj( $this ); # in case we already had an article ID
3226 $cached = $linkCache->getGoodLinkFieldObj( $this, 'revision' );
3227 if ( $cached === null ) {
3228 # Trust LinkCache's state over our own, as for isRedirect()
3229 $this->mLatestID
= 0;
3230 return $this->mLatestID
;
3233 $this->mLatestID
= intval( $cached );
3235 return $this->mLatestID
;
3239 * This clears some fields in this object, and clears any associated
3240 * keys in the "bad links" section of the link cache.
3242 * - This is called from WikiPage::doEdit() and WikiPage::insertOn() to allow
3243 * loading of the new page_id. It's also called from
3244 * WikiPage::doDeleteArticleReal()
3246 * @param int $newid The new Article ID
3248 public function resetArticleID( $newid ) {
3249 $linkCache = LinkCache
::singleton();
3250 $linkCache->clearLink( $this );
3252 if ( $newid === false ) {
3253 $this->mArticleID
= -1;
3255 $this->mArticleID
= intval( $newid );
3257 $this->mRestrictionsLoaded
= false;
3258 $this->mRestrictions
= [];
3259 $this->mOldRestrictions
= false;
3260 $this->mRedirect
= null;
3261 $this->mLength
= -1;
3262 $this->mLatestID
= false;
3263 $this->mContentModel
= false;
3264 $this->mEstimateRevisions
= null;
3265 $this->mPageLanguage
= false;
3266 $this->mDbPageLanguage
= false;
3267 $this->mIsBigDeletion
= null;
3270 public static function clearCaches() {
3271 $linkCache = LinkCache
::singleton();
3272 $linkCache->clear();
3274 $titleCache = self
::getTitleCache();
3275 $titleCache->clear();
3279 * Capitalize a text string for a title if it belongs to a namespace that capitalizes
3281 * @param string $text Containing title to capitalize
3282 * @param int $ns Namespace index, defaults to NS_MAIN
3283 * @return string Containing capitalized title
3285 public static function capitalize( $text, $ns = NS_MAIN
) {
3288 if ( MWNamespace
::isCapitalized( $ns ) ) {
3289 return $wgContLang->ucfirst( $text );
3296 * Secure and split - main initialisation function for this object
3298 * Assumes that mDbkeyform has been set, and is urldecoded
3299 * and uses underscores, but not otherwise munged. This function
3300 * removes illegal characters, splits off the interwiki and
3301 * namespace prefixes, sets the other forms, and canonicalizes
3304 * @throws MalformedTitleException On invalid titles
3305 * @return bool True on success
3307 private function secureAndSplit() {
3309 $this->mInterwiki
= '';
3310 $this->mFragment
= '';
3311 $this->mNamespace
= $this->mDefaultNamespace
; # Usually NS_MAIN
3313 $dbkey = $this->mDbkeyform
;
3315 // @note: splitTitleString() is a temporary hack to allow MediaWikiTitleCodec to share
3316 // the parsing code with Title, while avoiding massive refactoring.
3317 // @todo: get rid of secureAndSplit, refactor parsing code.
3318 // @note: getTitleParser() returns a TitleParser implementation which does not have a
3319 // splitTitleString method, but the only implementation (MediaWikiTitleCodec) does
3320 $titleCodec = MediaWikiServices
::getInstance()->getTitleParser();
3321 // MalformedTitleException can be thrown here
3322 $parts = $titleCodec->splitTitleString( $dbkey, $this->getDefaultNamespace() );
3325 $this->setFragment( '#' . $parts['fragment'] );
3326 $this->mInterwiki
= $parts['interwiki'];
3327 $this->mLocalInterwiki
= $parts['local_interwiki'];
3328 $this->mNamespace
= $parts['namespace'];
3329 $this->mUserCaseDBKey
= $parts['user_case_dbkey'];
3331 $this->mDbkeyform
= $parts['dbkey'];
3332 $this->mUrlform
= wfUrlencode( $this->mDbkeyform
);
3333 $this->mTextform
= strtr( $this->mDbkeyform
, '_', ' ' );
3335 # We already know that some pages won't be in the database!
3336 if ( $this->isExternal() ||
$this->mNamespace
== NS_SPECIAL
) {
3337 $this->mArticleID
= 0;
3344 * Get an array of Title objects linking to this Title
3345 * Also stores the IDs in the link cache.
3347 * WARNING: do not use this function on arbitrary user-supplied titles!
3348 * On heavily-used templates it will max out the memory.
3350 * @param array $options May be FOR UPDATE
3351 * @param string $table Table name
3352 * @param string $prefix Fields prefix
3353 * @return Title[] Array of Title objects linking here
3355 public function getLinksTo( $options = [], $table = 'pagelinks', $prefix = 'pl' ) {
3356 if ( count( $options ) > 0 ) {
3357 $db = wfGetDB( DB_MASTER
);
3359 $db = wfGetDB( DB_SLAVE
);
3364 self
::getSelectFields(),
3366 "{$prefix}_from=page_id",
3367 "{$prefix}_namespace" => $this->getNamespace(),
3368 "{$prefix}_title" => $this->getDBkey() ],
3374 if ( $res->numRows() ) {
3375 $linkCache = LinkCache
::singleton();
3376 foreach ( $res as $row ) {
3377 $titleObj = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
3379 $linkCache->addGoodLinkObjFromRow( $titleObj, $row );
3380 $retVal[] = $titleObj;
3388 * Get an array of Title objects using this Title as a template
3389 * Also stores the IDs in the link cache.
3391 * WARNING: do not use this function on arbitrary user-supplied titles!
3392 * On heavily-used templates it will max out the memory.
3394 * @param array $options Query option to Database::select()
3395 * @return Title[] Array of Title the Title objects linking here
3397 public function getTemplateLinksTo( $options = [] ) {
3398 return $this->getLinksTo( $options, 'templatelinks', 'tl' );
3402 * Get an array of Title objects linked from this Title
3403 * Also stores the IDs in the link cache.
3405 * WARNING: do not use this function on arbitrary user-supplied titles!
3406 * On heavily-used templates it will max out the memory.
3408 * @param array $options Query option to Database::select()
3409 * @param string $table Table name
3410 * @param string $prefix Fields prefix
3411 * @return array Array of Title objects linking here
3413 public function getLinksFrom( $options = [], $table = 'pagelinks', $prefix = 'pl' ) {
3414 $id = $this->getArticleID();
3416 # If the page doesn't exist; there can't be any link from this page
3421 $db = wfGetDB( DB_SLAVE
);
3423 $blNamespace = "{$prefix}_namespace";
3424 $blTitle = "{$prefix}_title";
3429 [ $blNamespace, $blTitle ],
3430 WikiPage
::selectFields()
3432 [ "{$prefix}_from" => $id ],
3437 [ "page_namespace=$blNamespace", "page_title=$blTitle" ]
3442 $linkCache = LinkCache
::singleton();
3443 foreach ( $res as $row ) {
3444 if ( $row->page_id
) {
3445 $titleObj = Title
::newFromRow( $row );
3447 $titleObj = Title
::makeTitle( $row->$blNamespace, $row->$blTitle );
3448 $linkCache->addBadLinkObj( $titleObj );
3450 $retVal[] = $titleObj;
3457 * Get an array of Title objects used on this Title as a template
3458 * Also stores the IDs in the link cache.
3460 * WARNING: do not use this function on arbitrary user-supplied titles!
3461 * On heavily-used templates it will max out the memory.
3463 * @param array $options May be FOR UPDATE
3464 * @return Title[] Array of Title the Title objects used here
3466 public function getTemplateLinksFrom( $options = [] ) {
3467 return $this->getLinksFrom( $options, 'templatelinks', 'tl' );
3471 * Get an array of Title objects referring to non-existent articles linked
3474 * @todo check if needed (used only in SpecialBrokenRedirects.php, and
3475 * should use redirect table in this case).
3476 * @return Title[] Array of Title the Title objects
3478 public function getBrokenLinksFrom() {
3479 if ( $this->getArticleID() == 0 ) {
3480 # All links from article ID 0 are false positives
3484 $dbr = wfGetDB( DB_SLAVE
);
3485 $res = $dbr->select(
3486 [ 'page', 'pagelinks' ],
3487 [ 'pl_namespace', 'pl_title' ],
3489 'pl_from' => $this->getArticleID(),
3490 'page_namespace IS NULL'
3496 [ 'pl_namespace=page_namespace', 'pl_title=page_title' ]
3502 foreach ( $res as $row ) {
3503 $retVal[] = Title
::makeTitle( $row->pl_namespace
, $row->pl_title
);
3509 * Get a list of URLs to purge from the CDN cache when this
3512 * @return string[] Array of String the URLs
3514 public function getCdnUrls() {
3516 $this->getInternalURL(),
3517 $this->getInternalURL( 'action=history' )
3520 $pageLang = $this->getPageLanguage();
3521 if ( $pageLang->hasVariants() ) {
3522 $variants = $pageLang->getVariants();
3523 foreach ( $variants as $vCode ) {
3524 $urls[] = $this->getInternalURL( $vCode );
3528 // If we are looking at a css/js user subpage, purge the action=raw.
3529 if ( $this->isJsSubpage() ) {
3530 $urls[] = $this->getInternalURL( 'action=raw&ctype=text/javascript' );
3531 } elseif ( $this->isCssSubpage() ) {
3532 $urls[] = $this->getInternalURL( 'action=raw&ctype=text/css' );
3535 Hooks
::run( 'TitleSquidURLs', [ $this, &$urls ] );
3540 * @deprecated since 1.27 use getCdnUrls()
3542 public function getSquidURLs() {
3543 return $this->getCdnUrls();
3547 * Purge all applicable CDN URLs
3549 public function purgeSquid() {
3550 DeferredUpdates
::addUpdate(
3551 new CdnCacheUpdate( $this->getCdnUrls() ),
3552 DeferredUpdates
::PRESEND
3557 * Move this page without authentication
3559 * @deprecated since 1.25 use MovePage class instead
3560 * @param Title $nt The new page Title
3561 * @return array|bool True on success, getUserPermissionsErrors()-like array on failure
3563 public function moveNoAuth( &$nt ) {
3564 wfDeprecated( __METHOD__
, '1.25' );
3565 return $this->moveTo( $nt, false );
3569 * Check whether a given move operation would be valid.
3570 * Returns true if ok, or a getUserPermissionsErrors()-like array otherwise
3572 * @deprecated since 1.25, use MovePage's methods instead
3573 * @param Title $nt The new title
3574 * @param bool $auth Whether to check user permissions (uses $wgUser)
3575 * @param string $reason Is the log summary of the move, used for spam checking
3576 * @return array|bool True on success, getUserPermissionsErrors()-like array on failure
3578 public function isValidMoveOperation( &$nt, $auth = true, $reason = '' ) {
3581 if ( !( $nt instanceof Title
) ) {
3582 // Normally we'd add this to $errors, but we'll get
3583 // lots of syntax errors if $nt is not an object
3584 return [ [ 'badtitletext' ] ];
3587 $mp = new MovePage( $this, $nt );
3588 $errors = $mp->isValidMove()->getErrorsArray();
3590 $errors = wfMergeErrorArrays(
3592 $mp->checkPermissions( $wgUser, $reason )->getErrorsArray()
3596 return $errors ?
: true;
3600 * Check if the requested move target is a valid file move target
3601 * @todo move this to MovePage
3602 * @param Title $nt Target title
3603 * @return array List of errors
3605 protected function validateFileMoveOperation( $nt ) {
3610 $destFile = wfLocalFile( $nt );
3611 $destFile->load( File
::READ_LATEST
);
3612 if ( !$wgUser->isAllowed( 'reupload-shared' )
3613 && !$destFile->exists() && wfFindFile( $nt )
3615 $errors[] = [ 'file-exists-sharedrepo' ];
3622 * Move a title to a new location
3624 * @deprecated since 1.25, use the MovePage class instead
3625 * @param Title $nt The new title
3626 * @param bool $auth Indicates whether $wgUser's permissions
3628 * @param string $reason The reason for the move
3629 * @param bool $createRedirect Whether to create a redirect from the old title to the new title.
3630 * Ignored if the user doesn't have the suppressredirect right.
3631 * @return array|bool True on success, getUserPermissionsErrors()-like array on failure
3633 public function moveTo( &$nt, $auth = true, $reason = '', $createRedirect = true ) {
3635 $err = $this->isValidMoveOperation( $nt, $auth, $reason );
3636 if ( is_array( $err ) ) {
3637 // Auto-block user's IP if the account was "hard" blocked
3638 $wgUser->spreadAnyEditBlock();
3641 // Check suppressredirect permission
3642 if ( $auth && !$wgUser->isAllowed( 'suppressredirect' ) ) {
3643 $createRedirect = true;
3646 $mp = new MovePage( $this, $nt );
3647 $status = $mp->move( $wgUser, $reason, $createRedirect );
3648 if ( $status->isOK() ) {
3651 return $status->getErrorsArray();
3656 * Move this page's subpages to be subpages of $nt
3658 * @param Title $nt Move target
3659 * @param bool $auth Whether $wgUser's permissions should be checked
3660 * @param string $reason The reason for the move
3661 * @param bool $createRedirect Whether to create redirects from the old subpages to
3662 * the new ones Ignored if the user doesn't have the 'suppressredirect' right
3663 * @return array Array with old page titles as keys, and strings (new page titles) or
3664 * arrays (errors) as values, or an error array with numeric indices if no pages
3667 public function moveSubpages( $nt, $auth = true, $reason = '', $createRedirect = true ) {
3668 global $wgMaximumMovedPages;
3669 // Check permissions
3670 if ( !$this->userCan( 'move-subpages' ) ) {
3671 return [ 'cant-move-subpages' ];
3673 // Do the source and target namespaces support subpages?
3674 if ( !MWNamespace
::hasSubpages( $this->getNamespace() ) ) {
3675 return [ 'namespace-nosubpages',
3676 MWNamespace
::getCanonicalName( $this->getNamespace() ) ];
3678 if ( !MWNamespace
::hasSubpages( $nt->getNamespace() ) ) {
3679 return [ 'namespace-nosubpages',
3680 MWNamespace
::getCanonicalName( $nt->getNamespace() ) ];
3683 $subpages = $this->getSubpages( $wgMaximumMovedPages +
1 );
3686 foreach ( $subpages as $oldSubpage ) {
3688 if ( $count > $wgMaximumMovedPages ) {
3689 $retval[$oldSubpage->getPrefixedText()] =
3690 [ 'movepage-max-pages',
3691 $wgMaximumMovedPages ];
3695 // We don't know whether this function was called before
3696 // or after moving the root page, so check both
3698 if ( $oldSubpage->getArticleID() == $this->getArticleID()
3699 ||
$oldSubpage->getArticleID() == $nt->getArticleID()
3701 // When moving a page to a subpage of itself,
3702 // don't move it twice
3705 $newPageName = preg_replace(
3706 '#^' . preg_quote( $this->getDBkey(), '#' ) . '#',
3707 StringUtils
::escapeRegexReplacement( $nt->getDBkey() ), # bug 21234
3708 $oldSubpage->getDBkey() );
3709 if ( $oldSubpage->isTalkPage() ) {
3710 $newNs = $nt->getTalkPage()->getNamespace();
3712 $newNs = $nt->getSubjectPage()->getNamespace();
3714 # Bug 14385: we need makeTitleSafe because the new page names may
3715 # be longer than 255 characters.
3716 $newSubpage = Title
::makeTitleSafe( $newNs, $newPageName );
3718 $success = $oldSubpage->moveTo( $newSubpage, $auth, $reason, $createRedirect );
3719 if ( $success === true ) {
3720 $retval[$oldSubpage->getPrefixedText()] = $newSubpage->getPrefixedText();
3722 $retval[$oldSubpage->getPrefixedText()] = $success;
3729 * Checks if this page is just a one-rev redirect.
3730 * Adds lock, so don't use just for light purposes.
3734 public function isSingleRevRedirect() {
3735 global $wgContentHandlerUseDB;
3737 $dbw = wfGetDB( DB_MASTER
);
3740 $fields = [ 'page_is_redirect', 'page_latest', 'page_id' ];
3741 if ( $wgContentHandlerUseDB ) {
3742 $fields[] = 'page_content_model';
3745 $row = $dbw->selectRow( 'page',
3751 # Cache some fields we may want
3752 $this->mArticleID
= $row ?
intval( $row->page_id
) : 0;
3753 $this->mRedirect
= $row ?
(bool)$row->page_is_redirect
: false;
3754 $this->mLatestID
= $row ?
intval( $row->page_latest
) : false;
3755 $this->mContentModel
= $row && isset( $row->page_content_model
)
3756 ?
strval( $row->page_content_model
)
3759 if ( !$this->mRedirect
) {
3762 # Does the article have a history?
3763 $row = $dbw->selectField( [ 'page', 'revision' ],
3765 [ 'page_namespace' => $this->getNamespace(),
3766 'page_title' => $this->getDBkey(),
3768 'page_latest != rev_id'
3773 # Return true if there was no history
3774 return ( $row === false );
3778 * Checks if $this can be moved to a given Title
3779 * - Selects for update, so don't call it unless you mean business
3781 * @deprecated since 1.25, use MovePage's methods instead
3782 * @param Title $nt The new title to check
3785 public function isValidMoveTarget( $nt ) {
3786 # Is it an existing file?
3787 if ( $nt->getNamespace() == NS_FILE
) {
3788 $file = wfLocalFile( $nt );
3789 $file->load( File
::READ_LATEST
);
3790 if ( $file->exists() ) {
3791 wfDebug( __METHOD__
. ": file exists\n" );
3795 # Is it a redirect with no history?
3796 if ( !$nt->isSingleRevRedirect() ) {
3797 wfDebug( __METHOD__
. ": not a one-rev redirect\n" );
3800 # Get the article text
3801 $rev = Revision
::newFromTitle( $nt, false, Revision
::READ_LATEST
);
3802 if ( !is_object( $rev ) ) {
3805 $content = $rev->getContent();
3806 # Does the redirect point to the source?
3807 # Or is it a broken self-redirect, usually caused by namespace collisions?
3808 $redirTitle = $content ?
$content->getRedirectTarget() : null;
3810 if ( $redirTitle ) {
3811 if ( $redirTitle->getPrefixedDBkey() != $this->getPrefixedDBkey() &&
3812 $redirTitle->getPrefixedDBkey() != $nt->getPrefixedDBkey() ) {
3813 wfDebug( __METHOD__
. ": redirect points to other page\n" );
3819 # Fail safe (not a redirect after all. strange.)
3820 wfDebug( __METHOD__
. ": failsafe: database sais " . $nt->getPrefixedDBkey() .
3821 " is a redirect, but it doesn't contain a valid redirect.\n" );
3827 * Get categories to which this Title belongs and return an array of
3828 * categories' names.
3830 * @return array Array of parents in the form:
3831 * $parent => $currentarticle
3833 public function getParentCategories() {
3838 $titleKey = $this->getArticleID();
3840 if ( $titleKey === 0 ) {
3844 $dbr = wfGetDB( DB_SLAVE
);
3846 $res = $dbr->select(
3849 [ 'cl_from' => $titleKey ],
3853 if ( $res->numRows() > 0 ) {
3854 foreach ( $res as $row ) {
3855 // $data[] = Title::newFromText($wgContLang->getNsText ( NS_CATEGORY ).':'.$row->cl_to);
3856 $data[$wgContLang->getNsText( NS_CATEGORY
) . ':' . $row->cl_to
] = $this->getFullText();
3863 * Get a tree of parent categories
3865 * @param array $children Array with the children in the keys, to check for circular refs
3866 * @return array Tree of parent categories
3868 public function getParentCategoryTree( $children = [] ) {
3870 $parents = $this->getParentCategories();
3873 foreach ( $parents as $parent => $current ) {
3874 if ( array_key_exists( $parent, $children ) ) {
3875 # Circular reference
3876 $stack[$parent] = [];
3878 $nt = Title
::newFromText( $parent );
3880 $stack[$parent] = $nt->getParentCategoryTree( $children +
[ $parent => 1 ] );
3890 * Get an associative array for selecting this title from
3893 * @return array Array suitable for the $where parameter of DB::select()
3895 public function pageCond() {
3896 if ( $this->mArticleID
> 0 ) {
3897 // PK avoids secondary lookups in InnoDB, shouldn't hurt other DBs
3898 return [ 'page_id' => $this->mArticleID
];
3900 return [ 'page_namespace' => $this->mNamespace
, 'page_title' => $this->mDbkeyform
];
3905 * Get the revision ID of the previous revision
3907 * @param int $revId Revision ID. Get the revision that was before this one.
3908 * @param int $flags Title::GAID_FOR_UPDATE
3909 * @return int|bool Old revision ID, or false if none exists
3911 public function getPreviousRevisionID( $revId, $flags = 0 ) {
3912 $db = ( $flags & self
::GAID_FOR_UPDATE
) ?
wfGetDB( DB_MASTER
) : wfGetDB( DB_SLAVE
);
3913 $revId = $db->selectField( 'revision', 'rev_id',
3915 'rev_page' => $this->getArticleID( $flags ),
3916 'rev_id < ' . intval( $revId )
3919 [ 'ORDER BY' => 'rev_id DESC' ]
3922 if ( $revId === false ) {
3925 return intval( $revId );
3930 * Get the revision ID of the next revision
3932 * @param int $revId Revision ID. Get the revision that was after this one.
3933 * @param int $flags Title::GAID_FOR_UPDATE
3934 * @return int|bool Next revision ID, or false if none exists
3936 public function getNextRevisionID( $revId, $flags = 0 ) {
3937 $db = ( $flags & self
::GAID_FOR_UPDATE
) ?
wfGetDB( DB_MASTER
) : wfGetDB( DB_SLAVE
);
3938 $revId = $db->selectField( 'revision', 'rev_id',
3940 'rev_page' => $this->getArticleID( $flags ),
3941 'rev_id > ' . intval( $revId )
3944 [ 'ORDER BY' => 'rev_id' ]
3947 if ( $revId === false ) {
3950 return intval( $revId );
3955 * Get the first revision of the page
3957 * @param int $flags Title::GAID_FOR_UPDATE
3958 * @return Revision|null If page doesn't exist
3960 public function getFirstRevision( $flags = 0 ) {
3961 $pageId = $this->getArticleID( $flags );
3963 $db = ( $flags & self
::GAID_FOR_UPDATE
) ?
wfGetDB( DB_MASTER
) : wfGetDB( DB_SLAVE
);
3964 $row = $db->selectRow( 'revision', Revision
::selectFields(),
3965 [ 'rev_page' => $pageId ],
3967 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 1 ]
3970 return new Revision( $row );
3977 * Get the oldest revision timestamp of this page
3979 * @param int $flags Title::GAID_FOR_UPDATE
3980 * @return string MW timestamp
3982 public function getEarliestRevTime( $flags = 0 ) {
3983 $rev = $this->getFirstRevision( $flags );
3984 return $rev ?
$rev->getTimestamp() : null;
3988 * Check if this is a new page
3992 public function isNewPage() {
3993 $dbr = wfGetDB( DB_SLAVE
);
3994 return (bool)$dbr->selectField( 'page', 'page_is_new', $this->pageCond(), __METHOD__
);
3998 * Check whether the number of revisions of this page surpasses $wgDeleteRevisionsLimit
4002 public function isBigDeletion() {
4003 global $wgDeleteRevisionsLimit;
4005 if ( !$wgDeleteRevisionsLimit ) {
4009 if ( $this->mIsBigDeletion
=== null ) {
4010 $dbr = wfGetDB( DB_SLAVE
);
4012 $revCount = $dbr->selectRowCount(
4015 [ 'rev_page' => $this->getArticleID() ],
4017 [ 'LIMIT' => $wgDeleteRevisionsLimit +
1 ]
4020 $this->mIsBigDeletion
= $revCount > $wgDeleteRevisionsLimit;
4023 return $this->mIsBigDeletion
;
4027 * Get the approximate revision count of this page.
4031 public function estimateRevisionCount() {
4032 if ( !$this->exists() ) {
4036 if ( $this->mEstimateRevisions
=== null ) {
4037 $dbr = wfGetDB( DB_SLAVE
);
4038 $this->mEstimateRevisions
= $dbr->estimateRowCount( 'revision', '*',
4039 [ 'rev_page' => $this->getArticleID() ], __METHOD__
);
4042 return $this->mEstimateRevisions
;
4046 * Get the number of revisions between the given revision.
4047 * Used for diffs and other things that really need it.
4049 * @param int|Revision $old Old revision or rev ID (first before range)
4050 * @param int|Revision $new New revision or rev ID (first after range)
4051 * @param int|null $max Limit of Revisions to count, will be incremented to detect truncations
4052 * @return int Number of revisions between these revisions.
4054 public function countRevisionsBetween( $old, $new, $max = null ) {
4055 if ( !( $old instanceof Revision
) ) {
4056 $old = Revision
::newFromTitle( $this, (int)$old );
4058 if ( !( $new instanceof Revision
) ) {
4059 $new = Revision
::newFromTitle( $this, (int)$new );
4061 if ( !$old ||
!$new ) {
4062 return 0; // nothing to compare
4064 $dbr = wfGetDB( DB_SLAVE
);
4066 'rev_page' => $this->getArticleID(),
4067 'rev_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( $old->getTimestamp() ) ),
4068 'rev_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( $new->getTimestamp() ) )
4070 if ( $max !== null ) {
4071 return $dbr->selectRowCount( 'revision', '1',
4074 [ 'LIMIT' => $max +
1 ] // extra to detect truncation
4077 return (int)$dbr->selectField( 'revision', 'count(*)', $conds, __METHOD__
);
4082 * Get the authors between the given revisions or revision IDs.
4083 * Used for diffs and other things that really need it.
4087 * @param int|Revision $old Old revision or rev ID (first before range by default)
4088 * @param int|Revision $new New revision or rev ID (first after range by default)
4089 * @param int $limit Maximum number of authors
4090 * @param string|array $options (Optional): Single option, or an array of options:
4091 * 'include_old' Include $old in the range; $new is excluded.
4092 * 'include_new' Include $new in the range; $old is excluded.
4093 * 'include_both' Include both $old and $new in the range.
4094 * Unknown option values are ignored.
4095 * @return array|null Names of revision authors in the range; null if not both revisions exist
4097 public function getAuthorsBetween( $old, $new, $limit, $options = [] ) {
4098 if ( !( $old instanceof Revision
) ) {
4099 $old = Revision
::newFromTitle( $this, (int)$old );
4101 if ( !( $new instanceof Revision
) ) {
4102 $new = Revision
::newFromTitle( $this, (int)$new );
4104 // XXX: what if Revision objects are passed in, but they don't refer to this title?
4105 // Add $old->getPage() != $new->getPage() || $old->getPage() != $this->getArticleID()
4106 // in the sanity check below?
4107 if ( !$old ||
!$new ) {
4108 return null; // nothing to compare
4113 $options = (array)$options;
4114 if ( in_array( 'include_old', $options ) ) {
4117 if ( in_array( 'include_new', $options ) ) {
4120 if ( in_array( 'include_both', $options ) ) {
4124 // No DB query needed if $old and $new are the same or successive revisions:
4125 if ( $old->getId() === $new->getId() ) {
4126 return ( $old_cmp === '>' && $new_cmp === '<' ) ?
4128 [ $old->getUserText( Revision
::RAW
) ];
4129 } elseif ( $old->getId() === $new->getParentId() ) {
4130 if ( $old_cmp === '>=' && $new_cmp === '<=' ) {
4131 $authors[] = $old->getUserText( Revision
::RAW
);
4132 if ( $old->getUserText( Revision
::RAW
) != $new->getUserText( Revision
::RAW
) ) {
4133 $authors[] = $new->getUserText( Revision
::RAW
);
4135 } elseif ( $old_cmp === '>=' ) {
4136 $authors[] = $old->getUserText( Revision
::RAW
);
4137 } elseif ( $new_cmp === '<=' ) {
4138 $authors[] = $new->getUserText( Revision
::RAW
);
4142 $dbr = wfGetDB( DB_SLAVE
);
4143 $res = $dbr->select( 'revision', 'DISTINCT rev_user_text',
4145 'rev_page' => $this->getArticleID(),
4146 "rev_timestamp $old_cmp " . $dbr->addQuotes( $dbr->timestamp( $old->getTimestamp() ) ),
4147 "rev_timestamp $new_cmp " . $dbr->addQuotes( $dbr->timestamp( $new->getTimestamp() ) )
4149 [ 'LIMIT' => $limit +
1 ] // add one so caller knows it was truncated
4151 foreach ( $res as $row ) {
4152 $authors[] = $row->rev_user_text
;
4158 * Get the number of authors between the given revisions or revision IDs.
4159 * Used for diffs and other things that really need it.
4161 * @param int|Revision $old Old revision or rev ID (first before range by default)
4162 * @param int|Revision $new New revision or rev ID (first after range by default)
4163 * @param int $limit Maximum number of authors
4164 * @param string|array $options (Optional): Single option, or an array of options:
4165 * 'include_old' Include $old in the range; $new is excluded.
4166 * 'include_new' Include $new in the range; $old is excluded.
4167 * 'include_both' Include both $old and $new in the range.
4168 * Unknown option values are ignored.
4169 * @return int Number of revision authors in the range; zero if not both revisions exist
4171 public function countAuthorsBetween( $old, $new, $limit, $options = [] ) {
4172 $authors = $this->getAuthorsBetween( $old, $new, $limit, $options );
4173 return $authors ?
count( $authors ) : 0;
4177 * Compare with another title.
4179 * @param Title $title
4182 public function equals( Title
$title ) {
4183 // Note: === is necessary for proper matching of number-like titles.
4184 return $this->getInterwiki() === $title->getInterwiki()
4185 && $this->getNamespace() == $title->getNamespace()
4186 && $this->getDBkey() === $title->getDBkey();
4190 * Check if this title is a subpage of another title
4192 * @param Title $title
4195 public function isSubpageOf( Title
$title ) {
4196 return $this->getInterwiki() === $title->getInterwiki()
4197 && $this->getNamespace() == $title->getNamespace()
4198 && strpos( $this->getDBkey(), $title->getDBkey() . '/' ) === 0;
4202 * Check if page exists. For historical reasons, this function simply
4203 * checks for the existence of the title in the page table, and will
4204 * thus return false for interwiki links, special pages and the like.
4205 * If you want to know if a title can be meaningfully viewed, you should
4206 * probably call the isKnown() method instead.
4208 * @param int $flags An optional bit field; may be Title::GAID_FOR_UPDATE to check
4209 * from master/for update
4212 public function exists( $flags = 0 ) {
4213 $exists = $this->getArticleID( $flags ) != 0;
4214 Hooks
::run( 'TitleExists', [ $this, &$exists ] );
4219 * Should links to this title be shown as potentially viewable (i.e. as
4220 * "bluelinks"), even if there's no record by this title in the page
4223 * This function is semi-deprecated for public use, as well as somewhat
4224 * misleadingly named. You probably just want to call isKnown(), which
4225 * calls this function internally.
4227 * (ISSUE: Most of these checks are cheap, but the file existence check
4228 * can potentially be quite expensive. Including it here fixes a lot of
4229 * existing code, but we might want to add an optional parameter to skip
4230 * it and any other expensive checks.)
4234 public function isAlwaysKnown() {
4238 * Allows overriding default behavior for determining if a page exists.
4239 * If $isKnown is kept as null, regular checks happen. If it's
4240 * a boolean, this value is returned by the isKnown method.
4244 * @param Title $title
4245 * @param bool|null $isKnown
4247 Hooks
::run( 'TitleIsAlwaysKnown', [ $this, &$isKnown ] );
4249 if ( !is_null( $isKnown ) ) {
4253 if ( $this->isExternal() ) {
4254 return true; // any interwiki link might be viewable, for all we know
4257 switch ( $this->mNamespace
) {
4260 // file exists, possibly in a foreign repo
4261 return (bool)wfFindFile( $this );
4263 // valid special page
4264 return SpecialPageFactory
::exists( $this->getDBkey() );
4266 // selflink, possibly with fragment
4267 return $this->mDbkeyform
== '';
4269 // known system message
4270 return $this->hasSourceText() !== false;
4277 * Does this title refer to a page that can (or might) be meaningfully
4278 * viewed? In particular, this function may be used to determine if
4279 * links to the title should be rendered as "bluelinks" (as opposed to
4280 * "redlinks" to non-existent pages).
4281 * Adding something else to this function will cause inconsistency
4282 * since LinkHolderArray calls isAlwaysKnown() and does its own
4283 * page existence check.
4287 public function isKnown() {
4288 return $this->isAlwaysKnown() ||
$this->exists();
4292 * Does this page have source text?
4296 public function hasSourceText() {
4297 if ( $this->exists() ) {
4301 if ( $this->mNamespace
== NS_MEDIAWIKI
) {
4302 // If the page doesn't exist but is a known system message, default
4303 // message content will be displayed, same for language subpages-
4304 // Use always content language to avoid loading hundreds of languages
4305 // to get the link color.
4307 list( $name, ) = MessageCache
::singleton()->figureMessage(
4308 $wgContLang->lcfirst( $this->getText() )
4310 $message = wfMessage( $name )->inLanguage( $wgContLang )->useDatabase( false );
4311 return $message->exists();
4318 * Get the default message text or false if the message doesn't exist
4320 * @return string|bool
4322 public function getDefaultMessageText() {
4325 if ( $this->getNamespace() != NS_MEDIAWIKI
) { // Just in case
4329 list( $name, $lang ) = MessageCache
::singleton()->figureMessage(
4330 $wgContLang->lcfirst( $this->getText() )
4332 $message = wfMessage( $name )->inLanguage( $lang )->useDatabase( false );
4334 if ( $message->exists() ) {
4335 return $message->plain();
4342 * Updates page_touched for this page; called from LinksUpdate.php
4344 * @param string $purgeTime [optional] TS_MW timestamp
4345 * @return bool True if the update succeeded
4347 public function invalidateCache( $purgeTime = null ) {
4348 if ( wfReadOnly() ) {
4352 if ( $this->mArticleID
=== 0 ) {
4353 return true; // avoid gap locking if we know it's not there
4356 $method = __METHOD__
;
4357 $dbw = wfGetDB( DB_MASTER
);
4358 $conds = $this->pageCond();
4359 $dbw->onTransactionIdle( function () use ( $dbw, $conds, $method, $purgeTime ) {
4360 $dbTimestamp = $dbw->timestamp( $purgeTime ?
: time() );
4364 [ 'page_touched' => $dbTimestamp ],
4365 $conds +
[ 'page_touched < ' . $dbw->addQuotes( $dbTimestamp ) ],
4374 * Update page_touched timestamps and send CDN purge messages for
4375 * pages linking to this title. May be sent to the job queue depending
4376 * on the number of links. Typically called on create and delete.
4378 public function touchLinks() {
4379 DeferredUpdates
::addUpdate( new HTMLCacheUpdate( $this, 'pagelinks' ) );
4380 if ( $this->getNamespace() == NS_CATEGORY
) {
4381 DeferredUpdates
::addUpdate( new HTMLCacheUpdate( $this, 'categorylinks' ) );
4386 * Get the last touched timestamp
4388 * @param IDatabase $db Optional db
4389 * @return string Last-touched timestamp
4391 public function getTouched( $db = null ) {
4392 if ( $db === null ) {
4393 $db = wfGetDB( DB_SLAVE
);
4395 $touched = $db->selectField( 'page', 'page_touched', $this->pageCond(), __METHOD__
);
4400 * Get the timestamp when this page was updated since the user last saw it.
4403 * @return string|null
4405 public function getNotificationTimestamp( $user = null ) {
4408 // Assume current user if none given
4412 // Check cache first
4413 $uid = $user->getId();
4417 // avoid isset here, as it'll return false for null entries
4418 if ( array_key_exists( $uid, $this->mNotificationTimestamp
) ) {
4419 return $this->mNotificationTimestamp
[$uid];
4421 // Don't cache too much!
4422 if ( count( $this->mNotificationTimestamp
) >= self
::CACHE_MAX
) {
4423 $this->mNotificationTimestamp
= [];
4426 $store = MediaWikiServices
::getInstance()->getWatchedItemStore();
4427 $watchedItem = $store->getWatchedItem( $user, $this );
4428 if ( $watchedItem ) {
4429 $this->mNotificationTimestamp
[$uid] = $watchedItem->getNotificationTimestamp();
4431 $this->mNotificationTimestamp
[$uid] = false;
4434 return $this->mNotificationTimestamp
[$uid];
4438 * Generate strings used for xml 'id' names in monobook tabs
4440 * @param string $prepend Defaults to 'nstab-'
4441 * @return string XML 'id' name
4443 public function getNamespaceKey( $prepend = 'nstab-' ) {
4445 // Gets the subject namespace if this title
4446 $namespace = MWNamespace
::getSubject( $this->getNamespace() );
4447 // Checks if canonical namespace name exists for namespace
4448 if ( MWNamespace
::exists( $this->getNamespace() ) ) {
4449 // Uses canonical namespace name
4450 $namespaceKey = MWNamespace
::getCanonicalName( $namespace );
4452 // Uses text of namespace
4453 $namespaceKey = $this->getSubjectNsText();
4455 // Makes namespace key lowercase
4456 $namespaceKey = $wgContLang->lc( $namespaceKey );
4458 if ( $namespaceKey == '' ) {
4459 $namespaceKey = 'main';
4461 // Changes file to image for backwards compatibility
4462 if ( $namespaceKey == 'file' ) {
4463 $namespaceKey = 'image';
4465 return $prepend . $namespaceKey;
4469 * Get all extant redirects to this Title
4471 * @param int|null $ns Single namespace to consider; null to consider all namespaces
4472 * @return Title[] Array of Title redirects to this title
4474 public function getRedirectsHere( $ns = null ) {
4477 $dbr = wfGetDB( DB_SLAVE
);
4479 'rd_namespace' => $this->getNamespace(),
4480 'rd_title' => $this->getDBkey(),
4483 if ( $this->isExternal() ) {
4484 $where['rd_interwiki'] = $this->getInterwiki();
4486 $where[] = 'rd_interwiki = ' . $dbr->addQuotes( '' ) . ' OR rd_interwiki IS NULL';
4488 if ( !is_null( $ns ) ) {
4489 $where['page_namespace'] = $ns;
4492 $res = $dbr->select(
4493 [ 'redirect', 'page' ],
4494 [ 'page_namespace', 'page_title' ],
4499 foreach ( $res as $row ) {
4500 $redirs[] = self
::newFromRow( $row );
4506 * Check if this Title is a valid redirect target
4510 public function isValidRedirectTarget() {
4511 global $wgInvalidRedirectTargets;
4513 if ( $this->isSpecialPage() ) {
4514 // invalid redirect targets are stored in a global array, but explicitly disallow Userlogout here
4515 if ( $this->isSpecial( 'Userlogout' ) ) {
4519 foreach ( $wgInvalidRedirectTargets as $target ) {
4520 if ( $this->isSpecial( $target ) ) {
4530 * Get a backlink cache object
4532 * @return BacklinkCache
4534 public function getBacklinkCache() {
4535 return BacklinkCache
::get( $this );
4539 * Whether the magic words __INDEX__ and __NOINDEX__ function for this page.
4543 public function canUseNoindex() {
4544 global $wgContentNamespaces, $wgExemptFromUserRobotsControl;
4546 $bannedNamespaces = is_null( $wgExemptFromUserRobotsControl )
4547 ?
$wgContentNamespaces
4548 : $wgExemptFromUserRobotsControl;
4550 return !in_array( $this->mNamespace
, $bannedNamespaces );
4555 * Returns the raw sort key to be used for categories, with the specified
4556 * prefix. This will be fed to Collation::getSortKey() to get a
4557 * binary sortkey that can be used for actual sorting.
4559 * @param string $prefix The prefix to be used, specified using
4560 * {{defaultsort:}} or like [[Category:Foo|prefix]]. Empty for no
4564 public function getCategorySortkey( $prefix = '' ) {
4565 $unprefixed = $this->getText();
4567 // Anything that uses this hook should only depend
4568 // on the Title object passed in, and should probably
4569 // tell the users to run updateCollations.php --force
4570 // in order to re-sort existing category relations.
4571 Hooks
::run( 'GetDefaultSortkey', [ $this, &$unprefixed ] );
4572 if ( $prefix !== '' ) {
4573 # Separate with a line feed, so the unprefixed part is only used as
4574 # a tiebreaker when two pages have the exact same prefix.
4575 # In UCA, tab is the only character that can sort above LF
4576 # so we strip both of them from the original prefix.
4577 $prefix = strtr( $prefix, "\n\t", ' ' );
4578 return "$prefix\n$unprefixed";
4584 * Returns the page language code saved in the database, if $wgPageLanguageUseDB is set
4585 * to true in LocalSettings.php, otherwise returns false. If there is no language saved in
4586 * the db, it will return NULL.
4588 * @return string|null|bool
4590 private function getDbPageLanguageCode() {
4591 global $wgPageLanguageUseDB;
4593 // check, if the page language could be saved in the database, and if so and
4594 // the value is not requested already, lookup the page language using LinkCache
4595 if ( $wgPageLanguageUseDB && $this->mDbPageLanguage
=== false ) {
4596 $linkCache = LinkCache
::singleton();
4597 $linkCache->addLinkObj( $this );
4598 $this->mDbPageLanguage
= $linkCache->getGoodLinkFieldObj( $this, 'lang' );
4601 return $this->mDbPageLanguage
;
4605 * Get the language in which the content of this page is written in
4606 * wikitext. Defaults to $wgContLang, but in certain cases it can be
4607 * e.g. $wgLang (such as special pages, which are in the user language).
4612 public function getPageLanguage() {
4613 global $wgLang, $wgLanguageCode;
4614 if ( $this->isSpecialPage() ) {
4615 // special pages are in the user language
4619 // Checking if DB language is set
4620 $dbPageLanguage = $this->getDbPageLanguageCode();
4621 if ( $dbPageLanguage ) {
4622 return wfGetLangObj( $dbPageLanguage );
4625 if ( !$this->mPageLanguage ||
$this->mPageLanguage
[1] !== $wgLanguageCode ) {
4626 // Note that this may depend on user settings, so the cache should
4627 // be only per-request.
4628 // NOTE: ContentHandler::getPageLanguage() may need to load the
4629 // content to determine the page language!
4630 // Checking $wgLanguageCode hasn't changed for the benefit of unit
4632 $contentHandler = ContentHandler
::getForTitle( $this );
4633 $langObj = $contentHandler->getPageLanguage( $this );
4634 $this->mPageLanguage
= [ $langObj->getCode(), $wgLanguageCode ];
4636 $langObj = wfGetLangObj( $this->mPageLanguage
[0] );
4643 * Get the language in which the content of this page is written when
4644 * viewed by user. Defaults to $wgContLang, but in certain cases it can be
4645 * e.g. $wgLang (such as special pages, which are in the user language).
4650 public function getPageViewLanguage() {
4653 if ( $this->isSpecialPage() ) {
4654 // If the user chooses a variant, the content is actually
4655 // in a language whose code is the variant code.
4656 $variant = $wgLang->getPreferredVariant();
4657 if ( $wgLang->getCode() !== $variant ) {
4658 return Language
::factory( $variant );
4664 // Checking if DB language is set
4665 $dbPageLanguage = $this->getDbPageLanguageCode();
4666 if ( $dbPageLanguage ) {
4667 $pageLang = wfGetLangObj( $dbPageLanguage );
4668 $variant = $pageLang->getPreferredVariant();
4669 if ( $pageLang->getCode() !== $variant ) {
4670 $pageLang = Language
::factory( $variant );
4676 // @note Can't be cached persistently, depends on user settings.
4677 // @note ContentHandler::getPageViewLanguage() may need to load the
4678 // content to determine the page language!
4679 $contentHandler = ContentHandler
::getForTitle( $this );
4680 $pageLang = $contentHandler->getPageViewLanguage( $this );
4685 * Get a list of rendered edit notices for this page.
4687 * Array is keyed by the original message key, and values are rendered using parseAsBlock, so
4688 * they will already be wrapped in paragraphs.
4691 * @param int $oldid Revision ID that's being edited
4694 public function getEditNotices( $oldid = 0 ) {
4697 // Optional notice for the entire namespace
4698 $editnotice_ns = 'editnotice-' . $this->getNamespace();
4699 $msg = wfMessage( $editnotice_ns );
4700 if ( $msg->exists() ) {
4701 $html = $msg->parseAsBlock();
4702 // Edit notices may have complex logic, but output nothing (T91715)
4703 if ( trim( $html ) !== '' ) {
4704 $notices[$editnotice_ns] = Html
::rawElement(
4708 'mw-editnotice-namespace',
4709 Sanitizer
::escapeClass( "mw-$editnotice_ns" )
4716 if ( MWNamespace
::hasSubpages( $this->getNamespace() ) ) {
4717 // Optional notice for page itself and any parent page
4718 $parts = explode( '/', $this->getDBkey() );
4719 $editnotice_base = $editnotice_ns;
4720 while ( count( $parts ) > 0 ) {
4721 $editnotice_base .= '-' . array_shift( $parts );
4722 $msg = wfMessage( $editnotice_base );
4723 if ( $msg->exists() ) {
4724 $html = $msg->parseAsBlock();
4725 if ( trim( $html ) !== '' ) {
4726 $notices[$editnotice_base] = Html
::rawElement(
4730 'mw-editnotice-base',
4731 Sanitizer
::escapeClass( "mw-$editnotice_base" )
4739 // Even if there are no subpages in namespace, we still don't want "/" in MediaWiki message keys
4740 $editnoticeText = $editnotice_ns . '-' . strtr( $this->getDBkey(), '/', '-' );
4741 $msg = wfMessage( $editnoticeText );
4742 if ( $msg->exists() ) {
4743 $html = $msg->parseAsBlock();
4744 if ( trim( $html ) !== '' ) {
4745 $notices[$editnoticeText] = Html
::rawElement(
4749 'mw-editnotice-page',
4750 Sanitizer
::escapeClass( "mw-$editnoticeText" )
4758 Hooks
::run( 'TitleGetEditNotices', [ $this, $oldid, &$notices ] );
4765 public function __sleep() {
4773 'mDefaultNamespace',
4777 public function __wakeup() {
4778 $this->mArticleID
= ( $this->mNamespace
>= 0 ) ?
-1 : 0;
4779 $this->mUrlform
= wfUrlencode( $this->mDbkeyform
);
4780 $this->mTextform
= strtr( $this->mDbkeyform
, '_', ' ' );