e73c87176fc84a8c72224938810912331adca1af
[lhc/web/wiklou.git] / includes / specials / SpecialListRedirects.php
1 <?php
2 /**
3 * Implements Special:Listredirects
4 *
5 * Copyright © 2006 Rob Church
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup SpecialPage
24 * @author Rob Church <robchur@gmail.com>
25 */
26
27 use Wikimedia\Rdbms\IResultWrapper;
28 use Wikimedia\Rdbms\IDatabase;
29
30 /**
31 * Special:Listredirects - Lists all the redirects on the wiki.
32 * @ingroup SpecialPage
33 */
34 class SpecialListRedirects extends QueryPage {
35 function __construct( $name = 'Listredirects' ) {
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 public function getQueryInfo() {
52 return [
53 'tables' => [ 'p1' => 'page', 'redirect', 'p2' => 'page' ],
54 'fields' => [ 'namespace' => 'p1.page_namespace',
55 'title' => 'p1.page_title',
56 'rd_namespace',
57 'rd_title',
58 'rd_fragment',
59 'rd_interwiki',
60 'redirid' => 'p2.page_id' ],
61 'conds' => [ 'p1.page_is_redirect' => 1 ],
62 'join_conds' => [ 'redirect' => [
63 'LEFT JOIN', 'rd_from=p1.page_id' ],
64 'p2' => [ 'LEFT JOIN', [
65 'p2.page_namespace=rd_namespace',
66 'p2.page_title=rd_title' ] ] ]
67 ];
68 }
69
70 function getOrderFields() {
71 return [ 'p1.page_namespace', 'p1.page_title' ];
72 }
73
74 /**
75 * Cache page existence for performance
76 *
77 * @param IDatabase $db
78 * @param IResultWrapper $res
79 */
80 function preprocessResults( $db, $res ) {
81 if ( !$res->numRows() ) {
82 return;
83 }
84
85 $batch = new LinkBatch;
86 foreach ( $res as $row ) {
87 $batch->add( $row->namespace, $row->title );
88 $redirTarget = $this->getRedirectTarget( $row );
89 if ( $redirTarget ) {
90 $batch->addObj( $redirTarget );
91 }
92 }
93 $batch->execute();
94
95 // Back to start for display
96 $res->seek( 0 );
97 }
98
99 /**
100 * @param stdClass $row
101 * @return Title|null
102 */
103 protected function getRedirectTarget( $row ) {
104 if ( isset( $row->rd_title ) ) {
105 return Title::makeTitle( $row->rd_namespace,
106 $row->rd_title, $row->rd_fragment,
107 $row->rd_interwiki
108 );
109 } else {
110 $title = Title::makeTitle( $row->namespace, $row->title );
111 $article = WikiPage::factory( $title );
112
113 return $article->getRedirectTarget();
114 }
115 }
116
117 /**
118 * @param Skin $skin
119 * @param object $result Result row
120 * @return string
121 */
122 function formatResult( $skin, $result ) {
123 $linkRenderer = $this->getLinkRenderer();
124 # Make a link to the redirect itself
125 $rd_title = Title::makeTitle( $result->namespace, $result->title );
126 $rd_link = $linkRenderer->makeLink(
127 $rd_title,
128 null,
129 [],
130 [ 'redirect' => 'no' ]
131 );
132
133 # Find out where the redirect leads
134 $target = $this->getRedirectTarget( $result );
135 if ( $target ) {
136 # Make a link to the destination page
137 $lang = $this->getLanguage();
138 $arr = $lang->getArrow() . $lang->getDirMark();
139 $targetLink = $linkRenderer->makeLink( $target, $target->getFullText() );
140
141 return "$rd_link $arr $targetLink";
142 } else {
143 return "<del>$rd_link</del>";
144 }
145 }
146
147 public function execute( $par ) {
148 $this->addHelpLink( 'Help:Redirects' );
149 parent::execute( $par );
150 }
151
152 protected function getGroupName() {
153 return 'pages';
154 }
155 }