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