Merge "Convert Special:DeletedContributions to use OOUI."
[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 /**
28 * Special:Listredirects - Lists all the redirects on the wiki.
29 * @ingroup SpecialPage
30 */
31 class ListredirectsPage extends QueryPage {
32 function __construct( $name = 'Listredirects' ) {
33 parent::__construct( $name );
34 }
35
36 public function isExpensive() {
37 return true;
38 }
39
40 function isSyndicated() {
41 return false;
42 }
43
44 function sortDescending() {
45 return false;
46 }
47
48 public function getQueryInfo() {
49 return [
50 'tables' => [ 'p1' => 'page', 'redirect', 'p2' => 'page' ],
51 'fields' => [ 'namespace' => 'p1.page_namespace',
52 'title' => 'p1.page_title',
53 'value' => 'p1.page_title',
54 'rd_namespace',
55 'rd_title',
56 'rd_fragment',
57 'rd_interwiki',
58 'redirid' => 'p2.page_id' ],
59 'conds' => [ 'p1.page_is_redirect' => 1 ],
60 'join_conds' => [ 'redirect' => [
61 'LEFT JOIN', 'rd_from=p1.page_id' ],
62 'p2' => [ 'LEFT JOIN', [
63 'p2.page_namespace=rd_namespace',
64 'p2.page_title=rd_title' ] ] ]
65 ];
66 }
67
68 function getOrderFields() {
69 return [ 'p1.page_namespace', 'p1.page_title' ];
70 }
71
72 /**
73 * Cache page existence for performance
74 *
75 * @param IDatabase $db
76 * @param ResultWrapper $res
77 */
78 function preprocessResults( $db, $res ) {
79 if ( !$res->numRows() ) {
80 return;
81 }
82
83 $batch = new LinkBatch;
84 foreach ( $res as $row ) {
85 $batch->add( $row->namespace, $row->title );
86 $redirTarget = $this->getRedirectTarget( $row );
87 if ( $redirTarget ) {
88 $batch->addObj( $redirTarget );
89 }
90 }
91 $batch->execute();
92
93 // Back to start for display
94 $res->seek( 0 );
95 }
96
97 /**
98 * @param stdClass $row
99 * @return Title|null
100 */
101 protected function getRedirectTarget( $row ) {
102 if ( isset( $row->rd_title ) ) {
103 return Title::makeTitle( $row->rd_namespace,
104 $row->rd_title, $row->rd_fragment,
105 $row->rd_interwiki
106 );
107 } else {
108 $title = Title::makeTitle( $row->namespace, $row->title );
109 $article = WikiPage::factory( $title );
110
111 return $article->getRedirectTarget();
112 }
113 }
114
115 /**
116 * @param Skin $skin
117 * @param object $result Result row
118 * @return string
119 */
120 function formatResult( $skin, $result ) {
121 $linkRenderer = $this->getLinkRenderer();
122 # Make a link to the redirect itself
123 $rd_title = Title::makeTitle( $result->namespace, $result->title );
124 $rd_link = $linkRenderer->makeLink(
125 $rd_title,
126 null,
127 [],
128 [ 'redirect' => 'no' ]
129 );
130
131 # Find out where the redirect leads
132 $target = $this->getRedirectTarget( $result );
133 if ( $target ) {
134 # Make a link to the destination page
135 $lang = $this->getLanguage();
136 $arr = $lang->getArrow() . $lang->getDirMark();
137 $targetLink = $linkRenderer->makeLink( $target );
138
139 return "$rd_link $arr $targetLink";
140 } else {
141 return "<del>$rd_link</del>";
142 }
143 }
144
145 protected function getGroupName() {
146 return 'pages';
147 }
148 }