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