MediaWiki  master
SpecialPagesWithProp.php
Go to the documentation of this file.
1 <?php
26 
33 
37  private $propName = null;
38 
42  private $existingPropNames = null;
43 
47  private $ns;
48 
52  private $reverse = false;
53 
57  private $sortByValue = false;
58 
63  parent::__construct( 'PagesWithProp' );
64  $this->setDBLoadBalancer( $loadBalancer );
65  }
66 
67  public function isCacheable() {
68  return false;
69  }
70 
71  public function execute( $par ) {
72  $this->setHeaders();
73  $this->outputHeader();
74  $this->getOutput()->addModuleStyles( 'mediawiki.special' );
75 
76  $request = $this->getRequest();
77  $propname = $request->getVal( 'propname', $par );
78  $this->ns = $request->getIntOrNull( 'namespace' );
79  $this->reverse = $request->getBool( 'reverse' );
80  $this->sortByValue = $request->getBool( 'sortbyvalue' );
81 
82  $propnames = $this->getExistingPropNames();
83 
84  $fields = [
85  'propname' => [
86  'type' => 'combobox',
87  'name' => 'propname',
88  'options' => $propnames,
89  'default' => $propname,
90  'label-message' => 'pageswithprop-prop',
91  'required' => true,
92  ],
93  'namespace' => [
94  'type' => 'namespaceselect',
95  'name' => 'namespace',
96  'label-message' => 'namespace',
97  'all' => '',
98  'default' => $this->ns,
99  ],
100  'reverse' => [
101  'type' => 'check',
102  'name' => 'reverse',
103  'default' => $this->reverse,
104  'label-message' => 'pageswithprop-reverse',
105  'required' => false,
106  ],
107  'sortbyvalue' => [
108  'type' => 'check',
109  'name' => 'sortbyvalue',
110  'default' => $this->sortByValue,
111  'label-message' => 'pageswithprop-sortbyvalue',
112  'required' => false,
113  ]
114  ];
115 
116  $context = new DerivativeContext( $this->getContext() );
117  $context->setTitle( $this->getPageTitle() ); // Remove subpage
118  $form = HTMLForm::factory( 'ooui', $fields, $context )
119  ->setMethod( 'get' )
120  ->setSubmitCallback( [ $this, 'onSubmit' ] )
121  ->setWrapperLegendMsg( 'pageswithprop-legend' )
122  ->addHeaderText( $this->msg( 'pageswithprop-text' )->parseAsBlock() )
123  ->setSubmitTextMsg( 'pageswithprop-submit' )
124  ->prepareForm();
125  $form->displayForm( false );
126  if ( $propname !== '' && $propname !== null ) {
127  $form->trySubmit();
128  }
129  }
130 
131  public function onSubmit( $data, $form ) {
132  $this->propName = $data['propname'];
133  parent::execute( $data['propname'] );
134  }
135 
144  public function prefixSearchSubpages( $search, $limit, $offset ) {
145  $subpages = array_keys( $this->queryExistingProps( $limit, $offset ) );
146  // We've already limited and offsetted, set to N and 0 respectively.
147  return self::prefixSearchArray( $search, count( $subpages ), $subpages, 0 );
148  }
149 
154  public function isSyndicated() {
155  return false;
156  }
157 
161  protected function linkParameters() {
162  $params = [
163  'reverse' => $this->reverse,
164  'sortbyvalue' => $this->sortByValue,
165  ];
166  if ( $this->ns !== null ) {
167  $params['namespace'] = $this->ns;
168  }
169  return $params;
170  }
171 
172  public function getQueryInfo() {
173  $query = [
174  'tables' => [ 'page_props', 'page' ],
175  'fields' => [
176  'page_id' => 'pp_page',
177  'page_namespace',
178  'page_title',
179  'page_len',
180  'page_is_redirect',
181  'page_latest',
182  'pp_value',
183  ],
184  'conds' => [
185  'pp_propname' => $this->propName,
186  ],
187  'join_conds' => [
188  'page' => [ 'JOIN', 'page_id = pp_page' ]
189  ],
190  'options' => []
191  ];
192 
193  if ( $this->ns !== null ) {
194  $query['conds']['page_namespace'] = $this->ns;
195  }
196 
197  return $query;
198  }
199 
200  protected function getOrderFields() {
201  $sort = [ 'page_id' ];
202  if ( $this->sortByValue ) {
203  array_unshift( $sort, 'pp_sortkey' );
204  }
205  return $sort;
206  }
207 
211  public function sortDescending() {
212  return !$this->reverse;
213  }
214 
220  public function formatResult( $skin, $result ) {
221  $title = Title::newFromRow( $result );
222  $ret = $this->getLinkRenderer()->makeKnownLink( $title );
223  if ( $result->pp_value !== '' ) {
224  // Do not show very long or binary values on the special page
225  $valueLength = strlen( $result->pp_value );
226  $isBinary = strpos( $result->pp_value, "\0" ) !== false;
227  $isTooLong = $valueLength > 1024;
228 
229  if ( $isBinary || $isTooLong ) {
230  $message = $this
231  ->msg( $isBinary ? 'pageswithprop-prophidden-binary' : 'pageswithprop-prophidden-long' )
232  ->sizeParams( $valueLength );
233 
234  $propValue = Html::element( 'span', [ 'class' => 'prop-value-hidden' ], $message->text() );
235  } else {
236  $propValue = Html::element( 'span', [ 'class' => 'prop-value' ], $result->pp_value );
237  }
238 
239  $ret .= $this->msg( 'colon-separator' )->escaped() . $propValue;
240  }
241 
242  return $ret;
243  }
244 
245  public function getExistingPropNames() {
246  if ( $this->existingPropNames === null ) {
247  $this->existingPropNames = $this->queryExistingProps();
248  }
250  }
251 
252  protected function queryExistingProps( $limit = null, $offset = 0 ) {
253  $opts = [
254  'DISTINCT', 'ORDER BY' => 'pp_propname'
255  ];
256  if ( $limit ) {
257  $opts['LIMIT'] = $limit;
258  }
259  if ( $offset ) {
260  $opts['OFFSET'] = $offset;
261  }
262 
263  $dbr = $this->getDBLoadBalancer()->getConnectionRef( ILoadBalancer::DB_REPLICA );
264  $res = $dbr->select(
265  'page_props',
266  'pp_propname',
267  '',
268  __METHOD__,
269  $opts
270  );
271 
272  $propnames = [];
273  foreach ( $res as $row ) {
274  $propnames[$row->pp_propname] = $row->pp_propname;
275  }
276 
277  return $propnames;
278  }
279 
280  protected function getGroupName() {
281  return 'pages';
282  }
283 }
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:744
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:912
QueryPage\getDBLoadBalancer
getDBLoadBalancer()
Definition: QueryPage.php:900
SpecialPagesWithProp\onSubmit
onSubmit( $data, $form)
Definition: SpecialPagesWithProp.php:131
SpecialPagesWithProp\queryExistingProps
queryExistingProps( $limit=null, $offset=0)
Definition: SpecialPagesWithProp.php:252
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:790
SpecialPagesWithProp
Special:PagesWithProp to search the page_props table.
Definition: SpecialPagesWithProp.php:32
SpecialPagesWithProp\__construct
__construct(ILoadBalancer $loadBalancer)
Definition: SpecialPagesWithProp.php:62
$res
$res
Definition: testCompression.php:57
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:41
QueryPage\$offset
int $offset
The offset and limit in use, as passed to the query() function.
Definition: QueryPage.php:46
$dbr
$dbr
Definition: testCompression.php:54
QueryPage\$limit
int $limit
Definition: QueryPage.php:49
DerivativeContext
An IContextSource implementation which will inherit context from another source but allow individual ...
Definition: DerivativeContext.php:32
SpecialPagesWithProp\execute
execute( $par)
This is the actual workhorse.
Definition: SpecialPagesWithProp.php:71
SpecialPagesWithProp\isSyndicated
isSyndicated()
Disable RSS/Atom feeds.
Definition: SpecialPagesWithProp.php:154
Title\newFromRow
static newFromRow( $row)
Make a Title object from a DB row.
Definition: Title.php:592
SpecialPagesWithProp\$propName
string null $propName
Definition: SpecialPagesWithProp.php:37
SpecialPagesWithProp\linkParameters
linkParameters()
If using extra form wheely-dealies, return a set of parameters here as an associative array....
Definition: SpecialPagesWithProp.php:161
SpecialPagesWithProp\$existingPropNames
string[] null $existingPropNames
Definition: SpecialPagesWithProp.php:42
$title
$title
Definition: testCompression.php:38
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:618
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
SpecialPagesWithProp\$reverse
bool $reverse
Definition: SpecialPagesWithProp.php:52
SpecialPagesWithProp\sortDescending
sortDescending()
Definition: SpecialPagesWithProp.php:211
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:764
SpecialPagesWithProp\$sortByValue
bool $sortByValue
Definition: SpecialPagesWithProp.php:57
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:608
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:780
QueryPage\setDBLoadBalancer
setDBLoadBalancer(ILoadBalancer $loadBalancer)
Definition: QueryPage.php:892
SpecialPagesWithProp\$ns
int null $ns
Definition: SpecialPagesWithProp.php:47
SpecialPagesWithProp\prefixSearchSubpages
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
Definition: SpecialPagesWithProp.php:144
SpecialPage\getLinkRenderer
getLinkRenderer()
Definition: SpecialPage.php:1028
SpecialPagesWithProp\getQueryInfo
getQueryInfo()
Subclasses return an SQL query here, formatted as an array with the following keys: tables => Table(s...
Definition: SpecialPagesWithProp.php:172
SpecialPagesWithProp\formatResult
formatResult( $skin, $result)
Definition: SpecialPagesWithProp.php:220
QueryPage\$loadBalancer
ILoadBalancer null $loadBalancer
Definition: QueryPage.php:71
SpecialPagesWithProp\getExistingPropNames
getExistingPropNames()
Definition: SpecialPagesWithProp.php:245
SpecialPagesWithProp\isCacheable
isCacheable()
Is the output of this query cacheable? Non-cacheable expensive pages will be disabled in miser mode a...
Definition: SpecialPagesWithProp.php:67
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:232
SpecialPagesWithProp\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialPagesWithProp.php:280
SpecialPagesWithProp\getOrderFields
getOrderFields()
Subclasses return an array of fields to order by here.
Definition: SpecialPagesWithProp.php:200
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:326
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:709
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81