Debian Codegraph @

includes/specials/SpecialBrokenRedirects.php

raw symbols SCIP
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\Page\RedirectLookup;
26 use MediaWiki\SpecialPage\QueryPage;
27 use MediaWiki\Title\Title;
28 use Skin;
29 use Wikimedia\Rdbms\IConnectionProvider;
30 use Wikimedia\Rdbms\IDatabase;
31 use Wikimedia\Rdbms\IResultWrapper;
32
33 /**
34 * List of redirects to non-existent pages.
35 *
36 * Editors are encouraged to fix these by editing them to redirect to
37 * an existing page instead.
38 *
39 * @ingroup SpecialPage
40 */
41 class SpecialBrokenRedirects extends QueryPage {
42
43 private IContentHandlerFactory $contentHandlerFactory;
44 private RedirectLookup $redirectLookup;
45
46 /**
47 * @param IContentHandlerFactory $contentHandlerFactory
48 * @param IConnectionProvider $dbProvider
49 * @param LinkBatchFactory $linkBatchFactory
50 */
51 public function __construct(
52 IContentHandlerFactory $contentHandlerFactory,
53 IConnectionProvider $dbProvider,
54 LinkBatchFactory $linkBatchFactory,
55 RedirectLookup $redirectLookup
56 ) {
57 parent::__construct( 'BrokenRedirects' );
58 $this->contentHandlerFactory = $contentHandlerFactory;
59 $this->setDatabaseProvider( $dbProvider );
60 $this->setLinkBatchFactory( $linkBatchFactory );
61 $this->redirectLookup = $redirectLookup;
62 }
63
64 public function isExpensive() {
65 return true;
66 }
67
68 public function isSyndicated() {
69 return false;
70 }
71
72 protected function sortDescending() {
73 return false;
74 }
75
76 protected function getPageHeader() {
77 return $this->msg( 'brokenredirectstext' )->parseAsBlock();
78 }
79
80 public function getQueryInfo() {
81 $dbr = $this->getDatabaseProvider()->getReplicaDatabase();
82
83 return [
84 'tables' => [
85 'redirect',
86 'p1' => 'page',
87 'p2' => 'page',
88 ],
89 'fields' => [
90 'namespace' => 'p1.page_namespace',
91 'title' => 'p1.page_title',
92 'rd_namespace',
93 'rd_title',
94 'rd_fragment',
95 ],
96 'conds' => [
97 // Exclude pages that don't exist locally as wiki pages, but aren't "broken" either: special
98 // pages and interwiki links.
99 $dbr->expr( 'rd_namespace', '>=', 0 ),
100 'rd_interwiki' => '',
101 'p2.page_namespace' => null,
102 ],
103 'join_conds' => [
104 'p1' => [ 'JOIN', [
105 'rd_from=p1.page_id',
106 ] ],
107 'p2' => [ 'LEFT JOIN', [
108 'rd_namespace=p2.page_namespace',
109 'rd_title=p2.page_title'
110 ] ],
111 ],
112 ];
113 }
114
115 /**
116 * @return array
117 */
118 protected function getOrderFields() {
119 return [ 'rd_namespace', 'rd_title', 'rd_from' ];
120 }
121
122 /**
123 * @param Skin $skin
124 * @param \stdClass $result Result row
125 * @return string
126 */
127 public function formatResult( $skin, $result ) {
128 $fromObj = Title::makeTitle( $result->namespace, $result->title );
129 if ( isset( $result->rd_title ) ) {
130 $toObj = Title::makeTitle(
131 $result->rd_namespace,
132 $result->rd_title,
133 $result->rd_fragment
134 );
135 } else {
136 $toObj = Title::castFromLinkTarget(
137 $this->redirectLookup->getRedirectTarget( $fromObj )
138 );
139 }
140
141 $linkRenderer = $this->getLinkRenderer();
142
143 if ( !is_object( $toObj ) || $toObj->exists() ) {
144 return '<del>' . $linkRenderer->makeLink( $fromObj ) . '</del>';
145 }
146
147 $from = $linkRenderer->makeKnownLink(
148 $fromObj,
149 null,
150 [],
151 [ 'redirect' => 'no' ]
152 );
153 $links = [];
154 // if the page is editable, add an edit link
155 if (
156 // check user permissions
157 $this->getAuthority()->isAllowed( 'edit' ) &&
158 // check, if the content model is editable through action=edit
159 $this->contentHandlerFactory->getContentHandler( $fromObj->getContentModel() )
160 ->supportsDirectEditing()
161 ) {
162 $links[] = $linkRenderer->makeKnownLink(
163 $fromObj,
164 $this->msg( 'brokenredirects-edit' )->text(),
165 [],
166 [ 'action' => 'edit' ]
167 );
168 }
169 $to = $linkRenderer->makeBrokenLink( $toObj, $toObj->getFullText() );
170 $arr = $this->getLanguage()->getArrow();
171
172 $out = $from . $this->msg( 'word-separator' )->escaped();
173
174 if ( $this->getAuthority()->isAllowed( 'delete' ) ) {
175 $links[] = $linkRenderer->makeKnownLink(
176 $fromObj,
177 $this->msg( 'brokenredirects-delete' )->text(),
178 [],
179 [
180 'action' => 'delete',
181 'wpReason' => $this->msg( 'brokenredirects-delete-reason' )
182 ->inContentLanguage()
183 ->text()
184 ]
185 );
186 }
187
188 if ( $links ) {
189 $out .= $this->msg( 'parentheses' )->rawParams( $this->getLanguage()
190 ->pipeList( $links ) )->escaped();
191 }
192 $out .= " {$arr} {$to}";
193
194 return $out;
195 }
196
197 public function execute( $par ) {
198 $this->addHelpLink( 'Help:Redirects' );
199 parent::execute( $par );
200 }
201
202 /**
203 * Cache page content model for performance
204 *
205 * @param IDatabase $db
206 * @param IResultWrapper $res
207 */
208 public function preprocessResults( $db, $res ) {
209 $this->executeLBFromResultWrapper( $res );
210 }
211
212 protected function getGroupName() {
213 return 'maintenance';
214 }
215 }
216
217 /** @deprecated class alias since 1.41 */
218 class_alias( SpecialBrokenRedirects::class, 'SpecialBrokenRedirects' );