MediaWiki  1.34.0
SpecialPagesWithProp.php
Go to the documentation of this file.
1 <?php
31 
35  private $propName = null;
36 
40  private $existingPropNames = null;
41 
45  private $ns;
46 
50  private $reverse = false;
51 
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 
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 
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 
188  public function sortDescending() {
189  return !$this->reverse;
190  }
191 
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  }
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 }
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:792
SpecialPagesWithProp\onSubmit
onSubmit( $data, $form)
Definition: SpecialPagesWithProp.php:122
SpecialPagesWithProp\queryExistingProps
queryExistingProps( $limit=null, $offset=0)
Definition: SpecialPagesWithProp.php:229
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:719
SpecialPagesWithProp
Special:PagesWithProp to search the page_props table.
Definition: SpecialPagesWithProp.php:30
SpecialPagesWithProp\__construct
__construct( $name='PagesWithProp')
Definition: SpecialPagesWithProp.php:57
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:749
$res
$res
Definition: testCompression.php:52
QueryPage
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:36
QueryPage\$offset
int $offset
The offset and limit in use, as passed to the query() function.
Definition: QueryPage.php:41
QueryPage\$limit
int $limit
Definition: QueryPage.php:44
SpecialPagesWithProp\execute
execute( $par)
This is the actual workhorse.
Definition: SpecialPagesWithProp.php:65
SpecialPagesWithProp\isSyndicated
isSyndicated()
Disable RSS/Atom feeds.
Definition: SpecialPagesWithProp.php:145
Title\newFromRow
static newFromRow( $row)
Make a Title object from a DB row.
Definition: Title.php:518
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2575
SpecialPagesWithProp\$propName
string null $propName
Definition: SpecialPagesWithProp.php:35
SpecialPagesWithProp\$existingPropNames
string[] null $existingPropNames
Definition: SpecialPagesWithProp.php:40
$title
$title
Definition: testCompression.php:34
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:537
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
SpecialPagesWithProp\$reverse
bool $reverse
Definition: SpecialPagesWithProp.php:50
SpecialPagesWithProp\sortDescending
sortDescending()
Definition: SpecialPagesWithProp.php:188
$sort
$sort
Definition: profileinfo.php:331
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:692
SpecialPagesWithProp\$sortByValue
bool $sortByValue
Definition: SpecialPagesWithProp.php:55
SpecialPage\prefixSearchArray
static prefixSearchArray( $search, $limit, array $subpages, $offset)
Helper function for implementations of prefixSearchSubpages() that filter the values in memory (as op...
Definition: SpecialPage.php:528
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:709
SpecialPagesWithProp\$ns
int null $ns
Definition: SpecialPagesWithProp.php:45
SpecialPagesWithProp\prefixSearchSubpages
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
Definition: SpecialPagesWithProp.php:135
SpecialPage\getLinkRenderer
getLinkRenderer()
Definition: SpecialPage.php:904
SpecialPagesWithProp\getQueryInfo
getQueryInfo()
Subclasses return an SQL query here, formatted as an array with the following keys: tables => Table(s...
Definition: SpecialPagesWithProp.php:149
SpecialPagesWithProp\formatResult
formatResult( $skin, $result)
Definition: SpecialPagesWithProp.php:197
SpecialPagesWithProp\getExistingPropNames
getExistingPropNames()
Definition: SpecialPagesWithProp.php:222
SpecialPagesWithProp\isCacheable
isCacheable()
Is the output of this query cacheable? Non-cacheable expensive pages will be disabled in miser mode a...
Definition: SpecialPagesWithProp.php:61
SpecialPagesWithProp\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialPagesWithProp.php:256
SpecialPagesWithProp\getOrderFields
getOrderFields()
Subclasses return an array of fields to order by here.
Definition: SpecialPagesWithProp.php:177
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:303
SpecialPage\outputHeader
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
Definition: SpecialPage.php:639