1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
<?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
* @author Derick Alangi
*/
namespace MediaWiki\Page;
use MapCacheLRU;
use MediaWiki\Linker\LinkTarget;
use MediaWiki\Title\Title;
use MediaWiki\Title\TitleParser;
use MediaWiki\Title\TitleValue;
use Psr\Log\LoggerInterface;
use RepoGroup;
use Wikimedia\Rdbms\IConnectionProvider;
/**
* Service for storing and retrieving page redirect information.
*
* @unstable
* @since 1.38
*/
class RedirectStore implements RedirectLookup {
private IConnectionProvider $connectionProvider;
private PageLookup $pageLookup;
private TitleParser $titleParser;
private RepoGroup $repoGroup;
private LoggerInterface $logger;
private MapCacheLRU $procCache;
public function __construct(
IConnectionProvider $connectionProvider,
PageLookup $pageLookup,
TitleParser $titleParser,
RepoGroup $repoGroup,
LoggerInterface $logger
) {
$this->connectionProvider = $connectionProvider;
$this->pageLookup = $pageLookup;
$this->titleParser = $titleParser;
$this->repoGroup = $repoGroup;
$this->logger = $logger;
$this->procCache = new MapCacheLRU( 16 );
}
public function getRedirectTarget( PageIdentity $page ): ?LinkTarget {
$cacheKey = self::makeCacheKey( $page );
$cachedValue = $this->procCache->get( $cacheKey );
if ( $cachedValue !== null ) {
return $cachedValue ?: null;
}
// Handle redirects for files included from foreign image repositories.
if ( $page->getNamespace() === NS_FILE ) {
$file = $this->repoGroup->findFile( $page );
if ( $file && !$file->isLocal() ) {
$from = $file->getRedirected();
$to = $file->getName();
if ( $from === null || $from === $to ) {
$this->procCache->set( $cacheKey, false );
return null;
}
$target = new TitleValue( NS_FILE, $to );
$this->procCache->set( $cacheKey, $target );
return $target;
}
}
$page = $this->pageLookup->getPageByReference( $page );
if ( $page === null || !$page->isRedirect() ) {
$this->procCache->set( $cacheKey, false );
return null;
}
$dbr = $this->connectionProvider->getReplicaDatabase();
$row = $dbr->newSelectQueryBuilder()
->select( [ 'rd_namespace', 'rd_title', 'rd_fragment', 'rd_interwiki' ] )
->from( 'redirect' )
->where( [ 'rd_from' => $page->getId() ] )
->caller( __METHOD__ )
->fetchRow();
if ( !$row ) {
$this->logger->info(
'Found inconsistent redirect status; probably the page was deleted after it was loaded'
);
$this->procCache->set( $cacheKey, false );
return null;
}
$target = $this->createRedirectTarget(
$row->rd_namespace,
$row->rd_title,
$row->rd_fragment,
$row->rd_interwiki
);
$this->procCache->set( $cacheKey, $target );
return $target;
}
/**
* Update the redirect target for a page.
*
* @param PageIdentity $page The page to update the redirect target for.
* @param LinkTarget|null $target The new redirect target, or `null` if this is not a redirect.
* @param bool|null $lastRevWasRedirect Whether the last revision was a redirect, or `null`
* if not known. If set, this allows eliding writes to the redirect table.
*
* @return bool `true` on success, `false` on failure.
*/
public function updateRedirectTarget(
PageIdentity $page,
?LinkTarget $target,
?bool $lastRevWasRedirect = null
) {
// Always update redirects (target link might have changed)
// Update/Insert if we don't know if the last revision was a redirect or not
// Delete if changing from redirect to non-redirect
$isRedirect = $target !== null;
$cacheKey = self::makeCacheKey( $page );
if ( !$isRedirect && $lastRevWasRedirect === false ) {
$this->procCache->set( $cacheKey, false );
return true;
}
if ( $isRedirect ) {
$rt = Title::newFromLinkTarget( $target );
if ( !$rt->isValidRedirectTarget() ) {
// Don't put a bad redirect into the database (T278367)
$this->procCache->set( $cacheKey, false );
return false;
}
$dbw = $this->connectionProvider->getPrimaryDatabase();
$dbw->startAtomic( __METHOD__ );
$truncatedFragment = self::truncateFragment( $rt->getFragment() );
$dbw->newInsertQueryBuilder()
->insertInto( 'redirect' )
->row( [
'rd_from' => $page->getId(),
'rd_namespace' => $rt->getNamespace(),
'rd_title' => $rt->getDBkey(),
'rd_fragment' => $truncatedFragment,
'rd_interwiki' => $rt->getInterwiki(),
] )
->onDuplicateKeyUpdate()
->uniqueIndexFields( [ 'rd_from' ] )
->set( [
'rd_namespace' => $rt->getNamespace(),
'rd_title' => $rt->getDBkey(),
'rd_fragment' => $truncatedFragment,
'rd_interwiki' => $rt->getInterwiki(),
] )
->caller( __METHOD__ )
->execute();
$dbw->endAtomic( __METHOD__ );
$this->procCache->set(
$cacheKey,
$this->createRedirectTarget(
$rt->getNamespace(),
$rt->getDBkey(),
$truncatedFragment,
$rt->getInterwiki()
)
);
} else {
$dbw = $this->connectionProvider->getPrimaryDatabase();
// This is not a redirect, remove row from redirect table
$dbw->newDeleteQueryBuilder()
->deleteFrom( 'redirect' )
->where( [ 'rd_from' => $page->getId() ] )
->caller( __METHOD__ )
->execute();
$this->procCache->set( $cacheKey, false );
}
if ( $page->getNamespace() === NS_FILE ) {
$this->repoGroup->getLocalRepo()->invalidateImageRedirect( $page );
}
return true;
}
/**
* Clear process-cached redirect information for a page.
*
* @param LinkTarget|PageIdentity $page The page to clear the cache for.
* @return void
*/
public function clearCache( $page ) {
$this->procCache->clear( self::makeCacheKey( $page ) );
}
/**
* Create a process cache key for the given page.
* @param LinkTarget|PageIdentity $page The page to create a cache key for.
* @return string Cache key.
*/
private static function makeCacheKey( $page ) {
return "{$page->getNamespace()}:{$page->getDBkey()}";
}
/**
* Create a LinkTarget appropriate for use as a redirect target.
*
* @param int $namespace The namespace of the article
* @param string $title Database key form
* @param string $fragment The link fragment (after the "#")
* @param string $interwiki Interwiki prefix
*
* @return LinkTarget|null `LinkTarget`, or `null` if this is not a valid redirect
*/
private function createRedirectTarget( $namespace, $title, $fragment, $interwiki ): ?LinkTarget {
// (T203942) We can't redirect to Media namespace because it's virtual.
// We don't want to modify Title objects farther down the
// line. So, let's fix this here by changing to File namespace.
if ( $namespace == NS_MEDIA ) {
$namespace = NS_FILE;
}
// mimic behaviour of self::insertRedirectEntry for fragments that didn't
// come from the redirect table
$fragment = self::truncateFragment( $fragment );
// T261347: be defensive when fetching data from the redirect table.
// Use Title::makeTitleSafe(), and if that returns null, ignore the
// row. In an ideal world, the DB would be cleaned up after a
// namespace change, but nobody could be bothered to do that.
$target = $this->titleParser->makeTitleValueSafe( $namespace, $title, $fragment, $interwiki );
if ( $target !== null && Title::newFromLinkTarget( $target )->isValidRedirectTarget() ) {
return $target;
}
return null;
}
/**
* Truncate link fragment to maximum storable value
*
* @param string $fragment The link fragment (after the "#")
* @return string
*/
private static function truncateFragment( $fragment ) {
return mb_strcut( $fragment, 0, 255 );
}
}
|