Merge "Localisation updates from https://translatewiki.net."
[lhc/web/wiklou.git] / includes / Title.php
1 <?php
2 /**
3 * Representation of a title within %MediaWiki.
4 *
5 * See title.txt
6 *
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.
11 *
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.
16 *
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
21 *
22 * @file
23 */
24 use MediaWiki\Linker\LinkTarget;
25 use MediaWiki\MediaWikiServices;
26
27 /**
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.
34 */
35 class Title implements LinkTarget {
36 /** @var HashBagOStuff */
37 static private $titleCache = null;
38
39 /**
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.
43 */
44 const CACHE_MAX = 1000;
45
46 /**
47 * Used to be GAID_FOR_UPDATE define. Used with getArticleID() and friends
48 * to use the master DB
49 */
50 const GAID_FOR_UPDATE = 1;
51
52 /**
53 * @name Private member variables
54 * Please use the accessor functions instead.
55 * @private
56 */
57 // @{
58
59 /** @var string Text form (spaces not underscores) of the main part */
60 public $mTextform = '';
61
62 /** @var string URL-encoded form of the main part */
63 public $mUrlform = '';
64
65 /** @var string Main part with underscores */
66 public $mDbkeyform = '';
67
68 /** @var string Database key with the initial letter in the case specified by the user */
69 protected $mUserCaseDBKey;
70
71 /** @var int Namespace index, i.e. one of the NS_xxxx constants */
72 public $mNamespace = NS_MAIN;
73
74 /** @var string Interwiki prefix */
75 public $mInterwiki = '';
76
77 /** @var bool Was this Title created from a string with a local interwiki prefix? */
78 private $mLocalInterwiki = false;
79
80 /** @var string Title fragment (i.e. the bit after the #) */
81 public $mFragment = '';
82
83 /** @var int Article ID, fetched from the link cache on demand */
84 public $mArticleID = -1;
85
86 /** @var bool|int ID of most recent revision */
87 protected $mLatestID = false;
88
89 /**
90 * @var bool|string ID of the page's content model, i.e. one of the
91 * CONTENT_MODEL_XXX constants
92 */
93 public $mContentModel = false;
94
95 /** @var int Estimated number of revisions; null of not loaded */
96 private $mEstimateRevisions;
97
98 /** @var array Array of groups allowed to edit this article */
99 public $mRestrictions = [];
100
101 /** @var string|bool */
102 protected $mOldRestrictions = false;
103
104 /** @var bool Cascade restrictions on this page to included templates and images? */
105 public $mCascadeRestriction;
106
107 /** Caching the results of getCascadeProtectionSources */
108 public $mCascadingRestrictions;
109
110 /** @var array When do the restrictions on this page expire? */
111 protected $mRestrictionsExpiry = [];
112
113 /** @var bool Are cascading restrictions in effect on this page? */
114 protected $mHasCascadingRestrictions;
115
116 /** @var array Where are the cascading restrictions coming from on this page? */
117 public $mCascadeSources;
118
119 /** @var bool Boolean for initialisation on demand */
120 public $mRestrictionsLoaded = false;
121
122 /** @var string Text form including namespace/interwiki, initialised on demand */
123 protected $mPrefixedText = null;
124
125 /** @var mixed Cached value for getTitleProtection (create protection) */
126 public $mTitleProtection;
127
128 /**
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.
132 */
133 public $mDefaultNamespace = NS_MAIN;
134
135 /** @var int The page length, 0 for special pages */
136 protected $mLength = -1;
137
138 /** @var null Is the article at this title a redirect? */
139 public $mRedirect = null;
140
141 /** @var array Associative array of user ID -> timestamp/false */
142 private $mNotificationTimestamp = [];
143
144 /** @var bool Whether a page has any subpages */
145 private $mHasSubpages;
146
147 /** @var bool The (string) language code of the page's language and content code. */
148 private $mPageLanguage = false;
149
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;
153
154 /** @var TitleValue A corresponding TitleValue object */
155 private $mTitleValue = null;
156
157 /** @var bool Would deleting this page be a big deletion? */
158 private $mIsBigDeletion = null;
159 // @}
160
161 /**
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.
166 *
167 * @return TitleFormatter
168 */
169 private static function getTitleFormatter() {
170 return MediaWikiServices::getInstance()->getTitleFormatter();
171 }
172
173 /**
174 * @access protected
175 */
176 function __construct() {
177 }
178
179 /**
180 * Create a new Title from a prefixed DB key
181 *
182 * @param string $key The database key, which has underscores
183 * instead of spaces, possibly including namespace and
184 * interwiki prefixes
185 * @return Title|null Title, or null on an error
186 */
187 public static function newFromDBkey( $key ) {
188 $t = new Title();
189 $t->mDbkeyform = $key;
190
191 try {
192 $t->secureAndSplit();
193 return $t;
194 } catch ( MalformedTitleException $ex ) {
195 return null;
196 }
197 }
198
199 /**
200 * Create a new Title from a TitleValue
201 *
202 * @param TitleValue $titleValue Assumed to be safe.
203 *
204 * @return Title
205 */
206 public static function newFromTitleValue( TitleValue $titleValue ) {
207 return self::newFromLinkTarget( $titleValue );
208 }
209
210 /**
211 * Create a new Title from a LinkTarget
212 *
213 * @param LinkTarget $linkTarget Assumed to be safe.
214 *
215 * @return Title
216 */
217 public static function newFromLinkTarget( LinkTarget $linkTarget ) {
218 if ( $linkTarget instanceof Title ) {
219 // Special case if it's already a Title object
220 return $linkTarget;
221 }
222 return self::makeTitle(
223 $linkTarget->getNamespace(),
224 $linkTarget->getText(),
225 $linkTarget->getFragment(),
226 $linkTarget->getInterwiki()
227 );
228 }
229
230 /**
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.
233 *
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
239 * makeTitleSafe().
240 * @throws InvalidArgumentException
241 * @return Title|null Title or null on an error.
242 */
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.' );
247 }
248 if ( $text === null ) {
249 return null;
250 }
251
252 try {
253 return Title::newFromTextThrow( strval( $text ), $defaultNamespace );
254 } catch ( MalformedTitleException $ex ) {
255 return null;
256 }
257 }
258
259 /**
260 * Like Title::newFromText(), but throws MalformedTitleException when the title is invalid,
261 * rather than returning null.
262 *
263 * The exception subclasses encode detailed information about why the title is invalid.
264 *
265 * @see Title::newFromText
266 *
267 * @since 1.25
268 * @param string $text Title text to check
269 * @param int $defaultNamespace
270 * @throws MalformedTitleException If the title is invalid
271 * @return Title
272 */
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' );
276 }
277
278 $titleCache = self::getTitleCache();
279
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 );
286 if ( $t ) {
287 return $t;
288 }
289 }
290
291 // Convert things like &eacute; &#257; or &#x3017; into normalized (bug 14952) text
292 $filteredText = Sanitizer::decodeCharReferencesAndNormalize( $text );
293
294 $t = new Title();
295 $t->mDbkeyform = strtr( $filteredText, ' ', '_' );
296 $t->mDefaultNamespace = intval( $defaultNamespace );
297
298 $t->secureAndSplit();
299 if ( $defaultNamespace == NS_MAIN ) {
300 $titleCache->set( $text, $t );
301 }
302 return $t;
303 }
304
305 /**
306 * THIS IS NOT THE FUNCTION YOU WANT. Use Title::newFromText().
307 *
308 * Example of wrong and broken code:
309 * $title = Title::newFromURL( $wgRequest->getVal( 'title' ) );
310 *
311 * Example of right code:
312 * $title = Title::newFromText( $wgRequest->getVal( 'title' ) );
313 *
314 * Create a new Title from URL-encoded text. Ensures that
315 * the given title's length does not exceed the maximum.
316 *
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
319 */
320 public static function newFromURL( $url ) {
321 $t = new Title();
322
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, '+', ' ' );
328 }
329
330 $t->mDbkeyform = strtr( $url, ' ', '_' );
331
332 try {
333 $t->secureAndSplit();
334 return $t;
335 } catch ( MalformedTitleException $ex ) {
336 return null;
337 }
338 }
339
340 /**
341 * @return HashBagOStuff
342 */
343 private static function getTitleCache() {
344 if ( self::$titleCache == null ) {
345 self::$titleCache = new HashBagOStuff( [ 'maxKeys' => self::CACHE_MAX ] );
346 }
347 return self::$titleCache;
348 }
349
350 /**
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.
354 *
355 * @return array
356 */
357 protected static function getSelectFields() {
358 global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
359
360 $fields = [
361 'page_namespace', 'page_title', 'page_id',
362 'page_len', 'page_is_redirect', 'page_latest',
363 ];
364
365 if ( $wgContentHandlerUseDB ) {
366 $fields[] = 'page_content_model';
367 }
368
369 if ( $wgPageLanguageUseDB ) {
370 $fields[] = 'page_lang';
371 }
372
373 return $fields;
374 }
375
376 /**
377 * Create a new Title from an article ID
378 *
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
382 */
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(
386 'page',
387 self::getSelectFields(),
388 [ 'page_id' => $id ],
389 __METHOD__
390 );
391 if ( $row !== false ) {
392 $title = Title::newFromRow( $row );
393 } else {
394 $title = null;
395 }
396 return $title;
397 }
398
399 /**
400 * Make an array of titles from an array of IDs
401 *
402 * @param int[] $ids Array of IDs
403 * @return Title[] Array of Titles
404 */
405 public static function newFromIDs( $ids ) {
406 if ( !count( $ids ) ) {
407 return [];
408 }
409 $dbr = wfGetDB( DB_SLAVE );
410
411 $res = $dbr->select(
412 'page',
413 self::getSelectFields(),
414 [ 'page_id' => $ids ],
415 __METHOD__
416 );
417
418 $titles = [];
419 foreach ( $res as $row ) {
420 $titles[] = Title::newFromRow( $row );
421 }
422 return $titles;
423 }
424
425 /**
426 * Make a Title object from a DB row
427 *
428 * @param stdClass $row Object database row (needs at least page_title,page_namespace)
429 * @return Title Corresponding Title
430 */
431 public static function newFromRow( $row ) {
432 $t = self::makeTitle( $row->page_namespace, $row->page_title );
433 $t->loadFromRow( $row );
434 return $t;
435 }
436
437 /**
438 * Load Title object fields from a DB row.
439 * If false is given, the title will be treated as non-existing.
440 *
441 * @param stdClass|bool $row Database row
442 */
443 public function loadFromRow( $row ) {
444 if ( $row ) { // page found
445 if ( isset( $row->page_id ) ) {
446 $this->mArticleID = (int)$row->page_id;
447 }
448 if ( isset( $row->page_len ) ) {
449 $this->mLength = (int)$row->page_len;
450 }
451 if ( isset( $row->page_is_redirect ) ) {
452 $this->mRedirect = (bool)$row->page_is_redirect;
453 }
454 if ( isset( $row->page_latest ) ) {
455 $this->mLatestID = (int)$row->page_latest;
456 }
457 if ( isset( $row->page_content_model ) ) {
458 $this->mContentModel = strval( $row->page_content_model );
459 } else {
460 $this->mContentModel = false; # initialized lazily in getContentModel()
461 }
462 if ( isset( $row->page_lang ) ) {
463 $this->mDbPageLanguage = (string)$row->page_lang;
464 }
465 if ( isset( $row->page_restrictions ) ) {
466 $this->mOldRestrictions = $row->page_restrictions;
467 }
468 } else { // page not found
469 $this->mArticleID = 0;
470 $this->mLength = 0;
471 $this->mRedirect = false;
472 $this->mLatestID = 0;
473 $this->mContentModel = false; # initialized lazily in getContentModel()
474 }
475 }
476
477 /**
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.
483 *
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
489 */
490 public static function &makeTitle( $ns, $title, $fragment = '', $interwiki = '' ) {
491 $t = new Title();
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()
500 return $t;
501 }
502
503 /**
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.
507 *
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
513 */
514 public static function makeTitleSafe( $ns, $title, $fragment = '', $interwiki = '' ) {
515 if ( !MWNamespace::exists( $ns ) ) {
516 return null;
517 }
518
519 $t = new Title();
520 $t->mDbkeyform = Title::makeName( $ns, $title, $fragment, $interwiki, true );
521
522 try {
523 $t->secureAndSplit();
524 return $t;
525 } catch ( MalformedTitleException $ex ) {
526 return null;
527 }
528 }
529
530 /**
531 * Create a new Title for the Main Page
532 *
533 * @return Title The new object
534 */
535 public static function newMainPage() {
536 $title = Title::newFromText( wfMessage( 'mainpage' )->inContentLanguage()->text() );
537 // Don't give fatal errors if the message is broken
538 if ( !$title ) {
539 $title = Title::newFromText( 'Main Page' );
540 }
541 return $title;
542 }
543
544 /**
545 * Get the prefixed DB key associated with an ID
546 *
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
549 */
550 public static function nameOf( $id ) {
551 $dbr = wfGetDB( DB_SLAVE );
552
553 $s = $dbr->selectRow(
554 'page',
555 [ 'page_namespace', 'page_title' ],
556 [ 'page_id' => $id ],
557 __METHOD__
558 );
559 if ( $s === false ) {
560 return null;
561 }
562
563 $n = self::makeName( $s->page_namespace, $s->page_title );
564 return $n;
565 }
566
567 /**
568 * Get a regex character class describing the legal characters in a link
569 *
570 * @return string The list of characters, not delimited
571 */
572 public static function legalChars() {
573 global $wgLegalTitleChars;
574 return $wgLegalTitleChars;
575 }
576
577 /**
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.
581 *
582 * @deprecated since 1.25, use MediaWikiTitleCodec::getTitleInvalidRegex() instead
583 *
584 * @return string Regex string
585 */
586 static function getTitleInvalidRegex() {
587 wfDeprecated( __METHOD__, '1.25' );
588 return MediaWikiTitleCodec::getTitleInvalidRegex();
589 }
590
591 /**
592 * Utility method for converting a character sequence from bytes to Unicode.
593 *
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.
596 *
597 * @param string $byteClass
598 * @return string
599 */
600 public static function convertByteClassToUnicodeClass( $byteClass ) {
601 $length = strlen( $byteClass );
602 // Input token queue
603 $x0 = $x1 = $x2 = '';
604 // Decoded queue
605 $d0 = $d1 = $d2 = '';
606 // Decoded integer codepoints
607 $ord0 = $ord1 = $ord2 = 0;
608 // Re-encoded queue
609 $r0 = $r1 = $r2 = '';
610 // Output
611 $out = '';
612 // Flags
613 $allowUnicode = false;
614 for ( $pos = 0; $pos < $length; $pos++ ) {
615 // Shift the queues down
616 $x2 = $x1;
617 $x1 = $x0;
618 $d2 = $d1;
619 $d1 = $d0;
620 $ord2 = $ord1;
621 $ord1 = $ord0;
622 $r2 = $r1;
623 $r1 = $r0;
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 ) {
636 $x0 = $d0 = '\\';
637 } else {
638 $d0 = $byteClass[$pos + 1];
639 $x0 = $inChar . $d0;
640 $pos += 1;
641 }
642 } else {
643 $x0 = $d0 = $inChar;
644 }
645 $ord0 = ord( $d0 );
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 ) {
654 $r0 = '\\' . $d0;
655 } else {
656 $r0 = $d0;
657 }
658 // Do the output
659 if ( $x0 !== '' && $x1 === '-' && $x2 !== '' ) {
660 // Range
661 if ( $ord2 > $ord0 ) {
662 // Empty range
663 } elseif ( $ord0 >= 0x80 ) {
664 // Unicode range
665 $allowUnicode = true;
666 if ( $ord2 < 0x80 ) {
667 // Keep the non-unicode section of the range
668 $out .= "$r2-\\x7F";
669 }
670 } else {
671 // Normal range
672 $out .= "$r2-$r0";
673 }
674 // Reset state to the initial value
675 $x0 = $x1 = $d0 = $d1 = $r0 = $r1 = '';
676 } elseif ( $ord2 < 0x80 ) {
677 // ASCII character
678 $out .= $r2;
679 }
680 }
681 if ( $ord1 < 0x80 ) {
682 $out .= $r1;
683 }
684 if ( $ord0 < 0x80 ) {
685 $out .= $r0;
686 }
687 if ( $allowUnicode ) {
688 $out .= '\u0080-\uFFFF';
689 }
690 return $out;
691 }
692
693 /**
694 * Make a prefixed DB key from a DB key and a namespace index
695 *
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
703 */
704 public static function makeName( $ns, $title, $fragment = '', $interwiki = '',
705 $canonicalNamespace = false
706 ) {
707 global $wgContLang;
708
709 if ( $canonicalNamespace ) {
710 $namespace = MWNamespace::getCanonicalName( $ns );
711 } else {
712 $namespace = $wgContLang->getNsText( $ns );
713 }
714 $name = $namespace == '' ? $title : "$namespace:$title";
715 if ( strval( $interwiki ) != '' ) {
716 $name = "$interwiki:$name";
717 }
718 if ( strval( $fragment ) != '' ) {
719 $name .= '#' . $fragment;
720 }
721 return $name;
722 }
723
724 /**
725 * Escape a text fragment, say from a link, for a URL
726 *
727 * @param string $fragment Containing a URL or link fragment (after the "#")
728 * @return string Escaped string
729 */
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' );
736 }
737
738 /**
739 * Callback for usort() to do title sorts by (namespace, title)
740 *
741 * @param Title $a
742 * @param Title $b
743 *
744 * @return int Result of string comparison, or namespace comparison
745 */
746 public static function compare( $a, $b ) {
747 if ( $a->getNamespace() == $b->getNamespace() ) {
748 return strcmp( $a->getText(), $b->getText() );
749 } else {
750 return $a->getNamespace() - $b->getNamespace();
751 }
752 }
753
754 /**
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 )
758 *
759 * @return bool True if this is an in-project interwiki link or a wikilink, false otherwise
760 */
761 public function isLocal() {
762 if ( $this->isExternal() ) {
763 $iw = Interwiki::fetch( $this->mInterwiki );
764 if ( $iw ) {
765 return $iw->isLocal();
766 }
767 }
768 return true;
769 }
770
771 /**
772 * Is this Title interwiki?
773 *
774 * @return bool
775 */
776 public function isExternal() {
777 return $this->mInterwiki !== '';
778 }
779
780 /**
781 * Get the interwiki prefix
782 *
783 * Use Title::isExternal to check if a interwiki is set
784 *
785 * @return string Interwiki prefix
786 */
787 public function getInterwiki() {
788 return $this->mInterwiki;
789 }
790
791 /**
792 * Was this a local interwiki link?
793 *
794 * @return bool
795 */
796 public function wasLocalInterwiki() {
797 return $this->mLocalInterwiki;
798 }
799
800 /**
801 * Determine whether the object refers to a page within
802 * this project and is transcludable.
803 *
804 * @return bool True if this is transcludable
805 */
806 public function isTrans() {
807 if ( !$this->isExternal() ) {
808 return false;
809 }
810
811 return Interwiki::fetch( $this->mInterwiki )->isTranscludable();
812 }
813
814 /**
815 * Returns the DB name of the distant wiki which owns the object.
816 *
817 * @return string The DB name
818 */
819 public function getTransWikiID() {
820 if ( !$this->isExternal() ) {
821 return false;
822 }
823
824 return Interwiki::fetch( $this->mInterwiki )->getWikiID();
825 }
826
827 /**
828 * Get a TitleValue object representing this Title.
829 *
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).
833 *
834 * @return TitleValue|null
835 */
836 public function getTitleValue() {
837 if ( $this->mTitleValue === null ) {
838 try {
839 $this->mTitleValue = new TitleValue(
840 $this->getNamespace(),
841 $this->getDBkey(),
842 $this->getFragment(),
843 $this->getInterwiki()
844 );
845 } catch ( InvalidArgumentException $ex ) {
846 wfDebug( __METHOD__ . ': Can\'t create a TitleValue for [[' .
847 $this->getPrefixedText() . ']]: ' . $ex->getMessage() . "\n" );
848 }
849 }
850
851 return $this->mTitleValue;
852 }
853
854 /**
855 * Get the text form (spaces not underscores) of the main part
856 *
857 * @return string Main part of the title
858 */
859 public function getText() {
860 return $this->mTextform;
861 }
862
863 /**
864 * Get the URL-encoded form of the main part
865 *
866 * @return string Main part of the title, URL-encoded
867 */
868 public function getPartialURL() {
869 return $this->mUrlform;
870 }
871
872 /**
873 * Get the main part with underscores
874 *
875 * @return string Main part of the title, with underscores
876 */
877 public function getDBkey() {
878 return $this->mDbkeyform;
879 }
880
881 /**
882 * Get the DB key with the initial letter case as specified by the user
883 *
884 * @return string DB key
885 */
886 function getUserCaseDBKey() {
887 if ( !is_null( $this->mUserCaseDBKey ) ) {
888 return $this->mUserCaseDBKey;
889 } else {
890 // If created via makeTitle(), $this->mUserCaseDBKey is not set.
891 return $this->mDbkeyform;
892 }
893 }
894
895 /**
896 * Get the namespace index, i.e. one of the NS_xxxx constants.
897 *
898 * @return int Namespace index
899 */
900 public function getNamespace() {
901 return $this->mNamespace;
902 }
903
904 /**
905 * Get the page's content model id, see the CONTENT_MODEL_XXX constants.
906 *
907 * @param int $flags A bit field; may be Title::GAID_FOR_UPDATE to select for update
908 * @return string Content model id
909 */
910 public function getContentModel( $flags = 0 ) {
911 if ( ( !$this->mContentModel || $flags === Title::GAID_FOR_UPDATE ) &&
912 $this->getArticleID( $flags )
913 ) {
914 $linkCache = LinkCache::singleton();
915 $linkCache->addLinkObj( $this ); # in case we already had an article ID
916 $this->mContentModel = $linkCache->getGoodLinkFieldObj( $this, 'model' );
917 }
918
919 if ( !$this->mContentModel ) {
920 $this->mContentModel = ContentHandler::getDefaultModelFor( $this );
921 }
922
923 return $this->mContentModel;
924 }
925
926 /**
927 * Convenience method for checking a title's content model name
928 *
929 * @param string $id The content model ID (use the CONTENT_MODEL_XXX constants).
930 * @return bool True if $this->getContentModel() == $id
931 */
932 public function hasContentModel( $id ) {
933 return $this->getContentModel() == $id;
934 }
935
936 /**
937 * Get the namespace text
938 *
939 * @return string Namespace text
940 */
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 );
949 }
950 }
951
952 try {
953 $formatter = self::getTitleFormatter();
954 return $formatter->getNamespaceName( $this->mNamespace, $this->mDbkeyform );
955 } catch ( InvalidArgumentException $ex ) {
956 wfDebug( __METHOD__ . ': ' . $ex->getMessage() . "\n" );
957 return false;
958 }
959 }
960
961 /**
962 * Get the namespace text of the subject (rather than talk) page
963 *
964 * @return string Namespace text
965 */
966 public function getSubjectNsText() {
967 global $wgContLang;
968 return $wgContLang->getNsText( MWNamespace::getSubject( $this->mNamespace ) );
969 }
970
971 /**
972 * Get the namespace text of the talk page
973 *
974 * @return string Namespace text
975 */
976 public function getTalkNsText() {
977 global $wgContLang;
978 return $wgContLang->getNsText( MWNamespace::getTalk( $this->mNamespace ) );
979 }
980
981 /**
982 * Could this title have a corresponding talk page?
983 *
984 * @return bool
985 */
986 public function canTalk() {
987 return MWNamespace::canTalk( $this->mNamespace );
988 }
989
990 /**
991 * Is this in a namespace that allows actual pages?
992 *
993 * @return bool
994 */
995 public function canExist() {
996 return $this->mNamespace >= NS_MAIN;
997 }
998
999 /**
1000 * Can this title be added to a user's watchlist?
1001 *
1002 * @return bool
1003 */
1004 public function isWatchable() {
1005 return !$this->isExternal() && MWNamespace::isWatchable( $this->getNamespace() );
1006 }
1007
1008 /**
1009 * Returns true if this is a special page.
1010 *
1011 * @return bool
1012 */
1013 public function isSpecialPage() {
1014 return $this->getNamespace() == NS_SPECIAL;
1015 }
1016
1017 /**
1018 * Returns true if this title resolves to the named special page
1019 *
1020 * @param string $name The special page name
1021 * @return bool
1022 */
1023 public function isSpecial( $name ) {
1024 if ( $this->isSpecialPage() ) {
1025 list( $thisName, /* $subpage */ ) = SpecialPageFactory::resolveAlias( $this->getDBkey() );
1026 if ( $name == $thisName ) {
1027 return true;
1028 }
1029 }
1030 return false;
1031 }
1032
1033 /**
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
1036 *
1037 * @return Title
1038 */
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 );
1046 }
1047 }
1048 }
1049 return $this;
1050 }
1051
1052 /**
1053 * Returns true if the title is inside the specified namespace.
1054 *
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
1059 * @return bool
1060 * @since 1.19
1061 */
1062 public function inNamespace( $ns ) {
1063 return MWNamespace::equals( $this->getNamespace(), $ns );
1064 }
1065
1066 /**
1067 * Returns true if the title is inside one of the specified namespaces.
1068 *
1069 * @param int $namespaces,... The namespaces to check for
1070 * @return bool
1071 * @since 1.19
1072 */
1073 public function inNamespaces( /* ... */ ) {
1074 $namespaces = func_get_args();
1075 if ( count( $namespaces ) > 0 && is_array( $namespaces[0] ) ) {
1076 $namespaces = $namespaces[0];
1077 }
1078
1079 foreach ( $namespaces as $ns ) {
1080 if ( $this->inNamespace( $ns ) ) {
1081 return true;
1082 }
1083 }
1084
1085 return false;
1086 }
1087
1088 /**
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.
1094 *
1095 * This is MUCH simpler than individually testing for equivalence
1096 * against both NS_USER and NS_USER_TALK, and is also forward compatible.
1097 * @since 1.19
1098 * @param int $ns
1099 * @return bool
1100 */
1101 public function hasSubjectNamespace( $ns ) {
1102 return MWNamespace::subjectEquals( $this->getNamespace(), $ns );
1103 }
1104
1105 /**
1106 * Is this Title in a namespace which contains content?
1107 * In other words, is this a content page, for the purposes of calculating
1108 * statistics, etc?
1109 *
1110 * @return bool
1111 */
1112 public function isContentPage() {
1113 return MWNamespace::isContent( $this->getNamespace() );
1114 }
1115
1116 /**
1117 * Would anybody with sufficient privileges be able to move this page?
1118 * Some pages just aren't movable.
1119 *
1120 * @return bool
1121 */
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
1125 return false;
1126 }
1127
1128 $result = true;
1129 Hooks::run( 'TitleIsMovable', [ $this, &$result ] );
1130 return $result;
1131 }
1132
1133 /**
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();
1139 *
1140 * @since 1.18
1141 * @return bool
1142 */
1143 public function isMainPage() {
1144 return $this->equals( Title::newMainPage() );
1145 }
1146
1147 /**
1148 * Is this a subpage?
1149 *
1150 * @return bool
1151 */
1152 public function isSubpage() {
1153 return MWNamespace::hasSubpages( $this->mNamespace )
1154 ? strpos( $this->getText(), '/' ) !== false
1155 : false;
1156 }
1157
1158 /**
1159 * Is this a conversion table for the LanguageConverter?
1160 *
1161 * @return bool
1162 */
1163 public function isConversionTable() {
1164 // @todo ConversionTable should become a separate content model.
1165
1166 return $this->getNamespace() == NS_MEDIAWIKI &&
1167 strpos( $this->getText(), 'Conversiontable/' ) === 0;
1168 }
1169
1170 /**
1171 * Does that page contain wikitext, or it is JS, CSS or whatever?
1172 *
1173 * @return bool
1174 */
1175 public function isWikitextPage() {
1176 return $this->hasContentModel( CONTENT_MODEL_WIKITEXT );
1177 }
1178
1179 /**
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.
1183 *
1184 * This method does *not* return true for per-user JS/CSS. Use isCssJsSubpage()
1185 * for that!
1186 *
1187 * Note that this method should not return true for pages that contain and
1188 * show "inactive" CSS or JS.
1189 *
1190 * @return bool
1191 * @todo FIXME: Rename to isSiteConfigPage() and remove deprecated hook
1192 */
1193 public function isCssOrJsPage() {
1194 $isCssOrJsPage = NS_MEDIAWIKI == $this->mNamespace
1195 && ( $this->hasContentModel( CONTENT_MODEL_CSS )
1196 || $this->hasContentModel( CONTENT_MODEL_JAVASCRIPT ) );
1197
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.
1201
1202 Hooks::run( 'TitleIsCssOrJsPage', [ $this, &$isCssOrJsPage ], '1.25' );
1203
1204 return $isCssOrJsPage;
1205 }
1206
1207 /**
1208 * Is this a .css or .js subpage of a user page?
1209 * @return bool
1210 * @todo FIXME: Rename to isUserConfigPage()
1211 */
1212 public function isCssJsSubpage() {
1213 return ( NS_USER == $this->mNamespace && $this->isSubpage()
1214 && ( $this->hasContentModel( CONTENT_MODEL_CSS )
1215 || $this->hasContentModel( CONTENT_MODEL_JAVASCRIPT ) ) );
1216 }
1217
1218 /**
1219 * Trim down a .css or .js subpage title to get the corresponding skin name
1220 *
1221 * @return string Containing skin name from .css or .js subpage title
1222 */
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'
1229 }
1230 return substr( $subpage, 0, $lastdot );
1231 }
1232
1233 /**
1234 * Is this a .css subpage of a user page?
1235 *
1236 * @return bool
1237 */
1238 public function isCssSubpage() {
1239 return ( NS_USER == $this->mNamespace && $this->isSubpage()
1240 && $this->hasContentModel( CONTENT_MODEL_CSS ) );
1241 }
1242
1243 /**
1244 * Is this a .js subpage of a user page?
1245 *
1246 * @return bool
1247 */
1248 public function isJsSubpage() {
1249 return ( NS_USER == $this->mNamespace && $this->isSubpage()
1250 && $this->hasContentModel( CONTENT_MODEL_JAVASCRIPT ) );
1251 }
1252
1253 /**
1254 * Is this a talk page of some sort?
1255 *
1256 * @return bool
1257 */
1258 public function isTalkPage() {
1259 return MWNamespace::isTalk( $this->getNamespace() );
1260 }
1261
1262 /**
1263 * Get a Title object associated with the talk page of this article
1264 *
1265 * @return Title The object for the talk page
1266 */
1267 public function getTalkPage() {
1268 return Title::makeTitle( MWNamespace::getTalk( $this->getNamespace() ), $this->getDBkey() );
1269 }
1270
1271 /**
1272 * Get a title object associated with the subject page of this
1273 * talk page
1274 *
1275 * @return Title The object for the subject page
1276 */
1277 public function getSubjectPage() {
1278 // Is this the same title?
1279 $subjectNS = MWNamespace::getSubject( $this->getNamespace() );
1280 if ( $this->getNamespace() == $subjectNS ) {
1281 return $this;
1282 }
1283 return Title::makeTitle( $subjectNS, $this->getDBkey() );
1284 }
1285
1286 /**
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
1289 *
1290 * @since 1.25
1291 * @throws MWException
1292 * @return Title
1293 */
1294 public function getOtherPage() {
1295 if ( $this->isSpecialPage() ) {
1296 throw new MWException( 'Special pages cannot have other pages' );
1297 }
1298 if ( $this->isTalkPage() ) {
1299 return $this->getSubjectPage();
1300 } else {
1301 return $this->getTalkPage();
1302 }
1303 }
1304
1305 /**
1306 * Get the default namespace index, for when there is no namespace
1307 *
1308 * @return int Default namespace index
1309 */
1310 public function getDefaultNamespace() {
1311 return $this->mDefaultNamespace;
1312 }
1313
1314 /**
1315 * Get the Title fragment (i.e.\ the bit after the #) in text form
1316 *
1317 * Use Title::hasFragment to check for a fragment
1318 *
1319 * @return string Title fragment
1320 */
1321 public function getFragment() {
1322 return $this->mFragment;
1323 }
1324
1325 /**
1326 * Check if a Title fragment is set
1327 *
1328 * @return bool
1329 * @since 1.23
1330 */
1331 public function hasFragment() {
1332 return $this->mFragment !== '';
1333 }
1334
1335 /**
1336 * Get the fragment in URL form, including the "#" character if there is one
1337 * @return string Fragment in URL form
1338 */
1339 public function getFragmentForURL() {
1340 if ( !$this->hasFragment() ) {
1341 return '';
1342 } else {
1343 return '#' . Title::escapeFragmentForURL( $this->getFragment() );
1344 }
1345 }
1346
1347 /**
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
1350 * an initial "#".
1351 *
1352 * Deprecated for public use, use Title::makeTitle() with fragment parameter,
1353 * or Title::createFragmentTarget().
1354 * Still in active use privately.
1355 *
1356 * @private
1357 * @param string $fragment Text
1358 */
1359 public function setFragment( $fragment ) {
1360 $this->mFragment = strtr( substr( $fragment, 1 ), '_', ' ' );
1361 }
1362
1363 /**
1364 * Creates a new Title for a different fragment of the same page.
1365 *
1366 * @since 1.27
1367 * @param string $fragment
1368 * @return Title
1369 */
1370 public function createFragmentTarget( $fragment ) {
1371 return self::makeTitle(
1372 $this->getNamespace(),
1373 $this->getText(),
1374 $fragment,
1375 $this->getInterwiki()
1376 );
1377
1378 }
1379
1380 /**
1381 * Prefix some arbitrary text with the namespace or interwiki prefix
1382 * of this object
1383 *
1384 * @param string $name The text
1385 * @return string The prefixed text
1386 */
1387 private function prefix( $name ) {
1388 $p = '';
1389 if ( $this->isExternal() ) {
1390 $p = $this->mInterwiki . ':';
1391 }
1392
1393 if ( 0 != $this->mNamespace ) {
1394 $p .= $this->getNsText() . ':';
1395 }
1396 return $p . $name;
1397 }
1398
1399 /**
1400 * Get the prefixed database key form
1401 *
1402 * @return string The prefixed title, with underscores and
1403 * any interwiki and namespace prefixes
1404 */
1405 public function getPrefixedDBkey() {
1406 $s = $this->prefix( $this->mDbkeyform );
1407 $s = strtr( $s, ' ', '_' );
1408 return $s;
1409 }
1410
1411 /**
1412 * Get the prefixed title with spaces.
1413 * This is the form usually used for display
1414 *
1415 * @return string The prefixed title, with spaces
1416 */
1417 public function getPrefixedText() {
1418 if ( $this->mPrefixedText === null ) {
1419 $s = $this->prefix( $this->mTextform );
1420 $s = strtr( $s, '_', ' ' );
1421 $this->mPrefixedText = $s;
1422 }
1423 return $this->mPrefixedText;
1424 }
1425
1426 /**
1427 * Return a string representation of this title
1428 *
1429 * @return string Representation of this title
1430 */
1431 public function __toString() {
1432 return $this->getPrefixedText();
1433 }
1434
1435 /**
1436 * Get the prefixed title with spaces, plus any fragment
1437 * (part beginning with '#')
1438 *
1439 * @return string The prefixed title, with spaces and the fragment, including '#'
1440 */
1441 public function getFullText() {
1442 $text = $this->getPrefixedText();
1443 if ( $this->hasFragment() ) {
1444 $text .= '#' . $this->getFragment();
1445 }
1446 return $text;
1447 }
1448
1449 /**
1450 * Get the root page name text without a namespace, i.e. the leftmost part before any slashes
1451 *
1452 * @par Example:
1453 * @code
1454 * Title::newFromText('User:Foo/Bar/Baz')->getRootText();
1455 * # returns: 'Foo'
1456 * @endcode
1457 *
1458 * @return string Root name
1459 * @since 1.20
1460 */
1461 public function getRootText() {
1462 if ( !MWNamespace::hasSubpages( $this->mNamespace ) ) {
1463 return $this->getText();
1464 }
1465
1466 return strtok( $this->getText(), '/' );
1467 }
1468
1469 /**
1470 * Get the root page name title, i.e. the leftmost part before any slashes
1471 *
1472 * @par Example:
1473 * @code
1474 * Title::newFromText('User:Foo/Bar/Baz')->getRootTitle();
1475 * # returns: Title{User:Foo}
1476 * @endcode
1477 *
1478 * @return Title Root title
1479 * @since 1.20
1480 */
1481 public function getRootTitle() {
1482 return Title::makeTitle( $this->getNamespace(), $this->getRootText() );
1483 }
1484
1485 /**
1486 * Get the base page name without a namespace, i.e. the part before the subpage name
1487 *
1488 * @par Example:
1489 * @code
1490 * Title::newFromText('User:Foo/Bar/Baz')->getBaseText();
1491 * # returns: 'Foo/Bar'
1492 * @endcode
1493 *
1494 * @return string Base name
1495 */
1496 public function getBaseText() {
1497 if ( !MWNamespace::hasSubpages( $this->mNamespace ) ) {
1498 return $this->getText();
1499 }
1500
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] );
1505 }
1506 return implode( '/', $parts );
1507 }
1508
1509 /**
1510 * Get the base page name title, i.e. the part before the subpage name
1511 *
1512 * @par Example:
1513 * @code
1514 * Title::newFromText('User:Foo/Bar/Baz')->getBaseTitle();
1515 * # returns: Title{User:Foo/Bar}
1516 * @endcode
1517 *
1518 * @return Title Base title
1519 * @since 1.20
1520 */
1521 public function getBaseTitle() {
1522 return Title::makeTitle( $this->getNamespace(), $this->getBaseText() );
1523 }
1524
1525 /**
1526 * Get the lowest-level subpage name, i.e. the rightmost part after any slashes
1527 *
1528 * @par Example:
1529 * @code
1530 * Title::newFromText('User:Foo/Bar/Baz')->getSubpageText();
1531 * # returns: "Baz"
1532 * @endcode
1533 *
1534 * @return string Subpage name
1535 */
1536 public function getSubpageText() {
1537 if ( !MWNamespace::hasSubpages( $this->mNamespace ) ) {
1538 return $this->mTextform;
1539 }
1540 $parts = explode( '/', $this->mTextform );
1541 return $parts[count( $parts ) - 1];
1542 }
1543
1544 /**
1545 * Get the title for a subpage of the current page
1546 *
1547 * @par Example:
1548 * @code
1549 * Title::newFromText('User:Foo/Bar/Baz')->getSubpage("Asdf");
1550 * # returns: Title{User:Foo/Bar/Baz/Asdf}
1551 * @endcode
1552 *
1553 * @param string $text The subpage name to add to the title
1554 * @return Title Subpage title
1555 * @since 1.20
1556 */
1557 public function getSubpage( $text ) {
1558 return Title::makeTitleSafe( $this->getNamespace(), $this->getText() . '/' . $text );
1559 }
1560
1561 /**
1562 * Get a URL-encoded form of the subpage text
1563 *
1564 * @return string URL-encoded subpage name
1565 */
1566 public function getSubpageUrlForm() {
1567 $text = $this->getSubpageText();
1568 $text = wfUrlencode( strtr( $text, ' ', '_' ) );
1569 return $text;
1570 }
1571
1572 /**
1573 * Get a URL-encoded title (not an actual URL) including interwiki
1574 *
1575 * @return string The URL-encoded form
1576 */
1577 public function getPrefixedURL() {
1578 $s = $this->prefix( $this->mDbkeyform );
1579 $s = wfUrlencode( strtr( $s, ' ', '_' ) );
1580 return $s;
1581 }
1582
1583 /**
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();
1590 *
1591 * @since 1.19 (r105919)
1592 * @param array|string $query
1593 * @param bool $query2
1594 * @return string
1595 */
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" );
1601 }
1602 if ( is_array( $query ) ) {
1603 $query = wfArrayToCgi( $query );
1604 }
1605 if ( $query2 ) {
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 ] );
1610 } else {
1611 $query2 = wfArrayToCgi( $query2 );
1612 }
1613 // If we have $query content add a & to it first
1614 if ( $query ) {
1615 $query .= '&';
1616 }
1617 // Now append the queries together
1618 $query .= $query2;
1619 }
1620 return $query;
1621 }
1622
1623 /**
1624 * Get a real URL referring to this title, with interwiki link and
1625 * fragment
1626 *
1627 * @see self::getLocalURL for the arguments.
1628 * @see wfExpandUrl
1629 * @param array|string $query
1630 * @param bool $query2
1631 * @param string $proto Protocol type to use in URL
1632 * @return string The URL
1633 */
1634 public function getFullURL( $query = '', $query2 = false, $proto = PROTO_RELATIVE ) {
1635 $query = self::fixUrlQueryArgs( $query, $query2 );
1636
1637 # Hand off all the decisions on urls to getLocalURL
1638 $url = $this->getLocalURL( $query );
1639
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 );
1644
1645 # Finally, add the fragment.
1646 $url .= $this->getFragmentForURL();
1647
1648 Hooks::run( 'GetFullURL', [ &$this, &$url, $query ] );
1649 return $url;
1650 }
1651
1652 /**
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.
1656 *
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.
1661 *
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.
1672 *
1673 * @return string String of the URL.
1674 */
1675 public function getLocalURL( $query = '', $query2 = false ) {
1676 global $wgArticlePath, $wgScript, $wgServer, $wgRequest;
1677
1678 $query = self::fixUrlQueryArgs( $query, $query2 );
1679
1680 $interwiki = Interwiki::fetch( $this->mInterwiki );
1681 if ( $interwiki ) {
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.
1686 $namespace .= ':';
1687 }
1688 $url = $interwiki->getURL( $namespace . $this->getDBkey() );
1689 $url = wfAppendQuery( $url, $query );
1690 } else {
1691 $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
1692 if ( $query == '' ) {
1693 $url = str_replace( '$1', $dbkey, $wgArticlePath );
1694 Hooks::run( 'GetLocalURL::Article', [ &$this, &$url ] );
1695 } else {
1696 global $wgVariantArticlePath, $wgActionPaths, $wgContLang;
1697 $url = false;
1698 $matches = [];
1699
1700 if ( !empty( $wgActionPaths )
1701 && preg_match( '/^(.*&|)action=([^&]*)(&(.*)|)$/', $query, $matches )
1702 ) {
1703 $action = urldecode( $matches[2] );
1704 if ( isset( $wgActionPaths[$action] ) ) {
1705 $query = $matches[1];
1706 if ( isset( $matches[4] ) ) {
1707 $query .= $matches[4];
1708 }
1709 $url = str_replace( '$1', $dbkey, $wgActionPaths[$action] );
1710 if ( $query != '' ) {
1711 $url = wfAppendQuery( $url, $query );
1712 }
1713 }
1714 }
1715
1716 if ( $url === false
1717 && $wgVariantArticlePath
1718 && preg_match( '/^variant=([^&]*)$/', $query, $matches )
1719 && $wgContLang->getCode() === $this->getPageLanguage()->getCode()
1720 && $this->getPageLanguage()->hasVariants()
1721 ) {
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 );
1728 }
1729 }
1730
1731 if ( $url === false ) {
1732 if ( $query == '-' ) {
1733 $query = '';
1734 }
1735 $url = "{$wgScript}?title={$dbkey}&{$query}";
1736 }
1737 }
1738
1739 Hooks::run( 'GetLocalURL::Internal', [ &$this, &$url, $query ] );
1740
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;
1745 }
1746 }
1747 Hooks::run( 'GetLocalURL', [ &$this, &$url, $query ] );
1748 return $url;
1749 }
1750
1751 /**
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
1756 * to the fragment.
1757 *
1758 * The result obviously should not be URL-escaped, but does need to be
1759 * HTML-escaped if it's being output in HTML.
1760 *
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
1766 */
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();
1772 } else {
1773 $ret = $this->getLocalURL( $query, $query2 ) . $this->getFragmentForURL();
1774 }
1775 return $ret;
1776 }
1777
1778 /**
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.
1782 *
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://
1786 *
1787 * @see self::getLocalURL for the arguments.
1788 * @return string The URL
1789 */
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 ] );
1796 return $url;
1797 }
1798
1799 /**
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.
1803 *
1804 * NOTE: Unlike getInternalURL(), the canonical URL includes the fragment
1805 *
1806 * @see self::getLocalURL for the arguments.
1807 * @return string The URL
1808 * @since 1.18
1809 */
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 ] );
1814 return $url;
1815 }
1816
1817 /**
1818 * Get the edit URL for this Title
1819 *
1820 * @return string The URL, or a null string if this is an interwiki link
1821 */
1822 public function getEditURL() {
1823 if ( $this->isExternal() ) {
1824 return '';
1825 }
1826 $s = $this->getLocalURL( 'action=edit' );
1827
1828 return $s;
1829 }
1830
1831 /**
1832 * Can $user perform $action on this page?
1833 * This skips potentially expensive cascading permission checks
1834 * as well as avoids expensive error formatting
1835 *
1836 * Suitable for use for nonessential UI controls in common cases, but
1837 * _not_ for functional access control.
1838 *
1839 * May provide false positives, but should never provide a false negative.
1840 *
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.
1843 * @return bool
1844 */
1845 public function quickUserCan( $action, $user = null ) {
1846 return $this->userCan( $action, $user, false );
1847 }
1848
1849 /**
1850 * Can $user perform $action on this page?
1851 *
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
1854 * provided.
1855 * @param string $rigor Same format as Title::getUserPermissionsErrors()
1856 * @return bool
1857 */
1858 public function userCan( $action, $user = null, $rigor = 'secure' ) {
1859 if ( !$user instanceof User ) {
1860 global $wgUser;
1861 $user = $wgUser;
1862 }
1863
1864 return !count( $this->getUserPermissionsErrorsInternal( $action, $user, $rigor, true ) );
1865 }
1866
1867 /**
1868 * Can $user perform $action on this page?
1869 *
1870 * @todo FIXME: This *does not* check throttles (User::pingLimiter()).
1871 *
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.
1881 */
1882 public function getUserPermissionsErrors(
1883 $action, $user, $rigor = 'secure', $ignoreErrors = []
1884 ) {
1885 $errors = $this->getUserPermissionsErrorsInternal( $action, $user, $rigor );
1886
1887 // Remove the errors being ignored.
1888 foreach ( $errors as $index => $error ) {
1889 $errKey = is_array( $error ) ? $error[0] : $error;
1890
1891 if ( in_array( $errKey, $ignoreErrors ) ) {
1892 unset( $errors[$index] );
1893 }
1894 if ( $errKey instanceof MessageSpecifier && in_array( $errKey->getKey(), $ignoreErrors ) ) {
1895 unset( $errors[$index] );
1896 }
1897 }
1898
1899 return $errors;
1900 }
1901
1902 /**
1903 * Permissions checks that fail most often, and which are easiest to test.
1904 *
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
1910 *
1911 * @return array List of errors
1912 */
1913 private function checkQuickPermissions( $action, $user, $errors, $rigor, $short ) {
1914 if ( !Hooks::run( 'TitleQuickPermissions',
1915 [ $this, $user, $action, &$errors, ( $rigor !== 'quick' ), $short ] )
1916 ) {
1917 return $errors;
1918 }
1919
1920 if ( $action == 'create' ) {
1921 if (
1922 ( $this->isTalkPage() && !$user->isAllowed( 'createtalk' ) ) ||
1923 ( !$this->isTalkPage() && !$user->isAllowed( 'createpage' ) )
1924 ) {
1925 $errors[] = $user->isAnon() ? [ 'nocreatetext' ] : [ 'nocreate-loggedin' ];
1926 }
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' ];
1932 }
1933
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' ];
1937 }
1938
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' ];
1942 }
1943
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' ];
1951 } else {
1952 $errors[] = [ 'movenotallowed' ];
1953 }
1954 }
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' ];
1967 }
1968 } elseif ( !$user->isAllowed( $action ) ) {
1969 $errors[] = $this->missingPermissionError( $action, $short );
1970 }
1971
1972 return $errors;
1973 }
1974
1975 /**
1976 * Add the resulting error code to the errors array
1977 *
1978 * @param array $errors List of current errors
1979 * @param array $result Result of errors
1980 *
1981 * @return array List of errors
1982 */
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' ];
1999 }
2000 return $errors;
2001 }
2002
2003 /**
2004 * Check various permission hooks
2005 *
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
2011 *
2012 * @return array List of errors
2013 */
2014 private function checkPermissionHooks( $action, $user, $errors, $rigor, $short ) {
2015 // Use getUserPermissionsErrors instead
2016 $result = '';
2017 if ( !Hooks::run( 'userCan', [ &$this, &$user, $action, &$result ] ) ) {
2018 return $result ? [] : [ [ 'badaccess-group0' ] ];
2019 }
2020 // Check getUserPermissionsErrors hook
2021 if ( !Hooks::run( 'getUserPermissionsErrors', [ &$this, &$user, $action, &$result ] ) ) {
2022 $errors = $this->resultToError( $errors, $result );
2023 }
2024 // Check getUserPermissionsErrorsExpensive hook
2025 if (
2026 $rigor !== 'quick'
2027 && !( $short && count( $errors ) > 0 )
2028 && !Hooks::run( 'getUserPermissionsErrorsExpensive', [ &$this, &$user, $action, &$result ] )
2029 ) {
2030 $errors = $this->resultToError( $errors, $result );
2031 }
2032
2033 return $errors;
2034 }
2035
2036 /**
2037 * Check permissions on special pages & namespaces
2038 *
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
2044 *
2045 * @return array List of errors
2046 */
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' ];
2052 }
2053
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 ];
2060 }
2061
2062 return $errors;
2063 }
2064
2065 /**
2066 * Check CSS/JS sub-page permissions
2067 *
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
2073 *
2074 * @return array List of errors
2075 */
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 ];
2086 }
2087 } else {
2088 if ( $this->isCssSubpage() && !$user->isAllowed( 'editusercss' ) ) {
2089 $errors[] = [ 'customcssprotected', $action ];
2090 } elseif ( $this->isJsSubpage() && !$user->isAllowed( 'edituserjs' ) ) {
2091 $errors[] = [ 'customjsprotected', $action ];
2092 }
2093 }
2094 }
2095
2096 return $errors;
2097 }
2098
2099 /**
2100 * Check against page_restrictions table requirements on this
2101 * page. The user must possess all required rights for this
2102 * action.
2103 *
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
2109 *
2110 * @return array List of errors
2111 */
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';
2117 }
2118 // Backwards compatibility, rewrite autoconfirmed -> editsemiprotected
2119 if ( $right == 'autoconfirmed' ) {
2120 $right = 'editsemiprotected';
2121 }
2122 if ( $right == '' ) {
2123 continue;
2124 }
2125 if ( !$user->isAllowed( $right ) ) {
2126 $errors[] = [ 'protectedpagetext', $right, $action ];
2127 } elseif ( $this->mCascadeRestriction && !$user->isAllowed( 'protect' ) ) {
2128 $errors[] = [ 'protectedpagetext', 'protect', $action ];
2129 }
2130 }
2131
2132 return $errors;
2133 }
2134
2135 /**
2136 * Check restrictions on cascading pages.
2137 *
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
2143 *
2144 * @return array List of errors
2145 */
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';
2163 }
2164 // Backwards compatibility, rewrite autoconfirmed -> editsemiprotected
2165 if ( $right == 'autoconfirmed' ) {
2166 $right = 'editsemiprotected';
2167 }
2168 if ( $right != '' && !$user->isAllowedAll( 'protect', $right ) ) {
2169 $pages = '';
2170 foreach ( $cascadingSources as $page ) {
2171 $pages .= '* [[:' . $page->getPrefixedText() . "]]\n";
2172 }
2173 $errors[] = [ 'cascadeprotected', count( $cascadingSources ), $pages, $action ];
2174 }
2175 }
2176 }
2177 }
2178
2179 return $errors;
2180 }
2181
2182 /**
2183 * Check action permissions not already checked in checkQuickPermissions
2184 *
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
2190 *
2191 * @return array List of errors
2192 */
2193 private function checkActionPermissions( $action, $user, $errors, $rigor, $short ) {
2194 global $wgDeleteRevisionsLimit, $wgLang;
2195
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' ];
2200 }
2201 } elseif ( $action == 'create' ) {
2202 $title_protection = $this->getTitleProtection();
2203 if ( $title_protection ) {
2204 if ( $title_protection['permission'] == ''
2205 || !$user->isAllowed( $title_protection['permission'] )
2206 ) {
2207 $errors[] = [
2208 'titleprotected',
2209 User::whoIs( $title_protection['user'] ),
2210 $title_protection['reason']
2211 ];
2212 }
2213 }
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' ];
2222 }
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' ];
2228 }
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 );
2234 }
2235 if ( $tempErrors ) {
2236 // If protection keeps them from editing, they shouldn't be able to delete.
2237 $errors[] = [ 'deleteprotected' ];
2238 }
2239 if ( $rigor !== 'quick' && $wgDeleteRevisionsLimit
2240 && !$this->userCan( 'bigdelete', $user ) && $this->isBigDeletion()
2241 ) {
2242 $errors[] = [ 'delete-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ];
2243 }
2244 }
2245 return $errors;
2246 }
2247
2248 /**
2249 * Check that the user isn't blocked from editing.
2250 *
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
2256 *
2257 * @return array List of errors
2258 */
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' ] ) ) {
2263 return $errors;
2264 }
2265
2266 global $wgEmailConfirmToEdit;
2267
2268 if ( $wgEmailConfirmToEdit && !$user->isEmailConfirmed() ) {
2269 $errors[] = [ 'confirmedittext' ];
2270 }
2271
2272 $useSlave = ( $rigor !== 'secure' );
2273 if ( ( $action == 'edit' || $action == 'create' )
2274 && !$user->isBlockedFrom( $this, $useSlave )
2275 ) {
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() );
2281 }
2282
2283 return $errors;
2284 }
2285
2286 /**
2287 * Check that the user is allowed to read this page.
2288 *
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
2294 *
2295 * @return array List of errors
2296 */
2297 private function checkReadPermissions( $action, $user, $errors, $rigor, $short ) {
2298 global $wgWhitelistRead, $wgWhitelistReadRegexp;
2299
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' )
2310 ) {
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();
2319
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;
2328 }
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 );
2333 if ( $name ) {
2334 $pure = SpecialPage::getTitleFor( $name )->getPrefixedText();
2335 if ( in_array( $pure, $wgWhitelistRead, true ) ) {
2336 $whitelisted = true;
2337 }
2338 }
2339 }
2340 }
2341
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;
2348 break;
2349 }
2350 }
2351 }
2352
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 );
2358 }
2359 }
2360
2361 return $errors;
2362 }
2363
2364 /**
2365 * Get a description array when the user doesn't have the right to perform
2366 * $action (i.e. when User::isAllowed() returns false)
2367 *
2368 * @param string $action The action to check
2369 * @param bool $short Short circuit on first error
2370 * @return array List of errors
2371 */
2372 private function missingPermissionError( $action, $short ) {
2373 // We avoid expensive display logic for quickUserCan's and such
2374 if ( $short ) {
2375 return [ 'badaccess-group0' ];
2376 }
2377
2378 $groups = array_map( [ 'User', 'makeGroupLinkWiki' ],
2379 User::getGroupsWithPermission( $action ) );
2380
2381 if ( count( $groups ) ) {
2382 global $wgLang;
2383 return [
2384 'badaccess-groups',
2385 $wgLang->commaList( $groups ),
2386 count( $groups )
2387 ];
2388 } else {
2389 return [ 'badaccess-group0' ];
2390 }
2391 }
2392
2393 /**
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().
2397 *
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.
2406 */
2407 protected function getUserPermissionsErrorsInternal(
2408 $action, $user, $rigor = 'secure', $short = false
2409 ) {
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'." );
2416 }
2417
2418 # Read has special handling
2419 if ( $action == 'read' ) {
2420 $checks = [
2421 'checkPermissionHooks',
2422 'checkReadPermissions',
2423 ];
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' ) {
2429 $checks = [
2430 'checkQuickPermissions',
2431 'checkPermissionHooks',
2432 'checkPageRestrictions',
2433 'checkCascadingSourcesRestrictions',
2434 'checkActionPermissions',
2435 'checkUserBlock'
2436 ];
2437 } else {
2438 $checks = [
2439 'checkQuickPermissions',
2440 'checkPermissionHooks',
2441 'checkSpecialsAndNSPermissions',
2442 'checkCSSandJSPermissions',
2443 'checkPageRestrictions',
2444 'checkCascadingSourcesRestrictions',
2445 'checkActionPermissions',
2446 'checkUserBlock'
2447 ];
2448 }
2449
2450 $errors = [];
2451 while ( count( $checks ) > 0 &&
2452 !( $short && count( $errors ) > 0 ) ) {
2453 $method = array_shift( $checks );
2454 $errors = $this->$method( $action, $user, $errors, $rigor, $short );
2455 }
2456
2457 return $errors;
2458 }
2459
2460 /**
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
2465 * @return array
2466 */
2467 public static function getFilteredRestrictionTypes( $exists = true ) {
2468 global $wgRestrictionTypes;
2469 $types = $wgRestrictionTypes;
2470 if ( $exists ) {
2471 # Remove the create restriction for existing titles
2472 $types = array_diff( $types, [ 'create' ] );
2473 } else {
2474 # Only the create and upload restrictions apply to non-existing titles
2475 $types = array_intersect( $types, [ 'create', 'upload' ] );
2476 }
2477 return $types;
2478 }
2479
2480 /**
2481 * Returns restriction types for the current Title
2482 *
2483 * @return array Applicable restriction types
2484 */
2485 public function getRestrictionTypes() {
2486 if ( $this->isSpecialPage() ) {
2487 return [];
2488 }
2489
2490 $types = self::getFilteredRestrictionTypes( $this->exists() );
2491
2492 if ( $this->getNamespace() != NS_FILE ) {
2493 # Remove the upload restriction for non-file titles
2494 $types = array_diff( $types, [ 'upload' ] );
2495 }
2496
2497 Hooks::run( 'TitleGetRestrictionTypes', [ $this, &$types ] );
2498
2499 wfDebug( __METHOD__ . ': applicable restrictions to [[' .
2500 $this->getPrefixedText() . ']] are {' . implode( ',', $types ) . "}\n" );
2501
2502 return $types;
2503 }
2504
2505 /**
2506 * Is this title subject to title protection?
2507 * Title protection is the one applied against creation of such title.
2508 *
2509 * @return array|bool An associative array representing any existent title
2510 * protection, or false if there's none.
2511 */
2512 public function getTitleProtection() {
2513 // Can't protect pages in special namespaces
2514 if ( $this->getNamespace() < 0 ) {
2515 return false;
2516 }
2517
2518 // Can't protect pages that exist.
2519 if ( $this->exists() ) {
2520 return false;
2521 }
2522
2523 if ( $this->mTitleProtection === null ) {
2524 $dbr = wfGetDB( DB_SLAVE );
2525 $res = $dbr->select(
2526 'protected_titles',
2527 [
2528 'user' => 'pt_user',
2529 'reason' => 'pt_reason',
2530 'expiry' => 'pt_expiry',
2531 'permission' => 'pt_create_perm'
2532 ],
2533 [ 'pt_namespace' => $this->getNamespace(), 'pt_title' => $this->getDBkey() ],
2534 __METHOD__
2535 );
2536
2537 // fetchRow returns false if there are no rows.
2538 $row = $dbr->fetchRow( $res );
2539 if ( $row ) {
2540 if ( $row['permission'] == 'sysop' ) {
2541 $row['permission'] = 'editprotected'; // B/C
2542 }
2543 if ( $row['permission'] == 'autoconfirmed' ) {
2544 $row['permission'] = 'editsemiprotected'; // B/C
2545 }
2546 $row['expiry'] = $dbr->decodeExpiry( $row['expiry'] );
2547 }
2548 $this->mTitleProtection = $row;
2549 }
2550 return $this->mTitleProtection;
2551 }
2552
2553 /**
2554 * Remove any title protection due to page existing
2555 */
2556 public function deleteTitleProtection() {
2557 $dbw = wfGetDB( DB_MASTER );
2558
2559 $dbw->delete(
2560 'protected_titles',
2561 [ 'pt_namespace' => $this->getNamespace(), 'pt_title' => $this->getDBkey() ],
2562 __METHOD__
2563 );
2564 $this->mTitleProtection = false;
2565 }
2566
2567 /**
2568 * Is this page "semi-protected" - the *only* protection levels are listed
2569 * in $wgSemiprotectedRestrictionLevels?
2570 *
2571 * @param string $action Action to check (default: edit)
2572 * @return bool
2573 */
2574 public function isSemiProtected( $action = 'edit' ) {
2575 global $wgSemiprotectedRestrictionLevels;
2576
2577 $restrictions = $this->getRestrictions( $action );
2578 $semi = $wgSemiprotectedRestrictionLevels;
2579 if ( !$restrictions || !$semi ) {
2580 // Not protected, or all protection is full protection
2581 return false;
2582 }
2583
2584 // Remap autoconfirmed to editsemiprotected for BC
2585 foreach ( array_keys( $semi, 'autoconfirmed' ) as $key ) {
2586 $semi[$key] = 'editsemiprotected';
2587 }
2588 foreach ( array_keys( $restrictions, 'autoconfirmed' ) as $key ) {
2589 $restrictions[$key] = 'editsemiprotected';
2590 }
2591
2592 return !array_diff( $restrictions, $semi );
2593 }
2594
2595 /**
2596 * Does the title correspond to a protected article?
2597 *
2598 * @param string $action The action the page is protected from,
2599 * by default checks all actions.
2600 * @return bool
2601 */
2602 public function isProtected( $action = '' ) {
2603 global $wgRestrictionLevels;
2604
2605 $restrictionTypes = $this->getRestrictionTypes();
2606
2607 # Special pages have inherent protection
2608 if ( $this->isSpecialPage() ) {
2609 return true;
2610 }
2611
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 != '' ) {
2618 return true;
2619 }
2620 }
2621 }
2622 }
2623
2624 return false;
2625 }
2626
2627 /**
2628 * Determines if $user is unable to edit this page because it has been protected
2629 * by $wgNamespaceProtection.
2630 *
2631 * @param User $user User object to check permissions
2632 * @return bool
2633 */
2634 public function isNamespaceProtected( User $user ) {
2635 global $wgNamespaceProtection;
2636
2637 if ( isset( $wgNamespaceProtection[$this->mNamespace] ) ) {
2638 foreach ( (array)$wgNamespaceProtection[$this->mNamespace] as $right ) {
2639 if ( $right != '' && !$user->isAllowed( $right ) ) {
2640 return true;
2641 }
2642 }
2643 }
2644 return false;
2645 }
2646
2647 /**
2648 * Cascading protection: Return true if cascading restrictions apply to this page, false if not.
2649 *
2650 * @return bool If the page is subject to cascading restrictions.
2651 */
2652 public function isCascadeProtected() {
2653 list( $sources, /* $restrictions */ ) = $this->getCascadeProtectionSources( false );
2654 return ( $sources > 0 );
2655 }
2656
2657 /**
2658 * Determines whether cascading protection sources have already been loaded from
2659 * the database.
2660 *
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
2664 * @since 1.23
2665 */
2666 public function areCascadeProtectionSourcesLoaded( $getPages = true ) {
2667 return $getPages ? $this->mCascadeSources !== null : $this->mHasCascadingRestrictions !== null;
2668 }
2669
2670 /**
2671 * Cascading protection: Get the source of any cascading restrictions on this page.
2672 *
2673 * @param bool $getPages Whether or not to retrieve the actual pages
2674 * that the restrictions have come from and the actual restrictions
2675 * themselves.
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
2681 * false.
2682 */
2683 public function getCascadeProtectionSources( $getPages = true ) {
2684 $pagerestrictions = [];
2685
2686 if ( $this->mCascadeSources !== null && $getPages ) {
2687 return [ $this->mCascadeSources, $this->mCascadingRestrictions ];
2688 } elseif ( $this->mHasCascadingRestrictions !== null && !$getPages ) {
2689 return [ $this->mHasCascadingRestrictions, $pagerestrictions ];
2690 }
2691
2692 $dbr = wfGetDB( DB_SLAVE );
2693
2694 if ( $this->getNamespace() == NS_FILE ) {
2695 $tables = [ 'imagelinks', 'page_restrictions' ];
2696 $where_clauses = [
2697 'il_to' => $this->getDBkey(),
2698 'il_from=pr_page',
2699 'pr_cascade' => 1
2700 ];
2701 } else {
2702 $tables = [ 'templatelinks', 'page_restrictions' ];
2703 $where_clauses = [
2704 'tl_namespace' => $this->getNamespace(),
2705 'tl_title' => $this->getDBkey(),
2706 'tl_from=pr_page',
2707 'pr_cascade' => 1
2708 ];
2709 }
2710
2711 if ( $getPages ) {
2712 $cols = [ 'pr_page', 'page_namespace', 'page_title',
2713 'pr_expiry', 'pr_type', 'pr_level' ];
2714 $where_clauses[] = 'page_id=pr_page';
2715 $tables[] = 'page';
2716 } else {
2717 $cols = [ 'pr_expiry' ];
2718 }
2719
2720 $res = $dbr->select( $tables, $cols, $where_clauses, __METHOD__ );
2721
2722 $sources = $getPages ? [] : false;
2723 $now = wfTimestampNow();
2724
2725 foreach ( $res as $row ) {
2726 $expiry = $dbr->decodeExpiry( $row->pr_expiry );
2727 if ( $expiry > $now ) {
2728 if ( $getPages ) {
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
2735
2736 if ( !isset( $pagerestrictions[$row->pr_type] ) ) {
2737 $pagerestrictions[$row->pr_type] = [];
2738 }
2739
2740 if (
2741 isset( $pagerestrictions[$row->pr_type] )
2742 && !in_array( $row->pr_level, $pagerestrictions[$row->pr_type] )
2743 ) {
2744 $pagerestrictions[$row->pr_type][] = $row->pr_level;
2745 }
2746 } else {
2747 $sources = true;
2748 }
2749 }
2750 }
2751
2752 if ( $getPages ) {
2753 $this->mCascadeSources = $sources;
2754 $this->mCascadingRestrictions = $pagerestrictions;
2755 } else {
2756 $this->mHasCascadingRestrictions = $sources;
2757 }
2758
2759 return [ $sources, $pagerestrictions ];
2760 }
2761
2762 /**
2763 * Accessor for mRestrictionsLoaded
2764 *
2765 * @return bool Whether or not the page's restrictions have already been
2766 * loaded from the database
2767 * @since 1.23
2768 */
2769 public function areRestrictionsLoaded() {
2770 return $this->mRestrictionsLoaded;
2771 }
2772
2773 /**
2774 * Accessor/initialisation for mRestrictions
2775 *
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.
2781 */
2782 public function getRestrictions( $action ) {
2783 if ( !$this->mRestrictionsLoaded ) {
2784 $this->loadRestrictions();
2785 }
2786 return isset( $this->mRestrictions[$action] )
2787 ? $this->mRestrictions[$action]
2788 : [];
2789 }
2790
2791 /**
2792 * Accessor/initialisation for mRestrictions
2793 *
2794 * @return array Keys are actions, values are arrays as returned by
2795 * Title::getRestrictions()
2796 * @since 1.23
2797 */
2798 public function getAllRestrictions() {
2799 if ( !$this->mRestrictionsLoaded ) {
2800 $this->loadRestrictions();
2801 }
2802 return $this->mRestrictions;
2803 }
2804
2805 /**
2806 * Get the expiry time for the restriction against a given action
2807 *
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.
2811 */
2812 public function getRestrictionExpiry( $action ) {
2813 if ( !$this->mRestrictionsLoaded ) {
2814 $this->loadRestrictions();
2815 }
2816 return isset( $this->mRestrictionsExpiry[$action] ) ? $this->mRestrictionsExpiry[$action] : false;
2817 }
2818
2819 /**
2820 * Returns cascading restrictions for the current article
2821 *
2822 * @return bool
2823 */
2824 function areRestrictionsCascading() {
2825 if ( !$this->mRestrictionsLoaded ) {
2826 $this->loadRestrictions();
2827 }
2828
2829 return $this->mCascadeRestriction;
2830 }
2831
2832 /**
2833 * Loads a string into mRestrictions array
2834 *
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)
2838 */
2839 private function loadRestrictionsFromResultWrapper( $res, $oldFashionedRestrictions = null ) {
2840 $rows = [];
2841
2842 foreach ( $res as $row ) {
2843 $rows[] = $row;
2844 }
2845
2846 $this->loadRestrictionsFromRows( $rows, $oldFashionedRestrictions );
2847 }
2848
2849 /**
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.
2853 *
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)
2857 */
2858 public function loadRestrictionsFromRows( $rows, $oldFashionedRestrictions = null ) {
2859 $dbr = wfGetDB( DB_SLAVE );
2860
2861 $restrictionTypes = $this->getRestrictionTypes();
2862
2863 foreach ( $restrictionTypes as $type ) {
2864 $this->mRestrictions[$type] = [];
2865 $this->mRestrictionsExpiry[$type] = 'infinity';
2866 }
2867
2868 $this->mCascadeRestriction = false;
2869
2870 # Backwards-compatibility: also load the restrictions from the page record (old format).
2871 if ( $oldFashionedRestrictions !== null ) {
2872 $this->mOldRestrictions = $oldFashionedRestrictions;
2873 }
2874
2875 if ( $this->mOldRestrictions === false ) {
2876 $this->mOldRestrictions = $dbr->selectField( 'page', 'page_restrictions',
2877 [ 'page_id' => $this->getArticleID() ], __METHOD__ );
2878 }
2879
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] ) );
2887 } else {
2888 $restriction = trim( $temp[1] );
2889 if ( $restriction != '' ) { // some old entries are empty
2890 $this->mRestrictions[$temp[0]] = explode( ',', $restriction );
2891 }
2892 }
2893 }
2894 }
2895
2896 if ( count( $rows ) ) {
2897 # Current system - load second to make them override.
2898 $now = wfTimestampNow();
2899
2900 # Cycle through all the restrictions.
2901 foreach ( $rows as $row ) {
2902
2903 // Don't take care of restrictions types that aren't allowed
2904 if ( !in_array( $row->pr_type, $restrictionTypes ) ) {
2905 continue;
2906 }
2907
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 );
2911
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 ) );
2916
2917 $this->mCascadeRestriction |= $row->pr_cascade;
2918 }
2919 }
2920 }
2921
2922 $this->mRestrictionsLoaded = true;
2923 }
2924
2925 /**
2926 * Load restrictions from the page_restrictions table
2927 *
2928 * @param string $oldFashionedRestrictions Comma-separated list of page
2929 * restrictions from page table (pre 1.10)
2930 */
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() ],
2939 __METHOD__
2940 );
2941
2942 $this->loadRestrictionsFromResultWrapper( $res, $oldFashionedRestrictions );
2943 } else {
2944 $title_protection = $this->getTitleProtection();
2945
2946 if ( $title_protection ) {
2947 $now = wfTimestampNow();
2948 $expiry = $dbr->decodeExpiry( $title_protection['expiry'] );
2949
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;
2956 }
2957 } else {
2958 $this->mRestrictionsExpiry['create'] = 'infinity';
2959 }
2960 $this->mRestrictionsLoaded = true;
2961 }
2962 }
2963 }
2964
2965 /**
2966 * Flush the protection cache in this object and force reload from the database.
2967 * This is used when updating protection from WikiPage::doUpdateRestrictions().
2968 */
2969 public function flushRestrictions() {
2970 $this->mRestrictionsLoaded = false;
2971 $this->mTitleProtection = null;
2972 }
2973
2974 /**
2975 * Purge expired restrictions from the page_restrictions table
2976 */
2977 static function purgeExpiredRestrictions() {
2978 if ( wfReadOnly() ) {
2979 return;
2980 }
2981
2982 DeferredUpdates::addUpdate( new AtomicSectionUpdate(
2983 wfGetDB( DB_MASTER ),
2984 __METHOD__,
2985 function ( IDatabase $dbw, $fname ) {
2986 $dbw->delete(
2987 'page_restrictions',
2988 [ 'pr_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ],
2989 $fname
2990 );
2991 $dbw->delete(
2992 'protected_titles',
2993 [ 'pt_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ],
2994 $fname
2995 );
2996 }
2997 ) );
2998 }
2999
3000 /**
3001 * Does this have subpages? (Warning, usually requires an extra DB query.)
3002 *
3003 * @return bool
3004 */
3005 public function hasSubpages() {
3006 if ( !MWNamespace::hasSubpages( $this->mNamespace ) ) {
3007 # Duh
3008 return false;
3009 }
3010
3011 # We dynamically add a member variable for the purpose of this method
3012 # alone to cache the result. There's no point in having it hanging
3013 # around uninitialized in every Title object; therefore we only add it
3014 # if needed and don't declare it statically.
3015 if ( $this->mHasSubpages === null ) {
3016 $this->mHasSubpages = false;
3017 $subpages = $this->getSubpages( 1 );
3018 if ( $subpages instanceof TitleArray ) {
3019 $this->mHasSubpages = (bool)$subpages->count();
3020 }
3021 }
3022
3023 return $this->mHasSubpages;
3024 }
3025
3026 /**
3027 * Get all subpages of this page.
3028 *
3029 * @param int $limit Maximum number of subpages to fetch; -1 for no limit
3030 * @return TitleArray|array TitleArray, or empty array if this page's namespace
3031 * doesn't allow subpages
3032 */
3033 public function getSubpages( $limit = -1 ) {
3034 if ( !MWNamespace::hasSubpages( $this->getNamespace() ) ) {
3035 return [];
3036 }
3037
3038 $dbr = wfGetDB( DB_SLAVE );
3039 $conds['page_namespace'] = $this->getNamespace();
3040 $conds[] = 'page_title ' . $dbr->buildLike( $this->getDBkey() . '/', $dbr->anyString() );
3041 $options = [];
3042 if ( $limit > -1 ) {
3043 $options['LIMIT'] = $limit;
3044 }
3045 $this->mSubpages = TitleArray::newFromResult(
3046 $dbr->select( 'page',
3047 [ 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' ],
3048 $conds,
3049 __METHOD__,
3050 $options
3051 )
3052 );
3053 return $this->mSubpages;
3054 }
3055
3056 /**
3057 * Is there a version of this page in the deletion archive?
3058 *
3059 * @return int The number of archived revisions
3060 */
3061 public function isDeleted() {
3062 if ( $this->getNamespace() < 0 ) {
3063 $n = 0;
3064 } else {
3065 $dbr = wfGetDB( DB_SLAVE );
3066
3067 $n = $dbr->selectField( 'archive', 'COUNT(*)',
3068 [ 'ar_namespace' => $this->getNamespace(), 'ar_title' => $this->getDBkey() ],
3069 __METHOD__
3070 );
3071 if ( $this->getNamespace() == NS_FILE ) {
3072 $n += $dbr->selectField( 'filearchive', 'COUNT(*)',
3073 [ 'fa_name' => $this->getDBkey() ],
3074 __METHOD__
3075 );
3076 }
3077 }
3078 return (int)$n;
3079 }
3080
3081 /**
3082 * Is there a version of this page in the deletion archive?
3083 *
3084 * @return bool
3085 */
3086 public function isDeletedQuick() {
3087 if ( $this->getNamespace() < 0 ) {
3088 return false;
3089 }
3090 $dbr = wfGetDB( DB_SLAVE );
3091 $deleted = (bool)$dbr->selectField( 'archive', '1',
3092 [ 'ar_namespace' => $this->getNamespace(), 'ar_title' => $this->getDBkey() ],
3093 __METHOD__
3094 );
3095 if ( !$deleted && $this->getNamespace() == NS_FILE ) {
3096 $deleted = (bool)$dbr->selectField( 'filearchive', '1',
3097 [ 'fa_name' => $this->getDBkey() ],
3098 __METHOD__
3099 );
3100 }
3101 return $deleted;
3102 }
3103
3104 /**
3105 * Get the article ID for this Title from the link cache,
3106 * adding it if necessary
3107 *
3108 * @param int $flags A bit field; may be Title::GAID_FOR_UPDATE to select
3109 * for update
3110 * @return int The ID
3111 */
3112 public function getArticleID( $flags = 0 ) {
3113 if ( $this->getNamespace() < 0 ) {
3114 $this->mArticleID = 0;
3115 return $this->mArticleID;
3116 }
3117 $linkCache = LinkCache::singleton();
3118 if ( $flags & self::GAID_FOR_UPDATE ) {
3119 $oldUpdate = $linkCache->forUpdate( true );
3120 $linkCache->clearLink( $this );
3121 $this->mArticleID = $linkCache->addLinkObj( $this );
3122 $linkCache->forUpdate( $oldUpdate );
3123 } else {
3124 if ( -1 == $this->mArticleID ) {
3125 $this->mArticleID = $linkCache->addLinkObj( $this );
3126 }
3127 }
3128 return $this->mArticleID;
3129 }
3130
3131 /**
3132 * Is this an article that is a redirect page?
3133 * Uses link cache, adding it if necessary
3134 *
3135 * @param int $flags A bit field; may be Title::GAID_FOR_UPDATE to select for update
3136 * @return bool
3137 */
3138 public function isRedirect( $flags = 0 ) {
3139 if ( !is_null( $this->mRedirect ) ) {
3140 return $this->mRedirect;
3141 }
3142 if ( !$this->getArticleID( $flags ) ) {
3143 $this->mRedirect = false;
3144 return $this->mRedirect;
3145 }
3146
3147 $linkCache = LinkCache::singleton();
3148 $linkCache->addLinkObj( $this ); # in case we already had an article ID
3149 $cached = $linkCache->getGoodLinkFieldObj( $this, 'redirect' );
3150 if ( $cached === null ) {
3151 # Trust LinkCache's state over our own
3152 # LinkCache is telling us that the page doesn't exist, despite there being cached
3153 # data relating to an existing page in $this->mArticleID. Updaters should clear
3154 # LinkCache as appropriate, or use $flags = Title::GAID_FOR_UPDATE. If that flag is
3155 # set, then LinkCache will definitely be up to date here, since getArticleID() forces
3156 # LinkCache to refresh its data from the master.
3157 $this->mRedirect = false;
3158 return $this->mRedirect;
3159 }
3160
3161 $this->mRedirect = (bool)$cached;
3162
3163 return $this->mRedirect;
3164 }
3165
3166 /**
3167 * What is the length of this page?
3168 * Uses link cache, adding it if necessary
3169 *
3170 * @param int $flags A bit field; may be Title::GAID_FOR_UPDATE to select for update
3171 * @return int
3172 */
3173 public function getLength( $flags = 0 ) {
3174 if ( $this->mLength != -1 ) {
3175 return $this->mLength;
3176 }
3177 if ( !$this->getArticleID( $flags ) ) {
3178 $this->mLength = 0;
3179 return $this->mLength;
3180 }
3181 $linkCache = LinkCache::singleton();
3182 $linkCache->addLinkObj( $this ); # in case we already had an article ID
3183 $cached = $linkCache->getGoodLinkFieldObj( $this, 'length' );
3184 if ( $cached === null ) {
3185 # Trust LinkCache's state over our own, as for isRedirect()
3186 $this->mLength = 0;
3187 return $this->mLength;
3188 }
3189
3190 $this->mLength = intval( $cached );
3191
3192 return $this->mLength;
3193 }
3194
3195 /**
3196 * What is the page_latest field for this page?
3197 *
3198 * @param int $flags A bit field; may be Title::GAID_FOR_UPDATE to select for update
3199 * @return int Int or 0 if the page doesn't exist
3200 */
3201 public function getLatestRevID( $flags = 0 ) {
3202 if ( !( $flags & Title::GAID_FOR_UPDATE ) && $this->mLatestID !== false ) {
3203 return intval( $this->mLatestID );
3204 }
3205 if ( !$this->getArticleID( $flags ) ) {
3206 $this->mLatestID = 0;
3207 return $this->mLatestID;
3208 }
3209 $linkCache = LinkCache::singleton();
3210 $linkCache->addLinkObj( $this ); # in case we already had an article ID
3211 $cached = $linkCache->getGoodLinkFieldObj( $this, 'revision' );
3212 if ( $cached === null ) {
3213 # Trust LinkCache's state over our own, as for isRedirect()
3214 $this->mLatestID = 0;
3215 return $this->mLatestID;
3216 }
3217
3218 $this->mLatestID = intval( $cached );
3219
3220 return $this->mLatestID;
3221 }
3222
3223 /**
3224 * This clears some fields in this object, and clears any associated
3225 * keys in the "bad links" section of the link cache.
3226 *
3227 * - This is called from WikiPage::doEdit() and WikiPage::insertOn() to allow
3228 * loading of the new page_id. It's also called from
3229 * WikiPage::doDeleteArticleReal()
3230 *
3231 * @param int $newid The new Article ID
3232 */
3233 public function resetArticleID( $newid ) {
3234 $linkCache = LinkCache::singleton();
3235 $linkCache->clearLink( $this );
3236
3237 if ( $newid === false ) {
3238 $this->mArticleID = -1;
3239 } else {
3240 $this->mArticleID = intval( $newid );
3241 }
3242 $this->mRestrictionsLoaded = false;
3243 $this->mRestrictions = [];
3244 $this->mOldRestrictions = false;
3245 $this->mRedirect = null;
3246 $this->mLength = -1;
3247 $this->mLatestID = false;
3248 $this->mContentModel = false;
3249 $this->mEstimateRevisions = null;
3250 $this->mPageLanguage = false;
3251 $this->mDbPageLanguage = false;
3252 $this->mIsBigDeletion = null;
3253 }
3254
3255 public static function clearCaches() {
3256 $linkCache = LinkCache::singleton();
3257 $linkCache->clear();
3258
3259 $titleCache = self::getTitleCache();
3260 $titleCache->clear();
3261 }
3262
3263 /**
3264 * Capitalize a text string for a title if it belongs to a namespace that capitalizes
3265 *
3266 * @param string $text Containing title to capitalize
3267 * @param int $ns Namespace index, defaults to NS_MAIN
3268 * @return string Containing capitalized title
3269 */
3270 public static function capitalize( $text, $ns = NS_MAIN ) {
3271 global $wgContLang;
3272
3273 if ( MWNamespace::isCapitalized( $ns ) ) {
3274 return $wgContLang->ucfirst( $text );
3275 } else {
3276 return $text;
3277 }
3278 }
3279
3280 /**
3281 * Secure and split - main initialisation function for this object
3282 *
3283 * Assumes that mDbkeyform has been set, and is urldecoded
3284 * and uses underscores, but not otherwise munged. This function
3285 * removes illegal characters, splits off the interwiki and
3286 * namespace prefixes, sets the other forms, and canonicalizes
3287 * everything.
3288 *
3289 * @throws MalformedTitleException On invalid titles
3290 * @return bool True on success
3291 */
3292 private function secureAndSplit() {
3293 # Initialisation
3294 $this->mInterwiki = '';
3295 $this->mFragment = '';
3296 $this->mNamespace = $this->mDefaultNamespace; # Usually NS_MAIN
3297
3298 $dbkey = $this->mDbkeyform;
3299
3300 // @note: splitTitleString() is a temporary hack to allow MediaWikiTitleCodec to share
3301 // the parsing code with Title, while avoiding massive refactoring.
3302 // @todo: get rid of secureAndSplit, refactor parsing code.
3303 // @note: getTitleParser() returns a TitleParser implementation which does not have a
3304 // splitTitleString method, but the only implementation (MediaWikiTitleCodec) does
3305 $titleCodec = MediaWikiServices::getInstance()->getTitleParser();
3306 // MalformedTitleException can be thrown here
3307 $parts = $titleCodec->splitTitleString( $dbkey, $this->getDefaultNamespace() );
3308
3309 # Fill fields
3310 $this->setFragment( '#' . $parts['fragment'] );
3311 $this->mInterwiki = $parts['interwiki'];
3312 $this->mLocalInterwiki = $parts['local_interwiki'];
3313 $this->mNamespace = $parts['namespace'];
3314 $this->mUserCaseDBKey = $parts['user_case_dbkey'];
3315
3316 $this->mDbkeyform = $parts['dbkey'];
3317 $this->mUrlform = wfUrlencode( $this->mDbkeyform );
3318 $this->mTextform = strtr( $this->mDbkeyform, '_', ' ' );
3319
3320 # We already know that some pages won't be in the database!
3321 if ( $this->isExternal() || $this->mNamespace == NS_SPECIAL ) {
3322 $this->mArticleID = 0;
3323 }
3324
3325 return true;
3326 }
3327
3328 /**
3329 * Get an array of Title objects linking to this Title
3330 * Also stores the IDs in the link cache.
3331 *
3332 * WARNING: do not use this function on arbitrary user-supplied titles!
3333 * On heavily-used templates it will max out the memory.
3334 *
3335 * @param array $options May be FOR UPDATE
3336 * @param string $table Table name
3337 * @param string $prefix Fields prefix
3338 * @return Title[] Array of Title objects linking here
3339 */
3340 public function getLinksTo( $options = [], $table = 'pagelinks', $prefix = 'pl' ) {
3341 if ( count( $options ) > 0 ) {
3342 $db = wfGetDB( DB_MASTER );
3343 } else {
3344 $db = wfGetDB( DB_SLAVE );
3345 }
3346
3347 $res = $db->select(
3348 [ 'page', $table ],
3349 self::getSelectFields(),
3350 [
3351 "{$prefix}_from=page_id",
3352 "{$prefix}_namespace" => $this->getNamespace(),
3353 "{$prefix}_title" => $this->getDBkey() ],
3354 __METHOD__,
3355 $options
3356 );
3357
3358 $retVal = [];
3359 if ( $res->numRows() ) {
3360 $linkCache = LinkCache::singleton();
3361 foreach ( $res as $row ) {
3362 $titleObj = Title::makeTitle( $row->page_namespace, $row->page_title );
3363 if ( $titleObj ) {
3364 $linkCache->addGoodLinkObjFromRow( $titleObj, $row );
3365 $retVal[] = $titleObj;
3366 }
3367 }
3368 }
3369 return $retVal;
3370 }
3371
3372 /**
3373 * Get an array of Title objects using this Title as a template
3374 * Also stores the IDs in the link cache.
3375 *
3376 * WARNING: do not use this function on arbitrary user-supplied titles!
3377 * On heavily-used templates it will max out the memory.
3378 *
3379 * @param array $options Query option to Database::select()
3380 * @return Title[] Array of Title the Title objects linking here
3381 */
3382 public function getTemplateLinksTo( $options = [] ) {
3383 return $this->getLinksTo( $options, 'templatelinks', 'tl' );
3384 }
3385
3386 /**
3387 * Get an array of Title objects linked from this Title
3388 * Also stores the IDs in the link cache.
3389 *
3390 * WARNING: do not use this function on arbitrary user-supplied titles!
3391 * On heavily-used templates it will max out the memory.
3392 *
3393 * @param array $options Query option to Database::select()
3394 * @param string $table Table name
3395 * @param string $prefix Fields prefix
3396 * @return array Array of Title objects linking here
3397 */
3398 public function getLinksFrom( $options = [], $table = 'pagelinks', $prefix = 'pl' ) {
3399 $id = $this->getArticleID();
3400
3401 # If the page doesn't exist; there can't be any link from this page
3402 if ( !$id ) {
3403 return [];
3404 }
3405
3406 $db = wfGetDB( DB_SLAVE );
3407
3408 $blNamespace = "{$prefix}_namespace";
3409 $blTitle = "{$prefix}_title";
3410
3411 $res = $db->select(
3412 [ $table, 'page' ],
3413 array_merge(
3414 [ $blNamespace, $blTitle ],
3415 WikiPage::selectFields()
3416 ),
3417 [ "{$prefix}_from" => $id ],
3418 __METHOD__,
3419 $options,
3420 [ 'page' => [
3421 'LEFT JOIN',
3422 [ "page_namespace=$blNamespace", "page_title=$blTitle" ]
3423 ] ]
3424 );
3425
3426 $retVal = [];
3427 $linkCache = LinkCache::singleton();
3428 foreach ( $res as $row ) {
3429 if ( $row->page_id ) {
3430 $titleObj = Title::newFromRow( $row );
3431 } else {
3432 $titleObj = Title::makeTitle( $row->$blNamespace, $row->$blTitle );
3433 $linkCache->addBadLinkObj( $titleObj );
3434 }
3435 $retVal[] = $titleObj;
3436 }
3437
3438 return $retVal;
3439 }
3440
3441 /**
3442 * Get an array of Title objects used on this Title as a template
3443 * Also stores the IDs in the link cache.
3444 *
3445 * WARNING: do not use this function on arbitrary user-supplied titles!
3446 * On heavily-used templates it will max out the memory.
3447 *
3448 * @param array $options May be FOR UPDATE
3449 * @return Title[] Array of Title the Title objects used here
3450 */
3451 public function getTemplateLinksFrom( $options = [] ) {
3452 return $this->getLinksFrom( $options, 'templatelinks', 'tl' );
3453 }
3454
3455 /**
3456 * Get an array of Title objects referring to non-existent articles linked
3457 * from this page.
3458 *
3459 * @todo check if needed (used only in SpecialBrokenRedirects.php, and
3460 * should use redirect table in this case).
3461 * @return Title[] Array of Title the Title objects
3462 */
3463 public function getBrokenLinksFrom() {
3464 if ( $this->getArticleID() == 0 ) {
3465 # All links from article ID 0 are false positives
3466 return [];
3467 }
3468
3469 $dbr = wfGetDB( DB_SLAVE );
3470 $res = $dbr->select(
3471 [ 'page', 'pagelinks' ],
3472 [ 'pl_namespace', 'pl_title' ],
3473 [
3474 'pl_from' => $this->getArticleID(),
3475 'page_namespace IS NULL'
3476 ],
3477 __METHOD__, [],
3478 [
3479 'page' => [
3480 'LEFT JOIN',
3481 [ 'pl_namespace=page_namespace', 'pl_title=page_title' ]
3482 ]
3483 ]
3484 );
3485
3486 $retVal = [];
3487 foreach ( $res as $row ) {
3488 $retVal[] = Title::makeTitle( $row->pl_namespace, $row->pl_title );
3489 }
3490 return $retVal;
3491 }
3492
3493 /**
3494 * Get a list of URLs to purge from the CDN cache when this
3495 * page changes
3496 *
3497 * @return string[] Array of String the URLs
3498 */
3499 public function getCdnUrls() {
3500 $urls = [
3501 $this->getInternalURL(),
3502 $this->getInternalURL( 'action=history' )
3503 ];
3504
3505 $pageLang = $this->getPageLanguage();
3506 if ( $pageLang->hasVariants() ) {
3507 $variants = $pageLang->getVariants();
3508 foreach ( $variants as $vCode ) {
3509 $urls[] = $this->getInternalURL( $vCode );
3510 }
3511 }
3512
3513 // If we are looking at a css/js user subpage, purge the action=raw.
3514 if ( $this->isJsSubpage() ) {
3515 $urls[] = $this->getInternalURL( 'action=raw&ctype=text/javascript' );
3516 } elseif ( $this->isCssSubpage() ) {
3517 $urls[] = $this->getInternalURL( 'action=raw&ctype=text/css' );
3518 }
3519
3520 Hooks::run( 'TitleSquidURLs', [ $this, &$urls ] );
3521 return $urls;
3522 }
3523
3524 /**
3525 * @deprecated since 1.27 use getCdnUrls()
3526 */
3527 public function getSquidURLs() {
3528 return $this->getCdnUrls();
3529 }
3530
3531 /**
3532 * Purge all applicable CDN URLs
3533 */
3534 public function purgeSquid() {
3535 DeferredUpdates::addUpdate(
3536 new CdnCacheUpdate( $this->getCdnUrls() ),
3537 DeferredUpdates::PRESEND
3538 );
3539 }
3540
3541 /**
3542 * Move this page without authentication
3543 *
3544 * @deprecated since 1.25 use MovePage class instead
3545 * @param Title $nt The new page Title
3546 * @return array|bool True on success, getUserPermissionsErrors()-like array on failure
3547 */
3548 public function moveNoAuth( &$nt ) {
3549 wfDeprecated( __METHOD__, '1.25' );
3550 return $this->moveTo( $nt, false );
3551 }
3552
3553 /**
3554 * Check whether a given move operation would be valid.
3555 * Returns true if ok, or a getUserPermissionsErrors()-like array otherwise
3556 *
3557 * @deprecated since 1.25, use MovePage's methods instead
3558 * @param Title $nt The new title
3559 * @param bool $auth Whether to check user permissions (uses $wgUser)
3560 * @param string $reason Is the log summary of the move, used for spam checking
3561 * @return array|bool True on success, getUserPermissionsErrors()-like array on failure
3562 */
3563 public function isValidMoveOperation( &$nt, $auth = true, $reason = '' ) {
3564 global $wgUser;
3565
3566 if ( !( $nt instanceof Title ) ) {
3567 // Normally we'd add this to $errors, but we'll get
3568 // lots of syntax errors if $nt is not an object
3569 return [ [ 'badtitletext' ] ];
3570 }
3571
3572 $mp = new MovePage( $this, $nt );
3573 $errors = $mp->isValidMove()->getErrorsArray();
3574 if ( $auth ) {
3575 $errors = wfMergeErrorArrays(
3576 $errors,
3577 $mp->checkPermissions( $wgUser, $reason )->getErrorsArray()
3578 );
3579 }
3580
3581 return $errors ?: true;
3582 }
3583
3584 /**
3585 * Check if the requested move target is a valid file move target
3586 * @todo move this to MovePage
3587 * @param Title $nt Target title
3588 * @return array List of errors
3589 */
3590 protected function validateFileMoveOperation( $nt ) {
3591 global $wgUser;
3592
3593 $errors = [];
3594
3595 $destFile = wfLocalFile( $nt );
3596 $destFile->load( File::READ_LATEST );
3597 if ( !$wgUser->isAllowed( 'reupload-shared' )
3598 && !$destFile->exists() && wfFindFile( $nt )
3599 ) {
3600 $errors[] = [ 'file-exists-sharedrepo' ];
3601 }
3602
3603 return $errors;
3604 }
3605
3606 /**
3607 * Move a title to a new location
3608 *
3609 * @deprecated since 1.25, use the MovePage class instead
3610 * @param Title $nt The new title
3611 * @param bool $auth Indicates whether $wgUser's permissions
3612 * should be checked
3613 * @param string $reason The reason for the move
3614 * @param bool $createRedirect Whether to create a redirect from the old title to the new title.
3615 * Ignored if the user doesn't have the suppressredirect right.
3616 * @return array|bool True on success, getUserPermissionsErrors()-like array on failure
3617 */
3618 public function moveTo( &$nt, $auth = true, $reason = '', $createRedirect = true ) {
3619 global $wgUser;
3620 $err = $this->isValidMoveOperation( $nt, $auth, $reason );
3621 if ( is_array( $err ) ) {
3622 // Auto-block user's IP if the account was "hard" blocked
3623 $wgUser->spreadAnyEditBlock();
3624 return $err;
3625 }
3626 // Check suppressredirect permission
3627 if ( $auth && !$wgUser->isAllowed( 'suppressredirect' ) ) {
3628 $createRedirect = true;
3629 }
3630
3631 $mp = new MovePage( $this, $nt );
3632 $status = $mp->move( $wgUser, $reason, $createRedirect );
3633 if ( $status->isOK() ) {
3634 return true;
3635 } else {
3636 return $status->getErrorsArray();
3637 }
3638 }
3639
3640 /**
3641 * Move this page's subpages to be subpages of $nt
3642 *
3643 * @param Title $nt Move target
3644 * @param bool $auth Whether $wgUser's permissions should be checked
3645 * @param string $reason The reason for the move
3646 * @param bool $createRedirect Whether to create redirects from the old subpages to
3647 * the new ones Ignored if the user doesn't have the 'suppressredirect' right
3648 * @return array Array with old page titles as keys, and strings (new page titles) or
3649 * arrays (errors) as values, or an error array with numeric indices if no pages
3650 * were moved
3651 */
3652 public function moveSubpages( $nt, $auth = true, $reason = '', $createRedirect = true ) {
3653 global $wgMaximumMovedPages;
3654 // Check permissions
3655 if ( !$this->userCan( 'move-subpages' ) ) {
3656 return [ 'cant-move-subpages' ];
3657 }
3658 // Do the source and target namespaces support subpages?
3659 if ( !MWNamespace::hasSubpages( $this->getNamespace() ) ) {
3660 return [ 'namespace-nosubpages',
3661 MWNamespace::getCanonicalName( $this->getNamespace() ) ];
3662 }
3663 if ( !MWNamespace::hasSubpages( $nt->getNamespace() ) ) {
3664 return [ 'namespace-nosubpages',
3665 MWNamespace::getCanonicalName( $nt->getNamespace() ) ];
3666 }
3667
3668 $subpages = $this->getSubpages( $wgMaximumMovedPages + 1 );
3669 $retval = [];
3670 $count = 0;
3671 foreach ( $subpages as $oldSubpage ) {
3672 $count++;
3673 if ( $count > $wgMaximumMovedPages ) {
3674 $retval[$oldSubpage->getPrefixedText()] =
3675 [ 'movepage-max-pages',
3676 $wgMaximumMovedPages ];
3677 break;
3678 }
3679
3680 // We don't know whether this function was called before
3681 // or after moving the root page, so check both
3682 // $this and $nt
3683 if ( $oldSubpage->getArticleID() == $this->getArticleID()
3684 || $oldSubpage->getArticleID() == $nt->getArticleID()
3685 ) {
3686 // When moving a page to a subpage of itself,
3687 // don't move it twice
3688 continue;
3689 }
3690 $newPageName = preg_replace(
3691 '#^' . preg_quote( $this->getDBkey(), '#' ) . '#',
3692 StringUtils::escapeRegexReplacement( $nt->getDBkey() ), # bug 21234
3693 $oldSubpage->getDBkey() );
3694 if ( $oldSubpage->isTalkPage() ) {
3695 $newNs = $nt->getTalkPage()->getNamespace();
3696 } else {
3697 $newNs = $nt->getSubjectPage()->getNamespace();
3698 }
3699 # Bug 14385: we need makeTitleSafe because the new page names may
3700 # be longer than 255 characters.
3701 $newSubpage = Title::makeTitleSafe( $newNs, $newPageName );
3702
3703 $success = $oldSubpage->moveTo( $newSubpage, $auth, $reason, $createRedirect );
3704 if ( $success === true ) {
3705 $retval[$oldSubpage->getPrefixedText()] = $newSubpage->getPrefixedText();
3706 } else {
3707 $retval[$oldSubpage->getPrefixedText()] = $success;
3708 }
3709 }
3710 return $retval;
3711 }
3712
3713 /**
3714 * Checks if this page is just a one-rev redirect.
3715 * Adds lock, so don't use just for light purposes.
3716 *
3717 * @return bool
3718 */
3719 public function isSingleRevRedirect() {
3720 global $wgContentHandlerUseDB;
3721
3722 $dbw = wfGetDB( DB_MASTER );
3723
3724 # Is it a redirect?
3725 $fields = [ 'page_is_redirect', 'page_latest', 'page_id' ];
3726 if ( $wgContentHandlerUseDB ) {
3727 $fields[] = 'page_content_model';
3728 }
3729
3730 $row = $dbw->selectRow( 'page',
3731 $fields,
3732 $this->pageCond(),
3733 __METHOD__,
3734 [ 'FOR UPDATE' ]
3735 );
3736 # Cache some fields we may want
3737 $this->mArticleID = $row ? intval( $row->page_id ) : 0;
3738 $this->mRedirect = $row ? (bool)$row->page_is_redirect : false;
3739 $this->mLatestID = $row ? intval( $row->page_latest ) : false;
3740 $this->mContentModel = $row && isset( $row->page_content_model )
3741 ? strval( $row->page_content_model )
3742 : false;
3743
3744 if ( !$this->mRedirect ) {
3745 return false;
3746 }
3747 # Does the article have a history?
3748 $row = $dbw->selectField( [ 'page', 'revision' ],
3749 'rev_id',
3750 [ 'page_namespace' => $this->getNamespace(),
3751 'page_title' => $this->getDBkey(),
3752 'page_id=rev_page',
3753 'page_latest != rev_id'
3754 ],
3755 __METHOD__,
3756 [ 'FOR UPDATE' ]
3757 );
3758 # Return true if there was no history
3759 return ( $row === false );
3760 }
3761
3762 /**
3763 * Checks if $this can be moved to a given Title
3764 * - Selects for update, so don't call it unless you mean business
3765 *
3766 * @deprecated since 1.25, use MovePage's methods instead
3767 * @param Title $nt The new title to check
3768 * @return bool
3769 */
3770 public function isValidMoveTarget( $nt ) {
3771 # Is it an existing file?
3772 if ( $nt->getNamespace() == NS_FILE ) {
3773 $file = wfLocalFile( $nt );
3774 $file->load( File::READ_LATEST );
3775 if ( $file->exists() ) {
3776 wfDebug( __METHOD__ . ": file exists\n" );
3777 return false;
3778 }
3779 }
3780 # Is it a redirect with no history?
3781 if ( !$nt->isSingleRevRedirect() ) {
3782 wfDebug( __METHOD__ . ": not a one-rev redirect\n" );
3783 return false;
3784 }
3785 # Get the article text
3786 $rev = Revision::newFromTitle( $nt, false, Revision::READ_LATEST );
3787 if ( !is_object( $rev ) ) {
3788 return false;
3789 }
3790 $content = $rev->getContent();
3791 # Does the redirect point to the source?
3792 # Or is it a broken self-redirect, usually caused by namespace collisions?
3793 $redirTitle = $content ? $content->getRedirectTarget() : null;
3794
3795 if ( $redirTitle ) {
3796 if ( $redirTitle->getPrefixedDBkey() != $this->getPrefixedDBkey() &&
3797 $redirTitle->getPrefixedDBkey() != $nt->getPrefixedDBkey() ) {
3798 wfDebug( __METHOD__ . ": redirect points to other page\n" );
3799 return false;
3800 } else {
3801 return true;
3802 }
3803 } else {
3804 # Fail safe (not a redirect after all. strange.)
3805 wfDebug( __METHOD__ . ": failsafe: database sais " . $nt->getPrefixedDBkey() .
3806 " is a redirect, but it doesn't contain a valid redirect.\n" );
3807 return false;
3808 }
3809 }
3810
3811 /**
3812 * Get categories to which this Title belongs and return an array of
3813 * categories' names.
3814 *
3815 * @return array Array of parents in the form:
3816 * $parent => $currentarticle
3817 */
3818 public function getParentCategories() {
3819 global $wgContLang;
3820
3821 $data = [];
3822
3823 $titleKey = $this->getArticleID();
3824
3825 if ( $titleKey === 0 ) {
3826 return $data;
3827 }
3828
3829 $dbr = wfGetDB( DB_SLAVE );
3830
3831 $res = $dbr->select(
3832 'categorylinks',
3833 'cl_to',
3834 [ 'cl_from' => $titleKey ],
3835 __METHOD__
3836 );
3837
3838 if ( $res->numRows() > 0 ) {
3839 foreach ( $res as $row ) {
3840 // $data[] = Title::newFromText($wgContLang->getNsText ( NS_CATEGORY ).':'.$row->cl_to);
3841 $data[$wgContLang->getNsText( NS_CATEGORY ) . ':' . $row->cl_to] = $this->getFullText();
3842 }
3843 }
3844 return $data;
3845 }
3846
3847 /**
3848 * Get a tree of parent categories
3849 *
3850 * @param array $children Array with the children in the keys, to check for circular refs
3851 * @return array Tree of parent categories
3852 */
3853 public function getParentCategoryTree( $children = [] ) {
3854 $stack = [];
3855 $parents = $this->getParentCategories();
3856
3857 if ( $parents ) {
3858 foreach ( $parents as $parent => $current ) {
3859 if ( array_key_exists( $parent, $children ) ) {
3860 # Circular reference
3861 $stack[$parent] = [];
3862 } else {
3863 $nt = Title::newFromText( $parent );
3864 if ( $nt ) {
3865 $stack[$parent] = $nt->getParentCategoryTree( $children + [ $parent => 1 ] );
3866 }
3867 }
3868 }
3869 }
3870
3871 return $stack;
3872 }
3873
3874 /**
3875 * Get an associative array for selecting this title from
3876 * the "page" table
3877 *
3878 * @return array Array suitable for the $where parameter of DB::select()
3879 */
3880 public function pageCond() {
3881 if ( $this->mArticleID > 0 ) {
3882 // PK avoids secondary lookups in InnoDB, shouldn't hurt other DBs
3883 return [ 'page_id' => $this->mArticleID ];
3884 } else {
3885 return [ 'page_namespace' => $this->mNamespace, 'page_title' => $this->mDbkeyform ];
3886 }
3887 }
3888
3889 /**
3890 * Get the revision ID of the previous revision
3891 *
3892 * @param int $revId Revision ID. Get the revision that was before this one.
3893 * @param int $flags Title::GAID_FOR_UPDATE
3894 * @return int|bool Old revision ID, or false if none exists
3895 */
3896 public function getPreviousRevisionID( $revId, $flags = 0 ) {
3897 $db = ( $flags & self::GAID_FOR_UPDATE ) ? wfGetDB( DB_MASTER ) : wfGetDB( DB_SLAVE );
3898 $revId = $db->selectField( 'revision', 'rev_id',
3899 [
3900 'rev_page' => $this->getArticleID( $flags ),
3901 'rev_id < ' . intval( $revId )
3902 ],
3903 __METHOD__,
3904 [ 'ORDER BY' => 'rev_id DESC' ]
3905 );
3906
3907 if ( $revId === false ) {
3908 return false;
3909 } else {
3910 return intval( $revId );
3911 }
3912 }
3913
3914 /**
3915 * Get the revision ID of the next revision
3916 *
3917 * @param int $revId Revision ID. Get the revision that was after this one.
3918 * @param int $flags Title::GAID_FOR_UPDATE
3919 * @return int|bool Next revision ID, or false if none exists
3920 */
3921 public function getNextRevisionID( $revId, $flags = 0 ) {
3922 $db = ( $flags & self::GAID_FOR_UPDATE ) ? wfGetDB( DB_MASTER ) : wfGetDB( DB_SLAVE );
3923 $revId = $db->selectField( 'revision', 'rev_id',
3924 [
3925 'rev_page' => $this->getArticleID( $flags ),
3926 'rev_id > ' . intval( $revId )
3927 ],
3928 __METHOD__,
3929 [ 'ORDER BY' => 'rev_id' ]
3930 );
3931
3932 if ( $revId === false ) {
3933 return false;
3934 } else {
3935 return intval( $revId );
3936 }
3937 }
3938
3939 /**
3940 * Get the first revision of the page
3941 *
3942 * @param int $flags Title::GAID_FOR_UPDATE
3943 * @return Revision|null If page doesn't exist
3944 */
3945 public function getFirstRevision( $flags = 0 ) {
3946 $pageId = $this->getArticleID( $flags );
3947 if ( $pageId ) {
3948 $db = ( $flags & self::GAID_FOR_UPDATE ) ? wfGetDB( DB_MASTER ) : wfGetDB( DB_SLAVE );
3949 $row = $db->selectRow( 'revision', Revision::selectFields(),
3950 [ 'rev_page' => $pageId ],
3951 __METHOD__,
3952 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 1 ]
3953 );
3954 if ( $row ) {
3955 return new Revision( $row );
3956 }
3957 }
3958 return null;
3959 }
3960
3961 /**
3962 * Get the oldest revision timestamp of this page
3963 *
3964 * @param int $flags Title::GAID_FOR_UPDATE
3965 * @return string MW timestamp
3966 */
3967 public function getEarliestRevTime( $flags = 0 ) {
3968 $rev = $this->getFirstRevision( $flags );
3969 return $rev ? $rev->getTimestamp() : null;
3970 }
3971
3972 /**
3973 * Check if this is a new page
3974 *
3975 * @return bool
3976 */
3977 public function isNewPage() {
3978 $dbr = wfGetDB( DB_SLAVE );
3979 return (bool)$dbr->selectField( 'page', 'page_is_new', $this->pageCond(), __METHOD__ );
3980 }
3981
3982 /**
3983 * Check whether the number of revisions of this page surpasses $wgDeleteRevisionsLimit
3984 *
3985 * @return bool
3986 */
3987 public function isBigDeletion() {
3988 global $wgDeleteRevisionsLimit;
3989
3990 if ( !$wgDeleteRevisionsLimit ) {
3991 return false;
3992 }
3993
3994 if ( $this->mIsBigDeletion === null ) {
3995 $dbr = wfGetDB( DB_SLAVE );
3996
3997 $revCount = $dbr->selectRowCount(
3998 'revision',
3999 '1',
4000 [ 'rev_page' => $this->getArticleID() ],
4001 __METHOD__,
4002 [ 'LIMIT' => $wgDeleteRevisionsLimit + 1 ]
4003 );
4004
4005 $this->mIsBigDeletion = $revCount > $wgDeleteRevisionsLimit;
4006 }
4007
4008 return $this->mIsBigDeletion;
4009 }
4010
4011 /**
4012 * Get the approximate revision count of this page.
4013 *
4014 * @return int
4015 */
4016 public function estimateRevisionCount() {
4017 if ( !$this->exists() ) {
4018 return 0;
4019 }
4020
4021 if ( $this->mEstimateRevisions === null ) {
4022 $dbr = wfGetDB( DB_SLAVE );
4023 $this->mEstimateRevisions = $dbr->estimateRowCount( 'revision', '*',
4024 [ 'rev_page' => $this->getArticleID() ], __METHOD__ );
4025 }
4026
4027 return $this->mEstimateRevisions;
4028 }
4029
4030 /**
4031 * Get the number of revisions between the given revision.
4032 * Used for diffs and other things that really need it.
4033 *
4034 * @param int|Revision $old Old revision or rev ID (first before range)
4035 * @param int|Revision $new New revision or rev ID (first after range)
4036 * @param int|null $max Limit of Revisions to count, will be incremented to detect truncations
4037 * @return int Number of revisions between these revisions.
4038 */
4039 public function countRevisionsBetween( $old, $new, $max = null ) {
4040 if ( !( $old instanceof Revision ) ) {
4041 $old = Revision::newFromTitle( $this, (int)$old );
4042 }
4043 if ( !( $new instanceof Revision ) ) {
4044 $new = Revision::newFromTitle( $this, (int)$new );
4045 }
4046 if ( !$old || !$new ) {
4047 return 0; // nothing to compare
4048 }
4049 $dbr = wfGetDB( DB_SLAVE );
4050 $conds = [
4051 'rev_page' => $this->getArticleID(),
4052 'rev_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( $old->getTimestamp() ) ),
4053 'rev_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( $new->getTimestamp() ) )
4054 ];
4055 if ( $max !== null ) {
4056 return $dbr->selectRowCount( 'revision', '1',
4057 $conds,
4058 __METHOD__,
4059 [ 'LIMIT' => $max + 1 ] // extra to detect truncation
4060 );
4061 } else {
4062 return (int)$dbr->selectField( 'revision', 'count(*)', $conds, __METHOD__ );
4063 }
4064 }
4065
4066 /**
4067 * Get the authors between the given revisions or revision IDs.
4068 * Used for diffs and other things that really need it.
4069 *
4070 * @since 1.23
4071 *
4072 * @param int|Revision $old Old revision or rev ID (first before range by default)
4073 * @param int|Revision $new New revision or rev ID (first after range by default)
4074 * @param int $limit Maximum number of authors
4075 * @param string|array $options (Optional): Single option, or an array of options:
4076 * 'include_old' Include $old in the range; $new is excluded.
4077 * 'include_new' Include $new in the range; $old is excluded.
4078 * 'include_both' Include both $old and $new in the range.
4079 * Unknown option values are ignored.
4080 * @return array|null Names of revision authors in the range; null if not both revisions exist
4081 */
4082 public function getAuthorsBetween( $old, $new, $limit, $options = [] ) {
4083 if ( !( $old instanceof Revision ) ) {
4084 $old = Revision::newFromTitle( $this, (int)$old );
4085 }
4086 if ( !( $new instanceof Revision ) ) {
4087 $new = Revision::newFromTitle( $this, (int)$new );
4088 }
4089 // XXX: what if Revision objects are passed in, but they don't refer to this title?
4090 // Add $old->getPage() != $new->getPage() || $old->getPage() != $this->getArticleID()
4091 // in the sanity check below?
4092 if ( !$old || !$new ) {
4093 return null; // nothing to compare
4094 }
4095 $authors = [];
4096 $old_cmp = '>';
4097 $new_cmp = '<';
4098 $options = (array)$options;
4099 if ( in_array( 'include_old', $options ) ) {
4100 $old_cmp = '>=';
4101 }
4102 if ( in_array( 'include_new', $options ) ) {
4103 $new_cmp = '<=';
4104 }
4105 if ( in_array( 'include_both', $options ) ) {
4106 $old_cmp = '>=';
4107 $new_cmp = '<=';
4108 }
4109 // No DB query needed if $old and $new are the same or successive revisions:
4110 if ( $old->getId() === $new->getId() ) {
4111 return ( $old_cmp === '>' && $new_cmp === '<' ) ?
4112 [] :
4113 [ $old->getUserText( Revision::RAW ) ];
4114 } elseif ( $old->getId() === $new->getParentId() ) {
4115 if ( $old_cmp === '>=' && $new_cmp === '<=' ) {
4116 $authors[] = $old->getUserText( Revision::RAW );
4117 if ( $old->getUserText( Revision::RAW ) != $new->getUserText( Revision::RAW ) ) {
4118 $authors[] = $new->getUserText( Revision::RAW );
4119 }
4120 } elseif ( $old_cmp === '>=' ) {
4121 $authors[] = $old->getUserText( Revision::RAW );
4122 } elseif ( $new_cmp === '<=' ) {
4123 $authors[] = $new->getUserText( Revision::RAW );
4124 }
4125 return $authors;
4126 }
4127 $dbr = wfGetDB( DB_SLAVE );
4128 $res = $dbr->select( 'revision', 'DISTINCT rev_user_text',
4129 [
4130 'rev_page' => $this->getArticleID(),
4131 "rev_timestamp $old_cmp " . $dbr->addQuotes( $dbr->timestamp( $old->getTimestamp() ) ),
4132 "rev_timestamp $new_cmp " . $dbr->addQuotes( $dbr->timestamp( $new->getTimestamp() ) )
4133 ], __METHOD__,
4134 [ 'LIMIT' => $limit + 1 ] // add one so caller knows it was truncated
4135 );
4136 foreach ( $res as $row ) {
4137 $authors[] = $row->rev_user_text;
4138 }
4139 return $authors;
4140 }
4141
4142 /**
4143 * Get the number of authors between the given revisions or revision IDs.
4144 * Used for diffs and other things that really need it.
4145 *
4146 * @param int|Revision $old Old revision or rev ID (first before range by default)
4147 * @param int|Revision $new New revision or rev ID (first after range by default)
4148 * @param int $limit Maximum number of authors
4149 * @param string|array $options (Optional): Single option, or an array of options:
4150 * 'include_old' Include $old in the range; $new is excluded.
4151 * 'include_new' Include $new in the range; $old is excluded.
4152 * 'include_both' Include both $old and $new in the range.
4153 * Unknown option values are ignored.
4154 * @return int Number of revision authors in the range; zero if not both revisions exist
4155 */
4156 public function countAuthorsBetween( $old, $new, $limit, $options = [] ) {
4157 $authors = $this->getAuthorsBetween( $old, $new, $limit, $options );
4158 return $authors ? count( $authors ) : 0;
4159 }
4160
4161 /**
4162 * Compare with another title.
4163 *
4164 * @param Title $title
4165 * @return bool
4166 */
4167 public function equals( Title $title ) {
4168 // Note: === is necessary for proper matching of number-like titles.
4169 return $this->getInterwiki() === $title->getInterwiki()
4170 && $this->getNamespace() == $title->getNamespace()
4171 && $this->getDBkey() === $title->getDBkey();
4172 }
4173
4174 /**
4175 * Check if this title is a subpage of another title
4176 *
4177 * @param Title $title
4178 * @return bool
4179 */
4180 public function isSubpageOf( Title $title ) {
4181 return $this->getInterwiki() === $title->getInterwiki()
4182 && $this->getNamespace() == $title->getNamespace()
4183 && strpos( $this->getDBkey(), $title->getDBkey() . '/' ) === 0;
4184 }
4185
4186 /**
4187 * Check if page exists. For historical reasons, this function simply
4188 * checks for the existence of the title in the page table, and will
4189 * thus return false for interwiki links, special pages and the like.
4190 * If you want to know if a title can be meaningfully viewed, you should
4191 * probably call the isKnown() method instead.
4192 *
4193 * @param int $flags An optional bit field; may be Title::GAID_FOR_UPDATE to check
4194 * from master/for update
4195 * @return bool
4196 */
4197 public function exists( $flags = 0 ) {
4198 $exists = $this->getArticleID( $flags ) != 0;
4199 Hooks::run( 'TitleExists', [ $this, &$exists ] );
4200 return $exists;
4201 }
4202
4203 /**
4204 * Should links to this title be shown as potentially viewable (i.e. as
4205 * "bluelinks"), even if there's no record by this title in the page
4206 * table?
4207 *
4208 * This function is semi-deprecated for public use, as well as somewhat
4209 * misleadingly named. You probably just want to call isKnown(), which
4210 * calls this function internally.
4211 *
4212 * (ISSUE: Most of these checks are cheap, but the file existence check
4213 * can potentially be quite expensive. Including it here fixes a lot of
4214 * existing code, but we might want to add an optional parameter to skip
4215 * it and any other expensive checks.)
4216 *
4217 * @return bool
4218 */
4219 public function isAlwaysKnown() {
4220 $isKnown = null;
4221
4222 /**
4223 * Allows overriding default behavior for determining if a page exists.
4224 * If $isKnown is kept as null, regular checks happen. If it's
4225 * a boolean, this value is returned by the isKnown method.
4226 *
4227 * @since 1.20
4228 *
4229 * @param Title $title
4230 * @param bool|null $isKnown
4231 */
4232 Hooks::run( 'TitleIsAlwaysKnown', [ $this, &$isKnown ] );
4233
4234 if ( !is_null( $isKnown ) ) {
4235 return $isKnown;
4236 }
4237
4238 if ( $this->isExternal() ) {
4239 return true; // any interwiki link might be viewable, for all we know
4240 }
4241
4242 switch ( $this->mNamespace ) {
4243 case NS_MEDIA:
4244 case NS_FILE:
4245 // file exists, possibly in a foreign repo
4246 return (bool)wfFindFile( $this );
4247 case NS_SPECIAL:
4248 // valid special page
4249 return SpecialPageFactory::exists( $this->getDBkey() );
4250 case NS_MAIN:
4251 // selflink, possibly with fragment
4252 return $this->mDbkeyform == '';
4253 case NS_MEDIAWIKI:
4254 // known system message
4255 return $this->hasSourceText() !== false;
4256 default:
4257 return false;
4258 }
4259 }
4260
4261 /**
4262 * Does this title refer to a page that can (or might) be meaningfully
4263 * viewed? In particular, this function may be used to determine if
4264 * links to the title should be rendered as "bluelinks" (as opposed to
4265 * "redlinks" to non-existent pages).
4266 * Adding something else to this function will cause inconsistency
4267 * since LinkHolderArray calls isAlwaysKnown() and does its own
4268 * page existence check.
4269 *
4270 * @return bool
4271 */
4272 public function isKnown() {
4273 return $this->isAlwaysKnown() || $this->exists();
4274 }
4275
4276 /**
4277 * Does this page have source text?
4278 *
4279 * @return bool
4280 */
4281 public function hasSourceText() {
4282 if ( $this->exists() ) {
4283 return true;
4284 }
4285
4286 if ( $this->mNamespace == NS_MEDIAWIKI ) {
4287 // If the page doesn't exist but is a known system message, default
4288 // message content will be displayed, same for language subpages-
4289 // Use always content language to avoid loading hundreds of languages
4290 // to get the link color.
4291 global $wgContLang;
4292 list( $name, ) = MessageCache::singleton()->figureMessage(
4293 $wgContLang->lcfirst( $this->getText() )
4294 );
4295 $message = wfMessage( $name )->inLanguage( $wgContLang )->useDatabase( false );
4296 return $message->exists();
4297 }
4298
4299 return false;
4300 }
4301
4302 /**
4303 * Get the default message text or false if the message doesn't exist
4304 *
4305 * @return string|bool
4306 */
4307 public function getDefaultMessageText() {
4308 global $wgContLang;
4309
4310 if ( $this->getNamespace() != NS_MEDIAWIKI ) { // Just in case
4311 return false;
4312 }
4313
4314 list( $name, $lang ) = MessageCache::singleton()->figureMessage(
4315 $wgContLang->lcfirst( $this->getText() )
4316 );
4317 $message = wfMessage( $name )->inLanguage( $lang )->useDatabase( false );
4318
4319 if ( $message->exists() ) {
4320 return $message->plain();
4321 } else {
4322 return false;
4323 }
4324 }
4325
4326 /**
4327 * Updates page_touched for this page; called from LinksUpdate.php
4328 *
4329 * @param string $purgeTime [optional] TS_MW timestamp
4330 * @return bool True if the update succeeded
4331 */
4332 public function invalidateCache( $purgeTime = null ) {
4333 if ( wfReadOnly() ) {
4334 return false;
4335 }
4336
4337 if ( $this->mArticleID === 0 ) {
4338 return true; // avoid gap locking if we know it's not there
4339 }
4340
4341 $method = __METHOD__;
4342 $dbw = wfGetDB( DB_MASTER );
4343 $conds = $this->pageCond();
4344 $dbw->onTransactionIdle( function () use ( $dbw, $conds, $method, $purgeTime ) {
4345 $dbTimestamp = $dbw->timestamp( $purgeTime ?: time() );
4346
4347 $dbw->update(
4348 'page',
4349 [ 'page_touched' => $dbTimestamp ],
4350 $conds + [ 'page_touched < ' . $dbw->addQuotes( $dbTimestamp ) ],
4351 $method
4352 );
4353 } );
4354
4355 return true;
4356 }
4357
4358 /**
4359 * Update page_touched timestamps and send CDN purge messages for
4360 * pages linking to this title. May be sent to the job queue depending
4361 * on the number of links. Typically called on create and delete.
4362 */
4363 public function touchLinks() {
4364 DeferredUpdates::addUpdate( new HTMLCacheUpdate( $this, 'pagelinks' ) );
4365 if ( $this->getNamespace() == NS_CATEGORY ) {
4366 DeferredUpdates::addUpdate( new HTMLCacheUpdate( $this, 'categorylinks' ) );
4367 }
4368 }
4369
4370 /**
4371 * Get the last touched timestamp
4372 *
4373 * @param IDatabase $db Optional db
4374 * @return string Last-touched timestamp
4375 */
4376 public function getTouched( $db = null ) {
4377 if ( $db === null ) {
4378 $db = wfGetDB( DB_SLAVE );
4379 }
4380 $touched = $db->selectField( 'page', 'page_touched', $this->pageCond(), __METHOD__ );
4381 return $touched;
4382 }
4383
4384 /**
4385 * Get the timestamp when this page was updated since the user last saw it.
4386 *
4387 * @param User $user
4388 * @return string|null
4389 */
4390 public function getNotificationTimestamp( $user = null ) {
4391 global $wgUser;
4392
4393 // Assume current user if none given
4394 if ( !$user ) {
4395 $user = $wgUser;
4396 }
4397 // Check cache first
4398 $uid = $user->getId();
4399 if ( !$uid ) {
4400 return false;
4401 }
4402 // avoid isset here, as it'll return false for null entries
4403 if ( array_key_exists( $uid, $this->mNotificationTimestamp ) ) {
4404 return $this->mNotificationTimestamp[$uid];
4405 }
4406 // Don't cache too much!
4407 if ( count( $this->mNotificationTimestamp ) >= self::CACHE_MAX ) {
4408 $this->mNotificationTimestamp = [];
4409 }
4410
4411 $store = MediaWikiServices::getInstance()->getWatchedItemStore();
4412 $watchedItem = $store->getWatchedItem( $user, $this );
4413 if ( $watchedItem ) {
4414 $this->mNotificationTimestamp[$uid] = $watchedItem->getNotificationTimestamp();
4415 } else {
4416 $this->mNotificationTimestamp[$uid] = false;
4417 }
4418
4419 return $this->mNotificationTimestamp[$uid];
4420 }
4421
4422 /**
4423 * Generate strings used for xml 'id' names in monobook tabs
4424 *
4425 * @param string $prepend Defaults to 'nstab-'
4426 * @return string XML 'id' name
4427 */
4428 public function getNamespaceKey( $prepend = 'nstab-' ) {
4429 global $wgContLang;
4430 // Gets the subject namespace if this title
4431 $namespace = MWNamespace::getSubject( $this->getNamespace() );
4432 // Checks if canonical namespace name exists for namespace
4433 if ( MWNamespace::exists( $this->getNamespace() ) ) {
4434 // Uses canonical namespace name
4435 $namespaceKey = MWNamespace::getCanonicalName( $namespace );
4436 } else {
4437 // Uses text of namespace
4438 $namespaceKey = $this->getSubjectNsText();
4439 }
4440 // Makes namespace key lowercase
4441 $namespaceKey = $wgContLang->lc( $namespaceKey );
4442 // Uses main
4443 if ( $namespaceKey == '' ) {
4444 $namespaceKey = 'main';
4445 }
4446 // Changes file to image for backwards compatibility
4447 if ( $namespaceKey == 'file' ) {
4448 $namespaceKey = 'image';
4449 }
4450 return $prepend . $namespaceKey;
4451 }
4452
4453 /**
4454 * Get all extant redirects to this Title
4455 *
4456 * @param int|null $ns Single namespace to consider; null to consider all namespaces
4457 * @return Title[] Array of Title redirects to this title
4458 */
4459 public function getRedirectsHere( $ns = null ) {
4460 $redirs = [];
4461
4462 $dbr = wfGetDB( DB_SLAVE );
4463 $where = [
4464 'rd_namespace' => $this->getNamespace(),
4465 'rd_title' => $this->getDBkey(),
4466 'rd_from = page_id'
4467 ];
4468 if ( $this->isExternal() ) {
4469 $where['rd_interwiki'] = $this->getInterwiki();
4470 } else {
4471 $where[] = 'rd_interwiki = ' . $dbr->addQuotes( '' ) . ' OR rd_interwiki IS NULL';
4472 }
4473 if ( !is_null( $ns ) ) {
4474 $where['page_namespace'] = $ns;
4475 }
4476
4477 $res = $dbr->select(
4478 [ 'redirect', 'page' ],
4479 [ 'page_namespace', 'page_title' ],
4480 $where,
4481 __METHOD__
4482 );
4483
4484 foreach ( $res as $row ) {
4485 $redirs[] = self::newFromRow( $row );
4486 }
4487 return $redirs;
4488 }
4489
4490 /**
4491 * Check if this Title is a valid redirect target
4492 *
4493 * @return bool
4494 */
4495 public function isValidRedirectTarget() {
4496 global $wgInvalidRedirectTargets;
4497
4498 if ( $this->isSpecialPage() ) {
4499 // invalid redirect targets are stored in a global array, but explicitly disallow Userlogout here
4500 if ( $this->isSpecial( 'Userlogout' ) ) {
4501 return false;
4502 }
4503
4504 foreach ( $wgInvalidRedirectTargets as $target ) {
4505 if ( $this->isSpecial( $target ) ) {
4506 return false;
4507 }
4508 }
4509 }
4510
4511 return true;
4512 }
4513
4514 /**
4515 * Get a backlink cache object
4516 *
4517 * @return BacklinkCache
4518 */
4519 public function getBacklinkCache() {
4520 return BacklinkCache::get( $this );
4521 }
4522
4523 /**
4524 * Whether the magic words __INDEX__ and __NOINDEX__ function for this page.
4525 *
4526 * @return bool
4527 */
4528 public function canUseNoindex() {
4529 global $wgContentNamespaces, $wgExemptFromUserRobotsControl;
4530
4531 $bannedNamespaces = is_null( $wgExemptFromUserRobotsControl )
4532 ? $wgContentNamespaces
4533 : $wgExemptFromUserRobotsControl;
4534
4535 return !in_array( $this->mNamespace, $bannedNamespaces );
4536
4537 }
4538
4539 /**
4540 * Returns the raw sort key to be used for categories, with the specified
4541 * prefix. This will be fed to Collation::getSortKey() to get a
4542 * binary sortkey that can be used for actual sorting.
4543 *
4544 * @param string $prefix The prefix to be used, specified using
4545 * {{defaultsort:}} or like [[Category:Foo|prefix]]. Empty for no
4546 * prefix.
4547 * @return string
4548 */
4549 public function getCategorySortkey( $prefix = '' ) {
4550 $unprefixed = $this->getText();
4551
4552 // Anything that uses this hook should only depend
4553 // on the Title object passed in, and should probably
4554 // tell the users to run updateCollations.php --force
4555 // in order to re-sort existing category relations.
4556 Hooks::run( 'GetDefaultSortkey', [ $this, &$unprefixed ] );
4557 if ( $prefix !== '' ) {
4558 # Separate with a line feed, so the unprefixed part is only used as
4559 # a tiebreaker when two pages have the exact same prefix.
4560 # In UCA, tab is the only character that can sort above LF
4561 # so we strip both of them from the original prefix.
4562 $prefix = strtr( $prefix, "\n\t", ' ' );
4563 return "$prefix\n$unprefixed";
4564 }
4565 return $unprefixed;
4566 }
4567
4568 /**
4569 * Returns the page language code saved in the database, if $wgPageLanguageUseDB is set
4570 * to true in LocalSettings.php, otherwise returns false. If there is no language saved in
4571 * the db, it will return NULL.
4572 *
4573 * @return string|null|bool
4574 */
4575 private function getDbPageLanguageCode() {
4576 global $wgPageLanguageUseDB;
4577
4578 // check, if the page language could be saved in the database, and if so and
4579 // the value is not requested already, lookup the page language using LinkCache
4580 if ( $wgPageLanguageUseDB && $this->mDbPageLanguage === false ) {
4581 $linkCache = LinkCache::singleton();
4582 $linkCache->addLinkObj( $this );
4583 $this->mDbPageLanguage = $linkCache->getGoodLinkFieldObj( $this, 'lang' );
4584 }
4585
4586 return $this->mDbPageLanguage;
4587 }
4588
4589 /**
4590 * Get the language in which the content of this page is written in
4591 * wikitext. Defaults to $wgContLang, but in certain cases it can be
4592 * e.g. $wgLang (such as special pages, which are in the user language).
4593 *
4594 * @since 1.18
4595 * @return Language
4596 */
4597 public function getPageLanguage() {
4598 global $wgLang, $wgLanguageCode;
4599 if ( $this->isSpecialPage() ) {
4600 // special pages are in the user language
4601 return $wgLang;
4602 }
4603
4604 // Checking if DB language is set
4605 $dbPageLanguage = $this->getDbPageLanguageCode();
4606 if ( $dbPageLanguage ) {
4607 return wfGetLangObj( $dbPageLanguage );
4608 }
4609
4610 if ( !$this->mPageLanguage || $this->mPageLanguage[1] !== $wgLanguageCode ) {
4611 // Note that this may depend on user settings, so the cache should
4612 // be only per-request.
4613 // NOTE: ContentHandler::getPageLanguage() may need to load the
4614 // content to determine the page language!
4615 // Checking $wgLanguageCode hasn't changed for the benefit of unit
4616 // tests.
4617 $contentHandler = ContentHandler::getForTitle( $this );
4618 $langObj = $contentHandler->getPageLanguage( $this );
4619 $this->mPageLanguage = [ $langObj->getCode(), $wgLanguageCode ];
4620 } else {
4621 $langObj = wfGetLangObj( $this->mPageLanguage[0] );
4622 }
4623
4624 return $langObj;
4625 }
4626
4627 /**
4628 * Get the language in which the content of this page is written when
4629 * viewed by user. Defaults to $wgContLang, but in certain cases it can be
4630 * e.g. $wgLang (such as special pages, which are in the user language).
4631 *
4632 * @since 1.20
4633 * @return Language
4634 */
4635 public function getPageViewLanguage() {
4636 global $wgLang;
4637
4638 if ( $this->isSpecialPage() ) {
4639 // If the user chooses a variant, the content is actually
4640 // in a language whose code is the variant code.
4641 $variant = $wgLang->getPreferredVariant();
4642 if ( $wgLang->getCode() !== $variant ) {
4643 return Language::factory( $variant );
4644 }
4645
4646 return $wgLang;
4647 }
4648
4649 // Checking if DB language is set
4650 $dbPageLanguage = $this->getDbPageLanguageCode();
4651 if ( $dbPageLanguage ) {
4652 $pageLang = wfGetLangObj( $dbPageLanguage );
4653 $variant = $pageLang->getPreferredVariant();
4654 if ( $pageLang->getCode() !== $variant ) {
4655 $pageLang = Language::factory( $variant );
4656 }
4657
4658 return $pageLang;
4659 }
4660
4661 // @note Can't be cached persistently, depends on user settings.
4662 // @note ContentHandler::getPageViewLanguage() may need to load the
4663 // content to determine the page language!
4664 $contentHandler = ContentHandler::getForTitle( $this );
4665 $pageLang = $contentHandler->getPageViewLanguage( $this );
4666 return $pageLang;
4667 }
4668
4669 /**
4670 * Get a list of rendered edit notices for this page.
4671 *
4672 * Array is keyed by the original message key, and values are rendered using parseAsBlock, so
4673 * they will already be wrapped in paragraphs.
4674 *
4675 * @since 1.21
4676 * @param int $oldid Revision ID that's being edited
4677 * @return array
4678 */
4679 public function getEditNotices( $oldid = 0 ) {
4680 $notices = [];
4681
4682 // Optional notice for the entire namespace
4683 $editnotice_ns = 'editnotice-' . $this->getNamespace();
4684 $msg = wfMessage( $editnotice_ns );
4685 if ( $msg->exists() ) {
4686 $html = $msg->parseAsBlock();
4687 // Edit notices may have complex logic, but output nothing (T91715)
4688 if ( trim( $html ) !== '' ) {
4689 $notices[$editnotice_ns] = Html::rawElement(
4690 'div',
4691 [ 'class' => [
4692 'mw-editnotice',
4693 'mw-editnotice-namespace',
4694 Sanitizer::escapeClass( "mw-$editnotice_ns" )
4695 ] ],
4696 $html
4697 );
4698 }
4699 }
4700
4701 if ( MWNamespace::hasSubpages( $this->getNamespace() ) ) {
4702 // Optional notice for page itself and any parent page
4703 $parts = explode( '/', $this->getDBkey() );
4704 $editnotice_base = $editnotice_ns;
4705 while ( count( $parts ) > 0 ) {
4706 $editnotice_base .= '-' . array_shift( $parts );
4707 $msg = wfMessage( $editnotice_base );
4708 if ( $msg->exists() ) {
4709 $html = $msg->parseAsBlock();
4710 if ( trim( $html ) !== '' ) {
4711 $notices[$editnotice_base] = Html::rawElement(
4712 'div',
4713 [ 'class' => [
4714 'mw-editnotice',
4715 'mw-editnotice-base',
4716 Sanitizer::escapeClass( "mw-$editnotice_base" )
4717 ] ],
4718 $html
4719 );
4720 }
4721 }
4722 }
4723 } else {
4724 // Even if there are no subpages in namespace, we still don't want "/" in MediaWiki message keys
4725 $editnoticeText = $editnotice_ns . '-' . strtr( $this->getDBkey(), '/', '-' );
4726 $msg = wfMessage( $editnoticeText );
4727 if ( $msg->exists() ) {
4728 $html = $msg->parseAsBlock();
4729 if ( trim( $html ) !== '' ) {
4730 $notices[$editnoticeText] = Html::rawElement(
4731 'div',
4732 [ 'class' => [
4733 'mw-editnotice',
4734 'mw-editnotice-page',
4735 Sanitizer::escapeClass( "mw-$editnoticeText" )
4736 ] ],
4737 $html
4738 );
4739 }
4740 }
4741 }
4742
4743 Hooks::run( 'TitleGetEditNotices', [ $this, $oldid, &$notices ] );
4744 return $notices;
4745 }
4746
4747 /**
4748 * @return array
4749 */
4750 public function __sleep() {
4751 return [
4752 'mNamespace',
4753 'mDbkeyform',
4754 'mFragment',
4755 'mInterwiki',
4756 'mLocalInterwiki',
4757 'mUserCaseDBKey',
4758 'mDefaultNamespace',
4759 ];
4760 }
4761
4762 public function __wakeup() {
4763 $this->mArticleID = ( $this->mNamespace >= 0 ) ? -1 : 0;
4764 $this->mUrlform = wfUrlencode( $this->mDbkeyform );
4765 $this->mTextform = strtr( $this->mDbkeyform, '_', ' ' );
4766 }
4767
4768 }