Merge "maintenance: Document secondary purpose of --server"
[lhc/web/wiklou.git] / includes / specials / SpecialShortpages.php
1 <?php
2 /**
3 * Implements Special:Shortpages
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\IResultWrapper;
25 use Wikimedia\Rdbms\IDatabase;
26
27 /**
28 * SpecialShortpages extends QueryPage. It is used to return the shortest
29 * pages in the database.
30 *
31 * @ingroup SpecialPage
32 */
33 class ShortPagesPage extends QueryPage {
34
35 function __construct( $name = 'Shortpages' ) {
36 parent::__construct( $name );
37 }
38
39 function isSyndicated() {
40 return false;
41 }
42
43 public function getQueryInfo() {
44 $config = $this->getConfig();
45 $blacklist = $config->get( 'ShortPagesNamespaceBlacklist' );
46 $tables = [ 'page' ];
47 $conds = [
48 'page_namespace' => array_diff( MWNamespace::getContentNamespaces(), $blacklist ),
49 'page_is_redirect' => 0
50 ];
51 $joinConds = [];
52 $options = [ 'USE INDEX' => [ 'page' => 'page_redirect_namespace_len' ] ];
53
54 // Allow extensions to modify the query
55 Hooks::run( 'ShortPagesQuery', [ &$tables, &$conds, &$joinConds, &$options ] );
56
57 return [
58 'tables' => $tables,
59 'fields' => [
60 'namespace' => 'page_namespace',
61 'title' => 'page_title',
62 'value' => 'page_len'
63 ],
64 'conds' => $conds,
65 'join_conds' => $joinConds,
66 'options' => $options
67 ];
68 }
69
70 public function reallyDoQuery( $limit, $offset = false ) {
71 $fname = static::class . '::reallyDoQuery';
72 $dbr = $this->getRecacheDB();
73 $query = $this->getQueryInfo();
74 $order = $this->getOrderFields();
75
76 if ( $this->sortDescending() ) {
77 foreach ( $order as &$field ) {
78 $field .= ' DESC';
79 }
80 }
81
82 $tables = isset( $query['tables'] ) ? (array)$query['tables'] : [];
83 $fields = isset( $query['fields'] ) ? (array)$query['fields'] : [];
84 $conds = isset( $query['conds'] ) ? (array)$query['conds'] : [];
85 $options = isset( $query['options'] ) ? (array)$query['options'] : [];
86 $join_conds = isset( $query['join_conds'] ) ? (array)$query['join_conds'] : [];
87
88 if ( $limit !== false ) {
89 $options['LIMIT'] = intval( $limit );
90 }
91
92 if ( $offset !== false ) {
93 $options['OFFSET'] = intval( $offset );
94 }
95
96 $namespaces = $conds['page_namespace'];
97 if ( count( $namespaces ) === 1 ) {
98 $options['ORDER BY'] = $order;
99 $res = $dbr->select( $tables, $fields, $conds, $fname,
100 $options, $join_conds
101 );
102 } else {
103 unset( $conds['page_namespace'] );
104 $options['INNER ORDER BY'] = $order;
105 $options['ORDER BY'] = [ 'value' . ( $this->sortDescending() ? ' DESC' : '' ) ];
106 $sql = $dbr->unionConditionPermutations(
107 $tables,
108 $fields,
109 [ 'page_namespace' => $namespaces ],
110 $conds,
111 $fname,
112 $options,
113 $join_conds
114 );
115 $res = $dbr->query( $sql, $fname );
116 }
117
118 return $res;
119 }
120
121 function getOrderFields() {
122 return [ 'page_len' ];
123 }
124
125 /**
126 * @param IDatabase $db
127 * @param IResultWrapper $res
128 */
129 function preprocessResults( $db, $res ) {
130 $this->executeLBFromResultWrapper( $res );
131 }
132
133 function sortDescending() {
134 return false;
135 }
136
137 /**
138 * @param Skin $skin
139 * @param object $result Result row
140 * @return string
141 */
142 function formatResult( $skin, $result ) {
143 $dm = $this->getLanguage()->getDirMark();
144
145 $title = Title::makeTitleSafe( $result->namespace, $result->title );
146 if ( !$title ) {
147 return Html::element( 'span', [ 'class' => 'mw-invalidtitle' ],
148 Linker::getInvalidTitleDescription( $this->getContext(), $result->namespace, $result->title ) );
149 }
150
151 $linkRenderer = $this->getLinkRenderer();
152 $hlink = $linkRenderer->makeKnownLink(
153 $title,
154 $this->msg( 'hist' )->text(),
155 [],
156 [ 'action' => 'history' ]
157 );
158 $hlinkInParentheses = $this->msg( 'parentheses' )->rawParams( $hlink )->escaped();
159
160 if ( $this->isCached() ) {
161 $plink = $linkRenderer->makeLink( $title );
162 $exists = $title->exists();
163 } else {
164 $plink = $linkRenderer->makeKnownLink( $title );
165 $exists = true;
166 }
167
168 $size = $this->msg( 'nbytes' )->numParams( $result->value )->escaped();
169
170 return $exists
171 ? "${hlinkInParentheses} {$dm}{$plink} {$dm}[{$size}]"
172 : "<del>${hlinkInParentheses} {$dm}{$plink} {$dm}[{$size}]</del>";
173 }
174
175 protected function getGroupName() {
176 return 'maintenance';
177 }
178 }