Do not insert page titles into querycache.qc_value
[lhc/web/wiklou.git] / includes / specials / SpecialBrokenRedirects.php
1 <?php
2 /**
3 * Implements Special:Brokenredirects
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 * @ingroup SpecialPage
22 */
23
24 use MediaWiki\MediaWikiServices;
25 use Wikimedia\Rdbms\IResultWrapper;
26 use Wikimedia\Rdbms\IDatabase;
27
28 /**
29 * A special page listing redirects to non existent page. Those should be
30 * fixed to point to an existing page.
31 *
32 * @ingroup SpecialPage
33 */
34 class SpecialBrokenRedirects extends QueryPage {
35 function __construct( $name = 'BrokenRedirects' ) {
36 parent::__construct( $name );
37 }
38
39 public function isExpensive() {
40 return true;
41 }
42
43 function isSyndicated() {
44 return false;
45 }
46
47 function sortDescending() {
48 return false;
49 }
50
51 function getPageHeader() {
52 return $this->msg( 'brokenredirectstext' )->parseAsBlock();
53 }
54
55 public function getQueryInfo() {
56 $dbr = wfGetDB( DB_REPLICA );
57
58 return [
59 'tables' => [
60 'redirect',
61 'p1' => 'page',
62 'p2' => 'page',
63 ],
64 'fields' => [
65 'namespace' => 'p1.page_namespace',
66 'title' => 'p1.page_title',
67 'rd_namespace',
68 'rd_title',
69 'rd_fragment',
70 ],
71 'conds' => [
72 // Exclude pages that don't exist locally as wiki pages,
73 // but aren't "broken" either.
74 // Special pages and interwiki links
75 'rd_namespace >= 0',
76 'rd_interwiki IS NULL OR rd_interwiki = ' . $dbr->addQuotes( '' ),
77 'p2.page_namespace IS NULL',
78 ],
79 'join_conds' => [
80 'p1' => [ 'JOIN', [
81 'rd_from=p1.page_id',
82 ] ],
83 'p2' => [ 'LEFT JOIN', [
84 'rd_namespace=p2.page_namespace',
85 'rd_title=p2.page_title'
86 ] ],
87 ],
88 ];
89 }
90
91 /**
92 * @return array
93 */
94 function getOrderFields() {
95 return [ 'rd_namespace', 'rd_title', 'rd_from' ];
96 }
97
98 /**
99 * @param Skin $skin
100 * @param object $result Result row
101 * @return string
102 */
103 function formatResult( $skin, $result ) {
104 $fromObj = Title::makeTitle( $result->namespace, $result->title );
105 if ( isset( $result->rd_title ) ) {
106 $toObj = Title::makeTitle( $result->rd_namespace, $result->rd_title, $result->rd_fragment );
107 } else {
108 $blinks = $fromObj->getBrokenLinksFrom(); # TODO: check for redirect, not for links
109 if ( $blinks ) {
110 $toObj = $blinks[0];
111 } else {
112 $toObj = false;
113 }
114 }
115
116 $linkRenderer = $this->getLinkRenderer();
117 $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
118
119 // $toObj may very easily be false if the $result list is cached
120 if ( !is_object( $toObj ) ) {
121 return '<del>' . $linkRenderer->makeLink( $fromObj ) . '</del>';
122 }
123
124 $from = $linkRenderer->makeKnownLink(
125 $fromObj,
126 null,
127 [],
128 [ 'redirect' => 'no' ]
129 );
130 $links = [];
131 // if the page is editable, add an edit link
132 if (
133 // check user permissions
134 $permissionManager->userHasRight( $this->getUser(), 'edit' ) &&
135 // check, if the content model is editable through action=edit
136 ContentHandler::getForTitle( $fromObj )->supportsDirectEditing()
137 ) {
138 $links[] = $linkRenderer->makeKnownLink(
139 $fromObj,
140 $this->msg( 'brokenredirects-edit' )->text(),
141 [],
142 [ 'action' => 'edit' ]
143 );
144 }
145 $to = $linkRenderer->makeBrokenLink( $toObj, $toObj->getFullText() );
146 $arr = $this->getLanguage()->getArrow();
147
148 $out = $from . $this->msg( 'word-separator' )->escaped();
149
150 if ( $permissionManager->userHasRight( $this->getUser(), 'delete' ) ) {
151 $links[] = $linkRenderer->makeKnownLink(
152 $fromObj,
153 $this->msg( 'brokenredirects-delete' )->text(),
154 [],
155 [ 'action' => 'delete' ]
156 );
157 }
158
159 if ( $links ) {
160 $out .= $this->msg( 'parentheses' )->rawParams( $this->getLanguage()
161 ->pipeList( $links ) )->escaped();
162 }
163 $out .= " {$arr} {$to}";
164
165 return $out;
166 }
167
168 public function execute( $par ) {
169 $this->addHelpLink( 'Help:Redirects' );
170 parent::execute( $par );
171 }
172
173 /**
174 * Cache page content model for performance
175 *
176 * @param IDatabase $db
177 * @param IResultWrapper $res
178 */
179 function preprocessResults( $db, $res ) {
180 $this->executeLBFromResultWrapper( $res );
181 }
182
183 protected function getGroupName() {
184 return 'maintenance';
185 }
186 }