*/
use MediaWiki\Linker\LinkRenderer;
use MediaWiki\MediaWikiServices;
+use MediaWiki\Special\SpecialPageFactory;
use Wikimedia\ScopedCallback;
/**
/** @var MagicWordFactory */
private $magicWordFactory;
+ /** @var Language */
+ private $contLang;
+
+ /** @var ParserFactory */
+ private $factory;
+
+ /** @var SpecialPageFactory */
+ private $specialPageFactory;
+
/**
- * @param array $conf
+ * @param array $conf See $wgParserConf documentation
* @param MagicWordFactory|null $magicWordFactory
- */
- public function __construct( $conf = [], MagicWordFactory $magicWordFactory = null ) {
+ * @param Language|null $contLang Content language
+ * @param ParserFactory|null $factory
+ * @param string|null $urlProtocols As returned from wfUrlProtocols()
+ * @param SpecialPageFactory|null $spFactory
+ */
+ public function __construct(
+ array $conf = [], MagicWordFactory $magicWordFactory = null, Language $contLang = null,
+ ParserFactory $factory = null, $urlProtocols = null, SpecialPageFactory $spFactory = null
+ ) {
$this->mConf = $conf;
- $this->mUrlProtocols = wfUrlProtocols();
+ $this->mUrlProtocols = $urlProtocols ?? wfUrlProtocols();
$this->mExtLinkBracketedRegex = '/\[(((?i)' . $this->mUrlProtocols . ')' .
self::EXT_LINK_ADDR .
self::EXT_LINK_URL_CLASS . '*)\p{Zs}*([^\]\\x00-\\x08\\x0a-\\x1F\\x{FFFD}]*?)\]/Su';
}
wfDebug( __CLASS__ . ": using preprocessor: {$this->mPreprocessorClass}\n" );
- $this->magicWordFactory = $magicWordFactory;
- if ( !$magicWordFactory ) {
- $this->magicWordFactory = MediaWikiServices::getInstance()->getMagicWordFactory();
- }
+ $services = MediaWikiServices::getInstance();
+ $this->magicWordFactory = $magicWordFactory ??
+ $services->getMagicWordFactory();
+
+ $this->contLang = $contLang ?? $services->getContentLanguage();
+
+ $this->factory = $factory ?? $services->getParserFactory();
+ $this->specialPageFactory = $spFactory ?? $services->getSpecialPageFactory();
}
/**
* @return Language
*/
public function getContentLanguage() {
- return $this->magicWordFactory->getContentLanguage();
+ return $this->contLang;
}
/**
if ( $useLinkPrefixExtension ) {
# Match the end of a line for a word that's not followed by whitespace,
# e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
- $charset = $this->getContentLanguage()->linkPrefixCharset();
+ $charset = $this->contLang->linkPrefixCharset();
$e2 = "/^((?>.*[^$charset]|))(.+)$/sDu";
}
break;
case 'namespace':
$value = str_replace( '_', ' ',
- $this->getContentLanguage()->getNsText( $this->mTitle->getNamespace() ) );
+ $this->contLang->getNsText( $this->mTitle->getNamespace() ) );
break;
case 'namespacee':
- $value = wfUrlencode( $this->getContentLanguage()->
- getNsText( $this->mTitle->getNamespace() ) );
+ $value = wfUrlencode( $this->contLang->getNsText( $this->mTitle->getNamespace() ) );
break;
case 'namespacenumber':
$value = $this->mTitle->getNamespace();
if ( !$this->getRevisionObject() ) {
# Get the timezone-adjusted timestamp $mtts seconds in the future
$resThen = substr(
- $this->getContentLanguage()->userAdjust( wfTimestamp( TS_MW, time() + $mtts ), '' ),
+ $this->contLang->userAdjust( wfTimestamp( TS_MW, time() + $mtts ), '' ),
$start,
$len
);
&& $this->mOptions->getAllowSpecialInclusion()
&& $this->ot['html']
) {
- $specialPage = SpecialPageFactory::getPage( $title->getDBkey() );
+ $specialPage = $this->specialPageFactory->getPage( $title->getDBkey() );
// Pass the template arguments as URL parameters.
// "uselang" will have no effect since the Language object
// is forced to the one defined in ParserOptions.
$context->setUser( User::newFromName( '127.0.0.1', false ) );
}
$context->setLanguage( $this->mOptions->getUserLangObj() );
- $ret = SpecialPageFactory::capturePath(
- $title, $context, $this->getLinkRenderer() );
+ $ret = $this->specialPageFactory->capturePath( $title, $context, $this->getLinkRenderer() );
if ( $ret ) {
$text = $context->getOutput()->getHTML();
$this->mOutput->addOutputPageMetadata( $context->getOutput() );
$function = $this->mFunctionSynonyms[1][$function];
} else {
# Case insensitive functions
- $function = $this->getContentLanguage()->lc( $function );
+ $function = $this->contLang->lc( $function );
if ( isset( $this->mFunctionSynonyms[0][$function] ) ) {
$function = $this->mFunctionSynonyms[0][$function];
} else {
* Transclude an interwiki link.
*
* @param Title $title
- * @param string $action
+ * @param string $action Usually one of (raw, render)
*
* @return string
*/
public function interwikiTransclude( $title, $action ) {
- global $wgEnableScaryTranscluding;
+ global $wgEnableScaryTranscluding, $wgTranscludeCacheExpiry;
if ( !$wgEnableScaryTranscluding ) {
return wfMessage( 'scarytranscludedisabled' )->inContentLanguage()->text();
}
$url = $title->getFullURL( [ 'action' => $action ] );
-
- if ( strlen( $url ) > 255 ) {
+ if ( strlen( $url ) > 1024 ) {
return wfMessage( 'scarytranscludetoolong' )->inContentLanguage()->text();
}
- return $this->fetchScaryTemplateMaybeFromCache( $url );
- }
- /**
- * @param string $url
- * @return mixed|string
- */
- public function fetchScaryTemplateMaybeFromCache( $url ) {
- global $wgTranscludeCacheExpiry;
- $dbr = wfGetDB( DB_REPLICA );
- $tsCond = $dbr->timestamp( time() - $wgTranscludeCacheExpiry );
- $obj = $dbr->selectRow( 'transcache', [ 'tc_time', 'tc_contents' ],
- [ 'tc_url' => $url, "tc_time >= " . $dbr->addQuotes( $tsCond ) ] );
- if ( $obj ) {
- return $obj->tc_contents;
- }
-
- $req = MWHttpRequest::factory( $url, [], __METHOD__ );
- $status = $req->execute(); // Status object
- if ( $status->isOK() ) {
- $text = $req->getContent();
- } elseif ( $req->getStatus() != 200 ) {
+ $wikiId = $title->getTransWikiID(); // remote wiki ID or false
+
+ $fname = __METHOD__;
+ $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
+
+ $data = $cache->getWithSetCallback(
+ $cache->makeGlobalKey(
+ 'interwiki-transclude',
+ ( $wikiId !== false ) ? $wikiId : 'external',
+ sha1( $url )
+ ),
+ $wgTranscludeCacheExpiry,
+ function ( $oldValue, &$ttl ) use ( $url, $fname, $cache ) {
+ $req = MWHttpRequest::factory( $url, [], $fname );
+
+ $status = $req->execute(); // Status object
+ if ( !$status->isOK() ) {
+ $ttl = $cache::TTL_UNCACHEABLE;
+ } elseif ( $req->getResponseHeader( 'X-Database-Lagged' ) !== null ) {
+ $ttl = min( $cache::TTL_LAGGED, $ttl );
+ }
+
+ return [
+ 'text' => $status->isOK() ? $req->getContent() : null,
+ 'code' => $req->getStatus()
+ ];
+ },
+ [
+ 'checkKeys' => ( $wikiId !== false )
+ ? [ $cache->makeGlobalKey( 'interwiki-page', $wikiId, $title->getDBkey() ) ]
+ : [],
+ 'pcGroup' => 'interwiki-transclude:5',
+ 'pcTTL' => $cache::TTL_PROC_LONG
+ ]
+ );
+
+ if ( is_string( $data['text'] ) ) {
+ $text = $data['text'];
+ } elseif ( $data['code'] != 200 ) {
// Though we failed to fetch the content, this status is useless.
- return wfMessage( 'scarytranscludefailed-httpstatus' )
- ->params( $url, $req->getStatus() /* HTTP status */ )->inContentLanguage()->text();
+ $text = wfMessage( 'scarytranscludefailed-httpstatus' )
+ ->params( $url, $data['code'] )->inContentLanguage()->text();
} else {
- return wfMessage( 'scarytranscludefailed', $url )->inContentLanguage()->text();
+ $text = wfMessage( 'scarytranscludefailed', $url )->inContentLanguage()->text();
}
- $dbw = wfGetDB( DB_MASTER );
- $dbw->replace( 'transcache', [ 'tc_url' ], [
- 'tc_url' => $url,
- 'tc_time' => $dbw->timestamp( time() ),
- 'tc_contents' => $text
- ] );
return $text;
}
*/
private function pstPass2( $text, $user ) {
# Note: This is the timestamp saved as hardcoded wikitext to the database, we use
- # $this->getContentLanguage() here in order to give everyone the same signature and use the
- # default one rather than the one selected in each user's preferences. (see also T14815)
+ # $this->contLang here in order to give everyone the same signature and use the default one
+ # rather than the one selected in each user's preferences. (see also T14815)
$ts = $this->mOptions->getTimestamp();
$timestamp = MWTimestamp::getLocalInstance( $ts );
$ts = $timestamp->format( 'YmdHis' );
$tzMsg = $timestamp->getTimezoneMessage()->inContentLanguage()->text();
- $d = $this->getContentLanguage()->timeanddate( $ts, false, false ) . " ($tzMsg)";
+ $d = $this->contLang->timeanddate( $ts, false, false ) . " ($tzMsg)";
# Variable replacement
# Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
foreach ( $synonyms as $syn ) {
# Case
if ( !$sensitive ) {
- $syn = $this->getContentLanguage()->lc( $syn );
+ $syn = $this->contLang->lc( $syn );
}
# Add leading hash
if ( !( $flags & self::SFH_NO_HASH ) ) {
* @return array
*/
public function getFunctionHooks() {
+ $this->firstCallInit();
return array_keys( $this->mFunctionHooks );
}
* @return array
*/
public function getTags() {
+ $this->firstCallInit();
return array_merge(
array_keys( $this->mTransparentTagHooks ),
array_keys( $this->mTagHooks ),
);
}
+ /**
+ * @since 1.32
+ * @return array
+ */
+ public function getFunctionSynonyms() {
+ $this->firstCallInit();
+ return $this->mFunctionSynonyms;
+ }
+
+ /**
+ * @since 1.32
+ * @return string
+ */
+ public function getUrlProtocols() {
+ return $this->mUrlProtocols;
+ }
+
/**
* Replace transparent tags in $text with the values given by the callbacks.
*
# Since this value will be saved into the parser cache, served
# to other users, and potentially even used inside links and such,
# it needs to be consistent for all visitors.
- $this->mRevisionTimestamp = $this->getContentLanguage()->userAdjust( $timestamp, '' );
-
+ $this->mRevisionTimestamp = $this->contLang->userAdjust( $timestamp, '' );
}
return $this->mRevisionTimestamp;
}
* @return Parser A parser object that is not parsing anything
*/
public function getFreshParser() {
- global $wgParserConf;
if ( $this->mInParse ) {
- return new $wgParserConf['class']( $wgParserConf );
+ return $this->factory->create();
} else {
return $this;
}