2008-08-16 10:13:35 +00:00
|
|
|
<?php
|
2010-08-22 14:31:05 +00:00
|
|
|
/**
|
|
|
|
|
* Holder of replacement pairs for wiki links
|
|
|
|
|
*
|
2012-04-30 09:22:16 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2010-08-22 14:31:05 +00:00
|
|
|
* @file
|
2012-04-30 09:22:16 +00:00
|
|
|
* @ingroup Parser
|
2010-08-22 14:31:05 +00:00
|
|
|
*/
|
2008-08-16 10:13:35 +00:00
|
|
|
|
2024-10-03 18:39:06 +00:00
|
|
|
namespace MediaWiki\Parser;
|
|
|
|
|
|
|
|
|
|
use HtmlArmor;
|
2024-02-08 19:09:50 +00:00
|
|
|
use MediaWiki\Cache\LinkCache;
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
use MediaWiki\HookContainer\HookContainer;
|
|
|
|
|
use MediaWiki\HookContainer\HookRunner;
|
2024-08-08 09:39:26 +00:00
|
|
|
use MediaWiki\Language\ILanguageConverter;
|
2022-12-05 11:29:37 +00:00
|
|
|
use MediaWiki\Linker\Linker;
|
2022-04-26 15:48:03 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2018-06-11 06:55:11 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2018-06-11 06:55:11 +00:00
|
|
|
|
2010-08-22 14:31:05 +00:00
|
|
|
/**
|
2020-02-10 14:08:01 +00:00
|
|
|
* @internal for using in Parser only.
|
2020-02-04 12:42:03 +00:00
|
|
|
*
|
2010-08-22 14:31:05 +00:00
|
|
|
* @ingroup Parser
|
|
|
|
|
*/
|
2008-08-16 10:13:35 +00:00
|
|
|
class LinkHolderArray {
|
2022-12-05 20:25:30 +00:00
|
|
|
/** @var array<int,array<int,array>> Indexed by numeric namespace and link ids, {@see Parser::nextLinkID} */
|
2023-03-09 08:15:06 +00:00
|
|
|
private $internals = [];
|
2022-12-05 20:25:30 +00:00
|
|
|
/** @var array<int,array> Indexed by numeric link id */
|
2023-03-09 08:15:06 +00:00
|
|
|
private $interwikis = [];
|
2019-10-11 14:31:51 +00:00
|
|
|
/** @var int */
|
2023-03-09 08:15:06 +00:00
|
|
|
private $size = 0;
|
|
|
|
|
/** @var Parser */
|
|
|
|
|
private $parent;
|
|
|
|
|
/** @var ILanguageConverter */
|
2020-01-23 18:39:23 +00:00
|
|
|
private $languageConverter;
|
2023-03-09 08:15:06 +00:00
|
|
|
/** @var HookRunner */
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
private $hookRunner;
|
|
|
|
|
|
2014-07-01 00:40:19 +00:00
|
|
|
/**
|
|
|
|
|
* @param Parser $parent
|
2022-06-18 08:24:00 +00:00
|
|
|
* @param ILanguageConverter $languageConverter
|
|
|
|
|
* @param HookContainer $hookContainer
|
2014-07-01 00:40:19 +00:00
|
|
|
*/
|
2022-06-18 08:24:00 +00:00
|
|
|
public function __construct( Parser $parent, ILanguageConverter $languageConverter,
|
|
|
|
|
HookContainer $hookContainer
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
) {
|
2008-08-16 10:13:35 +00:00
|
|
|
$this->parent = $parent;
|
2020-02-14 12:50:31 +00:00
|
|
|
$this->languageConverter = $languageConverter;
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
$this->hookRunner = new HookRunner( $hookContainer );
|
2008-08-16 10:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
2008-08-26 14:37:15 +00:00
|
|
|
/**
|
|
|
|
|
* Reduce memory usage to reduce the impact of circular references
|
|
|
|
|
*/
|
2014-08-11 20:24:54 +00:00
|
|
|
public function __destruct() {
|
2019-08-29 09:59:59 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeSuspiciousNonTraversableForeach
|
2020-06-18 14:52:09 +00:00
|
|
|
foreach ( $this as $name => $_ ) {
|
2008-08-26 14:37:15 +00:00
|
|
|
unset( $this->$name );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-16 10:13:35 +00:00
|
|
|
/**
|
|
|
|
|
* Merge another LinkHolderArray into this one
|
2014-04-21 23:38:39 +00:00
|
|
|
* @param LinkHolderArray $other
|
2008-08-16 10:13:35 +00:00
|
|
|
*/
|
2014-08-11 20:24:54 +00:00
|
|
|
public function merge( $other ) {
|
2008-08-16 10:13:35 +00:00
|
|
|
foreach ( $other->internals as $ns => $entries ) {
|
|
|
|
|
$this->size += count( $entries );
|
|
|
|
|
if ( !isset( $this->internals[$ns] ) ) {
|
|
|
|
|
$this->internals[$ns] = $entries;
|
|
|
|
|
} else {
|
|
|
|
|
$this->internals[$ns] += $entries;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$this->interwikis += $other->interwikis;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns true if the memory requirements of this object are getting large
|
2012-02-09 21:35:05 +00:00
|
|
|
* @return bool
|
2008-08-16 10:13:35 +00:00
|
|
|
*/
|
2014-08-11 20:24:54 +00:00
|
|
|
public function isBig() {
|
2022-04-26 15:48:03 +00:00
|
|
|
$linkHolderBatchSize = MediaWikiServices::getInstance()->getMainConfig()
|
|
|
|
|
->get( MainConfigNames::LinkHolderBatchSize );
|
2022-01-06 18:44:56 +00:00
|
|
|
return $this->size > $linkHolderBatchSize;
|
2008-08-16 10:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clear all stored link holders.
|
|
|
|
|
* Make sure you don't have any text left using these link holders, before you call this
|
|
|
|
|
*/
|
2014-08-11 20:24:54 +00:00
|
|
|
public function clear() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->internals = [];
|
|
|
|
|
$this->interwikis = [];
|
2008-08-16 10:13:35 +00:00
|
|
|
$this->size = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Make a link placeholder. The text returned can be later resolved to a real link with
|
|
|
|
|
* replaceLinkHolders(). This is done for two reasons: firstly to avoid further
|
|
|
|
|
* parsing of interwiki links, and secondly to allow all existence checks and
|
|
|
|
|
* article length checks (for stub links) to be bundled into a single query.
|
|
|
|
|
*
|
2014-04-21 23:38:39 +00:00
|
|
|
* @param Title $nt
|
|
|
|
|
* @param string $text
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $trail [optional]
|
|
|
|
|
* @param string $prefix [optional]
|
2012-02-09 21:35:05 +00:00
|
|
|
* @return string
|
2008-08-16 10:13:35 +00:00
|
|
|
*/
|
2020-06-18 12:25:45 +00:00
|
|
|
public function makeHolder( Title $nt, $text = '', $trail = '', $prefix = '' ) {
|
2020-06-18 12:19:41 +00:00
|
|
|
# Separate the link trail from the rest of the link
|
|
|
|
|
[ $inside, $trail ] = Linker::splitTrail( $trail );
|
|
|
|
|
|
|
|
|
|
$key = $this->parent->nextLinkID();
|
|
|
|
|
$entry = [
|
|
|
|
|
'title' => $nt,
|
|
|
|
|
'text' => $prefix . $text . $inside,
|
|
|
|
|
'pdbk' => $nt->getPrefixedDBkey(),
|
|
|
|
|
];
|
2008-08-16 10:13:35 +00:00
|
|
|
|
2020-06-18 12:22:12 +00:00
|
|
|
$this->size++;
|
2020-06-18 12:19:41 +00:00
|
|
|
if ( $nt->isExternal() ) {
|
|
|
|
|
// Use a globally unique ID to keep the objects mergable
|
|
|
|
|
$this->interwikis[$key] = $entry;
|
2020-06-18 12:22:12 +00:00
|
|
|
return "<!--IWLINK'\" $key-->{$trail}";
|
2020-06-18 12:19:41 +00:00
|
|
|
} else {
|
|
|
|
|
$ns = $nt->getNamespace();
|
|
|
|
|
$this->internals[$ns][$key] = $entry;
|
2020-06-18 12:22:12 +00:00
|
|
|
return "<!--LINK'\" $ns:$key-->{$trail}";
|
2008-08-16 10:13:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Replace <!--LINK--> link placeholders with actual links, in the buffer
|
2013-10-23 05:58:29 +00:00
|
|
|
*
|
2017-08-11 00:23:16 +00:00
|
|
|
* @param string &$text
|
2008-08-16 10:13:35 +00:00
|
|
|
*/
|
2014-08-11 20:24:54 +00:00
|
|
|
public function replace( &$text ) {
|
2014-11-02 15:47:51 +00:00
|
|
|
$this->replaceInternal( $text );
|
2008-08-16 10:13:35 +00:00
|
|
|
$this->replaceInterwiki( $text );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Replace internal links
|
2017-08-11 00:23:16 +00:00
|
|
|
* @param string &$text
|
2008-08-16 10:13:35 +00:00
|
|
|
*/
|
|
|
|
|
protected function replaceInternal( &$text ) {
|
|
|
|
|
if ( !$this->internals ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-10 23:39:12 +00:00
|
|
|
$classes = [];
|
2020-08-07 22:22:37 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
|
$linkCache = $services->getLinkCache();
|
2008-08-16 10:13:35 +00:00
|
|
|
$output = $this->parent->getOutput();
|
2016-05-13 00:37:17 +00:00
|
|
|
$linkRenderer = $this->parent->getLinkRenderer();
|
2008-08-16 10:13:35 +00:00
|
|
|
|
2024-01-22 21:27:45 +00:00
|
|
|
$dbr = $services->getConnectionProvider()->getReplicaDatabase();
|
2008-08-16 10:13:35 +00:00
|
|
|
|
2014-01-12 23:59:44 +00:00
|
|
|
# Sort by namespace
|
|
|
|
|
ksort( $this->internals );
|
|
|
|
|
|
2020-12-10 23:39:12 +00:00
|
|
|
$pagemap = [];
|
2013-10-11 21:13:40 +00:00
|
|
|
|
2014-01-12 23:59:44 +00:00
|
|
|
# Generate query
|
2020-08-07 22:22:37 +00:00
|
|
|
$linkBatchFactory = $services->getLinkBatchFactory();
|
|
|
|
|
$lb = $linkBatchFactory->newLinkBatch();
|
2016-05-13 06:27:24 +00:00
|
|
|
$lb->setCaller( __METHOD__ );
|
|
|
|
|
|
2014-01-12 23:59:44 +00:00
|
|
|
foreach ( $this->internals as $ns => $entries ) {
|
2020-06-18 14:52:09 +00:00
|
|
|
foreach ( $entries as [ 'title' => $title, 'pdbk' => $pdbk ] ) {
|
2022-12-05 20:25:30 +00:00
|
|
|
/** @var Title $title */
|
2014-01-12 23:59:44 +00:00
|
|
|
# Check if it's a static known link, e.g. interwiki
|
|
|
|
|
if ( $title->isAlwaysKnown() ) {
|
2020-12-10 23:39:12 +00:00
|
|
|
$classes[$pdbk] = '';
|
2022-09-29 14:22:59 +00:00
|
|
|
} elseif ( $ns === NS_SPECIAL ) {
|
2020-12-10 23:39:12 +00:00
|
|
|
$classes[$pdbk] = 'new';
|
2014-01-12 23:59:44 +00:00
|
|
|
} else {
|
2015-11-18 18:32:05 +00:00
|
|
|
$id = $linkCache->getGoodLinkID( $pdbk );
|
2022-09-29 14:22:59 +00:00
|
|
|
if ( $id ) {
|
2020-12-10 23:39:12 +00:00
|
|
|
$classes[$pdbk] = $linkRenderer->getLinkClasses( $title );
|
2015-11-18 18:32:05 +00:00
|
|
|
$output->addLink( $title, $id );
|
2020-12-10 23:39:12 +00:00
|
|
|
$pagemap[$id] = $pdbk;
|
2015-11-18 18:32:05 +00:00
|
|
|
} elseif ( $linkCache->isBadLink( $pdbk ) ) {
|
2020-12-10 23:39:12 +00:00
|
|
|
$classes[$pdbk] = 'new';
|
2015-11-18 18:32:05 +00:00
|
|
|
} else {
|
|
|
|
|
# Not in the link cache, add it to the query
|
2016-05-13 06:27:24 +00:00
|
|
|
$lb->addObj( $title );
|
2015-11-18 18:32:05 +00:00
|
|
|
}
|
2008-08-16 10:13:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
2014-01-12 23:59:44 +00:00
|
|
|
}
|
2016-05-13 06:27:24 +00:00
|
|
|
if ( !$lb->isEmpty() ) {
|
2022-08-19 19:49:52 +00:00
|
|
|
$res = $dbr->newSelectQueryBuilder()
|
|
|
|
|
->select( LinkCache::getSelectFields() )
|
|
|
|
|
->from( 'page' )
|
|
|
|
|
->where( [ $lb->constructSet( 'page', $dbr ) ] )
|
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
|
->fetchResultSet();
|
2014-01-12 23:59:44 +00:00
|
|
|
|
|
|
|
|
# Fetch data and form into an associative array
|
|
|
|
|
# non-existent = broken
|
|
|
|
|
foreach ( $res as $s ) {
|
|
|
|
|
$title = Title::makeTitle( $s->page_namespace, $s->page_title );
|
|
|
|
|
$pdbk = $title->getPrefixedDBkey();
|
|
|
|
|
$linkCache->addGoodLinkObjFromRow( $title, $s );
|
|
|
|
|
$output->addLink( $title, $s->page_id );
|
2020-12-10 23:39:12 +00:00
|
|
|
$classes[$pdbk] = $linkRenderer->getLinkClasses( $title );
|
2015-09-11 13:44:59 +00:00
|
|
|
// add id to the extension todolist
|
2020-12-10 23:39:12 +00:00
|
|
|
$pagemap[$s->page_id] = $pdbk;
|
2014-01-12 23:59:44 +00:00
|
|
|
}
|
|
|
|
|
unset( $res );
|
|
|
|
|
}
|
2020-12-10 23:39:12 +00:00
|
|
|
if ( $pagemap !== [] ) {
|
2015-09-11 13:44:59 +00:00
|
|
|
// pass an array of page_ids to an extension
|
2020-12-10 23:39:12 +00:00
|
|
|
$this->hookRunner->onGetLinkColours( $pagemap, $classes, $this->parent->getTitle() );
|
2008-08-16 10:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Do a second query for different language variants of links and categories
|
2020-01-23 18:39:23 +00:00
|
|
|
if ( $this->languageConverter->hasVariants() ) {
|
2020-12-10 23:39:12 +00:00
|
|
|
$this->doVariants( $classes );
|
2008-08-16 10:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Construct search and replace arrays
|
2016-02-17 09:09:32 +00:00
|
|
|
$replacePairs = [];
|
2008-08-16 10:13:35 +00:00
|
|
|
foreach ( $this->internals as $ns => $entries ) {
|
|
|
|
|
foreach ( $entries as $index => $entry ) {
|
|
|
|
|
$pdbk = $entry['pdbk'];
|
|
|
|
|
$title = $entry['title'];
|
2017-10-06 22:17:58 +00:00
|
|
|
$query = $entry['query'] ?? [];
|
2022-12-06 13:09:09 +00:00
|
|
|
$searchkey = "$ns:$index";
|
2020-06-14 19:25:55 +00:00
|
|
|
$displayTextHtml = $entry['text'];
|
2013-10-15 13:42:48 +00:00
|
|
|
if ( isset( $entry['selflink'] ) ) {
|
2024-06-18 19:43:03 +00:00
|
|
|
$replacePairs[$searchkey] = Linker::makeSelfLinkObj(
|
|
|
|
|
$title, $displayTextHtml, $query, '', '',
|
|
|
|
|
Sanitizer::escapeIdForLink( $title->getFragment() )
|
|
|
|
|
);
|
2013-10-15 13:42:48 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2022-09-29 14:22:59 +00:00
|
|
|
|
|
|
|
|
$displayText = $displayTextHtml === '' ? null : new HtmlArmor( $displayTextHtml );
|
2020-12-10 23:39:12 +00:00
|
|
|
if ( !isset( $classes[$pdbk] ) ) {
|
|
|
|
|
$classes[$pdbk] = 'new';
|
2011-03-15 16:47:55 +00:00
|
|
|
}
|
2022-09-29 14:22:59 +00:00
|
|
|
if ( $classes[$pdbk] === 'new' ) {
|
2011-03-15 16:47:55 +00:00
|
|
|
$linkCache->addBadLinkObj( $title );
|
2008-08-16 10:13:35 +00:00
|
|
|
$output->addLink( $title, 0 );
|
2016-05-13 00:37:17 +00:00
|
|
|
$link = $linkRenderer->makeBrokenLink(
|
2020-06-18 12:13:30 +00:00
|
|
|
$title, $displayText, [], $query
|
2016-05-13 00:37:17 +00:00
|
|
|
);
|
2008-08-16 10:13:35 +00:00
|
|
|
} else {
|
2016-05-13 00:37:17 +00:00
|
|
|
$link = $linkRenderer->makePreloadedLink(
|
2020-12-10 23:39:12 +00:00
|
|
|
$title, $displayText, $classes[$pdbk], [], $query
|
2016-05-13 00:37:17 +00:00
|
|
|
);
|
2008-08-16 10:13:35 +00:00
|
|
|
}
|
2016-05-13 00:37:17 +00:00
|
|
|
|
|
|
|
|
$replacePairs[$searchkey] = $link;
|
2008-08-16 10:13:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Do the thing
|
|
|
|
|
$text = preg_replace_callback(
|
2023-10-05 18:52:10 +00:00
|
|
|
'/<!--LINK\'" (-?[\d:]+)-->/',
|
2021-02-10 22:31:02 +00:00
|
|
|
static function ( array $matches ) use ( $replacePairs ) {
|
2018-07-10 15:14:29 +00:00
|
|
|
return $replacePairs[$matches[1]];
|
|
|
|
|
},
|
2013-03-24 10:01:51 +00:00
|
|
|
$text
|
|
|
|
|
);
|
2008-08-16 10:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Replace interwiki links
|
2017-08-11 00:23:16 +00:00
|
|
|
* @param string &$text
|
2008-08-16 10:13:35 +00:00
|
|
|
*/
|
|
|
|
|
protected function replaceInterwiki( &$text ) {
|
2022-09-29 14:22:59 +00:00
|
|
|
if ( !$this->interwikis ) {
|
2008-08-16 10:13:35 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Make interwiki link HTML
|
2010-04-18 00:39:12 +00:00
|
|
|
$output = $this->parent->getOutput();
|
2016-02-17 09:09:32 +00:00
|
|
|
$replacePairs = [];
|
2016-05-13 00:37:17 +00:00
|
|
|
$linkRenderer = $this->parent->getLinkRenderer();
|
2022-09-29 14:22:59 +00:00
|
|
|
foreach ( $this->interwikis as $key => [ 'title' => $title, 'text' => $linkText ] ) {
|
|
|
|
|
$replacePairs[$key] = $linkRenderer->makeLink( $title, new HtmlArmor( $linkText ) );
|
|
|
|
|
$output->addInterwikiLink( $title );
|
2008-08-16 10:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$text = preg_replace_callback(
|
2022-12-06 13:09:09 +00:00
|
|
|
'/<!--IWLINK\'" (\d+)-->/',
|
2021-02-10 22:31:02 +00:00
|
|
|
static function ( array $matches ) use ( $replacePairs ) {
|
2018-07-10 15:14:29 +00:00
|
|
|
return $replacePairs[$matches[1]];
|
|
|
|
|
},
|
|
|
|
|
$text
|
|
|
|
|
);
|
2008-08-16 10:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
2008-08-28 05:22:17 +00:00
|
|
|
/**
|
2020-12-10 23:39:12 +00:00
|
|
|
* Modify $this->internals and $classes according to language variant linking rules
|
2022-12-05 20:25:30 +00:00
|
|
|
* @param string[] &$classes
|
2008-08-28 05:22:17 +00:00
|
|
|
*/
|
2020-12-10 23:39:12 +00:00
|
|
|
protected function doVariants( &$classes ) {
|
2020-08-07 22:22:37 +00:00
|
|
|
$linkBatchFactory = MediaWikiServices::getInstance()->getLinkBatchFactory();
|
|
|
|
|
$linkBatch = $linkBatchFactory->newLinkBatch();
|
2016-02-17 09:09:32 +00:00
|
|
|
$variantMap = []; // maps $pdbkey_Variant => $keys (of link holders)
|
2008-08-28 05:22:17 +00:00
|
|
|
$output = $this->parent->getOutput();
|
2010-07-25 16:53:55 +00:00
|
|
|
$titlesToBeConverted = '';
|
2016-02-17 09:09:32 +00:00
|
|
|
$titlesAttrs = [];
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2010-07-25 16:53:55 +00:00
|
|
|
// Concatenate titles to a single string, thus we only need auto convert the
|
|
|
|
|
// single string to all variants. This would improve parser's performance
|
|
|
|
|
// significantly.
|
2008-08-28 05:22:17 +00:00
|
|
|
foreach ( $this->internals as $ns => $entries ) {
|
2022-09-29 14:22:59 +00:00
|
|
|
if ( $ns === NS_SPECIAL ) {
|
2013-10-15 13:42:48 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2020-06-18 14:52:09 +00:00
|
|
|
foreach ( $entries as $index => [ 'title' => $title, 'pdbk' => $pdbk ] ) {
|
2010-07-25 16:53:55 +00:00
|
|
|
// we only deal with new links (in its first query)
|
2020-12-10 23:39:12 +00:00
|
|
|
if ( !isset( $classes[$pdbk] ) || $classes[$pdbk] === 'new' ) {
|
2020-06-18 14:52:09 +00:00
|
|
|
$titlesAttrs[] = [ $index, $title ];
|
2010-07-25 16:53:55 +00:00
|
|
|
// separate titles with \0 because it would never appears
|
|
|
|
|
// in a valid title
|
2020-06-18 14:52:09 +00:00
|
|
|
$titlesToBeConverted .= $title->getText() . "\0";
|
2010-07-25 16:53:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-12 04:06:22 +00:00
|
|
|
|
2010-07-25 16:53:55 +00:00
|
|
|
// Now do the conversion and explode string to text of titles
|
2020-01-23 18:39:23 +00:00
|
|
|
$titlesAllVariants = $this->languageConverter->
|
2018-07-30 17:42:42 +00:00
|
|
|
autoConvertToAllVariants( rtrim( $titlesToBeConverted, "\0" ) );
|
2010-07-25 16:53:55 +00:00
|
|
|
foreach ( $titlesAllVariants as &$titlesVariant ) {
|
2010-07-26 16:09:06 +00:00
|
|
|
$titlesVariant = explode( "\0", $titlesVariant );
|
2010-07-25 16:53:55 +00:00
|
|
|
}
|
2013-10-15 13:42:48 +00:00
|
|
|
|
2010-07-25 16:53:55 +00:00
|
|
|
// Then add variants of links to link batch
|
2013-10-15 13:42:48 +00:00
|
|
|
$parentTitle = $this->parent->getTitle();
|
2020-06-18 14:52:09 +00:00
|
|
|
foreach ( $titlesAttrs as $i => [ $index, $title ] ) {
|
2014-07-01 00:40:19 +00:00
|
|
|
/** @var Title $title */
|
2013-10-15 13:42:48 +00:00
|
|
|
$ns = $title->getNamespace();
|
|
|
|
|
$text = $title->getText();
|
|
|
|
|
|
2022-09-21 19:05:03 +00:00
|
|
|
foreach ( $titlesAllVariants as $textVariants ) {
|
2020-06-18 12:13:30 +00:00
|
|
|
$textVariant = $textVariants[$i];
|
2013-10-15 13:42:48 +00:00
|
|
|
if ( $textVariant === $text ) {
|
|
|
|
|
continue;
|
2008-08-28 05:22:17 +00:00
|
|
|
}
|
2013-10-15 13:42:48 +00:00
|
|
|
|
|
|
|
|
$variantTitle = Title::makeTitle( $ns, $textVariant );
|
|
|
|
|
|
|
|
|
|
// Self-link checking for mixed/different variant titles. At this point, we
|
|
|
|
|
// already know the exact title does not exist, so the link cannot be to a
|
|
|
|
|
// variant of the current title that exists as a separate page.
|
2024-06-18 19:43:03 +00:00
|
|
|
if ( $variantTitle->equals( $parentTitle ) ) {
|
2013-10-15 13:42:48 +00:00
|
|
|
$this->internals[$ns][$index]['selflink'] = true;
|
|
|
|
|
continue 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$linkBatch->addObj( $variantTitle );
|
|
|
|
|
$variantMap[$variantTitle->getPrefixedDBkey()][] = "$ns:$index";
|
2008-08-28 05:22:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// process categories, check if a category exists in some variant
|
2016-02-17 09:09:32 +00:00
|
|
|
$categoryMap = []; // maps $category_variant => $category (dbkeys)
|
2021-10-08 14:44:52 +00:00
|
|
|
foreach ( $output->getCategoryNames() as $category ) {
|
2012-05-21 15:33:32 +00:00
|
|
|
$categoryTitle = Title::makeTitleSafe( NS_CATEGORY, $category );
|
|
|
|
|
$linkBatch->addObj( $categoryTitle );
|
2020-01-23 18:39:23 +00:00
|
|
|
$variants = $this->languageConverter->autoConvertToAllVariants( $category );
|
2012-05-21 15:33:32 +00:00
|
|
|
foreach ( $variants as $variant ) {
|
|
|
|
|
if ( $variant !== $category ) {
|
|
|
|
|
$variantTitle = Title::makeTitleSafe( NS_CATEGORY, $variant );
|
2022-09-29 14:22:59 +00:00
|
|
|
if ( $variantTitle ) {
|
|
|
|
|
$linkBatch->addObj( $variantTitle );
|
|
|
|
|
$categoryMap[$variant] = [ $category, $categoryTitle ];
|
2012-05-21 15:33:32 +00:00
|
|
|
}
|
2008-08-28 05:22:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-14 16:33:31 +00:00
|
|
|
if ( $linkBatch->isEmpty() ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-01-24 23:15:10 +00:00
|
|
|
|
2022-11-14 16:33:31 +00:00
|
|
|
// construct query
|
2024-01-22 21:27:45 +00:00
|
|
|
$dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
|
2008-08-28 05:22:17 +00:00
|
|
|
|
2022-11-14 16:33:31 +00:00
|
|
|
$varRes = $dbr->newSelectQueryBuilder()
|
|
|
|
|
->select( LinkCache::getSelectFields() )
|
|
|
|
|
->from( 'page' )
|
|
|
|
|
->where( [ $linkBatch->constructSet( 'page', $dbr ) ] )
|
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
|
->fetchResultSet();
|
2008-08-28 05:22:17 +00:00
|
|
|
|
2022-11-14 16:33:31 +00:00
|
|
|
$pagemap = [];
|
2022-09-29 14:22:59 +00:00
|
|
|
$varCategories = [];
|
|
|
|
|
$linkCache = MediaWikiServices::getInstance()->getLinkCache();
|
2022-11-14 16:33:31 +00:00
|
|
|
$linkRenderer = $this->parent->getLinkRenderer();
|
2008-08-28 05:22:17 +00:00
|
|
|
|
2022-11-14 16:33:31 +00:00
|
|
|
// for each found variants, figure out link holders and replace
|
|
|
|
|
foreach ( $varRes as $s ) {
|
|
|
|
|
$variantTitle = Title::makeTitle( $s->page_namespace, $s->page_title );
|
|
|
|
|
$varPdbk = $variantTitle->getPrefixedDBkey();
|
|
|
|
|
|
2022-09-29 14:22:59 +00:00
|
|
|
if ( !isset( $variantMap[$varPdbk] ) ) {
|
|
|
|
|
continue;
|
2022-11-14 16:33:31 +00:00
|
|
|
}
|
2008-08-28 05:22:17 +00:00
|
|
|
|
2022-09-29 14:22:59 +00:00
|
|
|
$linkCache->addGoodLinkObjFromRow( $variantTitle, $s );
|
|
|
|
|
$output->addLink( $variantTitle, $s->page_id );
|
|
|
|
|
|
2022-11-14 16:33:31 +00:00
|
|
|
// loop over link holders
|
2022-09-29 14:22:59 +00:00
|
|
|
foreach ( $variantMap[$varPdbk] as $key ) {
|
2022-11-14 16:33:31 +00:00
|
|
|
[ $ns, $index ] = explode( ':', $key, 2 );
|
2022-12-05 20:25:30 +00:00
|
|
|
$entry =& $this->internals[(int)$ns][(int)$index];
|
2008-08-28 05:22:17 +00:00
|
|
|
|
2024-06-18 19:43:03 +00:00
|
|
|
// The selflink we marked above might not have been the first
|
|
|
|
|
// $textVariants so be sure to skip any entries that have
|
|
|
|
|
// subsequently been marked.
|
|
|
|
|
if ( isset( $entry['selflink'] ) ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$pdbk = $entry['pdbk'];
|
2022-11-14 16:33:31 +00:00
|
|
|
if ( !isset( $classes[$pdbk] ) || $classes[$pdbk] === 'new' ) {
|
|
|
|
|
// found link in some of the variants, replace the link holder data
|
|
|
|
|
$entry['title'] = $variantTitle;
|
|
|
|
|
$entry['pdbk'] = $varPdbk;
|
2008-08-28 05:22:17 +00:00
|
|
|
|
2023-10-02 05:29:03 +00:00
|
|
|
// set pdbk and colour if we haven't checked this title yet.
|
|
|
|
|
if ( !isset( $classes[$varPdbk] ) ) {
|
|
|
|
|
$classes[$varPdbk] = $linkRenderer->getLinkClasses( $variantTitle );
|
|
|
|
|
$pagemap[$s->page_id] = $varPdbk;
|
|
|
|
|
}
|
2008-08-28 05:22:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-14 16:33:31 +00:00
|
|
|
// check if the object is a variant of a category
|
2022-09-29 14:22:59 +00:00
|
|
|
$vardbk = $variantTitle->getDBkey();
|
2022-11-14 16:33:31 +00:00
|
|
|
if ( isset( $categoryMap[$vardbk] ) ) {
|
|
|
|
|
[ $oldkey, $oldtitle ] = $categoryMap[$vardbk];
|
|
|
|
|
if ( !isset( $varCategories[$oldkey] ) && !$oldtitle->exists() ) {
|
|
|
|
|
$varCategories[$oldkey] = $vardbk;
|
2008-08-28 05:22:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-14 16:33:31 +00:00
|
|
|
$this->hookRunner->onGetLinkColours( $pagemap, $classes, $this->parent->getTitle() );
|
|
|
|
|
|
|
|
|
|
// rebuild the categories in original order (if there are replacements)
|
|
|
|
|
if ( $varCategories !== [] ) {
|
|
|
|
|
$newCats = [];
|
2023-09-21 17:06:50 +00:00
|
|
|
foreach ( $output->getCategoryNames() as $cat ) {
|
|
|
|
|
$sortkey = $output->getCategorySortKey( $cat );
|
2022-11-14 16:33:31 +00:00
|
|
|
// make the replacement
|
|
|
|
|
$newCats[$varCategories[$cat] ?? $cat] = $sortkey;
|
|
|
|
|
}
|
|
|
|
|
$output->setCategories( $newCats );
|
|
|
|
|
}
|
2008-08-28 05:22:17 +00:00
|
|
|
}
|
|
|
|
|
|
2008-08-16 10:13:35 +00:00
|
|
|
/**
|
2022-09-29 14:22:59 +00:00
|
|
|
* Replace <!--LINK'" …--> and <!--IWLINK'" …--> link placeholders with plain text of links
|
2008-08-16 10:13:35 +00:00
|
|
|
* (not HTML-formatted).
|
2010-06-09 14:57:59 +00:00
|
|
|
*
|
2014-04-21 23:38:39 +00:00
|
|
|
* @param string $text
|
|
|
|
|
* @return string
|
2008-08-16 10:13:35 +00:00
|
|
|
*/
|
2014-08-11 20:24:54 +00:00
|
|
|
public function replaceText( $text ) {
|
2019-03-24 20:19:54 +00:00
|
|
|
return preg_replace_callback(
|
2022-12-06 13:09:09 +00:00
|
|
|
'/<!--(IW)?LINK\'" (-?[\d:]+)-->/',
|
2019-03-24 20:19:54 +00:00
|
|
|
function ( $matches ) {
|
|
|
|
|
[ $unchanged, $isInterwiki, $key ] = $matches;
|
2008-08-16 10:13:35 +00:00
|
|
|
|
2019-03-24 20:19:54 +00:00
|
|
|
if ( !$isInterwiki ) {
|
|
|
|
|
[ $ns, $index ] = explode( ':', $key, 2 );
|
2022-12-05 20:25:30 +00:00
|
|
|
return $this->internals[(int)$ns][(int)$index]['text'] ?? $unchanged;
|
2019-03-24 20:19:54 +00:00
|
|
|
} else {
|
|
|
|
|
return $this->interwikis[$key]['text'] ?? $unchanged;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
$text
|
|
|
|
|
);
|
2008-08-16 10:13:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-10-03 18:39:06 +00:00
|
|
|
|
|
|
|
|
/** @deprecated class alias since 1.43 */
|
|
|
|
|
class_alias( LinkHolderArray::class, 'LinkHolderArray' );
|