Move remaining LoadBalancer classes to Rdbms
[mediawiki.git] / includes / specials / SpecialDoubleRedirects.php
blob9140bf1426b750150082a5750c740a11fd9616af
1 <?php
2 /**
3 * Implements Special:DoubleRedirects
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * A special page listing redirects to redirecting page.
26 * The software will automatically not follow double redirects, to prevent loops.
28 * @ingroup SpecialPage
30 class DoubleRedirectsPage extends QueryPage {
31 function __construct( $name = 'DoubleRedirects' ) {
32 parent::__construct( $name );
35 public function isExpensive() {
36 return true;
39 function isSyndicated() {
40 return false;
43 function sortDescending() {
44 return false;
47 function getPageHeader() {
48 return $this->msg( 'doubleredirectstext' )->parseAsBlock();
51 function reallyGetQueryInfo( $namespace = null, $title = null ) {
52 $limitToTitle = !( $namespace === null && $title === null );
53 $dbr = wfGetDB( DB_REPLICA );
54 $retval = [
55 'tables' => [
56 'ra' => 'redirect',
57 'rb' => 'redirect',
58 'pa' => 'page',
59 'pb' => 'page'
61 'fields' => [
62 'namespace' => 'pa.page_namespace',
63 'title' => 'pa.page_title',
64 'value' => 'pa.page_title',
66 'nsb' => 'pb.page_namespace',
67 'tb' => 'pb.page_title',
69 // Select fields from redirect instead of page. Because there may
70 // not actually be a page table row for this target (e.g. for interwiki redirects)
71 'nsc' => 'rb.rd_namespace',
72 'tc' => 'rb.rd_title',
73 'iwc' => 'rb.rd_interwiki',
75 'conds' => [
76 'ra.rd_from = pa.page_id',
78 // Filter out redirects where the target goes interwiki (bug 40353).
79 // This isn't an optimization, it is required for correct results,
80 // otherwise a non-double redirect like Bar -> w:Foo will show up
81 // like "Bar -> Foo -> w:Foo".
83 // Need to check both NULL and "" for some reason,
84 // apparently either can be stored for non-iw entries.
85 'ra.rd_interwiki IS NULL OR ra.rd_interwiki = ' . $dbr->addQuotes( '' ),
87 'pb.page_namespace = ra.rd_namespace',
88 'pb.page_title = ra.rd_title',
90 'rb.rd_from = pb.page_id',
94 if ( $limitToTitle ) {
95 $retval['conds']['pa.page_namespace'] = $namespace;
96 $retval['conds']['pa.page_title'] = $title;
99 return $retval;
102 public function getQueryInfo() {
103 return $this->reallyGetQueryInfo();
106 function getOrderFields() {
107 return [ 'ra.rd_namespace', 'ra.rd_title' ];
111 * @param Skin $skin
112 * @param object $result Result row
113 * @return string
115 function formatResult( $skin, $result ) {
116 $titleA = Title::makeTitle( $result->namespace, $result->title );
118 // If only titleA is in the query, it means this came from
119 // querycache (which only saves 3 columns).
120 // That does save the bulk of the query cost, but now we need to
121 // get a little more detail about each individual entry quickly
122 // using the filter of reallyGetQueryInfo.
123 if ( $result && !isset( $result->nsb ) ) {
124 $dbr = wfGetDB( DB_REPLICA );
125 $qi = $this->reallyGetQueryInfo(
126 $result->namespace,
127 $result->title
129 $res = $dbr->select(
130 $qi['tables'],
131 $qi['fields'],
132 $qi['conds'],
133 __METHOD__
136 if ( $res ) {
137 $result = $dbr->fetchObject( $res );
140 $linkRenderer = $this->getLinkRenderer();
141 if ( !$result ) {
142 return '<del>' . $linkRenderer->makeLink( $titleA, null, [], [ 'redirect' => 'no' ] ) . '</del>';
145 $titleB = Title::makeTitle( $result->nsb, $result->tb );
146 $titleC = Title::makeTitle( $result->nsc, $result->tc, '', $result->iwc );
148 $linkA = $linkRenderer->makeKnownLink(
149 $titleA,
150 null,
152 [ 'redirect' => 'no' ]
155 // if the page is editable, add an edit link
156 if (
157 // check user permissions
158 $this->getUser()->isAllowed( 'edit' ) &&
159 // check, if the content model is editable through action=edit
160 ContentHandler::getForTitle( $titleA )->supportsDirectEditing()
162 $edit = $linkRenderer->makeKnownLink(
163 $titleA,
164 $this->msg( 'parentheses', $this->msg( 'editlink' )->text() )->text(),
166 [ 'action' => 'edit' ]
168 } else {
169 $edit = '';
172 $linkB = $linkRenderer->makeKnownLink(
173 $titleB,
174 null,
176 [ 'redirect' => 'no' ]
179 $linkC = $linkRenderer->makeKnownLink( $titleC );
181 $lang = $this->getLanguage();
182 $arr = $lang->getArrow() . $lang->getDirMark();
184 return ( "{$linkA} {$edit} {$arr} {$linkB} {$arr} {$linkC}" );
188 * Cache page content model and gender distinction for performance
190 * @param IDatabase $db
191 * @param ResultWrapper $res
193 function preprocessResults( $db, $res ) {
194 if ( !$res->numRows() ) {
195 return;
198 $batch = new LinkBatch;
199 foreach ( $res as $row ) {
200 $batch->add( $row->namespace, $row->title );
201 if ( isset( $row->nsb ) ) {
202 // lazy loaded when using cached results
203 $batch->add( $row->nsb, $row->tb );
205 if ( isset( $row->iwc ) && !$row->iwc ) {
206 // lazy loaded when using cached result, not added when interwiki link
207 $batch->add( $row->nsc, $row->tc );
210 $batch->execute();
212 // Back to start for display
213 $res->seek( 0 );
216 protected function getGroupName() {
217 return 'maintenance';