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
21 * @ingroup SpecialPage
24 use MediaWiki\MediaWikiServices
;
25 use Wikimedia\Rdbms\IResultWrapper
;
26 use Wikimedia\Rdbms\IDatabase
;
29 * A special page listing redirects to redirecting page.
30 * The software will automatically not follow double redirects, to prevent loops.
32 * @ingroup SpecialPage
34 class SpecialDoubleRedirects
extends QueryPage
{
35 function __construct( $name = 'DoubleRedirects' ) {
36 parent
::__construct( $name );
39 public function isExpensive() {
43 function isSyndicated() {
47 function sortDescending() {
51 function getPageHeader() {
52 return $this->msg( 'doubleredirectstext' )->parseAsBlock();
55 function reallyGetQueryInfo( $namespace = null, $title = null ) {
56 $limitToTitle = !( $namespace === null && $title === null );
57 $dbr = wfGetDB( DB_REPLICA
);
66 'namespace' => 'pa.page_namespace',
67 'title' => 'pa.page_title',
68 'value' => 'pa.page_title',
70 'b_namespace' => 'pb.page_namespace',
71 'b_title' => 'pb.page_title',
73 // Select fields from redirect instead of page. Because there may
74 // not actually be a page table row for this target (e.g. for interwiki redirects)
75 'c_namespace' => 'rb.rd_namespace',
76 'c_title' => 'rb.rd_title',
77 'c_fragment' => 'rb.rd_fragment',
78 'c_interwiki' => 'rb.rd_interwiki',
81 'ra.rd_from = pa.page_id',
83 // Filter out redirects where the target goes interwiki (T42353).
84 // This isn't an optimization, it is required for correct results,
85 // otherwise a non-double redirect like Bar -> w:Foo will show up
86 // like "Bar -> Foo -> w:Foo".
88 // Need to check both NULL and "" for some reason,
89 // apparently either can be stored for non-iw entries.
90 'ra.rd_interwiki IS NULL OR ra.rd_interwiki = ' . $dbr->addQuotes( '' ),
92 'pb.page_namespace = ra.rd_namespace',
93 'pb.page_title = ra.rd_title',
95 'rb.rd_from = pb.page_id',
99 if ( $limitToTitle ) {
100 $retval['conds']['pa.page_namespace'] = $namespace;
101 $retval['conds']['pa.page_title'] = $title;
107 public function getQueryInfo() {
108 return $this->reallyGetQueryInfo();
111 function getOrderFields() {
112 return [ 'ra.rd_namespace', 'ra.rd_title' ];
117 * @param object $result Result row
120 function formatResult( $skin, $result ) {
121 // If no Title B or C is in the query, it means this came from
122 // querycache (which only saves the 3 columns for title A).
123 // That does save the bulk of the query cost, but now we need to
124 // get a little more detail about each individual entry quickly
125 // using the filter of reallyGetQueryInfo.
128 if ( isset( $result->b_namespace
) ) {
131 $dbr = wfGetDB( DB_REPLICA
);
132 $qi = $this->reallyGetQueryInfo(
144 $deep = $dbr->fetchObject( $res ) ?
: false;
149 $titleA = Title
::makeTitle( $result->namespace, $result->title
);
151 $linkRenderer = $this->getLinkRenderer();
153 return '<del>' . $linkRenderer->makeLink( $titleA, null, [], [ 'redirect' => 'no' ] ) . '</del>';
156 // if the page is editable, add an edit link
158 // check user permissions
159 MediaWikiServices
::getInstance()
160 ->getPermissionManager()
161 ->userHasRight( $this->getUser(), 'edit' ) &&
162 // check, if the content model is editable through action=edit
163 ContentHandler
::getForTitle( $titleA )->supportsDirectEditing()
165 $edit = $linkRenderer->makeKnownLink(
167 $this->msg( 'parentheses', $this->msg( 'editlink' )->text() )->text(),
169 [ 'action' => 'edit' ]
175 $linkA = $linkRenderer->makeKnownLink(
179 [ 'redirect' => 'no' ]
182 $titleB = Title
::makeTitle( $deep->b_namespace
, $deep->b_title
);
183 $linkB = $linkRenderer->makeKnownLink(
187 [ 'redirect' => 'no' ]
190 $titleC = Title
::makeTitle(
196 $linkC = $linkRenderer->makeKnownLink( $titleC, $titleC->getFullText() );
198 $lang = $this->getLanguage();
199 $arr = $lang->getArrow() . $lang->getDirMark();
201 return ( "{$linkA} {$edit} {$arr} {$linkB} {$arr} {$linkC}" );
204 public function execute( $par ) {
205 $this->addHelpLink( 'Help:Redirects' );
206 parent
::execute( $par );
210 * Cache page content model and gender distinction for performance
212 * @param IDatabase $db
213 * @param IResultWrapper $res
215 function preprocessResults( $db, $res ) {
216 if ( !$res->numRows() ) {
220 $batch = new LinkBatch
;
221 foreach ( $res as $row ) {
222 $batch->add( $row->namespace, $row->title
);
223 if ( isset( $row->b_namespace
) ) {
224 // lazy loaded when using cached results
225 $batch->add( $row->b_namespace
, $row->b_title
);
227 if ( isset( $row->c_interwiki
) && !$row->c_interwiki
) {
228 // lazy loaded when using cached result, not added when interwiki link
229 $batch->add( $row->c_namespace
, $row->c_title
);
234 // Back to start for display
238 protected function getGroupName() {
239 return 'maintenance';