From 730297567e91a4d97100f842aad2d18b8fb325fe Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Tue, 14 Aug 2018 22:33:56 -0700 Subject: [PATCH] Title: Access properties directly instead of wrapper functions Since this is pretty hot code, avoid the performance hit of a function call when we can directly access the property. Change-Id: I287de5eb003582b5704e9d93e2fc99d16c0ffb2a --- includes/Title.php | 158 ++++++++++++++++++++++----------------------- 1 file changed, 78 insertions(+), 80 deletions(-) diff --git a/includes/Title.php b/includes/Title.php index 01a28f5f7d..12186394c5 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -805,15 +805,13 @@ class Title implements LinkTarget { * @return bool */ public function isValid() { - $ns = $this->getNamespace(); - - if ( !MWNamespace::exists( $ns ) ) { + if ( !MWNamespace::exists( $this->mNamespace ) ) { return false; } try { $parser = MediaWikiServices::getInstance()->getTitleParser(); - $parser->parseTitle( $this->getDBkey(), $ns ); + $parser->parseTitle( $this->mDbkeyform, $this->mNamespace ); return true; } catch ( MalformedTitleException $ex ) { return false; @@ -906,10 +904,10 @@ class Title implements LinkTarget { if ( $this->mTitleValue === null ) { try { $this->mTitleValue = new TitleValue( - $this->getNamespace(), - $this->getDBkey(), - $this->getFragment(), - $this->getInterwiki() + $this->mNamespace, + $this->mDbkeyform, + $this->mFragment, + $this->mInterwiki ); } catch ( InvalidArgumentException $ex ) { wfDebug( __METHOD__ . ': Can\'t create a TitleValue for [[' . @@ -1101,7 +1099,7 @@ class Title implements LinkTarget { * @return bool */ public function isWatchable() { - return !$this->isExternal() && MWNamespace::isWatchable( $this->getNamespace() ); + return !$this->isExternal() && MWNamespace::isWatchable( $this->mNamespace ); } /** @@ -1110,7 +1108,7 @@ class Title implements LinkTarget { * @return bool */ public function isSpecialPage() { - return $this->getNamespace() == NS_SPECIAL; + return $this->mNamespace == NS_SPECIAL; } /** @@ -1121,7 +1119,7 @@ class Title implements LinkTarget { */ public function isSpecial( $name ) { if ( $this->isSpecialPage() ) { - list( $thisName, /* $subpage */ ) = SpecialPageFactory::resolveAlias( $this->getDBkey() ); + list( $thisName, /* $subpage */ ) = SpecialPageFactory::resolveAlias( $this->mDbkeyform ); if ( $name == $thisName ) { return true; } @@ -1159,7 +1157,7 @@ class Title implements LinkTarget { * @since 1.19 */ public function inNamespace( $ns ) { - return MWNamespace::equals( $this->getNamespace(), $ns ); + return MWNamespace::equals( $this->mNamespace, $ns ); } /** @@ -1198,7 +1196,7 @@ class Title implements LinkTarget { * @return bool */ public function hasSubjectNamespace( $ns ) { - return MWNamespace::subjectEquals( $this->getNamespace(), $ns ); + return MWNamespace::subjectEquals( $this->mNamespace, $ns ); } /** @@ -1209,7 +1207,7 @@ class Title implements LinkTarget { * @return bool */ public function isContentPage() { - return MWNamespace::isContent( $this->getNamespace() ); + return MWNamespace::isContent( $this->mNamespace ); } /** @@ -1219,7 +1217,7 @@ class Title implements LinkTarget { * @return bool */ public function isMovable() { - if ( !MWNamespace::isMovable( $this->getNamespace() ) || $this->isExternal() ) { + if ( !MWNamespace::isMovable( $this->mNamespace ) || $this->isExternal() ) { // Interwiki title or immovable namespace. Hooks don't get to override here return false; } @@ -1262,7 +1260,7 @@ class Title implements LinkTarget { public function isConversionTable() { // @todo ConversionTable should become a separate content model. - return $this->getNamespace() == NS_MEDIAWIKI && + return $this->mNamespace == NS_MEDIAWIKI && strpos( $this->getText(), 'Conversiontable/' ) === 0; } @@ -1431,7 +1429,7 @@ class Title implements LinkTarget { $this->hasContentModel( CONTENT_MODEL_CSS ) // paranoia - a MediaWiki: namespace page with mismatching extension and content // model is probably by mistake and might get handled incorrectly (see e.g. T112937) - || substr( $this->getDBkey(), -4 ) === '.css' + || substr( $this->mDbkeyform, -4 ) === '.css' ) ); } @@ -1449,7 +1447,7 @@ class Title implements LinkTarget { $this->hasContentModel( CONTENT_MODEL_JSON ) // paranoia - a MediaWiki: namespace page with mismatching extension and content // model is probably by mistake and might get handled incorrectly (see e.g. T112937) - || substr( $this->getDBkey(), -5 ) === '.json' + || substr( $this->mDbkeyform, -5 ) === '.json' ) ); } @@ -1467,7 +1465,7 @@ class Title implements LinkTarget { $this->hasContentModel( CONTENT_MODEL_JAVASCRIPT ) // paranoia - a MediaWiki: namespace page with mismatching extension and content // model is probably by mistake and might get handled incorrectly (see e.g. T112937) - || substr( $this->getDBkey(), -3 ) === '.js' + || substr( $this->mDbkeyform, -3 ) === '.js' ) ); } @@ -1478,7 +1476,7 @@ class Title implements LinkTarget { * @return bool */ public function isTalkPage() { - return MWNamespace::isTalk( $this->getNamespace() ); + return MWNamespace::isTalk( $this->mNamespace ); } /** @@ -1487,7 +1485,7 @@ class Title implements LinkTarget { * @return Title The object for the talk page */ public function getTalkPage() { - return self::makeTitle( MWNamespace::getTalk( $this->getNamespace() ), $this->getDBkey() ); + return self::makeTitle( MWNamespace::getTalk( $this->mNamespace ), $this->mDbkeyform ); } /** @@ -1515,11 +1513,11 @@ class Title implements LinkTarget { */ public function getSubjectPage() { // Is this the same title? - $subjectNS = MWNamespace::getSubject( $this->getNamespace() ); - if ( $this->getNamespace() == $subjectNS ) { + $subjectNS = MWNamespace::getSubject( $this->mNamespace ); + if ( $this->mNamespace == $subjectNS ) { return $this; } - return self::makeTitle( $subjectNS, $this->getDBkey() ); + return self::makeTitle( $subjectNS, $this->mDbkeyform ); } /** @@ -1585,9 +1583,9 @@ class Title implements LinkTarget { } elseif ( $this->isExternal() && !self::getInterwikiLookup()->fetch( $this->mInterwiki )->isLocal() ) { - return '#' . Sanitizer::escapeIdForExternalInterwiki( $this->getFragment() ); + return '#' . Sanitizer::escapeIdForExternalInterwiki( $this->mFragment ); } - return '#' . Sanitizer::escapeIdForLink( $this->getFragment() ); + return '#' . Sanitizer::escapeIdForLink( $this->mFragment ); } /** @@ -1615,10 +1613,10 @@ class Title implements LinkTarget { */ public function createFragmentTarget( $fragment ) { return self::makeTitle( - $this->getNamespace(), + $this->mNamespace, $this->getText(), $fragment, - $this->getInterwiki() + $this->mInterwiki ); } @@ -1694,7 +1692,7 @@ class Title implements LinkTarget { public function getFullText() { $text = $this->getPrefixedText(); if ( $this->hasFragment() ) { - $text .= '#' . $this->getFragment(); + $text .= '#' . $this->mFragment; } return $text; } @@ -1732,7 +1730,7 @@ class Title implements LinkTarget { * @since 1.20 */ public function getRootTitle() { - return self::makeTitle( $this->getNamespace(), $this->getRootText() ); + return self::makeTitle( $this->mNamespace, $this->getRootText() ); } /** @@ -1772,7 +1770,7 @@ class Title implements LinkTarget { * @since 1.20 */ public function getBaseTitle() { - return self::makeTitle( $this->getNamespace(), $this->getBaseText() ); + return self::makeTitle( $this->mNamespace, $this->getBaseText() ); } /** @@ -1808,7 +1806,7 @@ class Title implements LinkTarget { * @since 1.20 */ public function getSubpage( $text ) { - return self::makeTitleSafe( $this->getNamespace(), $this->getText() . '/' . $text ); + return self::makeTitleSafe( $this->mNamespace, $this->getText() . '/' . $text ); } /** @@ -1966,7 +1964,7 @@ class Title implements LinkTarget { # Yes! It can in interwiki transclusion. But... it probably shouldn't. $namespace .= ':'; } - $url = $interwiki->getURL( $namespace . $this->getDBkey() ); + $url = $interwiki->getURL( $namespace . $this->mDbkeyform ); $url = wfAppendQuery( $url, $query ); } else { $dbkey = wfUrlencode( $this->getPrefixedDBkey() ); @@ -2698,7 +2696,7 @@ class Title implements LinkTarget { // Check for explicit whitelisting with and without underscores if ( in_array( $name, $wgWhitelistRead, true ) || in_array( $dbName, $wgWhitelistRead, true ) ) { $whitelisted = true; - } elseif ( $this->getNamespace() == NS_MAIN ) { + } elseif ( $this->mNamespace == NS_MAIN ) { # Old settings might have the title prefixed with # a colon for main-namespace pages if ( in_array( ':' . $name, $wgWhitelistRead ) ) { @@ -2706,7 +2704,7 @@ class Title implements LinkTarget { } } elseif ( $this->isSpecialPage() ) { # If it's a special page, ditch the subpage bit and check again - $name = $this->getDBkey(); + $name = $this->mDbkeyform; list( $name, /* $subpage */ ) = SpecialPageFactory::resolveAlias( $name ); if ( $name ) { $pure = SpecialPage::getTitleFor( $name )->getPrefixedText(); @@ -2857,7 +2855,7 @@ class Title implements LinkTarget { $types = self::getFilteredRestrictionTypes( $this->exists() ); - if ( $this->getNamespace() != NS_FILE ) { + if ( $this->mNamespace != NS_FILE ) { # Remove the upload restriction for non-file titles $types = array_diff( $types, [ 'upload' ] ); } @@ -2902,7 +2900,7 @@ class Title implements LinkTarget { */ protected function getTitleProtectionInternal() { // Can't protect pages in special namespaces - if ( $this->getNamespace() < 0 ) { + if ( $this->mNamespace < 0 ) { return false; } @@ -2922,7 +2920,7 @@ class Title implements LinkTarget { 'expiry' => 'pt_expiry', 'permission' => 'pt_create_perm' ] + $commentQuery['fields'], - [ 'pt_namespace' => $this->getNamespace(), 'pt_title' => $this->getDBkey() ], + [ 'pt_namespace' => $this->mNamespace, 'pt_title' => $this->mDbkeyform ], __METHOD__, [], $commentQuery['joins'] @@ -2952,7 +2950,7 @@ class Title implements LinkTarget { $dbw->delete( 'protected_titles', - [ 'pt_namespace' => $this->getNamespace(), 'pt_title' => $this->getDBkey() ], + [ 'pt_namespace' => $this->mNamespace, 'pt_title' => $this->mDbkeyform ], __METHOD__ ); $this->mTitleProtection = false; @@ -3085,18 +3083,18 @@ class Title implements LinkTarget { $dbr = wfGetDB( DB_REPLICA ); - if ( $this->getNamespace() == NS_FILE ) { + if ( $this->mNamespace == NS_FILE ) { $tables = [ 'imagelinks', 'page_restrictions' ]; $where_clauses = [ - 'il_to' => $this->getDBkey(), + 'il_to' => $this->mDbkeyform, 'il_from=pr_page', 'pr_cascade' => 1 ]; } else { $tables = [ 'templatelinks', 'page_restrictions' ]; $where_clauses = [ - 'tl_namespace' => $this->getNamespace(), - 'tl_title' => $this->getDBkey(), + 'tl_namespace' => $this->mNamespace, + 'tl_title' => $this->mDbkeyform, 'tl_from=pr_page', 'pr_cascade' => 1 ]; @@ -3439,13 +3437,13 @@ class Title implements LinkTarget { * doesn't allow subpages */ public function getSubpages( $limit = -1 ) { - if ( !MWNamespace::hasSubpages( $this->getNamespace() ) ) { + if ( !MWNamespace::hasSubpages( $this->mNamespace ) ) { return []; } $dbr = wfGetDB( DB_REPLICA ); - $conds['page_namespace'] = $this->getNamespace(); - $conds[] = 'page_title ' . $dbr->buildLike( $this->getDBkey() . '/', $dbr->anyString() ); + $conds['page_namespace'] = $this->mNamespace; + $conds[] = 'page_title ' . $dbr->buildLike( $this->mDbkeyform . '/', $dbr->anyString() ); $options = []; if ( $limit > -1 ) { $options['LIMIT'] = $limit; @@ -3466,18 +3464,18 @@ class Title implements LinkTarget { * @return int The number of archived revisions */ public function isDeleted() { - if ( $this->getNamespace() < 0 ) { + if ( $this->mNamespace < 0 ) { $n = 0; } else { $dbr = wfGetDB( DB_REPLICA ); $n = $dbr->selectField( 'archive', 'COUNT(*)', - [ 'ar_namespace' => $this->getNamespace(), 'ar_title' => $this->getDBkey() ], + [ 'ar_namespace' => $this->mNamespace, 'ar_title' => $this->mDbkeyform ], __METHOD__ ); - if ( $this->getNamespace() == NS_FILE ) { + if ( $this->mNamespace == NS_FILE ) { $n += $dbr->selectField( 'filearchive', 'COUNT(*)', - [ 'fa_name' => $this->getDBkey() ], + [ 'fa_name' => $this->mDbkeyform ], __METHOD__ ); } @@ -3491,17 +3489,17 @@ class Title implements LinkTarget { * @return bool */ public function isDeletedQuick() { - if ( $this->getNamespace() < 0 ) { + if ( $this->mNamespace < 0 ) { return false; } $dbr = wfGetDB( DB_REPLICA ); $deleted = (bool)$dbr->selectField( 'archive', '1', - [ 'ar_namespace' => $this->getNamespace(), 'ar_title' => $this->getDBkey() ], + [ 'ar_namespace' => $this->mNamespace, 'ar_title' => $this->mDbkeyform ], __METHOD__ ); - if ( !$deleted && $this->getNamespace() == NS_FILE ) { + if ( !$deleted && $this->mNamespace == NS_FILE ) { $deleted = (bool)$dbr->selectField( 'filearchive', '1', - [ 'fa_name' => $this->getDBkey() ], + [ 'fa_name' => $this->mDbkeyform ], __METHOD__ ); } @@ -3517,7 +3515,7 @@ class Title implements LinkTarget { * @return int The ID */ public function getArticleID( $flags = 0 ) { - if ( $this->getNamespace() < 0 ) { + if ( $this->mNamespace < 0 ) { $this->mArticleID = 0; return $this->mArticleID; } @@ -3702,7 +3700,7 @@ class Title implements LinkTarget { // splitTitleString method, but the only implementation (MediaWikiTitleCodec) does $titleCodec = MediaWikiServices::getInstance()->getTitleParser(); // MalformedTitleException can be thrown here - $parts = $titleCodec->splitTitleString( $this->mDbkeyform, $this->getDefaultNamespace() ); + $parts = $titleCodec->splitTitleString( $this->mDbkeyform, $this->mDefaultNamespace ); # Fill fields $this->setFragment( '#' . $parts['fragment'] ); @@ -3747,8 +3745,8 @@ class Title implements LinkTarget { self::getSelectFields(), [ "{$prefix}_from=page_id", - "{$prefix}_namespace" => $this->getNamespace(), - "{$prefix}_title" => $this->getDBkey() ], + "{$prefix}_namespace" => $this->mNamespace, + "{$prefix}_title" => $this->mDbkeyform ], __METHOD__, $options ); @@ -4054,9 +4052,9 @@ class Title implements LinkTarget { ]; } // Do the source and target namespaces support subpages? - if ( !MWNamespace::hasSubpages( $this->getNamespace() ) ) { + if ( !MWNamespace::hasSubpages( $this->mNamespace ) ) { return [ - [ 'namespace-nosubpages', MWNamespace::getCanonicalName( $this->getNamespace() ) ], + [ 'namespace-nosubpages', MWNamespace::getCanonicalName( $this->mNamespace ) ], ]; } if ( !MWNamespace::hasSubpages( $nt->getNamespace() ) ) { @@ -4088,7 +4086,7 @@ class Title implements LinkTarget { continue; } $newPageName = preg_replace( - '#^' . preg_quote( $this->getDBkey(), '#' ) . '#', + '#^' . preg_quote( $this->mDbkeyform, '#' ) . '#', StringUtils::escapeRegexReplacement( $nt->getDBkey() ), # T23234 $oldSubpage->getDBkey() ); if ( $oldSubpage->isTalkPage() ) { @@ -4147,8 +4145,8 @@ class Title implements LinkTarget { # Does the article have a history? $row = $dbw->selectField( [ 'page', 'revision' ], 'rev_id', - [ 'page_namespace' => $this->getNamespace(), - 'page_title' => $this->getDBkey(), + [ 'page_namespace' => $this->mNamespace, + 'page_title' => $this->mDbkeyform, 'page_id=rev_page', 'page_latest != rev_id' ], @@ -4600,9 +4598,9 @@ class Title implements LinkTarget { */ public function equals( Title $title ) { // Note: === is necessary for proper matching of number-like titles. - return $this->getInterwiki() === $title->getInterwiki() - && $this->getNamespace() == $title->getNamespace() - && $this->getDBkey() === $title->getDBkey(); + return $this->mInterwiki === $title->mInterwiki + && $this->mNamespace == $title->mNamespace + && $this->mDbkeyform === $title->mDbkeyform; } /** @@ -4612,9 +4610,9 @@ class Title implements LinkTarget { * @return bool */ public function isSubpageOf( Title $title ) { - return $this->getInterwiki() === $title->getInterwiki() - && $this->getNamespace() == $title->getNamespace() - && strpos( $this->getDBkey(), $title->getDBkey() . '/' ) === 0; + return $this->mInterwiki === $title->mInterwiki + && $this->mNamespace == $title->mNamespace + && strpos( $this->mDbkeyform, $title->mDbkeyform . '/' ) === 0; } /** @@ -4680,7 +4678,7 @@ class Title implements LinkTarget { return (bool)wfFindFile( $this ); case NS_SPECIAL: // valid special page - return SpecialPageFactory::exists( $this->getDBkey() ); + return SpecialPageFactory::exists( $this->mDbkeyform ); case NS_MAIN: // selflink, possibly with fragment return $this->mDbkeyform == ''; @@ -4739,7 +4737,7 @@ class Title implements LinkTarget { * @return string|bool */ public function getDefaultMessageText() { - if ( $this->getNamespace() != NS_MEDIAWIKI ) { // Just in case + if ( $this->mNamespace != NS_MEDIAWIKI ) { // Just in case return false; } @@ -4805,7 +4803,7 @@ class Title implements LinkTarget { */ public function touchLinks() { DeferredUpdates::addUpdate( new HTMLCacheUpdate( $this, 'pagelinks', 'page-touch' ) ); - if ( $this->getNamespace() == NS_CATEGORY ) { + if ( $this->mNamespace == NS_CATEGORY ) { DeferredUpdates::addUpdate( new HTMLCacheUpdate( $this, 'categorylinks', 'category-touch' ) ); @@ -4872,7 +4870,7 @@ class Title implements LinkTarget { */ public function getNamespaceKey( $prepend = 'nstab-' ) { // Gets the subject namespace of this title - $subjectNS = MWNamespace::getSubject( $this->getNamespace() ); + $subjectNS = MWNamespace::getSubject( $this->mNamespace ); // Prefer canonical namespace name for HTML IDs $namespaceKey = MWNamespace::getCanonicalName( $subjectNS ); if ( $namespaceKey === false ) { @@ -4903,12 +4901,12 @@ class Title implements LinkTarget { $dbr = wfGetDB( DB_REPLICA ); $where = [ - 'rd_namespace' => $this->getNamespace(), - 'rd_title' => $this->getDBkey(), + 'rd_namespace' => $this->mNamespace, + 'rd_title' => $this->mDbkeyform, 'rd_from = page_id' ]; if ( $this->isExternal() ) { - $where['rd_interwiki'] = $this->getInterwiki(); + $where['rd_interwiki'] = $this->mInterwiki; } else { $where[] = 'rd_interwiki = ' . $dbr->addQuotes( '' ) . ' OR rd_interwiki IS NULL'; } @@ -5121,7 +5119,7 @@ class Title implements LinkTarget { $notices = []; // Optional notice for the entire namespace - $editnotice_ns = 'editnotice-' . $this->getNamespace(); + $editnotice_ns = 'editnotice-' . $this->mNamespace; $msg = wfMessage( $editnotice_ns ); if ( $msg->exists() ) { $html = $msg->parseAsBlock(); @@ -5139,9 +5137,9 @@ class Title implements LinkTarget { } } - if ( MWNamespace::hasSubpages( $this->getNamespace() ) ) { + if ( MWNamespace::hasSubpages( $this->mNamespace ) ) { // Optional notice for page itself and any parent page - $parts = explode( '/', $this->getDBkey() ); + $parts = explode( '/', $this->mDbkeyform ); $editnotice_base = $editnotice_ns; while ( count( $parts ) > 0 ) { $editnotice_base .= '-' . array_shift( $parts ); @@ -5163,7 +5161,7 @@ class Title implements LinkTarget { } } else { // Even if there are no subpages in namespace, we still don't want "/" in MediaWiki message keys - $editnoticeText = $editnotice_ns . '-' . strtr( $this->getDBkey(), '/', '-' ); + $editnoticeText = $editnotice_ns . '-' . strtr( $this->mDbkeyform, '/', '-' ); $msg = wfMessage( $editnoticeText ); if ( $msg->exists() ) { $html = $msg->parseAsBlock(); -- 2.20.1