2009-02-16 14:26:34 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2012-05-22 18:06:30 +00:00
|
|
|
* Class for fetching backlink lists, approximate backlink counts and
|
|
|
|
|
* partitions.
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*
|
2011-03-18 18:12:58 +00:00
|
|
|
* @file
|
2012-05-22 18:06:30 +00:00
|
|
|
* @author Tim Starling
|
|
|
|
|
* @copyright © 2009, Tim Starling, Domas Mituzas
|
|
|
|
|
* @copyright © 2010, Max Sem
|
|
|
|
|
* @copyright © 2011, Antoine Musso
|
2011-03-18 18:12:58 +00:00
|
|
|
*/
|
|
|
|
|
|
2021-08-03 16:01:36 +00:00
|
|
|
use MediaWiki\Cache\CacheKeyHelper;
|
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\ProtectedHookAccessorTrait;
|
2020-01-10 00:00:51 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2021-08-03 16:01:36 +00:00
|
|
|
use MediaWiki\Page\PageIdentityValue;
|
|
|
|
|
use MediaWiki\Page\PageReference;
|
2017-02-19 05:03:13 +00:00
|
|
|
use Wikimedia\Rdbms\FakeResultWrapper;
|
2017-02-10 18:09:05 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2019-06-06 19:56:18 +00:00
|
|
|
use Wikimedia\Rdbms\IResultWrapper;
|
2017-02-19 05:03:13 +00:00
|
|
|
|
2011-03-18 18:12:58 +00:00
|
|
|
/**
|
|
|
|
|
* Class for fetching backlink lists, approximate backlink counts and
|
|
|
|
|
* partitions. This is a shared cache.
|
|
|
|
|
*
|
|
|
|
|
* Instances of this class should typically be fetched with the method
|
2021-09-08 22:07:01 +00:00
|
|
|
* ::getBacklinkCache() from the BacklinkCacheFactory service.
|
2011-03-18 18:12:58 +00:00
|
|
|
*
|
|
|
|
|
* Ideally you should only get your backlinks from here when you think
|
2021-09-08 22:07:01 +00:00
|
|
|
* there is some advantage in caching them. Otherwise, it's just a waste
|
2011-03-18 18:12:58 +00:00
|
|
|
* of memory.
|
2009-02-16 14:26:34 +00:00
|
|
|
*
|
2011-03-18 18:12:58 +00:00
|
|
|
* Introduced by r47317
|
2009-02-16 14:26:34 +00:00
|
|
|
*/
|
|
|
|
|
class BacklinkCache {
|
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 ProtectedHookAccessorTrait;
|
|
|
|
|
|
2015-11-09 23:51:11 +00:00
|
|
|
/** @var BacklinkCache */
|
|
|
|
|
protected static $instance;
|
2011-03-18 18:12:58 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Multi dimensions array representing batches. Keys are:
|
|
|
|
|
* > (string) links table name
|
2013-11-09 20:33:59 +00:00
|
|
|
* > (int) batch size
|
2011-03-18 18:12:58 +00:00
|
|
|
* > 'numRows' : Number of rows for this link table
|
2016-08-13 01:10:40 +00:00
|
|
|
* > 'batches' : [ $start, $end ]
|
2011-03-18 18:12:58 +00:00
|
|
|
*
|
|
|
|
|
* @see BacklinkCache::partitionResult()
|
|
|
|
|
*
|
|
|
|
|
* Cleared with BacklinkCache::clear()
|
2015-11-03 10:56:30 +00:00
|
|
|
* @var array[]
|
2011-03-18 18:12:58 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $partitionCache = [];
|
2011-03-18 18:12:58 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Contains the whole links from a database result.
|
|
|
|
|
* This is raw data that will be partitioned in $partitionCache
|
|
|
|
|
*
|
|
|
|
|
* Initialized with BacklinkCache::getLinks()
|
|
|
|
|
* Cleared with BacklinkCache::clear()
|
2019-06-06 19:56:18 +00:00
|
|
|
* @var IResultWrapper[]
|
2011-03-18 18:12:58 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $fullResultCache = [];
|
2011-03-18 18:12:58 +00:00
|
|
|
|
2021-09-08 17:19:11 +00:00
|
|
|
/** @var WANObjectCache */
|
2018-01-10 18:02:02 +00:00
|
|
|
protected $wanCache;
|
|
|
|
|
|
2011-03-18 18:12:58 +00:00
|
|
|
/**
|
|
|
|
|
* Local copy of a database object.
|
|
|
|
|
*
|
|
|
|
|
* Accessor: BacklinkCache::getDB()
|
|
|
|
|
* Mutator : BacklinkCache::setDB()
|
|
|
|
|
* Cleared with BacklinkCache::clear()
|
|
|
|
|
*/
|
2011-03-18 18:16:38 +00:00
|
|
|
protected $db;
|
2009-02-16 14:26:34 +00:00
|
|
|
|
2011-03-18 18:12:58 +00:00
|
|
|
/**
|
2021-08-03 16:01:36 +00:00
|
|
|
* Local copy of a PageReference object
|
|
|
|
|
* @var PageReference
|
2011-03-18 18:12:58 +00:00
|
|
|
*/
|
2021-08-03 16:01:36 +00:00
|
|
|
protected $page;
|
2011-03-18 18:12:58 +00:00
|
|
|
|
2020-05-15 22:16:46 +00:00
|
|
|
private const CACHE_EXPIRY = 3600;
|
2009-02-16 14:26:34 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new BacklinkCache
|
2012-09-03 19:45:50 +00:00
|
|
|
*
|
2021-09-08 17:19:11 +00:00
|
|
|
* @param WANObjectCache $wanCache
|
2021-08-03 16:01:36 +00:00
|
|
|
* @param PageReference $page Page to create a backlink cache for
|
2009-02-16 14:26:34 +00:00
|
|
|
*/
|
2021-09-08 17:19:11 +00:00
|
|
|
public function __construct( WANObjectCache $wanCache, PageReference $page ) {
|
2021-08-03 16:01:36 +00:00
|
|
|
$this->page = $page;
|
2021-09-08 17:19:11 +00:00
|
|
|
$this->wanCache = $wanCache;
|
2009-02-16 14:26:34 +00:00
|
|
|
}
|
|
|
|
|
|
2012-09-03 19:45:50 +00:00
|
|
|
/**
|
2012-09-11 06:32:12 +00:00
|
|
|
* Create a new BacklinkCache or reuse any existing one.
|
|
|
|
|
* Currently, only one cache instance can exist; callers that
|
|
|
|
|
* need multiple backlink cache objects should keep them in scope.
|
2012-09-03 19:45:50 +00:00
|
|
|
*
|
2021-09-08 17:19:11 +00:00
|
|
|
* @deprecated since 1.37 Use BacklinkCacheFactory::getBacklinkCache() instead
|
|
|
|
|
*
|
2021-08-03 16:01:36 +00:00
|
|
|
* @param PageReference $page Page to get a backlink cache for
|
2012-09-03 19:45:50 +00:00
|
|
|
* @return BacklinkCache
|
|
|
|
|
*/
|
2021-08-03 16:01:36 +00:00
|
|
|
public static function get( PageReference $page ): self {
|
2021-09-08 17:19:11 +00:00
|
|
|
$backlinkCacheFactory = MediaWikiServices::getInstance()->getBacklinkCacheFactory();
|
|
|
|
|
|
|
|
|
|
return $backlinkCacheFactory->getBacklinkCache( $page );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @since 1.37
|
|
|
|
|
* @return PageReference
|
|
|
|
|
*/
|
|
|
|
|
public function getPage(): PageReference {
|
|
|
|
|
return $this->page;
|
2012-09-03 19:45:50 +00:00
|
|
|
}
|
|
|
|
|
|
2009-02-16 14:26:34 +00:00
|
|
|
/**
|
2018-01-10 18:02:02 +00:00
|
|
|
* Clear locally stored data and database object. Invalidate data in memcache.
|
2009-02-16 14:26:34 +00:00
|
|
|
*/
|
2011-03-18 18:16:38 +00:00
|
|
|
public function clear() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->partitionCache = [];
|
|
|
|
|
$this->fullResultCache = [];
|
2018-01-10 18:02:02 +00:00
|
|
|
$this->wanCache->touchCheckKey( $this->makeCheckKey() );
|
2019-08-29 13:19:39 +00:00
|
|
|
$this->db = null;
|
2009-02-16 14:26:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the Database object to use
|
2011-05-28 18:58:51 +00:00
|
|
|
*
|
2015-10-04 09:07:25 +00:00
|
|
|
* @param IDatabase $db
|
2009-02-16 14:26:34 +00:00
|
|
|
*/
|
|
|
|
|
public function setDB( $db ) {
|
|
|
|
|
$this->db = $db;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-18 18:12:58 +00:00
|
|
|
/**
|
2016-09-05 20:21:26 +00:00
|
|
|
* Get the replica DB connection to the database
|
2011-03-18 18:12:58 +00:00
|
|
|
* When non existing, will initialize the connection.
|
2017-02-07 04:49:57 +00:00
|
|
|
* @return IDatabase
|
2011-03-18 18:12:58 +00:00
|
|
|
*/
|
2009-02-16 14:26:34 +00:00
|
|
|
protected function getDB() {
|
2019-08-29 13:19:39 +00:00
|
|
|
if ( $this->db === null ) {
|
2016-09-05 19:55:19 +00:00
|
|
|
$this->db = wfGetDB( DB_REPLICA );
|
2009-02-16 14:26:34 +00:00
|
|
|
}
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2009-02-16 14:26:34 +00:00
|
|
|
return $this->db;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the backlinks for a given table. Cached in process memory only.
|
2013-12-03 13:59:09 +00:00
|
|
|
* @param string $table
|
|
|
|
|
* @param int|bool $startId
|
|
|
|
|
* @param int|bool $endId
|
2021-08-03 16:01:36 +00:00
|
|
|
* @param int|float $max Integer, or INF for no max
|
|
|
|
|
* @return Iterator Iterator of PageIdentity objects
|
|
|
|
|
* @since 1.37
|
|
|
|
|
*/
|
|
|
|
|
public function getLinkPages(
|
|
|
|
|
string $table, $startId = false, $endId = false, $max = INF
|
|
|
|
|
): Iterator {
|
|
|
|
|
return ( function () use ( $table, $startId, $endId, $max ): Iterator {
|
|
|
|
|
foreach ( $this->queryLinks( $table, $startId, $endId, $max ) as $row ) {
|
|
|
|
|
yield PageIdentityValue::localIdentity(
|
|
|
|
|
$row->page_id, $row->page_namespace, $row->page_title );
|
|
|
|
|
}
|
|
|
|
|
} )();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the backlinks for a given table. Cached in process memory only.
|
|
|
|
|
*
|
|
|
|
|
* @deprecated in 1.37, use getLinkPages()
|
|
|
|
|
* @param string $table
|
|
|
|
|
* @param int|bool $startId
|
|
|
|
|
* @param int|bool $endId
|
2015-08-27 09:20:54 +00:00
|
|
|
* @param int $max
|
2011-04-25 21:25:45 +00:00
|
|
|
* @return TitleArrayFromResult
|
2009-02-16 14:26:34 +00:00
|
|
|
*/
|
2013-04-04 18:51:22 +00:00
|
|
|
public function getLinks( $table, $startId = false, $endId = false, $max = INF ) {
|
|
|
|
|
return TitleArray::newFromResult( $this->queryLinks( $table, $startId, $endId, $max ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the backlinks for a given table. Cached in process memory only.
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param string $table
|
|
|
|
|
* @param int|bool $startId
|
|
|
|
|
* @param int|bool $endId
|
2015-08-27 09:20:54 +00:00
|
|
|
* @param int $max
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param string $select 'all' or 'ids'
|
2019-06-06 19:56:18 +00:00
|
|
|
* @return IResultWrapper
|
2013-04-04 18:51:22 +00:00
|
|
|
*/
|
2013-11-09 20:33:59 +00:00
|
|
|
protected function queryLinks( $table, $startId, $endId, $max, $select = 'all' ) {
|
2013-04-04 18:51:22 +00:00
|
|
|
if ( !$startId && !$endId && is_infinite( $max )
|
2013-11-17 20:36:27 +00:00
|
|
|
&& isset( $this->fullResultCache[$table] )
|
|
|
|
|
) {
|
2020-06-01 05:00:39 +00:00
|
|
|
wfDebug( __METHOD__ . ": got results from cache" );
|
2013-04-04 18:51:22 +00:00
|
|
|
$res = $this->fullResultCache[$table];
|
|
|
|
|
} else {
|
2020-06-01 05:00:39 +00:00
|
|
|
wfDebug( __METHOD__ . ": got results from DB" );
|
2020-05-07 20:17:02 +00:00
|
|
|
$fromField = $this->getPrefix( $table ) . '_from';
|
2009-02-16 14:26:34 +00:00
|
|
|
$conds = $this->getConditions( $table );
|
|
|
|
|
// Use the from field in the condition rather than the joined page_id,
|
|
|
|
|
// because databases are stupid and don't necessarily propagate indexes.
|
|
|
|
|
if ( $startId ) {
|
|
|
|
|
$conds[] = "$fromField >= " . intval( $startId );
|
|
|
|
|
}
|
|
|
|
|
if ( $endId ) {
|
|
|
|
|
$conds[] = "$fromField <= " . intval( $endId );
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
$options = [ 'ORDER BY' => $fromField ];
|
2013-04-04 18:51:22 +00:00
|
|
|
if ( is_finite( $max ) && $max > 0 ) {
|
|
|
|
|
$options['LIMIT'] = $max;
|
|
|
|
|
}
|
2010-05-30 14:48:30 +00:00
|
|
|
|
2013-11-09 20:33:59 +00:00
|
|
|
if ( $select === 'ids' ) {
|
|
|
|
|
// Just select from the backlink table and ignore the page JOIN
|
|
|
|
|
$res = $this->getDB()->select(
|
|
|
|
|
$table,
|
2020-05-07 20:17:02 +00:00
|
|
|
[ 'page_id' => $fromField ],
|
2021-02-10 22:31:02 +00:00
|
|
|
array_filter( $conds, static function ( $clause ) { // kind of janky
|
2013-11-09 20:33:59 +00:00
|
|
|
return !preg_match( '/(\b|=)page_id(\b|=)/', $clause );
|
|
|
|
|
} ),
|
|
|
|
|
__METHOD__,
|
|
|
|
|
$options
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
// Select from the backlink table and JOIN with page title information
|
|
|
|
|
$res = $this->getDB()->select(
|
2016-02-17 09:09:32 +00:00
|
|
|
[ $table, 'page' ],
|
|
|
|
|
[ 'page_namespace', 'page_title', 'page_id' ],
|
2013-11-09 20:33:59 +00:00
|
|
|
$conds,
|
|
|
|
|
__METHOD__,
|
2016-02-17 09:09:32 +00:00
|
|
|
array_merge( [ 'STRAIGHT_JOIN' ], $options )
|
2013-11-09 20:33:59 +00:00
|
|
|
);
|
|
|
|
|
}
|
2009-02-16 14:26:34 +00:00
|
|
|
|
2013-11-19 23:35:37 +00:00
|
|
|
if ( $select === 'all' && !$startId && !$endId && $res->numRows() < $max ) {
|
2013-04-04 18:51:22 +00:00
|
|
|
// The full results fit within the limit, so cache them
|
|
|
|
|
$this->fullResultCache[$table] = $res;
|
|
|
|
|
} else {
|
2020-06-01 05:00:39 +00:00
|
|
|
wfDebug( __METHOD__ . ": results from DB were uncacheable" );
|
2013-04-04 18:51:22 +00:00
|
|
|
}
|
2009-02-16 14:26:34 +00:00
|
|
|
}
|
2010-05-30 14:48:30 +00:00
|
|
|
|
2013-04-04 18:51:22 +00:00
|
|
|
return $res;
|
2009-02-16 14:26:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the field name prefix for a given table
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param string $table
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2012-02-09 21:33:27 +00:00
|
|
|
* @return null|string
|
2009-02-16 14:26:34 +00:00
|
|
|
*/
|
|
|
|
|
protected function getPrefix( $table ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
static $prefixes = [
|
2013-03-07 16:50:43 +00:00
|
|
|
'pagelinks' => 'pl',
|
|
|
|
|
'imagelinks' => 'il',
|
2009-02-16 14:26:34 +00:00
|
|
|
'categorylinks' => 'cl',
|
|
|
|
|
'templatelinks' => 'tl',
|
2013-03-07 16:50:43 +00:00
|
|
|
'redirect' => 'rd',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2010-02-14 22:07:30 +00:00
|
|
|
|
2009-02-16 14:26:34 +00:00
|
|
|
if ( isset( $prefixes[$table] ) ) {
|
|
|
|
|
return $prefixes[$table];
|
|
|
|
|
} else {
|
2011-08-13 22:42:09 +00:00
|
|
|
$prefix = null;
|
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->getHookRunner()->onBacklinkCacheGetPrefix( $table, $prefix );
|
2013-04-20 17:18:13 +00:00
|
|
|
if ( $prefix ) {
|
2011-08-13 22:42:09 +00:00
|
|
|
return $prefix;
|
|
|
|
|
} else {
|
|
|
|
|
throw new MWException( "Invalid table \"$table\" in " . __CLASS__ );
|
|
|
|
|
}
|
2009-02-16 14:26:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-03-18 18:12:58 +00:00
|
|
|
* Get the SQL condition array for selecting backlinks, with a join
|
|
|
|
|
* on the page table.
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param string $table
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2012-02-09 21:33:27 +00:00
|
|
|
* @return array|null
|
2009-02-16 14:26:34 +00:00
|
|
|
*/
|
|
|
|
|
protected function getConditions( $table ) {
|
|
|
|
|
$prefix = $this->getPrefix( $table );
|
2010-02-14 22:07:30 +00:00
|
|
|
|
2009-02-16 14:26:34 +00:00
|
|
|
switch ( $table ) {
|
|
|
|
|
case 'pagelinks':
|
|
|
|
|
case 'templatelinks':
|
2016-02-17 09:09:32 +00:00
|
|
|
$conds = [
|
2021-08-03 16:01:36 +00:00
|
|
|
"{$prefix}_namespace" => $this->page->getNamespace(),
|
|
|
|
|
"{$prefix}_title" => $this->page->getDBkey(),
|
2011-06-29 07:19:12 +00:00
|
|
|
"page_id={$prefix}_from"
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2011-06-29 07:19:12 +00:00
|
|
|
break;
|
2009-02-16 14:26:34 +00:00
|
|
|
case 'redirect':
|
2016-02-17 09:09:32 +00:00
|
|
|
$conds = [
|
2021-08-03 16:01:36 +00:00
|
|
|
"{$prefix}_namespace" => $this->page->getNamespace(),
|
|
|
|
|
"{$prefix}_title" => $this->page->getDBkey(),
|
2016-03-19 00:08:06 +00:00
|
|
|
$this->getDB()->makeList( [
|
2013-02-11 16:14:05 +00:00
|
|
|
"{$prefix}_interwiki" => '',
|
|
|
|
|
"{$prefix}_interwiki IS NULL",
|
2016-02-17 09:09:32 +00:00
|
|
|
], LIST_OR ),
|
2009-02-16 14:26:34 +00:00
|
|
|
"page_id={$prefix}_from"
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2009-02-16 14:26:34 +00:00
|
|
|
break;
|
|
|
|
|
case 'imagelinks':
|
|
|
|
|
case 'categorylinks':
|
2016-02-17 09:09:32 +00:00
|
|
|
$conds = [
|
2021-08-03 16:01:36 +00:00
|
|
|
"{$prefix}_to" => $this->page->getDBkey(),
|
2013-11-09 20:33:59 +00:00
|
|
|
"page_id={$prefix}_from"
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2009-02-16 14:26:34 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
2011-08-13 22:42:09 +00:00
|
|
|
$conds = null;
|
2021-08-03 16:01:36 +00:00
|
|
|
$this->getHookRunner()->onBacklinkCacheGetConditions( $table,
|
|
|
|
|
Title::castFromPageReference( $this->page ), $conds );
|
2013-04-20 17:18:13 +00:00
|
|
|
if ( !$conds ) {
|
2011-08-13 22:42:09 +00:00
|
|
|
throw new MWException( "Invalid table \"$table\" in " . __CLASS__ );
|
2012-09-27 19:46:22 +00:00
|
|
|
}
|
2009-02-16 14:26:34 +00:00
|
|
|
}
|
2010-05-30 14:48:30 +00:00
|
|
|
|
2009-02-16 14:26:34 +00:00
|
|
|
return $conds;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-08 22:01:40 +00:00
|
|
|
/**
|
|
|
|
|
* Check if there are any backlinks
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param string $table
|
2012-11-08 22:01:40 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function hasLinks( $table ) {
|
|
|
|
|
return ( $this->getNumLinks( $table, 1 ) > 0 );
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-16 14:26:34 +00:00
|
|
|
/**
|
|
|
|
|
* Get the approximate number of backlinks
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param string $table
|
2015-08-27 09:20:54 +00:00
|
|
|
* @param int $max Only count up to this many backlinks
|
2014-04-18 23:19:46 +00:00
|
|
|
* @return int
|
2009-02-16 14:26:34 +00:00
|
|
|
*/
|
2012-11-08 22:01:40 +00:00
|
|
|
public function getNumLinks( $table, $max = INF ) {
|
2015-10-15 02:45:03 +00:00
|
|
|
global $wgUpdateRowsPerJob;
|
2010-02-14 22:07:30 +00:00
|
|
|
|
2012-11-27 17:26:02 +00:00
|
|
|
// 1) try partition cache ...
|
2009-02-16 14:26:34 +00:00
|
|
|
if ( isset( $this->partitionCache[$table] ) ) {
|
|
|
|
|
$entry = reset( $this->partitionCache[$table] );
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2012-11-08 22:01:40 +00:00
|
|
|
return min( $max, $entry['numRows'] );
|
2009-02-16 14:26:34 +00:00
|
|
|
}
|
2010-02-14 22:07:30 +00:00
|
|
|
|
2012-11-27 17:26:02 +00:00
|
|
|
// 2) ... then try full result cache ...
|
|
|
|
|
if ( isset( $this->fullResultCache[$table] ) ) {
|
2012-11-08 22:01:40 +00:00
|
|
|
return min( $max, $this->fullResultCache[$table]->numRows() );
|
2012-11-27 17:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
2018-01-10 18:02:02 +00:00
|
|
|
$memcKey = $this->wanCache->makeKey(
|
2017-04-20 19:26:23 +00:00
|
|
|
'numbacklinks',
|
2021-08-03 16:01:36 +00:00
|
|
|
CacheKeyHelper::getKeyForPage( $this->page ),
|
2017-04-20 19:26:23 +00:00
|
|
|
$table
|
|
|
|
|
);
|
2012-11-27 17:26:02 +00:00
|
|
|
|
|
|
|
|
// 3) ... fallback to memcached ...
|
2018-01-10 18:02:02 +00:00
|
|
|
$curTTL = INF;
|
|
|
|
|
$count = $this->wanCache->get(
|
|
|
|
|
$memcKey,
|
|
|
|
|
$curTTL,
|
|
|
|
|
[
|
|
|
|
|
$this->makeCheckKey()
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
if ( $count && ( $curTTL > 0 ) ) {
|
2012-11-08 22:01:40 +00:00
|
|
|
return min( $max, $count );
|
2012-11-27 17:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4) fetch from the database ...
|
2013-11-09 20:33:59 +00:00
|
|
|
if ( is_infinite( $max ) ) { // no limit at all
|
|
|
|
|
// Use partition() since it will batch the query and skip the JOIN.
|
|
|
|
|
// Use $wgUpdateRowsPerJob just to encourage cache reuse for jobs.
|
|
|
|
|
$this->partition( $table, $wgUpdateRowsPerJob ); // updates $this->partitionCache
|
|
|
|
|
return $this->partitionCache[$table][$wgUpdateRowsPerJob]['numRows'];
|
|
|
|
|
} else { // probably some sane limit
|
|
|
|
|
// Fetch the full title info, since the caller will likely need it next
|
2021-09-03 14:39:13 +00:00
|
|
|
$count = iterator_count( $this->getLinkPages( $table, false, false, $max ) );
|
2013-11-09 20:33:59 +00:00
|
|
|
if ( $count < $max ) { // full count
|
2018-01-10 18:02:02 +00:00
|
|
|
$this->wanCache->set( $memcKey, $count, self::CACHE_EXPIRY );
|
2013-11-09 20:33:59 +00:00
|
|
|
}
|
2012-11-08 22:01:40 +00:00
|
|
|
}
|
2010-05-30 14:48:30 +00:00
|
|
|
|
2013-04-04 18:51:22 +00:00
|
|
|
return min( $max, $count );
|
2009-02-16 14:26:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Partition the backlinks into batches.
|
2011-06-26 11:48:55 +00:00
|
|
|
* Returns an array giving the start and end of each range. The first
|
2011-03-18 18:12:58 +00:00
|
|
|
* batch has a start of false, and the last batch has an end of false.
|
2009-02-16 14:26:34 +00:00
|
|
|
*
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param string $table The links table name
|
|
|
|
|
* @param int $batchSize
|
|
|
|
|
* @return array
|
2009-02-16 14:26:34 +00:00
|
|
|
*/
|
|
|
|
|
public function partition( $table, $batchSize ) {
|
2012-09-03 19:45:50 +00:00
|
|
|
// 1) try partition cache ...
|
2009-02-16 14:26:34 +00:00
|
|
|
if ( isset( $this->partitionCache[$table][$batchSize] ) ) {
|
2020-06-01 05:00:39 +00:00
|
|
|
wfDebug( __METHOD__ . ": got from partition cache" );
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2009-02-16 14:26:34 +00:00
|
|
|
return $this->partitionCache[$table][$batchSize]['batches'];
|
|
|
|
|
}
|
2010-02-14 22:07:30 +00:00
|
|
|
|
2009-02-16 14:26:34 +00:00
|
|
|
$this->partitionCache[$table][$batchSize] = false;
|
|
|
|
|
$cacheEntry =& $this->partitionCache[$table][$batchSize];
|
|
|
|
|
|
2011-06-26 11:48:55 +00:00
|
|
|
// 2) ... then try full result cache ...
|
2009-02-16 14:26:34 +00:00
|
|
|
if ( isset( $this->fullResultCache[$table] ) ) {
|
|
|
|
|
$cacheEntry = $this->partitionResult( $this->fullResultCache[$table], $batchSize );
|
2020-06-01 05:00:39 +00:00
|
|
|
wfDebug( __METHOD__ . ": got from full result cache" );
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2009-02-16 14:26:34 +00:00
|
|
|
return $cacheEntry['batches'];
|
|
|
|
|
}
|
2010-02-14 22:07:30 +00:00
|
|
|
|
2018-01-10 18:02:02 +00:00
|
|
|
$memcKey = $this->wanCache->makeKey(
|
2010-02-14 22:07:30 +00:00
|
|
|
'backlinks',
|
2021-08-03 16:01:36 +00:00
|
|
|
CacheKeyHelper::getKeyForPage( $this->page ),
|
2010-02-14 22:07:30 +00:00
|
|
|
$table,
|
|
|
|
|
$batchSize
|
|
|
|
|
);
|
2010-05-30 14:48:30 +00:00
|
|
|
|
2012-11-27 17:26:02 +00:00
|
|
|
// 3) ... fallback to memcached ...
|
2018-01-10 18:02:02 +00:00
|
|
|
$curTTL = 0;
|
|
|
|
|
$memcValue = $this->wanCache->get(
|
|
|
|
|
$memcKey,
|
|
|
|
|
$curTTL,
|
|
|
|
|
[
|
|
|
|
|
$this->makeCheckKey()
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
if ( is_array( $memcValue ) && ( $curTTL > 0 ) ) {
|
2009-02-16 14:26:34 +00:00
|
|
|
$cacheEntry = $memcValue;
|
2020-06-01 05:00:39 +00:00
|
|
|
wfDebug( __METHOD__ . ": got from memcached $memcKey" );
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2009-02-16 14:26:34 +00:00
|
|
|
return $cacheEntry['batches'];
|
|
|
|
|
}
|
2010-05-30 14:48:30 +00:00
|
|
|
|
2011-03-18 18:12:58 +00:00
|
|
|
// 4) ... finally fetch from the slow database :(
|
2016-02-17 09:09:32 +00:00
|
|
|
$cacheEntry = [ 'numRows' => 0, 'batches' => [] ]; // final result
|
2017-02-20 22:44:19 +00:00
|
|
|
// Do the selects in batches to avoid client-side OOMs (T45452).
|
2013-04-04 18:51:22 +00:00
|
|
|
// Use a LIMIT that plays well with $batchSize to keep equal sized partitions.
|
|
|
|
|
$selectSize = max( $batchSize, 200000 - ( 200000 % $batchSize ) );
|
|
|
|
|
$start = false;
|
|
|
|
|
do {
|
2013-11-09 20:33:59 +00:00
|
|
|
$res = $this->queryLinks( $table, $start, false, $selectSize, 'ids' );
|
2013-04-04 18:51:22 +00:00
|
|
|
$partitions = $this->partitionResult( $res, $batchSize, false );
|
|
|
|
|
// Merge the link count and range partitions for this chunk
|
|
|
|
|
$cacheEntry['numRows'] += $partitions['numRows'];
|
|
|
|
|
$cacheEntry['batches'] = array_merge( $cacheEntry['batches'], $partitions['batches'] );
|
|
|
|
|
if ( count( $partitions['batches'] ) ) {
|
2013-12-03 14:00:43 +00:00
|
|
|
list( , $lEnd ) = end( $partitions['batches'] );
|
2013-04-04 18:51:22 +00:00
|
|
|
$start = $lEnd + 1; // pick up after this inclusive range
|
|
|
|
|
}
|
|
|
|
|
} while ( $partitions['numRows'] >= $selectSize );
|
|
|
|
|
// Make sure the first range has start=false and the last one has end=false
|
|
|
|
|
if ( count( $cacheEntry['batches'] ) ) {
|
|
|
|
|
$cacheEntry['batches'][0][0] = false;
|
|
|
|
|
$cacheEntry['batches'][count( $cacheEntry['batches'] ) - 1][1] = false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-27 17:26:02 +00:00
|
|
|
// Save partitions to memcached
|
2018-01-10 18:02:02 +00:00
|
|
|
$this->wanCache->set( $memcKey, $cacheEntry, self::CACHE_EXPIRY );
|
2010-05-30 14:48:30 +00:00
|
|
|
|
2012-11-27 17:26:02 +00:00
|
|
|
// Save backlink count to memcached
|
2018-01-10 18:02:02 +00:00
|
|
|
$memcKey = $this->wanCache->makeKey(
|
2017-04-20 19:26:23 +00:00
|
|
|
'numbacklinks',
|
2021-08-03 16:01:36 +00:00
|
|
|
CacheKeyHelper::getKeyForPage( $this->page ),
|
2017-04-20 19:26:23 +00:00
|
|
|
$table
|
|
|
|
|
);
|
2018-01-10 18:02:02 +00:00
|
|
|
$this->wanCache->set( $memcKey, $cacheEntry['numRows'], self::CACHE_EXPIRY );
|
2012-11-27 17:26:02 +00:00
|
|
|
|
2020-06-01 05:00:39 +00:00
|
|
|
wfDebug( __METHOD__ . ": got from database" );
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2009-02-16 14:26:34 +00:00
|
|
|
return $cacheEntry['batches'];
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-14 22:07:30 +00:00
|
|
|
/**
|
2009-02-16 14:26:34 +00:00
|
|
|
* Partition a DB result with backlinks in it into batches
|
2019-06-06 19:56:18 +00:00
|
|
|
* @param IResultWrapper $res Database result
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param int $batchSize
|
|
|
|
|
* @param bool $isComplete Whether $res includes all the backlinks
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2013-04-04 18:51:22 +00:00
|
|
|
* @return array
|
2009-02-16 14:26:34 +00:00
|
|
|
*/
|
2013-04-04 18:51:22 +00:00
|
|
|
protected function partitionResult( $res, $batchSize, $isComplete = true ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$batches = [];
|
2009-02-16 14:26:34 +00:00
|
|
|
$numRows = $res->numRows();
|
|
|
|
|
$numBatches = ceil( $numRows / $batchSize );
|
2010-02-14 22:07:30 +00:00
|
|
|
|
2009-02-19 02:10:55 +00:00
|
|
|
for ( $i = 0; $i < $numBatches; $i++ ) {
|
2013-04-04 18:51:22 +00:00
|
|
|
if ( $i == 0 && $isComplete ) {
|
2009-02-19 02:10:55 +00:00
|
|
|
$start = false;
|
|
|
|
|
} else {
|
2013-04-04 18:51:22 +00:00
|
|
|
$rowNum = $i * $batchSize;
|
2009-02-19 02:10:55 +00:00
|
|
|
$res->seek( $rowNum );
|
|
|
|
|
$row = $res->fetchObject();
|
2013-04-04 18:51:22 +00:00
|
|
|
$start = (int)$row->page_id;
|
2009-02-19 02:10:55 +00:00
|
|
|
}
|
2010-02-14 22:07:30 +00:00
|
|
|
|
2013-04-04 18:51:22 +00:00
|
|
|
if ( $i == ( $numBatches - 1 ) && $isComplete ) {
|
2009-02-19 02:10:55 +00:00
|
|
|
$end = false;
|
|
|
|
|
} else {
|
2013-04-04 18:51:22 +00:00
|
|
|
$rowNum = min( $numRows - 1, ( $i + 1 ) * $batchSize - 1 );
|
2009-02-19 02:10:55 +00:00
|
|
|
$res->seek( $rowNum );
|
|
|
|
|
$row = $res->fetchObject();
|
2013-04-04 18:51:22 +00:00
|
|
|
$end = (int)$row->page_id;
|
2009-02-16 14:26:34 +00:00
|
|
|
}
|
2009-12-04 01:55:05 +00:00
|
|
|
|
|
|
|
|
# Sanity check order
|
|
|
|
|
if ( $start && $end && $start > $end ) {
|
2010-02-14 22:07:30 +00:00
|
|
|
throw new MWException( __METHOD__ . ': Internal error: query result out of order' );
|
2009-12-04 01:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$batches[] = [ $start, $end ];
|
2009-02-16 14:26:34 +00:00
|
|
|
}
|
2010-05-30 14:48:30 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ 'numRows' => $numRows, 'batches' => $batches ];
|
2009-02-16 14:26:34 +00:00
|
|
|
}
|
2015-02-12 23:03:24 +00:00
|
|
|
|
2021-08-03 16:01:36 +00:00
|
|
|
/**
|
|
|
|
|
* Get a PageIdentity iterator for cascade-protected template/file use backlinks
|
|
|
|
|
*
|
|
|
|
|
* @return Iterator Iterator of PageIdentity objects
|
|
|
|
|
* @since 1.37
|
|
|
|
|
*/
|
|
|
|
|
public function getCascadeProtectedLinkPages(): Iterator {
|
|
|
|
|
return ( function (): Iterator {
|
|
|
|
|
foreach ( $this->getCascadeProtectedLinksInternal() as $row ) {
|
|
|
|
|
yield PageIdentityValue::localIdentity(
|
|
|
|
|
$row->page_id, $row->page_namespace, $row->page_title );
|
|
|
|
|
}
|
|
|
|
|
} )();
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-12 23:03:24 +00:00
|
|
|
/**
|
|
|
|
|
* Get a Title iterator for cascade-protected template/file use backlinks
|
|
|
|
|
*
|
2021-08-03 16:01:36 +00:00
|
|
|
* @deprecated since 1.37, use getCascadeProtectedLinkPages()
|
2015-02-12 23:03:24 +00:00
|
|
|
* @return TitleArray
|
|
|
|
|
* @since 1.25
|
|
|
|
|
*/
|
|
|
|
|
public function getCascadeProtectedLinks() {
|
2021-08-03 16:01:36 +00:00
|
|
|
return TitleArray::newFromResult(
|
|
|
|
|
new FakeResultWrapper( $this->getCascadeProtectedLinksInternal() ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get an array of cascade-protected template/file use backlinks
|
|
|
|
|
*
|
|
|
|
|
* @return stdClass[]
|
|
|
|
|
*/
|
|
|
|
|
private function getCascadeProtectedLinksInternal(): array {
|
2015-02-12 23:03:24 +00:00
|
|
|
$dbr = $this->getDB();
|
|
|
|
|
|
|
|
|
|
// @todo: use UNION without breaking tests that use temp tables
|
2016-02-17 09:09:32 +00:00
|
|
|
$resSets = [];
|
2015-02-12 23:03:24 +00:00
|
|
|
$resSets[] = $dbr->select(
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'templatelinks', 'page_restrictions', 'page' ],
|
|
|
|
|
[ 'page_namespace', 'page_title', 'page_id' ],
|
|
|
|
|
[
|
2021-08-03 16:01:36 +00:00
|
|
|
'tl_namespace' => $this->page->getNamespace(),
|
|
|
|
|
'tl_title' => $this->page->getDBkey(),
|
2015-02-12 23:03:24 +00:00
|
|
|
'tl_from = pr_page',
|
|
|
|
|
'pr_cascade' => 1,
|
|
|
|
|
'page_id = tl_from'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2015-02-12 23:03:24 +00:00
|
|
|
__METHOD__,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'DISTINCT' ]
|
2015-02-12 23:03:24 +00:00
|
|
|
);
|
2021-08-03 16:01:36 +00:00
|
|
|
if ( $this->page->getNamespace() === NS_FILE ) {
|
2015-02-12 23:03:24 +00:00
|
|
|
$resSets[] = $dbr->select(
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'imagelinks', 'page_restrictions', 'page' ],
|
|
|
|
|
[ 'page_namespace', 'page_title', 'page_id' ],
|
|
|
|
|
[
|
2021-08-03 16:01:36 +00:00
|
|
|
'il_to' => $this->page->getDBkey(),
|
2015-02-12 23:03:24 +00:00
|
|
|
'il_from = pr_page',
|
|
|
|
|
'pr_cascade' => 1,
|
|
|
|
|
'page_id = il_from'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2015-02-12 23:03:24 +00:00
|
|
|
__METHOD__,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'DISTINCT' ]
|
2015-02-12 23:03:24 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Combine and de-duplicate the results
|
2016-02-17 09:09:32 +00:00
|
|
|
$mergedRes = [];
|
2015-02-12 23:03:24 +00:00
|
|
|
foreach ( $resSets as $res ) {
|
|
|
|
|
foreach ( $res as $row ) {
|
2021-08-03 16:01:36 +00:00
|
|
|
// Index by page_id to remove duplicates
|
2015-02-12 23:03:24 +00:00
|
|
|
$mergedRes[$row->page_id] = $row;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 16:01:36 +00:00
|
|
|
// Now that we've de-duplicated, throw away the keys
|
|
|
|
|
return array_values( $mergedRes );
|
2015-02-12 23:03:24 +00:00
|
|
|
}
|
2018-01-10 18:02:02 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns check key for the backlinks cache for a particular title
|
|
|
|
|
*
|
2018-10-21 11:38:39 +00:00
|
|
|
* @return string
|
2018-01-10 18:02:02 +00:00
|
|
|
*/
|
|
|
|
|
private function makeCheckKey() {
|
|
|
|
|
return $this->wanCache->makeKey(
|
|
|
|
|
'backlinks',
|
2021-08-03 16:01:36 +00:00
|
|
|
CacheKeyHelper::getKeyForPage( $this->page )
|
2018-01-10 18:02:02 +00:00
|
|
|
);
|
|
|
|
|
}
|
2009-02-16 14:26:34 +00:00
|
|
|
}
|