MediaWiki master
RedirectStore.php
Go to the documentation of this file.
1<?php
22namespace MediaWiki\Page;
23
24use MapCacheLRU;
29use Psr\Log\LoggerInterface;
30use RepoGroup;
32
39class RedirectStore implements RedirectLookup {
40 private IConnectionProvider $dbProvider;
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 $dbProvider,
49 PageLookup $pageLookup,
50 TitleParser $titleParser,
51 RepoGroup $repoGroup,
52 LoggerInterface $logger
53 ) {
54 $this->dbProvider = $dbProvider;
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 // Handle redirects for files included from foreign image repositories.
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->dbProvider->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
129 public function updateRedirectTarget(
130 PageIdentity $page,
131 ?LinkTarget $target,
132 ?bool $lastRevWasRedirect = null
133 ) {
134 // Always update redirects (target link might have changed)
135 // Update/Insert if we don't know if the last revision was a redirect or not
136 // Delete if changing from redirect to non-redirect
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 // Don't put a bad redirect into the database (T278367)
149 $this->procCache->set( $cacheKey, false );
150 return false;
151 }
152
153 $dbw = $this->dbProvider->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->dbProvider->getPrimaryDatabase();
190 // This is not a redirect, remove row from redirect table
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
213 public function clearCache( $page ) {
214 $this->procCache->clear( self::makeCacheKey( $page ) );
215 }
216
222 private static function makeCacheKey( $page ) {
223 return "{$page->getNamespace()}:{$page->getDBkey()}";
224 }
225
236 private function createRedirectTarget( $namespace, $title, $fragment, $interwiki ): ?LinkTarget {
237 // (T203942) We can't redirect to Media namespace because it's virtual.
238 // We don't want to modify Title objects farther down the
239 // line. So, let's fix this here by changing to File namespace.
240 if ( $namespace == NS_MEDIA ) {
241 $namespace = NS_FILE;
242 }
243
244 // mimic behaviour of self::insertRedirectEntry for fragments that didn't
245 // come from the redirect table
246 $fragment = self::truncateFragment( $fragment );
247
248 // T261347: be defensive when fetching data from the redirect table.
249 // Use Title::makeTitleSafe(), and if that returns null, ignore the
250 // row. In an ideal world, the DB would be cleaned up after a
251 // namespace change, but nobody could be bothered to do that.
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
266 private static function truncateFragment( $fragment ) {
267 return mb_strcut( $fragment, 0, 255 );
268 }
269}
const NS_FILE
Definition Defines.php:71
const NS_MEDIA
Definition Defines.php:53
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
Store key-value entries in a size-limited in-memory LRU cache.
Service for storing and retrieving page redirect information.
clearCache( $page)
Clear process-cached redirect information for a page.
updateRedirectTarget(PageIdentity $page, ?LinkTarget $target, ?bool $lastRevWasRedirect=null)
Update the redirect target for a page.
getRedirectTarget(PageIdentity $page)
Get the redirect destination.
__construct(IConnectionProvider $dbProvider, PageLookup $pageLookup, TitleParser $titleParser, RepoGroup $repoGroup, LoggerInterface $logger)
Represents the target of a wiki link.
Represents a title within MediaWiki.
Definition Title.php:78
Prioritized list of file repositories.
Definition RepoGroup.php:32
Represents the target of a wiki link.
Interface for objects (potentially) representing an editable wiki page.
getId( $wikiId=self::LOCAL)
Returns the page ID.
Service for looking up information about wiki pages.
getNamespace()
Returns the page's namespace number.
Service for resolving a wiki page redirect.
A title parser service for MediaWiki.
Provide primary and replica IDatabase connections.