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