Debian Codegraph @

includes/specials/SpecialListRedirects.php

raw symbols SCIP
1 <?php
2 /**
3 * Copyright © 2006 Rob Church
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 namespace MediaWiki\Specials;
24
25 use MediaWiki\Cache\LinkBatchFactory;
26 use MediaWiki\Html\Html;
27 use MediaWiki\Page\RedirectLookup;
28 use MediaWiki\Page\WikiPageFactory;
29 use MediaWiki\SpecialPage\QueryPage;
30 use MediaWiki\Title\Title;
31 use Skin;
32 use stdClass;
33 use Wikimedia\Rdbms\IConnectionProvider;
34 use Wikimedia\Rdbms\IDatabase;
35 use Wikimedia\Rdbms\IResultWrapper;
36
37 /**
38 * Lists all the redirecting pages on the wiki.
39 *
40 * @ingroup SpecialPage
41 * @author Rob Church <[email protected]>
42 */
43 class SpecialListRedirects extends QueryPage {
44
45 private LinkBatchFactory $linkBatchFactory;
46 private WikiPageFactory $wikiPageFactory;
47 private RedirectLookup $redirectLookup;
48
49 /**
50 * @param LinkBatchFactory $linkBatchFactory
51 * @param IConnectionProvider $dbProvider
52 * @param WikiPageFactory $wikiPageFactory
53 * @param RedirectLookup $redirectLookup
54 */
55 public function __construct(
56 LinkBatchFactory $linkBatchFactory,
57 IConnectionProvider $dbProvider,
58 WikiPageFactory $wikiPageFactory,
59 RedirectLookup $redirectLookup
60 ) {
61 parent::__construct( 'Listredirects' );
62 $this->linkBatchFactory = $linkBatchFactory;
63 $this->setDatabaseProvider( $dbProvider );
64 $this->wikiPageFactory = $wikiPageFactory;
65 $this->redirectLookup = $redirectLookup;
66 }
67
68 public function isExpensive() {
69 return true;
70 }
71
72 public function isSyndicated() {
73 return false;
74 }
75
76 protected function sortDescending() {
77 return false;
78 }
79
80 public function getQueryInfo() {
81 return [
82 'tables' => [ 'page', 'redirect' ],
83 'fields' => [ 'namespace' => 'page_namespace',
84 'title' => 'page_title',
85 'rd_namespace',
86 'rd_title',
87 'rd_fragment',
88 'rd_interwiki',
89 ],
90 'conds' => [ 'page_is_redirect' => 1 ],
91 'join_conds' => [ 'redirect' => [
92 'LEFT JOIN', 'rd_from=page_id' ],
93 ]
94 ];
95 }
96
97 protected function getOrderFields() {
98 return [ 'page_namespace', 'page_title' ];
99 }
100
101 /**
102 * Cache page existence for performance
103 *
104 * @param IDatabase $db
105 * @param IResultWrapper $res
106 */
107 public function preprocessResults( $db, $res ) {
108 if ( !$res->numRows() ) {
109 return;
110 }
111
112 $batch = $this->linkBatchFactory->newLinkBatch();
113 foreach ( $res as $row ) {
114 $batch->add( $row->namespace, $row->title );
115 $redirTarget = $this->getRedirectTarget( $row );
116 if ( $redirTarget ) {
117 $batch->addObj( $redirTarget );
118 }
119 }
120 $batch->execute();
121
122 // Back to start for display
123 $res->seek( 0 );
124 }
125
126 /**
127 * @param stdClass $row
128 * @return Title|null
129 */
130 protected function getRedirectTarget( $row ) {
131 if ( isset( $row->rd_title ) ) {
132 return Title::makeTitle(
133 $row->rd_namespace,
134 $row->rd_title,
135 $row->rd_fragment,
136 $row->rd_interwiki
137 );
138 } else {
139 $title = Title::makeTitle( $row->namespace, $row->title );
140 if ( !$title->canExist() ) {
141 return null;
142 }
143
144 return Title::castFromLinkTarget(
145 $this->redirectLookup->getRedirectTarget( $title )
146 );
147 }
148 }
149
150 /**
151 * @param Skin $skin
152 * @param stdClass $result Result row
153 * @return string
154 */
155 public function formatResult( $skin, $result ) {
156 $linkRenderer = $this->getLinkRenderer();
157 # Make a link to the redirect itself
158 $rd_title = Title::makeTitle( $result->namespace, $result->title );
159 $rd_link = $linkRenderer->makeLink(
160 $rd_title,
161 null,
162 [],
163 [ 'redirect' => 'no' ]
164 );
165
166 # Find out where the redirect leads
167 $target = $this->getRedirectTarget( $result );
168 if ( $target ) {
169 # Make a link to the destination page
170 $lang = $this->getLanguage();
171 $arr = $lang->getArrow();
172 $rd_link = Html::rawElement( 'bdi', [ 'dir' => $lang->getDir() ], $rd_link );
173 $targetLink = $linkRenderer->makeLink( $target, $target->getFullText() );
174 $targetLink = Html::rawElement( 'bdi', [ 'dir' => $lang->getDir() ], $targetLink );
175
176 return "$rd_link $arr $targetLink";
177 } else {
178 return "<del>$rd_link</del>";
179 }
180 }
181
182 public function execute( $par ) {
183 $this->addHelpLink( 'Help:Redirects' );
184 parent::execute( $par );
185 }
186
187 protected function getGroupName() {
188 return 'pages';
189 }
190 }
191
192 /** @deprecated class alias since 1.41 */
193 class_alias( SpecialListRedirects::class, 'SpecialListRedirects' );