Merge "Use HTML::hidden to create input fields"
[lhc/web/wiklou.git] / includes / specials / SpecialPagesWithProp.php
1 <?php
2 /**
3 * Implements Special:PagesWithProp
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 * @since 1.21
21 * @file
22 * @ingroup SpecialPage
23 */
24
25 /**
26 * Special:PagesWithProp to search the page_props table
27 * @ingroup SpecialPage
28 * @since 1.21
29 */
30 class SpecialPagesWithProp extends QueryPage {
31
32 /**
33 * @var string|null
34 */
35 private $propName = null;
36
37 /**
38 * @var string[]|null
39 */
40 private $existingPropNames = null;
41
42 /**
43 * @var bool
44 */
45 private $reverse = false;
46
47 /**
48 * @var bool
49 */
50 private $sortByValue = false;
51
52 function __construct( $name = 'PagesWithProp' ) {
53 parent::__construct( $name );
54 }
55
56 function isCacheable() {
57 return false;
58 }
59
60 public function execute( $par ) {
61 $this->setHeaders();
62 $this->outputHeader();
63 $this->getOutput()->addModuleStyles( 'mediawiki.special.pagesWithProp' );
64
65 $request = $this->getRequest();
66 $propname = $request->getVal( 'propname', $par );
67 $this->reverse = $request->getBool( 'reverse' );
68 $this->sortByValue = $request->getBool( 'sortbyvalue' );
69
70 $propnames = $this->getExistingPropNames();
71
72 $form = HTMLForm::factory( 'ooui', [
73 'propname' => [
74 'type' => 'combobox',
75 'name' => 'propname',
76 'options' => $propnames,
77 'default' => $propname,
78 'label-message' => 'pageswithprop-prop',
79 'required' => true,
80 ],
81 'reverse' => [
82 'type' => 'check',
83 'name' => 'reverse',
84 'default' => $this->reverse,
85 'label-message' => 'pageswithprop-reverse',
86 'required' => false,
87 ],
88 'sortbyvalue' => [
89 'type' => 'check',
90 'name' => 'sortbyvalue',
91 'default' => $this->sortByValue,
92 'label-message' => 'pageswithprop-sortbyvalue',
93 'required' => false,
94 ]
95 ], $this->getContext() );
96 $form->setMethod( 'get' );
97 $form->setSubmitCallback( [ $this, 'onSubmit' ] );
98 $form->setWrapperLegendMsg( 'pageswithprop-legend' );
99 $form->addHeaderText( $this->msg( 'pageswithprop-text' )->parseAsBlock() );
100 $form->setSubmitTextMsg( 'pageswithprop-submit' );
101
102 $form->prepareForm();
103 $form->displayForm( false );
104 if ( $propname !== '' && $propname !== null ) {
105 $form->trySubmit();
106 }
107 }
108
109 public function onSubmit( $data, $form ) {
110 $this->propName = $data['propname'];
111 parent::execute( $data['propname'] );
112 }
113
114 /**
115 * Return an array of subpages beginning with $search that this special page will accept.
116 *
117 * @param string $search Prefix to search for
118 * @param int $limit Maximum number of results to return
119 * @param int $offset Number of pages to skip
120 * @return string[] Matching subpages
121 */
122 public function prefixSearchSubpages( $search, $limit, $offset ) {
123 $subpages = array_keys( $this->queryExistingProps( $limit, $offset ) );
124 // We've already limited and offsetted, set to N and 0 respectively.
125 return self::prefixSearchArray( $search, count( $subpages ), $subpages, 0 );
126 }
127
128 /**
129 * Disable RSS/Atom feeds
130 * @return bool
131 */
132 function isSyndicated() {
133 return false;
134 }
135
136 public function getQueryInfo() {
137 return [
138 'tables' => [ 'page_props', 'page' ],
139 'fields' => [
140 'page_id' => 'pp_page',
141 'page_namespace',
142 'page_title',
143 'page_len',
144 'page_is_redirect',
145 'page_latest',
146 'pp_value',
147 ],
148 'conds' => [
149 'pp_propname' => $this->propName,
150 ],
151 'join_conds' => [
152 'page' => [ 'INNER JOIN', 'page_id = pp_page' ]
153 ],
154 'options' => []
155 ];
156 }
157
158 function getOrderFields() {
159 $sort = [ 'page_id' ];
160 if ( $this->sortByValue ) {
161 array_unshift( $sort, 'pp_sortkey' );
162 }
163 return $sort;
164 }
165
166 /**
167 * @return bool
168 */
169 public function sortDescending() {
170 return !$this->reverse;
171 }
172
173 /**
174 * @param Skin $skin
175 * @param object $result Result row
176 * @return string
177 */
178 function formatResult( $skin, $result ) {
179 $title = Title::newFromRow( $result );
180 $ret = $this->getLinkRenderer()->makeKnownLink( $title );
181 if ( $result->pp_value !== '' ) {
182 // Do not show very long or binary values on the special page
183 $valueLength = strlen( $result->pp_value );
184 $isBinary = strpos( $result->pp_value, "\0" ) !== false;
185 $isTooLong = $valueLength > 1024;
186
187 if ( $isBinary || $isTooLong ) {
188 $message = $this
189 ->msg( $isBinary ? 'pageswithprop-prophidden-binary' : 'pageswithprop-prophidden-long' )
190 ->params( $this->getLanguage()->formatSize( $valueLength ) );
191
192 $propValue = Html::element( 'span', [ 'class' => 'prop-value-hidden' ], $message->text() );
193 } else {
194 $propValue = Html::element( 'span', [ 'class' => 'prop-value' ], $result->pp_value );
195 }
196
197 $ret .= $this->msg( 'colon-separator' )->escaped() . $propValue;
198 }
199
200 return $ret;
201 }
202
203 public function getExistingPropNames() {
204 if ( $this->existingPropNames === null ) {
205 $this->existingPropNames = $this->queryExistingProps();
206 }
207 return $this->existingPropNames;
208 }
209
210 protected function queryExistingProps( $limit = null, $offset = 0 ) {
211 $opts = [
212 'DISTINCT', 'ORDER BY' => 'pp_propname'
213 ];
214 if ( $limit ) {
215 $opts['LIMIT'] = $limit;
216 }
217 if ( $offset ) {
218 $opts['OFFSET'] = $offset;
219 }
220
221 $res = wfGetDB( DB_REPLICA )->select(
222 'page_props',
223 'pp_propname',
224 '',
225 __METHOD__,
226 $opts
227 );
228
229 $propnames = [];
230 foreach ( $res as $row ) {
231 $propnames[$row->pp_propname] = $row->pp_propname;
232 }
233
234 return $propnames;
235 }
236
237 protected function getGroupName() {
238 return 'pages';
239 }
240 }