| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace MediaWiki\Page; |
| 23 |
|
| 24 |
use MapCacheLRU; |
| 25 |
use MediaWiki\Linker\LinkTarget; |
| 26 |
use MediaWiki\Title\Title; |
| 27 |
use MediaWiki\Title\TitleParser; |
| 28 |
use MediaWiki\Title\TitleValue; |
| 29 |
use Psr\Log\LoggerInterface; |
| 30 |
use RepoGroup; |
| 31 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class RedirectStore implements RedirectLookup { |
| 40 |
private IConnectionProvider $connectionProvider; |
| 41 |
private PageLookup $pageLookup; |
| 42 |
private TitleParser $titleParser; |
| 43 |
private RepoGroup $repoGroup; |
| 44 |
private LoggerInterface $logger; |
| 45 |
private MapCacheLRU $procCache; |
| 46 |
|
| 47 |
public function __construct( |
| 48 |
IConnectionProvider $connectionProvider, |
| 49 |
PageLookup $pageLookup, |
| 50 |
TitleParser $titleParser, |
| 51 |
RepoGroup $repoGroup, |
| 52 |
LoggerInterface $logger |
| 53 |
) { |
| 54 |
$this->connectionProvider = $connectionProvider; |
| 55 |
$this->pageLookup = $pageLookup; |
| 56 |
$this->titleParser = $titleParser; |
| 57 |
$this->repoGroup = $repoGroup; |
| 58 |
$this->logger = $logger; |
| 59 |
$this->procCache = new MapCacheLRU( 16 ); |
| 60 |
} |
| 61 |
|
| 62 |
public function getRedirectTarget( PageIdentity $page ): ?LinkTarget { |
| 63 |
$cacheKey = self::makeCacheKey( $page ); |
| 64 |
$cachedValue = $this->procCache->get( $cacheKey ); |
| 65 |
if ( $cachedValue !== null ) { |
| 66 |
return $cachedValue ?: null; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
if ( $page->getNamespace() === NS_FILE ) { |
| 71 |
$file = $this->repoGroup->findFile( $page ); |
| 72 |
if ( $file && !$file->isLocal() ) { |
| 73 |
$from = $file->getRedirected(); |
| 74 |
$to = $file->getName(); |
| 75 |
if ( $from === null || $from === $to ) { |
| 76 |
$this->procCache->set( $cacheKey, false ); |
| 77 |
return null; |
| 78 |
} |
| 79 |
|
| 80 |
$target = new TitleValue( NS_FILE, $to ); |
| 81 |
$this->procCache->set( $cacheKey, $target ); |
| 82 |
return $target; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
$page = $this->pageLookup->getPageByReference( $page ); |
| 87 |
if ( $page === null || !$page->isRedirect() ) { |
| 88 |
$this->procCache->set( $cacheKey, false ); |
| 89 |
return null; |
| 90 |
} |
| 91 |
|
| 92 |
$dbr = $this->connectionProvider->getReplicaDatabase(); |
| 93 |
$row = $dbr->newSelectQueryBuilder() |
| 94 |
->select( [ 'rd_namespace', 'rd_title', 'rd_fragment', 'rd_interwiki' ] ) |
| 95 |
->from( 'redirect' ) |
| 96 |
->where( [ 'rd_from' => $page->getId() ] ) |
| 97 |
->caller( __METHOD__ ) |
| 98 |
->fetchRow(); |
| 99 |
|
| 100 |
if ( !$row ) { |
| 101 |
$this->logger->info( |
| 102 |
'Found inconsistent redirect status; probably the page was deleted after it was loaded' |
| 103 |
); |
| 104 |
$this->procCache->set( $cacheKey, false ); |
| 105 |
return null; |
| 106 |
} |
| 107 |
|
| 108 |
$target = $this->createRedirectTarget( |
| 109 |
$row->rd_namespace, |
| 110 |
$row->rd_title, |
| 111 |
$row->rd_fragment, |
| 112 |
$row->rd_interwiki |
| 113 |
); |
| 114 |
|
| 115 |
$this->procCache->set( $cacheKey, $target ); |
| 116 |
return $target; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public function updateRedirectTarget( |
| 130 |
PageIdentity $page, |
| 131 |
?LinkTarget $target, |
| 132 |
?bool $lastRevWasRedirect = null |
| 133 |
) { |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
$isRedirect = $target !== null; |
| 138 |
$cacheKey = self::makeCacheKey( $page ); |
| 139 |
|
| 140 |
if ( !$isRedirect && $lastRevWasRedirect === false ) { |
| 141 |
$this->procCache->set( $cacheKey, false ); |
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
if ( $isRedirect ) { |
| 146 |
$rt = Title::newFromLinkTarget( $target ); |
| 147 |
if ( !$rt->isValidRedirectTarget() ) { |
| 148 |
|
| 149 |
$this->procCache->set( $cacheKey, false ); |
| 150 |
return false; |
| 151 |
} |
| 152 |
|
| 153 |
$dbw = $this->connectionProvider->getPrimaryDatabase(); |
| 154 |
$dbw->startAtomic( __METHOD__ ); |
| 155 |
|
| 156 |
$truncatedFragment = self::truncateFragment( $rt->getFragment() ); |
| 157 |
$dbw->newInsertQueryBuilder() |
| 158 |
->insertInto( 'redirect' ) |
| 159 |
->row( [ |
| 160 |
'rd_from' => $page->getId(), |
| 161 |
'rd_namespace' => $rt->getNamespace(), |
| 162 |
'rd_title' => $rt->getDBkey(), |
| 163 |
'rd_fragment' => $truncatedFragment, |
| 164 |
'rd_interwiki' => $rt->getInterwiki(), |
| 165 |
] ) |
| 166 |
->onDuplicateKeyUpdate() |
| 167 |
->uniqueIndexFields( [ 'rd_from' ] ) |
| 168 |
->set( [ |
| 169 |
'rd_namespace' => $rt->getNamespace(), |
| 170 |
'rd_title' => $rt->getDBkey(), |
| 171 |
'rd_fragment' => $truncatedFragment, |
| 172 |
'rd_interwiki' => $rt->getInterwiki(), |
| 173 |
] ) |
| 174 |
->caller( __METHOD__ ) |
| 175 |
->execute(); |
| 176 |
|
| 177 |
$dbw->endAtomic( __METHOD__ ); |
| 178 |
|
| 179 |
$this->procCache->set( |
| 180 |
$cacheKey, |
| 181 |
$this->createRedirectTarget( |
| 182 |
$rt->getNamespace(), |
| 183 |
$rt->getDBkey(), |
| 184 |
$truncatedFragment, |
| 185 |
$rt->getInterwiki() |
| 186 |
) |
| 187 |
); |
| 188 |
} else { |
| 189 |
$dbw = $this->connectionProvider->getPrimaryDatabase(); |
| 190 |
|
| 191 |
$dbw->newDeleteQueryBuilder() |
| 192 |
->deleteFrom( 'redirect' ) |
| 193 |
->where( [ 'rd_from' => $page->getId() ] ) |
| 194 |
->caller( __METHOD__ ) |
| 195 |
->execute(); |
| 196 |
|
| 197 |
$this->procCache->set( $cacheKey, false ); |
| 198 |
} |
| 199 |
|
| 200 |
if ( $page->getNamespace() === NS_FILE ) { |
| 201 |
$this->repoGroup->getLocalRepo()->invalidateImageRedirect( $page ); |
| 202 |
} |
| 203 |
|
| 204 |
return true; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public function clearCache( $page ) { |
| 214 |
$this->procCache->clear( self::makeCacheKey( $page ) ); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
private static function makeCacheKey( $page ) { |
| 223 |
return "{$page->getNamespace()}:{$page->getDBkey()}"; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
private function createRedirectTarget( $namespace, $title, $fragment, $interwiki ): ?LinkTarget { |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
if ( $namespace == NS_MEDIA ) { |
| 241 |
$namespace = NS_FILE; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
$fragment = self::truncateFragment( $fragment ); |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
$target = $this->titleParser->makeTitleValueSafe( $namespace, $title, $fragment, $interwiki ); |
| 253 |
if ( $target !== null && Title::newFromLinkTarget( $target )->isValidRedirectTarget() ) { |
| 254 |
return $target; |
| 255 |
} |
| 256 |
|
| 257 |
return null; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
private static function truncateFragment( $fragment ) { |
| 267 |
return mb_strcut( $fragment, 0, 255 ); |
| 268 |
} |
| 269 |
} |