37 private $linkRenderer;
39 private $linkBatchFactory;
49 private $namespaceInfo;
66 private const MAX_ID_SIZE = 7;
68 private const MARKER_PREFIX =
"\x1B\"'";
92 $this->linkRenderer = $linkRenderer;
93 $this->linkBatchFactory = $linkBatchFactory;
94 $this->linkCache = $linkCache;
95 $this->repoGroup = $repoGroup;
96 $this->userLang = $userLang;
97 $this->contLang = $contLang;
98 $this->titleParser = $titleParser;
99 $this->namespaceInfo = $namespaceInfo;
100 $this->hookRunner =
new HookRunner( $hookContainer );
115 $samePage =
false, $wikiId =
false, $enableSectionLinks =
true
117 return $this->preprocessInternal( $comment,
false, $selfLinkTarget,
118 $samePage, $wikiId, $enableSectionLinks );
132 $samePage =
false, $wikiId =
false, $enableSectionLinks =
true
134 return $this->preprocessInternal( $comment,
true, $selfLinkTarget,
135 $samePage, $wikiId, $enableSectionLinks );
146 $this->flushLinkBatches();
147 return preg_replace_callback(
148 '/' . self::MARKER_PREFIX .
'([0-9]{' . self::MAX_ID_SIZE .
'})/',
150 $callback = $this->links[(int)$m[1]] ??
null;
154 return '<!-- MISSING -->';
170 private function preprocessInternal( $comment, $unsafe, $selfLinkTarget, $samePage, $wikiId,
175 $comment = strtr( $comment,
"\n\x1b",
" " );
178 $comment = Sanitizer::escapeHtmlAllowEntities( $comment );
180 if ( $enableSectionLinks ) {
181 $comment = $this->doSectionLinks( $comment, $selfLinkTarget, $samePage, $wikiId );
183 return $this->doWikiLinks( $comment, $selfLinkTarget, $samePage, $wikiId );
202 private function doSectionLinks(
204 $selfLinkTarget =
null,
214 $comment = preg_replace_callback(
220 '!(?:(?<=(.)))?/\*\s*(.*?)\s*\*/(?:(?=(.)))?!',
221 function ( $match ) use ( &$append, $selfLinkTarget, $samePage, $wikiId ) {
223 $match += [
'',
'',
'',
'' ];
225 $pre = $match[1] !==
'';
227 $post = $match[3] !==
'';
230 $this->hookRunner->onFormatAutocomments(
231 $comment, $pre, $auto, $post,
232 Title::castFromLinkTarget( $selfLinkTarget ),
235 if ( $comment !==
null ) {
239 if ( $selfLinkTarget ) {
241 # Remove links that a user may have manually put in the autosummary
242 # This could be improved by copying as much of Parser::stripSectionName as desired.
243 $section = str_replace( [
251 $sectionText = str_replace(
'[[',
'[[', $auto );
253 $section = substr( Parser::guessSectionNameFromStrippedText( $section ), 1 );
254 if ( $section !==
'' ) {
256 $sectionTitle =
new TitleValue(
NS_MAIN,
'', $section );
258 $sectionTitle = $selfLinkTarget->createFragmentTarget( $section );
260 $auto = $this->makeSectionLink(
262 $this->userLang->getArrow() . $this->userLang->getDirMark() . $sectionText,
268 # written summary $presep autocomment (summary )
269 $pre =
wfMessage(
'autocomment-prefix' )->inContentLanguage()->escaped();
272 # autocomment $postsep written summary ( summary)
273 $auto .=
wfMessage(
'colon-separator' )->inContentLanguage()->escaped();
276 $auto =
'<span dir="auto"><span class="autocomment">' . $auto .
'</span>';
277 $append .=
'</span>';
279 $comment = $pre . $auto;
284 return $comment . $append;
298 private function makeSectionLink(
299 LinkTarget $target, $text, $wikiId
301 if ( $wikiId !==
null && $wikiId !==
false && !$target->isExternal() ) {
302 return Linker::makeExternalLink(
303 WikiMap::getForeignURL(
305 $target->getNamespace() === 0
306 ? $target->getDBkey()
307 : $this->namespaceInfo->getCanonicalName( $target->getNamespace() ) .
308 ':' . $target->getDBkey(),
309 $target->getFragment()
315 return $this->linkRenderer->makePreloadedLink( $target,
new HtmlArmor( $text ),
'' );
336 private function doWikiLinks( $comment, $selfLinkTarget =
null, $samePage =
false, $wikiId =
false ) {
337 return preg_replace_callback(
340 \s*+ # ignore leading whitespace, the *+ quantifier disallows backtracking
341 :? # ignore optional leading colon
342 ([^[\]|]+) # 1. link target; page names cannot include [, ] or |
345 # Stop matching at ]] without relying on backtracking.
349 ([^[]*) # 3. link trail (the text up until the next link)
351 function ( $match ) use ( $selfLinkTarget, $samePage, $wikiId ) {
353 $medians .= preg_quote(
354 $this->namespaceInfo->getCanonicalName(
NS_MEDIA ),
'/' );
356 $medians .= preg_quote(
357 $this->contLang->getNsText(
NS_MEDIA ),
361 $comment = $match[0];
364 if ( strpos( $match[1],
'%' ) !==
false ) {
366 rawurldecode( $match[1] ),
367 [
'<' =>
'<',
'>' =>
'>' ]
372 if ( $match[2] !=
"" ) {
379 if ( preg_match(
'/^' . $medians .
'(.*)$/i', $match[1], $submatch ) ) {
381 $linkRegexp =
'/\[\[(.*?)\]\]/';
382 $linkTarget = $this->titleParser->makeTitleValueSafe(
NS_FILE, $submatch[1] );
384 $linkMarker = $this->addFileLink( $linkTarget, $text );
389 if ( isset( $match[1][0] ) && $match[1][0] ==
':' ) {
390 $match[1] = substr( $match[1], 1 );
393 if ( $match[1] !==
false && $match[1] !==
null && $match[1] !==
'' ) {
395 $this->contLang->linkTrail(),
399 $trail = $submatch[1];
403 $linkRegexp =
'/\[\[(.*?)\]\]' . preg_quote( $trail,
'/' ) .
'/';
404 [ $inside, $trail ] = Linker::splitTrail( $trail );
407 $linkTarget = Linker::normalizeSubpageLink( $selfLinkTarget, $match[1], $linkText );
410 $target = $this->titleParser->parseTitle( $linkTarget );
412 if ( $target->getText() ==
'' && !$target->isExternal()
413 && !$samePage && $selfLinkTarget
415 $target = $selfLinkTarget->createFragmentTarget( $target->getFragment() );
418 $linkMarker = $this->addPageLink( $target, $linkText . $inside, $wikiId );
419 $linkMarker .= $trail;
420 }
catch ( MalformedTitleException $e ) {
427 $comment = preg_replace(
449 private function addLinkMarker( $callback ) {
450 $nextId = count( $this->links );
451 if ( strlen( (
string)$nextId ) > self::MAX_ID_SIZE ) {
452 throw new \RuntimeException(
'Too many links in comment batch' );
454 $this->links[] = $callback;
455 return sprintf( self::MARKER_PREFIX .
"%0" . self::MAX_ID_SIZE .
'd', $nextId );
467 private function addPageLink( LinkTarget $target, $text, $wikiId ) {
468 if ( $wikiId !==
null && $wikiId !==
false && !$target->isExternal() ) {
470 return Linker::makeExternalLink(
471 WikiMap::getForeignURL(
473 $target->getNamespace() === 0
474 ? $target->getDBkey()
475 : $this->namespaceInfo->getCanonicalName( $target->getNamespace() ) .
476 ':' . $target->getDBkey(),
477 $target->getFragment()
482 } elseif ( $this->linkCache->getGoodLinkID( $target ) ||
483 Title::newFromLinkTarget( $target )->isAlwaysKnown()
486 return $this->linkRenderer->makeKnownLink( $target,
new HtmlArmor( $text ) );
487 } elseif ( $this->linkCache->isBadLink( $target ) ) {
489 return $this->linkRenderer->makeBrokenLink( $target,
new HtmlArmor( $text ) );
493 if ( !$this->linkBatch ) {
494 $this->linkBatch = $this->linkBatchFactory->newLinkBatch();
495 $this->linkBatch->setCaller( __METHOD__ );
497 $this->linkBatch->addObj( $target );
498 return $this->addLinkMarker(
function () use ( $target, $text ) {
499 return $this->linkRenderer->makeLink( $target,
new HtmlArmor( $text ) );
510 private function addFileLink( LinkTarget $target, $html ) {
511 $this->fileBatch[] = [
514 return $this->addLinkMarker(
function () use ( $target, $html ) {
515 return Linker::makeMediaLinkFile(
517 $this->files[$target->getDBkey()] ??
false,
526 private function flushLinkBatches() {
527 if ( $this->linkBatch ) {
528 $this->linkBatch->execute();
529 $this->linkBatch =
null;
531 if ( $this->fileBatch ) {
532 $this->files += $this->repoGroup->findFiles( $this->fileBatch );
533 $this->fileBatch = [];