MediaWiki master
SpecialPagesWithProp.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
27use Skin;
28use stdClass;
30
38
42 private $propName = null;
43
47 private $existingPropNames = null;
48
52 private $ns;
53
57 private $reverse = false;
58
62 private $sortByValue = false;
63
67 public function __construct( IConnectionProvider $dbProvider ) {
68 parent::__construct( 'PagesWithProp' );
69 $this->setDatabaseProvider( $dbProvider );
70 }
71
72 public function isCacheable() {
73 return false;
74 }
75
76 public function execute( $par ) {
77 $this->setHeaders();
78 $this->outputHeader();
79 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
80
81 $request = $this->getRequest();
82 $propname = $request->getVal( 'propname', $par );
83 $this->ns = $request->getIntOrNull( 'namespace' );
84 $this->reverse = $request->getBool( 'reverse' );
85 $this->sortByValue = $request->getBool( 'sortbyvalue' );
86
87 $propnames = $this->getExistingPropNames();
88
89 $fields = [
90 'propname' => [
91 'type' => 'combobox',
92 'name' => 'propname',
93 'options' => $propnames,
94 'default' => $propname,
95 'label-message' => 'pageswithprop-prop',
96 'required' => true,
97 ],
98 'namespace' => [
99 'type' => 'namespaceselect',
100 'name' => 'namespace',
101 'label-message' => 'namespace',
102 'all' => '',
103 'default' => $this->ns,
104 ],
105 'reverse' => [
106 'type' => 'check',
107 'name' => 'reverse',
108 'default' => $this->reverse,
109 'label-message' => 'pageswithprop-reverse',
110 'required' => false,
111 ],
112 'sortbyvalue' => [
113 'type' => 'check',
114 'name' => 'sortbyvalue',
115 'default' => $this->sortByValue,
116 'label-message' => 'pageswithprop-sortbyvalue',
117 'required' => false,
118 ]
119 ];
120
121 $form = HTMLForm::factory( 'ooui', $fields, $this->getContext() )
122 ->setMethod( 'get' )
123 ->setTitle( $this->getPageTitle() ) // Remove subpage
124 ->setSubmitCallback( [ $this, 'onSubmit' ] )
125 ->setWrapperLegendMsg( 'pageswithprop-legend' )
126 ->addHeaderHtml( $this->msg( 'pageswithprop-text' )->parseAsBlock() )
127 ->setSubmitTextMsg( 'pageswithprop-submit' )
128 ->prepareForm();
129 $form->displayForm( false );
130 if ( $propname !== '' && $propname !== null ) {
131 $form->trySubmit();
132 }
133 }
134
135 public function onSubmit( $data, $form ) {
136 $this->propName = $data['propname'];
137 parent::execute( $data['propname'] );
138 }
139
148 public function prefixSearchSubpages( $search, $limit, $offset ) {
149 $subpages = array_keys( $this->queryExistingProps( $limit, $offset ) );
150 // We've already limited and offsetted, set to N and 0 respectively.
151 return self::prefixSearchArray( $search, count( $subpages ), $subpages, 0 );
152 }
153
158 public function isSyndicated() {
159 return false;
160 }
161
165 protected function linkParameters() {
166 $params = [
167 'reverse' => $this->reverse,
168 'sortbyvalue' => $this->sortByValue,
169 ];
170 if ( $this->ns !== null ) {
171 $params['namespace'] = $this->ns;
172 }
173 return $params;
174 }
175
176 public function getQueryInfo() {
177 $query = [
178 'tables' => [ 'page_props', 'page' ],
179 'fields' => [
180 'page_id' => 'pp_page',
181 'page_namespace',
182 'page_title',
183 'page_len',
184 'page_is_redirect',
185 'page_latest',
186 'pp_value',
187 ],
188 'conds' => [
189 'pp_propname' => $this->propName,
190 ],
191 'join_conds' => [
192 'page' => [ 'JOIN', 'page_id = pp_page' ]
193 ],
194 'options' => []
195 ];
196
197 if ( $this->ns !== null ) {
198 $query['conds']['page_namespace'] = $this->ns;
199 }
200
201 return $query;
202 }
203
204 protected function getOrderFields() {
205 $sort = [ 'page_id' ];
206 if ( $this->sortByValue ) {
207 array_unshift( $sort, 'pp_sortkey' );
208 }
209 return $sort;
210 }
211
215 public function sortDescending() {
216 return !$this->reverse;
217 }
218
224 public function formatResult( $skin, $result ) {
225 $title = Title::newFromRow( $result );
226 $ret = $this->getLinkRenderer()->makeKnownLink( $title );
227 if ( $result->pp_value !== '' ) {
228 // Do not show very long or binary values on the special page
229 $valueLength = strlen( $result->pp_value );
230 $isBinary = str_contains( $result->pp_value, "\0" );
231 $isTooLong = $valueLength > 1024;
232
233 if ( $isBinary || $isTooLong ) {
234 $message = $this
235 ->msg( $isBinary ? 'pageswithprop-prophidden-binary' : 'pageswithprop-prophidden-long' )
236 ->sizeParams( $valueLength );
237
238 $propValue = Html::element( 'span', [ 'class' => 'prop-value-hidden' ], $message->text() );
239 } else {
240 $propValue = Html::element( 'span', [ 'class' => 'prop-value' ], $result->pp_value );
241 }
242
243 $ret .= $this->msg( 'colon-separator' )->escaped() . $propValue;
244 }
245
246 return $ret;
247 }
248
249 public function getExistingPropNames() {
250 if ( $this->existingPropNames === null ) {
251 $this->existingPropNames = $this->queryExistingProps();
252 }
253 return $this->existingPropNames;
254 }
255
256 protected function queryExistingProps( $limit = null, $offset = 0 ) {
257 $queryBuilder = $this->getDatabaseProvider()
258 ->getReplicaDatabase()
259 ->newSelectQueryBuilder()
260 ->select( 'pp_propname' )
261 ->distinct()
262 ->from( 'page_props' )
263 ->orderBy( 'pp_propname' );
264
265 if ( $limit ) {
266 $queryBuilder->limit( $limit );
267 }
268 if ( $offset ) {
269 $queryBuilder->offset( $offset );
270 }
271 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
272
273 $propnames = [];
274 foreach ( $res as $row ) {
275 $propnames[$row->pp_propname] = $row->pp_propname;
276 }
277
278 return $propnames;
279 }
280
281 protected function getGroupName() {
282 return 'pages';
283 }
284}
285
290class_alias( SpecialPagesWithProp::class, 'SpecialPagesWithProp' );
array $params
The job parameters.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:208
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
This is a class for doing query pages; since they're almost all the same, we factor out some of the f...
Definition QueryPage.php:89
setDatabaseProvider(IConnectionProvider $databaseProvider)
int $offset
The offset and limit in use, as passed to the query() function.
Definition QueryPage.php:94
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
static prefixSearchArray( $search, $limit, array $subpages, $offset)
Helper function for implementations of prefixSearchSubpages() that filter the values in memory (as op...
getPageTitle( $subpage=false)
Get a self-referential title object.
getContext()
Gets the context this SpecialPage is executed in.
getRequest()
Get the WebRequest being used for this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages By default the message key is the canonical name of...
Special:PagesWithProp to search the page_props table.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
getQueryInfo()
Subclasses return an SQL query here, formatted as an array with the following keys: tables => Table(s...
__construct(IConnectionProvider $dbProvider)
getOrderFields()
Subclasses return an array of fields to order by here.
isCacheable()
Is the output of this query cacheable? Non-cacheable expensive pages will be disabled in miser mode a...
linkParameters()
If using extra form wheely-dealies, return a set of parameters here as an associative array....
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
execute( $par)
This is the actual workhorse.
Represents a title within MediaWiki.
Definition Title.php:79
The base class for all skins.
Definition Skin.php:59
Provide primary and replica IDatabase connections.
element(SerializerNode $parent, SerializerNode $node, $contents)