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