2022-02-07 19:13:22 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Linker;
|
|
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
2023-09-18 13:56:39 +00:00
|
|
|
use MediaWiki\Title\TitleValue;
|
2022-02-07 19:13:22 +00:00
|
|
|
use RuntimeException;
|
|
|
|
|
use stdClass;
|
2024-07-09 13:37:44 +00:00
|
|
|
use Wikimedia\ObjectCache\BagOStuff;
|
2024-09-27 18:13:02 +00:00
|
|
|
use Wikimedia\ObjectCache\WANObjectCache;
|
2023-09-15 17:07:11 +00:00
|
|
|
use Wikimedia\Rdbms\IConnectionProvider;
|
2022-02-07 19:13:22 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Service for retrieving and storing link targets.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.38
|
|
|
|
|
*/
|
|
|
|
|
class LinkTargetStore implements LinkTargetLookup {
|
|
|
|
|
|
2023-09-15 17:07:11 +00:00
|
|
|
/** @var IConnectionProvider */
|
|
|
|
|
private $dbProvider;
|
2022-02-07 19:13:22 +00:00
|
|
|
|
|
|
|
|
/** @var BagOStuff */
|
|
|
|
|
private $localCache;
|
|
|
|
|
|
|
|
|
|
/** @var WANObjectCache */
|
|
|
|
|
private $wanObjectCache;
|
|
|
|
|
|
|
|
|
|
/** @var array<int, LinkTarget> */
|
|
|
|
|
private $byIdCache;
|
|
|
|
|
|
|
|
|
|
/** @var array<string, int> */
|
|
|
|
|
private $byTitleCache;
|
|
|
|
|
|
|
|
|
|
/**
|
2023-09-15 17:07:11 +00:00
|
|
|
* @param IConnectionProvider $dbProvider
|
2022-02-07 19:13:22 +00:00
|
|
|
* @param BagOStuff $localCache
|
|
|
|
|
* @param WANObjectCache $WanObjectCache
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(
|
2023-09-15 17:07:11 +00:00
|
|
|
IConnectionProvider $dbProvider,
|
2022-02-07 19:13:22 +00:00
|
|
|
BagOStuff $localCache,
|
|
|
|
|
WANObjectCache $WanObjectCache
|
|
|
|
|
) {
|
2023-09-15 17:07:11 +00:00
|
|
|
$this->dbProvider = $dbProvider;
|
2022-02-07 19:13:22 +00:00
|
|
|
$this->localCache = $localCache;
|
|
|
|
|
$this->wanObjectCache = $WanObjectCache;
|
|
|
|
|
$this->byIdCache = [];
|
|
|
|
|
$this->byTitleCache = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
|
|
|
|
public function newLinkTargetFromRow( stdClass $row ): LinkTarget {
|
|
|
|
|
$ltId = (int)$row->lt_id;
|
|
|
|
|
if ( $ltId === 0 ) {
|
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
|
"LinkTarget ID is 0 for {$row->lt_title} (ns: {$row->lt_namespace})"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$titlevalue = new TitleValue( (int)$row->lt_namespace, $row->lt_title );
|
|
|
|
|
$this->addToClassCache( $ltId, $titlevalue );
|
|
|
|
|
return $titlevalue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Find a link target by $id.
|
|
|
|
|
*
|
|
|
|
|
* @param int $linkTargetId
|
|
|
|
|
* @return LinkTarget|null Returns null if no link target with this $linkTargetId exists in the database.
|
|
|
|
|
*/
|
|
|
|
|
public function getLinkTargetById( int $linkTargetId ): ?LinkTarget {
|
|
|
|
|
if ( !$linkTargetId ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $this->byIdCache[$linkTargetId] ) ) {
|
|
|
|
|
return $this->byIdCache[$linkTargetId];
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 17:07:11 +00:00
|
|
|
$value = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
|
2022-02-07 19:13:22 +00:00
|
|
|
->caller( __METHOD__ )
|
|
|
|
|
->table( 'linktarget' )
|
|
|
|
|
->conds( [ 'lt_id' => $linkTargetId ] )
|
|
|
|
|
->fields( [ 'lt_id', 'lt_namespace', 'lt_title' ] )
|
|
|
|
|
->fetchRow();
|
|
|
|
|
if ( !$value ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Use local and wan cache when writing read new code.
|
|
|
|
|
$linkTarget = $this->newLinkTargetFromRow( $value );
|
|
|
|
|
$this->addToClassCache( $linkTargetId, $linkTarget );
|
|
|
|
|
return $linkTarget;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 20:13:27 +00:00
|
|
|
/**
|
|
|
|
|
* Return link target id if exists
|
|
|
|
|
*
|
|
|
|
|
* @param LinkTarget $linkTarget
|
|
|
|
|
* @return int|null linktarget ID greater then 0, null if not found
|
|
|
|
|
*/
|
|
|
|
|
public function getLinkTargetId( LinkTarget $linkTarget ): ?int {
|
|
|
|
|
// allow cache to be used, because if it is in the cache, it already has a linktarget id
|
2022-12-05 20:37:13 +00:00
|
|
|
return $this->getLinkTargetIdFromCache( $linkTarget ) ?: null;
|
2022-04-05 20:13:27 +00:00
|
|
|
}
|
|
|
|
|
|
2022-02-07 19:13:22 +00:00
|
|
|
/**
|
|
|
|
|
* Attempt to assign a link target ID to the given $linkTarget. If it is already assigned,
|
|
|
|
|
* return the existing ID.
|
|
|
|
|
*
|
|
|
|
|
* @note If called within a transaction, the returned ID might become invalid
|
|
|
|
|
* if the transaction is rolled back, so it should not be passed outside of the
|
|
|
|
|
* transaction context.
|
|
|
|
|
*
|
|
|
|
|
* @param LinkTarget $linkTarget
|
|
|
|
|
* @param IDatabase $dbw The database connection to acquire the ID from.
|
|
|
|
|
* @return int linktarget ID greater then 0
|
|
|
|
|
* @throws RuntimeException if no linktarget ID has been assigned to this $linkTarget
|
|
|
|
|
*/
|
|
|
|
|
public function acquireLinkTargetId( LinkTarget $linkTarget, IDatabase $dbw ): int {
|
|
|
|
|
// allow cache to be used, because if it is in the cache, it already has a linktarget id
|
|
|
|
|
$existingLinktargetId = $this->getLinkTargetIdFromCache( $linkTarget );
|
|
|
|
|
if ( $existingLinktargetId ) {
|
|
|
|
|
return $existingLinktargetId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Checking primary when it doesn't exist in replica is not that useful but given
|
|
|
|
|
// the fact that failed inserts waste an auto_increment id better to avoid that.
|
2023-09-19 11:06:53 +00:00
|
|
|
$linkTargetId = $this->fetchIdFromDbPrimary( $linkTarget );
|
2022-02-07 19:13:22 +00:00
|
|
|
if ( $linkTargetId ) {
|
|
|
|
|
$this->addToClassCache( $linkTargetId, $linkTarget );
|
|
|
|
|
return $linkTargetId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$ns = $linkTarget->getNamespace();
|
|
|
|
|
$title = $linkTarget->getDBkey();
|
|
|
|
|
|
2023-07-05 13:51:10 +00:00
|
|
|
$dbw->newInsertQueryBuilder()
|
In query builders, use insertInto() and deleteFrom() instead of insert() and delete()
The design principle for SelectQueryBuilder was to make the chained
builder calls look as much like SQL as possible, so that developers
could leverage their knowledge of SQL to understand what the query
builder is doing.
That's why SelectQueryBuilder::select() takes a list of fields, and by
the same principle, it makes sense for UpdateQueryBuilder::update() to
take a table. However with "insert" and "delete", the SQL designers
chose to add prepositions "into" and "from", and I think it makes sense
to follow that here.
In terms of natural language, we update a table, but we don't delete a
table, or insert a table. We delete rows from a table, or insert rows
into a table. The table is not the object of the verb.
So, add insertInto() as an alias for insert(), and add deleteFrom() as
an alias for delete(). Use the new methods in MW core callers where
PHPStorm knows the type.
Change-Id: Idb327a54a57a0fb2288ea067472c1e9727016000
2023-09-08 00:06:59 +00:00
|
|
|
->insertInto( 'linktarget' )
|
2023-07-05 13:51:10 +00:00
|
|
|
->ignore()
|
|
|
|
|
->row( [ 'lt_namespace' => $ns, 'lt_title' => $title ] )
|
|
|
|
|
->caller( __METHOD__ )->execute();
|
2022-02-07 19:13:22 +00:00
|
|
|
|
|
|
|
|
if ( $dbw->affectedRows() ) {
|
|
|
|
|
$linkTargetId = $dbw->insertId();
|
|
|
|
|
} else {
|
|
|
|
|
// Use LOCK IN SHARE MODE to bypass any MySQL REPEATABLE-READ snapshot.
|
2023-09-19 11:06:53 +00:00
|
|
|
$linkTargetId = $this->fetchIdFromDbPrimary( $linkTarget, true );
|
2022-02-07 19:13:22 +00:00
|
|
|
if ( !$linkTargetId ) {
|
|
|
|
|
throw new RuntimeException(
|
|
|
|
|
"Failed to create link target ID for " .
|
|
|
|
|
"lt_namespace={$ns} lt_title=\"{$title}\""
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$this->addToClassCache( $linkTargetId, $linkTarget );
|
|
|
|
|
|
|
|
|
|
return $linkTargetId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Find lt_id of the given $linkTarget
|
|
|
|
|
*
|
|
|
|
|
* @param LinkTarget $linkTarget
|
2023-09-19 11:06:53 +00:00
|
|
|
* @param bool $lockInShareMode
|
2022-02-07 19:13:22 +00:00
|
|
|
* @return int|null
|
|
|
|
|
*/
|
|
|
|
|
private function fetchIdFromDbPrimary(
|
|
|
|
|
LinkTarget $linkTarget,
|
2023-09-19 11:06:53 +00:00
|
|
|
bool $lockInShareMode = false
|
2022-02-07 19:13:22 +00:00
|
|
|
): ?int {
|
2023-09-19 11:06:53 +00:00
|
|
|
$queryBuilder = $this->dbProvider->getPrimaryDatabase()->newSelectQueryBuilder()
|
|
|
|
|
->select( [ 'lt_id', 'lt_namespace', 'lt_title' ] )
|
|
|
|
|
->from( 'linktarget' )
|
|
|
|
|
->where( [ 'lt_namespace' => $linkTarget->getNamespace(), 'lt_title' => $linkTarget->getDBkey() ] );
|
|
|
|
|
if ( $lockInShareMode ) {
|
|
|
|
|
$queryBuilder->lockInShareMode();
|
|
|
|
|
}
|
|
|
|
|
$row = $queryBuilder->caller( __METHOD__ )->fetchRow();
|
2022-02-07 19:13:22 +00:00
|
|
|
|
|
|
|
|
if ( !$row || !$row->lt_id ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$this->addToClassCache( (int)$row->lt_id, $linkTarget );
|
|
|
|
|
|
|
|
|
|
return (int)$row->lt_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function addToClassCache( int $id, LinkTarget $linkTarget ) {
|
|
|
|
|
$this->byIdCache[$id] = $linkTarget;
|
|
|
|
|
$this->byTitleCache[(string)$linkTarget] = $id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* @internal use by tests only
|
|
|
|
|
*/
|
|
|
|
|
public function clearClassCache() {
|
|
|
|
|
$this->byIdCache = [];
|
|
|
|
|
$this->byTitleCache = [];
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-14 08:09:11 +00:00
|
|
|
/**
|
|
|
|
|
* @param LinkTarget $linkTarget
|
|
|
|
|
* @return int|false
|
|
|
|
|
*/
|
2022-02-07 19:13:22 +00:00
|
|
|
private function getLinkTargetIdFromCache( LinkTarget $linkTarget ) {
|
|
|
|
|
$linkTargetString = (string)$linkTarget;
|
|
|
|
|
if ( isset( $this->byTitleCache[$linkTargetString] ) ) {
|
|
|
|
|
return $this->byTitleCache[$linkTargetString];
|
|
|
|
|
}
|
|
|
|
|
$fname = __METHOD__;
|
|
|
|
|
$res = $this->localCache->getWithSetCallback(
|
|
|
|
|
$this->localCache->makeKey(
|
|
|
|
|
'linktargetstore-id',
|
|
|
|
|
$linkTargetString
|
|
|
|
|
),
|
|
|
|
|
$this->localCache::TTL_HOUR,
|
|
|
|
|
function () use ( $linkTarget, $fname ) {
|
|
|
|
|
return $this->wanObjectCache->getWithSetCallback(
|
|
|
|
|
$this->wanObjectCache->makeKey(
|
|
|
|
|
'linktargetstore-id',
|
|
|
|
|
(string)$linkTarget
|
|
|
|
|
),
|
|
|
|
|
WANObjectCache::TTL_DAY,
|
|
|
|
|
function () use ( $linkTarget, $fname ) {
|
2023-09-19 11:06:53 +00:00
|
|
|
$row = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
|
|
|
|
|
->select( [ 'lt_id', 'lt_namespace', 'lt_title' ] )
|
|
|
|
|
->from( 'linktarget' )
|
|
|
|
|
->where( [
|
2022-02-07 19:13:22 +00:00
|
|
|
'lt_namespace' => $linkTarget->getNamespace(),
|
|
|
|
|
'lt_title' => $linkTarget->getDBkey()
|
2023-09-19 11:06:53 +00:00
|
|
|
] )
|
|
|
|
|
->caller( $fname )->fetchRow();
|
2022-12-05 20:37:13 +00:00
|
|
|
return $row && $row->lt_id ? (int)$row->lt_id : false;
|
2022-02-07 19:13:22 +00:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ( $res ) {
|
|
|
|
|
$this->addToClassCache( $res, $linkTarget );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|