Use HTML::hidden to create input fields
[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\ResultWrapper;
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 $tables = [ 'page' ];
45 $conds = [
46 'page_namespace' => MWNamespace::getContentNamespaces(),
47 'page_is_redirect' => 0
48 ];
49 $joinConds = [];
50 $options = [ 'USE INDEX' => [ 'page' => 'page_redirect_namespace_len' ] ];
51
52 // Allow extensions to modify the query
53 Hooks::run( 'ShortPagesQuery', [ &$tables, &$conds, &$joinConds, &$options ] );
54
55 return [
56 'tables' => $tables,
57 'fields' => [
58 'namespace' => 'page_namespace',
59 'title' => 'page_title',
60 'value' => 'page_len'
61 ],
62 'conds' => $conds,
63 'join_conds' => $joinConds,
64 'options' => $options
65 ];
66 }
67
68 public function reallyDoQuery( $limit, $offset = false ) {
69 $fname = static::class . '::reallyDoQuery';
70 $dbr = $this->getRecacheDB();
71 $query = $this->getQueryInfo();
72 $order = $this->getOrderFields();
73
74 if ( $this->sortDescending() ) {
75 foreach ( $order as &$field ) {
76 $field .= ' DESC';
77 }
78 }
79
80 $tables = isset( $query['tables'] ) ? (array)$query['tables'] : [];
81 $fields = isset( $query['fields'] ) ? (array)$query['fields'] : [];
82 $conds = isset( $query['conds'] ) ? (array)$query['conds'] : [];
83 $options = isset( $query['options'] ) ? (array)$query['options'] : [];
84 $join_conds = isset( $query['join_conds'] ) ? (array)$query['join_conds'] : [];
85
86 if ( $limit !== false ) {
87 $options['LIMIT'] = intval( $limit );
88 }
89
90 if ( $offset !== false ) {
91 $options['OFFSET'] = intval( $offset );
92 }
93
94 $namespaces = $conds['page_namespace'];
95 if ( count( $namespaces ) === 1 ) {
96 $options['ORDER BY'] = $order;
97 $res = $dbr->select( $tables, $fields, $conds, $fname,
98 $options, $join_conds
99 );
100 } else {
101 unset( $conds['page_namespace'] );
102 $options['INNER ORDER BY'] = $order;
103 $options['ORDER BY'] = [ 'value' . ( $this->sortDescending() ? ' DESC' : '' ) ];
104 $sql = $dbr->unionConditionPermutations(
105 $tables,
106 $fields,
107 [ 'page_namespace' => $namespaces ],
108 $conds,
109 $fname,
110 $options,
111 $join_conds
112 );
113 $res = $dbr->query( $sql, $fname );
114 }
115
116 return $res;
117 }
118
119 function getOrderFields() {
120 return [ 'page_len' ];
121 }
122
123 /**
124 * @param IDatabase $db
125 * @param ResultWrapper $res
126 */
127 function preprocessResults( $db, $res ) {
128 $this->executeLBFromResultWrapper( $res );
129 }
130
131 function sortDescending() {
132 return false;
133 }
134
135 /**
136 * @param Skin $skin
137 * @param object $result Result row
138 * @return string
139 */
140 function formatResult( $skin, $result ) {
141 $dm = $this->getLanguage()->getDirMark();
142
143 $title = Title::makeTitleSafe( $result->namespace, $result->title );
144 if ( !$title ) {
145 return Html::element( 'span', [ 'class' => 'mw-invalidtitle' ],
146 Linker::getInvalidTitleDescription( $this->getContext(), $result->namespace, $result->title ) );
147 }
148
149 $linkRenderer = $this->getLinkRenderer();
150 $hlink = $linkRenderer->makeKnownLink(
151 $title,
152 $this->msg( 'hist' )->text(),
153 [],
154 [ 'action' => 'history' ]
155 );
156 $hlinkInParentheses = $this->msg( 'parentheses' )->rawParams( $hlink )->escaped();
157
158 if ( $this->isCached() ) {
159 $plink = $linkRenderer->makeLink( $title );
160 $exists = $title->exists();
161 } else {
162 $plink = $linkRenderer->makeKnownLink( $title );
163 $exists = true;
164 }
165
166 $size = $this->msg( 'nbytes' )->numParams( $result->value )->escaped();
167
168 return $exists
169 ? "${hlinkInParentheses} {$dm}{$plink} {$dm}[{$size}]"
170 : "<del>${hlinkInParentheses} {$dm}{$plink} {$dm}[{$size}]</del>";
171 }
172
173 protected function getGroupName() {
174 return 'maintenance';
175 }
176 }