MediaWiki master
SpecialPagesWithProp.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
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
64 public function __construct( IConnectionProvider $dbProvider ) {
65 parent::__construct( 'PagesWithProp' );
66 $this->setDatabaseProvider( $dbProvider );
67 }
68
69 public function isCacheable() {
70 return false;
71 }
72
73 public function execute( $par ) {
74 $this->setHeaders();
75 $this->outputHeader();
76 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
77
78 $request = $this->getRequest();
79 $propname = $request->getVal( 'propname', $par );
80 $this->ns = $request->getIntOrNull( 'namespace' );
81 $this->reverse = $request->getBool( 'reverse' );
82 $this->sortByValue = $request->getBool( 'sortbyvalue' );
83
84 $propnames = $this->getExistingPropNames();
85
86 $fields = [
87 'propname' => [
88 'type' => 'combobox',
89 'name' => 'propname',
90 'options' => $propnames,
91 'default' => $propname,
92 'label-message' => 'pageswithprop-prop',
93 'required' => true,
94 ],
95 'namespace' => [
96 'type' => 'namespaceselect',
97 'name' => 'namespace',
98 'label-message' => 'namespace',
99 'all' => '',
100 'default' => $this->ns,
101 ],
102 'reverse' => [
103 'type' => 'check',
104 'name' => 'reverse',
105 'default' => $this->reverse,
106 'label-message' => 'pageswithprop-reverse',
107 'required' => false,
108 ],
109 'sortbyvalue' => [
110 'type' => 'check',
111 'name' => 'sortbyvalue',
112 'default' => $this->sortByValue,
113 'label-message' => 'pageswithprop-sortbyvalue',
114 'required' => false,
115 ]
116 ];
117
118 $form = HTMLForm::factory( 'ooui', $fields, $this->getContext() )
119 ->setMethod( 'get' )
120 ->setTitle( $this->getPageTitle() ) // Remove subpage
121 ->setSubmitCallback( [ $this, 'onSubmit' ] )
122 ->setWrapperLegendMsg( 'pageswithprop-legend' )
123 ->addHeaderHtml( $this->msg( 'pageswithprop-text' )->parseAsBlock() )
124 ->setSubmitTextMsg( 'pageswithprop-submit' )
125 ->prepareForm();
126 $form->displayForm( false );
127 if ( $propname !== '' && $propname !== null ) {
128 $form->trySubmit();
129 }
130 }
131
132 public function onSubmit( $data, $form ) {
133 $this->propName = $data['propname'];
134 parent::execute( $data['propname'] );
135 }
136
145 public function prefixSearchSubpages( $search, $limit, $offset ) {
146 $subpages = array_keys( $this->queryExistingProps( $limit, $offset ) );
147 // We've already limited and offsetted, set to N and 0 respectively.
148 return self::prefixSearchArray( $search, count( $subpages ), $subpages, 0 );
149 }
150
155 public function isSyndicated() {
156 return false;
157 }
158
162 protected function linkParameters() {
163 $params = [
164 'reverse' => $this->reverse,
165 'sortbyvalue' => $this->sortByValue,
166 ];
167 if ( $this->ns !== null ) {
168 $params['namespace'] = $this->ns;
169 }
170 return $params;
171 }
172
173 public function getQueryInfo() {
174 $query = [
175 'tables' => [ 'page_props', 'page' ],
176 'fields' => [
177 'page_id' => 'pp_page',
178 'page_namespace',
179 'page_title',
180 'page_len',
181 'page_is_redirect',
182 'page_latest',
183 'pp_value',
184 ],
185 'conds' => [
186 'pp_propname' => $this->propName,
187 ],
188 'join_conds' => [
189 'page' => [ 'JOIN', 'page_id = pp_page' ]
190 ],
191 'options' => []
192 ];
193
194 if ( $this->ns !== null ) {
195 $query['conds']['page_namespace'] = $this->ns;
196 }
197
198 return $query;
199 }
200
201 protected function getOrderFields() {
202 $sort = [ 'page_id' ];
203 if ( $this->sortByValue ) {
204 array_unshift( $sort, 'pp_sortkey' );
205 }
206 return $sort;
207 }
208
212 public function sortDescending() {
213 return !$this->reverse;
214 }
215
221 public function formatResult( $skin, $result ) {
222 $title = Title::newFromRow( $result );
223 $ret = $this->getLinkRenderer()->makeKnownLink( $title );
224 if ( $result->pp_value !== '' ) {
225 // Do not show very long or binary values on the special page
226 $valueLength = strlen( $result->pp_value );
227 $isBinary = str_contains( $result->pp_value, "\0" );
228 $isTooLong = $valueLength > 1024;
229
230 if ( $isBinary || $isTooLong ) {
231 $message = $this
232 ->msg( $isBinary ? 'pageswithprop-prophidden-binary' : 'pageswithprop-prophidden-long' )
233 ->sizeParams( $valueLength );
234
235 $propValue = Html::element( 'span', [ 'class' => 'prop-value-hidden' ], $message->text() );
236 } else {
237 $propValue = Html::element( 'span', [ 'class' => 'prop-value' ], $result->pp_value );
238 }
239
240 $ret .= $this->msg( 'colon-separator' )->escaped() . $propValue;
241 }
242
243 return $ret;
244 }
245
246 public function getExistingPropNames() {
247 if ( $this->existingPropNames === null ) {
248 $this->existingPropNames = $this->queryExistingProps();
249 }
250 return $this->existingPropNames;
251 }
252
253 protected function queryExistingProps( $limit = null, $offset = 0 ) {
254 $queryBuilder = $this->getDatabaseProvider()
255 ->getReplicaDatabase()
256 ->newSelectQueryBuilder()
257 ->select( 'pp_propname' )
258 ->distinct()
259 ->from( 'page_props' )
260 ->orderBy( 'pp_propname' );
261
262 if ( $limit ) {
263 $queryBuilder->limit( $limit );
264 }
265 if ( $offset ) {
266 $queryBuilder->offset( $offset );
267 }
268 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
269
270 $propnames = [];
271 foreach ( $res as $row ) {
272 $propnames[$row->pp_propname] = $row->pp_propname;
273 }
274
275 return $propnames;
276 }
277
278 protected function getGroupName() {
279 return 'pages';
280 }
281}
282
287class_alias( SpecialPagesWithProp::class, 'SpecialPagesWithProp' );
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:209
This class is a collection of static functions that serve two purposes:
Definition Html.php:57
The base class for all skins.
Definition Skin.php:58
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:87
setDatabaseProvider(IConnectionProvider $databaseProvider)
int $offset
The offset and limit in use, as passed to the query() function.
Definition QueryPage.php:92
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:78
Provide primary and replica IDatabase connections.
element(SerializerNode $parent, SerializerNode $node, $contents)