MediaWiki master
SpecialDoubleRedirects.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
29use stdClass;
33
43
44 private IContentHandlerFactory $contentHandlerFactory;
45 private LinkBatchFactory $linkBatchFactory;
46
47 public function __construct(
48 IContentHandlerFactory $contentHandlerFactory,
49 LinkBatchFactory $linkBatchFactory,
50 IConnectionProvider $dbProvider
51 ) {
52 parent::__construct( 'DoubleRedirects' );
53 $this->contentHandlerFactory = $contentHandlerFactory;
54 $this->linkBatchFactory = $linkBatchFactory;
55 $this->setDatabaseProvider( $dbProvider );
56 }
57
58 public function isExpensive() {
59 return true;
60 }
61
62 public function isSyndicated() {
63 return false;
64 }
65
66 protected function sortDescending() {
67 return false;
68 }
69
70 protected function getPageHeader() {
71 return $this->msg( 'doubleredirectstext' )->parseAsBlock();
72 }
73
74 private function reallyGetQueryInfo( ?int $namespace = null, ?string $title = null ): array {
75 $limitToTitle = !( $namespace === null && $title === null );
76 $retval = [
77 'tables' => [
78 'ra' => 'redirect',
79 'rb' => 'redirect',
80 'pa' => 'page',
81 'pb' => 'page'
82 ],
83 'fields' => [
84 'namespace' => 'pa.page_namespace',
85 'title' => 'pa.page_title',
86
87 'b_namespace' => 'pb.page_namespace',
88 'b_title' => 'pb.page_title',
89 'b_fragment' => 'ra.rd_fragment',
90
91 // Select fields from redirect instead of page. Because there may
92 // not actually be a page table row for this target (e.g. for interwiki redirects)
93 'c_namespace' => 'rb.rd_namespace',
94 'c_title' => 'rb.rd_title',
95 'c_fragment' => 'rb.rd_fragment',
96 'c_interwiki' => 'rb.rd_interwiki',
97 ],
98 'conds' => [
99 'ra.rd_from = pa.page_id',
100
101 // Filter out redirects where the target goes interwiki (T42353).
102 // This isn't an optimization, it is required for correct results,
103 // otherwise a non-double redirect like Bar -> w:Foo will show up
104 // like "Bar -> Foo -> w:Foo".
105 'ra.rd_interwiki' => '',
106
107 'pb.page_namespace = ra.rd_namespace',
108 'pb.page_title = ra.rd_title',
109
110 'rb.rd_from = pb.page_id',
111 ]
112 ];
113
114 if ( $limitToTitle ) {
115 $retval['conds']['pa.page_namespace'] = $namespace;
116 $retval['conds']['pa.page_title'] = $title;
117 }
118
119 return $retval;
120 }
121
122 public function getQueryInfo() {
123 return $this->reallyGetQueryInfo();
124 }
125
126 protected function getOrderFields() {
127 return [ 'ra.rd_namespace', 'ra.rd_title' ];
128 }
129
135 public function formatResult( $skin, $result ) {
136 // If no Title B or C is in the query, it means this came from
137 // querycache (which only saves the 3 columns for title A).
138 // That does save the bulk of the query cost, but now we need to
139 // get a little more detail about each individual entry quickly
140 // using the filter of reallyGetQueryInfo.
141 $deep = false;
142 if ( $result ) {
143 if ( isset( $result->b_namespace ) ) {
144 $deep = $result;
145 } else {
146 $qi = $this->reallyGetQueryInfo(
147 $result->namespace,
148 $result->title
149 );
150 $deep = $this->getDatabaseProvider()->getReplicaDatabase()->newSelectQueryBuilder()
151 ->queryInfo( $qi )
152 ->caller( __METHOD__ )
153 ->fetchRow();
154 }
155 }
156
157 $titleA = Title::makeTitle( $result->namespace, $result->title );
158
159 $linkRenderer = $this->getLinkRenderer();
160 if ( !$deep ) {
161 return '<del>' . $linkRenderer->makeLink( $titleA, null, [], [ 'redirect' => 'no' ] ) . '</del>';
162 }
163
164 // if the page is editable, add an edit link
165 if (
166 // check user permissions
167 $this->getAuthority()->isAllowed( 'edit' ) &&
168 // check, if the content model is editable through action=edit
169 $this->contentHandlerFactory->getContentHandler( $titleA->getContentModel() )
170 ->supportsDirectEditing()
171 ) {
172 $edit = $linkRenderer->makeKnownLink(
173 $titleA,
174 $this->msg( 'parentheses', $this->msg( 'editlink' )->text() )->text(),
175 [],
176 [ 'action' => 'edit' ]
177 );
178 } else {
179 $edit = '';
180 }
181
182 $arrow = $this->getLanguage()->getArrow();
183 $contentLanguage = $this->getContentLanguage();
184 $bdiAttrs = [
185 'dir' => $contentLanguage->getDir(),
186 'lang' => $contentLanguage->getHtmlCode(),
187 ];
188 $linkA = Html::rawElement( 'bdi', $bdiAttrs, $linkRenderer->makeKnownLink(
189 $titleA,
190 null,
191 [],
192 [ 'redirect' => 'no' ]
193 ) );
194
195 $titleB = Title::makeTitle( $deep->b_namespace, $deep->b_title );
196 // We show fragment, but don't link to it, as it probably doesn't exist anymore.
197 $titleBFrag = Title::makeTitle( $deep->b_namespace, $deep->b_title, $deep->b_fragment );
198 $linkB = Html::rawElement( 'bdi', $bdiAttrs, $linkRenderer->makeKnownLink(
199 $titleB,
200 $titleBFrag->getFullText(),
201 [],
202 [ 'redirect' => 'no' ]
203 ) );
204
205 $titleC = Title::makeTitle(
206 $deep->c_namespace,
207 $deep->c_title,
208 $deep->c_fragment,
209 $deep->c_interwiki
210 );
211 $linkC = Html::rawElement( 'bdi', $bdiAttrs,
212 $linkRenderer->makeKnownLink( $titleC, $titleC->getFullText() )
213 );
214
215 return ( "{$linkA} {$edit} {$arrow} {$linkB} {$arrow} {$linkC}" );
216 }
217
218 public function execute( $par ) {
219 $this->addHelpLink( 'Help:Redirects' );
220 parent::execute( $par );
221 }
222
229 public function preprocessResults( $db, $res ) {
230 if ( !$res->numRows() ) {
231 return;
232 }
233
234 $batch = $this->linkBatchFactory->newLinkBatch();
235 foreach ( $res as $row ) {
236 $batch->add( $row->namespace, $row->title );
237 if ( isset( $row->b_namespace ) ) {
238 // lazy loaded when using cached results
239 $batch->add( $row->b_namespace, $row->b_title );
240 }
241 if ( isset( $row->c_interwiki ) && !$row->c_interwiki ) {
242 // lazy loaded when using cached result, not added when interwiki link
243 $batch->add( $row->c_namespace, $row->c_title );
244 }
245 }
246 $batch->execute();
247
248 // Back to start for display
249 $res->seek( 0 );
250 }
251
252 protected function getGroupName() {
253 return 'maintenance';
254 }
255}
256
258class_alias( SpecialDoubleRedirects::class, 'SpecialDoubleRedirects' );
This class is a collection of static functions that serve two purposes:
Definition Html.php:57
The base class for all skins.
Definition Skin.php:58
This is a class for doing query pages; since they're almost all the same, we factor out some of the f...
Definition QueryPage.php:87
setDatabaseProvider(IConnectionProvider $databaseProvider)
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
List of redirects to another redirecting page.
getQueryInfo()
Subclasses return an SQL query here, formatted as an array with the following keys: tables => Table(s...
getPageHeader()
The content returned by this function will be output before any result.
execute( $par)
This is the actual workhorse.
isSyndicated()
Sometimes we don't want to build rss / atom feeds.
__construct(IContentHandlerFactory $contentHandlerFactory, LinkBatchFactory $linkBatchFactory, IConnectionProvider $dbProvider)
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
getOrderFields()
Subclasses return an array of fields to order by here.
preprocessResults( $db, $res)
Cache page content model and gender distinction for performance.
sortDescending()
Override to sort by increasing values.
isExpensive()
Should this query page only be updated offline on large wikis?
Represents a title within MediaWiki.
Definition Title.php:78
Provide primary and replica IDatabase connections.
Interface to a relational database.
Definition IDatabase.php:45
Result wrapper for grabbing data queried from an IDatabase object.