Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 112 |
|
0.00% |
0 / 12 |
CRAP | |
0.00% |
0 / 1 |
SpecialDoubleRedirects | |
0.00% |
0 / 111 |
|
0.00% |
0 / 12 |
600 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
isExpensive | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isSyndicated | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
sortDescending | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getPageHeader | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
reallyGetQueryInfo | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
12 | |||
getQueryInfo | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getOrderFields | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
formatResult | |
0.00% |
0 / 56 |
|
0.00% |
0 / 1 |
42 | |||
execute | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
preprocessResults | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
42 | |||
getGroupName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | namespace MediaWiki\Specials; |
22 | |
23 | use MediaWiki\Cache\LinkBatchFactory; |
24 | use MediaWiki\Content\IContentHandlerFactory; |
25 | use MediaWiki\Html\Html; |
26 | use MediaWiki\SpecialPage\QueryPage; |
27 | use MediaWiki\Title\Title; |
28 | use Skin; |
29 | use stdClass; |
30 | use Wikimedia\Rdbms\IConnectionProvider; |
31 | use Wikimedia\Rdbms\IDatabase; |
32 | use Wikimedia\Rdbms\IResultWrapper; |
33 | |
34 | /** |
35 | * List of redirects to another redirecting page. |
36 | * |
37 | * The software will by default not follow double redirects, to prevent loops. |
38 | * Editors are encouraged to fix these, and can discover them via this page. |
39 | * |
40 | * @ingroup SpecialPage |
41 | */ |
42 | class SpecialDoubleRedirects extends QueryPage { |
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( $namespace = null, $title = null ) { |
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 | |
130 | /** |
131 | * @param Skin $skin |
132 | * @param stdClass $result Result row |
133 | * @return string |
134 | */ |
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 | |
223 | /** |
224 | * Cache page content model and gender distinction for performance |
225 | * |
226 | * @param IDatabase $db |
227 | * @param IResultWrapper $res |
228 | */ |
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 | |
257 | /** @deprecated class alias since 1.41 */ |
258 | class_alias( SpecialDoubleRedirects::class, 'SpecialDoubleRedirects' ); |