*
* @file
*/
+use MediaWiki\Linker\LinkTarget;
+use MediaWiki\Interwiki\InterwikiLookup;
+use MediaWiki\MediaWikiServices;
/**
* Represents a title within MediaWiki.
* Avoid usage of this singleton by using TitleValue
* and the associated services when possible.
*
- * @return MediaWikiTitleCodec
+ * @return TitleFormatter
*/
- private static function getMediaWikiTitleCodec() {
- global $wgContLang, $wgLocalInterwikis;
-
- static $titleCodec = null;
- static $titleCodecFingerprint = null;
-
- // $wgContLang and $wgLocalInterwikis may change (especially while testing),
- // make sure we are using the right one. To detect changes over the course
- // of a request, we remember a fingerprint of the config used to create the
- // codec singleton, and re-create it if the fingerprint doesn't match.
- $fingerprint = spl_object_hash( $wgContLang ) . '|' . implode( '+', $wgLocalInterwikis );
-
- if ( $fingerprint !== $titleCodecFingerprint ) {
- $titleCodec = null;
- }
-
- if ( !$titleCodec ) {
- $titleCodec = new MediaWikiTitleCodec(
- $wgContLang,
- GenderCache::singleton(),
- $wgLocalInterwikis
- );
- $titleCodecFingerprint = $fingerprint;
- }
-
- return $titleCodec;
+ private static function getTitleFormatter() {
+ return MediaWikiServices::getInstance()->getTitleFormatter();
}
/**
- * B/C kludge: provide a TitleParser for use by Title.
+ * B/C kludge: provide an InterwikiLookup for use by Title.
* Ideally, Title would have no methods that need this.
* Avoid usage of this singleton by using TitleValue
* and the associated services when possible.
*
- * @return TitleFormatter
+ * @return InterwikiLookup
*/
- private static function getTitleFormatter() {
- // NOTE: we know that getMediaWikiTitleCodec() returns a MediaWikiTitleCodec,
- // which implements TitleFormatter.
- return self::getMediaWikiTitleCodec();
+ private static function getInterwikiLookup() {
+ return MediaWikiServices::getInstance()->getInterwikiLookup();
}
+ /**
+ * @access protected
+ */
function __construct() {
}
* @return Title
*/
public static function newFromLinkTarget( LinkTarget $linkTarget ) {
+ if ( $linkTarget instanceof Title ) {
+ // Special case if it's already a Title object
+ return $linkTarget;
+ }
return self::makeTitle(
$linkTarget->getNamespace(),
$linkTarget->getText(),
- $linkTarget->getFragment() );
+ $linkTarget->getFragment(),
+ $linkTarget->getInterwiki()
+ );
}
/**
* @return Title|null Title or null on an error.
*/
public static function newFromText( $text, $defaultNamespace = NS_MAIN ) {
- if ( is_object( $text ) ) {
- throw new InvalidArgumentException( '$text must be a string.' );
- }
// DWIM: Integers can be passed in here when page titles are used as array keys.
if ( $text !== null && !is_string( $text ) && !is_int( $text ) ) {
- wfDebugLog( 'T76305', wfGetAllCallers( 5 ) );
- return null;
+ throw new InvalidArgumentException( '$text must be a string.' );
}
if ( $text === null ) {
return null;
*/
public function isLocal() {
if ( $this->isExternal() ) {
- $iw = Interwiki::fetch( $this->mInterwiki );
+ $iw = self::getInterwikiLookup()->fetch( $this->mInterwiki );
if ( $iw ) {
return $iw->isLocal();
}
return false;
}
- return Interwiki::fetch( $this->mInterwiki )->isTranscludable();
+ return self::getInterwikiLookup()->fetch( $this->mInterwiki )->isTranscludable();
}
/**
return false;
}
- return Interwiki::fetch( $this->mInterwiki )->getWikiID();
+ return self::getInterwikiLookup()->fetch( $this->mInterwiki )->getWikiID();
}
/**
$this->mTitleValue = new TitleValue(
$this->getNamespace(),
$this->getDBkey(),
- $this->getFragment() );
+ $this->getFragment(),
+ $this->getInterwiki()
+ );
} catch ( InvalidArgumentException $ex ) {
wfDebug( __METHOD__ . ': Can\'t create a TitleValue for [[' .
$this->getPrefixedText() . ']]: ' . $ex->getMessage() . "\n" );
* @return string Content model id
*/
public function getContentModel( $flags = 0 ) {
- if ( !$this->mContentModel && $this->getArticleID( $flags ) ) {
+ if ( ( !$this->mContentModel || $flags === Title::GAID_FOR_UPDATE ) &&
+ $this->getArticleID( $flags )
+ ) {
$linkCache = LinkCache::singleton();
$linkCache->addLinkObj( $this ); # in case we already had an article ID
$this->mContentModel = $linkCache->getGoodLinkFieldObj( $this, 'model' );
* specified fragment before setting, so it assumes you're passing it with
* an initial "#".
*
- * Deprecated for public use, use Title::makeTitle() with fragment parameter.
+ * Deprecated for public use, use Title::makeTitle() with fragment parameter,
+ * or Title::createFragmentTarget().
* Still in active use privately.
*
* @private
$this->mFragment = strtr( substr( $fragment, 1 ), '_', ' ' );
}
+ /**
+ * Creates a new Title for a different fragment of the same page.
+ *
+ * @since 1.27
+ * @param string $fragment
+ * @return Title
+ */
+ public function createFragmentTarget( $fragment ) {
+ return self::makeTitle(
+ $this->getNamespace(),
+ $this->getText(),
+ $fragment,
+ $this->getInterwiki()
+ );
+
+ }
+
/**
* Prefix some arbitrary text with the namespace or interwiki prefix
* of this object
$query = self::fixUrlQueryArgs( $query, $query2 );
- $interwiki = Interwiki::fetch( $this->mInterwiki );
+ $interwiki = self::getInterwikiLookup()->fetch( $this->mInterwiki );
if ( $interwiki ) {
$namespace = $this->getNsText();
if ( $namespace != '' ) {
if ( $url === false
&& $wgVariantArticlePath
- && $wgContLang->getCode() === $this->getPageLanguage()->getCode()
- && $this->getPageLanguage()->hasVariants()
&& preg_match( '/^variant=([^&]*)$/', $query, $matches )
+ && $this->getPageLanguage()->equals( $wgContLang )
+ && $this->getPageLanguage()->hasVariants()
) {
$variant = urldecode( $matches[1] );
if ( $this->getPageLanguage()->hasVariant( $variant ) ) {
/**
* Purge expired restrictions from the page_restrictions table
+ *
+ * This will purge no more than $wgUpdateRowsPerQuery page_restrictions rows
*/
static function purgeExpiredRestrictions() {
if ( wfReadOnly() ) {
wfGetDB( DB_MASTER ),
__METHOD__,
function ( IDatabase $dbw, $fname ) {
- $dbw->delete(
+ $config = MediaWikiServices::getInstance()->getMainConfig();
+ $ids = $dbw->selectFieldValues(
'page_restrictions',
+ 'pr_id',
[ 'pr_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ],
- $fname
+ $fname,
+ [ 'LIMIT' => $config->get( 'UpdateRowsPerQuery' ) ] // T135470
);
+ if ( $ids ) {
+ $dbw->delete( 'page_restrictions', [ 'pr_id' => $ids ], $fname );
+ }
+ }
+ ) );
+
+ DeferredUpdates::addUpdate( new AtomicSectionUpdate(
+ wfGetDB( DB_MASTER ),
+ __METHOD__,
+ function ( IDatabase $dbw, $fname ) {
$dbw->delete(
'protected_titles',
[ 'pt_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ],
// @note: splitTitleString() is a temporary hack to allow MediaWikiTitleCodec to share
// the parsing code with Title, while avoiding massive refactoring.
// @todo: get rid of secureAndSplit, refactor parsing code.
- $titleParser = self::getMediaWikiTitleCodec();
+ // @note: getTitleParser() returns a TitleParser implementation which does not have a
+ // splitTitleString method, but the only implementation (MediaWikiTitleCodec) does
+ $titleCodec = MediaWikiServices::getInstance()->getTitleParser();
// MalformedTitleException can be thrown here
- $parts = $titleParser->splitTitleString( $dbkey, $this->getDefaultNamespace() );
+ $parts = $titleCodec->splitTitleString( $dbkey, $this->getDefaultNamespace() );
# Fill fields
$this->setFragment( '#' . $parts['fragment'] );
$this->mNotificationTimestamp = [];
}
- $watchedItem = WatchedItemStore::getDefaultInstance()->getWatchedItem( $user, $this );
+ $store = MediaWikiServices::getInstance()->getWatchedItemStore();
+ $watchedItem = $store->getWatchedItem( $user, $this );
if ( $watchedItem ) {
$this->mNotificationTimestamp[$uid] = $watchedItem->getNotificationTimestamp();
} else {