MediaWiki  master
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  public function __construct( $name = 'PagesWithProp' ) {
58  parent::__construct( $name );
59  }
60 
61  public 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  $fields = [
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  ];
109 
110  $context = new DerivativeContext( $this->getContext() );
111  $context->setTitle( $this->getPageTitle() ); // Remove subpage
112  $form = HTMLForm::factory( 'ooui', $fields, $context );
113 
114  $form->setMethod( 'get' );
115  $form->setSubmitCallback( [ $this, 'onSubmit' ] );
116  $form->setWrapperLegendMsg( 'pageswithprop-legend' );
117  $form->addHeaderText( $this->msg( 'pageswithprop-text' )->parseAsBlock() );
118  $form->setSubmitTextMsg( 'pageswithprop-submit' );
119 
120  $form->prepareForm();
121  $form->displayForm( false );
122  if ( $propname !== '' && $propname !== null ) {
123  $form->trySubmit();
124  }
125  }
126 
127  public function onSubmit( $data, $form ) {
128  $this->propName = $data['propname'];
129  parent::execute( $data['propname'] );
130  }
131 
140  public function prefixSearchSubpages( $search, $limit, $offset ) {
141  $subpages = array_keys( $this->queryExistingProps( $limit, $offset ) );
142  // We've already limited and offsetted, set to N and 0 respectively.
143  return self::prefixSearchArray( $search, count( $subpages ), $subpages, 0 );
144  }
145 
150  public function isSyndicated() {
151  return false;
152  }
153 
154  public function getQueryInfo() {
155  $query = [
156  'tables' => [ 'page_props', 'page' ],
157  'fields' => [
158  'page_id' => 'pp_page',
159  'page_namespace',
160  'page_title',
161  'page_len',
162  'page_is_redirect',
163  'page_latest',
164  'pp_value',
165  ],
166  'conds' => [
167  'pp_propname' => $this->propName,
168  ],
169  'join_conds' => [
170  'page' => [ 'JOIN', 'page_id = pp_page' ]
171  ],
172  'options' => []
173  ];
174 
175  if ( $this->ns !== null ) {
176  $query['conds']['page_namespace'] = $this->ns;
177  }
178 
179  return $query;
180  }
181 
182  protected function getOrderFields() {
183  $sort = [ 'page_id' ];
184  if ( $this->sortByValue ) {
185  array_unshift( $sort, 'pp_sortkey' );
186  }
187  return $sort;
188  }
189 
193  public function sortDescending() {
194  return !$this->reverse;
195  }
196 
202  public function formatResult( $skin, $result ) {
203  $title = Title::newFromRow( $result );
204  $ret = $this->getLinkRenderer()->makeKnownLink( $title );
205  if ( $result->pp_value !== '' ) {
206  // Do not show very long or binary values on the special page
207  $valueLength = strlen( $result->pp_value );
208  $isBinary = strpos( $result->pp_value, "\0" ) !== false;
209  $isTooLong = $valueLength > 1024;
210 
211  if ( $isBinary || $isTooLong ) {
212  $message = $this
213  ->msg( $isBinary ? 'pageswithprop-prophidden-binary' : 'pageswithprop-prophidden-long' )
214  ->params( $this->getLanguage()->formatSize( $valueLength ) );
215 
216  $propValue = Html::element( 'span', [ 'class' => 'prop-value-hidden' ], $message->text() );
217  } else {
218  $propValue = Html::element( 'span', [ 'class' => 'prop-value' ], $result->pp_value );
219  }
220 
221  $ret .= $this->msg( 'colon-separator' )->escaped() . $propValue;
222  }
223 
224  return $ret;
225  }
226 
227  public function getExistingPropNames() {
228  if ( $this->existingPropNames === null ) {
229  $this->existingPropNames = $this->queryExistingProps();
230  }
232  }
233 
234  protected function queryExistingProps( $limit = null, $offset = 0 ) {
235  $opts = [
236  'DISTINCT', 'ORDER BY' => 'pp_propname'
237  ];
238  if ( $limit ) {
239  $opts['LIMIT'] = $limit;
240  }
241  if ( $offset ) {
242  $opts['OFFSET'] = $offset;
243  }
244 
245  $res = wfGetDB( DB_REPLICA )->select(
246  'page_props',
247  'pp_propname',
248  '',
249  __METHOD__,
250  $opts
251  );
252 
253  $propnames = [];
254  foreach ( $res as $row ) {
255  $propnames[$row->pp_propname] = $row->pp_propname;
256  }
257 
258  return $propnames;
259  }
260 
261  protected function getGroupName() {
262  return 'pages';
263  }
264 }
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:697
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:828
SpecialPagesWithProp\onSubmit
onSubmit( $data, $form)
Definition: SpecialPagesWithProp.php:127
SpecialPagesWithProp\queryExistingProps
queryExistingProps( $limit=null, $offset=0)
Definition: SpecialPagesWithProp.php:234
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:744
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:774
$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:39
QueryPage\$offset
int $offset
The offset and limit in use, as passed to the query() function.
Definition: QueryPage.php:44
QueryPage\$limit
int $limit
Definition: QueryPage.php:47
DerivativeContext
An IContextSource implementation which will inherit context from another source but allow individual ...
Definition: DerivativeContext.php:31
SpecialPagesWithProp\execute
execute( $par)
This is the actual workhorse.
Definition: SpecialPagesWithProp.php:65
SpecialPagesWithProp\isSyndicated
isSyndicated()
Disable RSS/Atom feeds.
Definition: SpecialPagesWithProp.php:150
Title\newFromRow
static newFromRow( $row)
Make a Title object from a DB row.
Definition: Title.php:524
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2467
SpecialPagesWithProp\$propName
string null $propName
Definition: SpecialPagesWithProp.php:35
SpecialPagesWithProp\$existingPropNames
string[] null $existingPropNames
Definition: SpecialPagesWithProp.php:40
$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:571
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
SpecialPagesWithProp\$reverse
bool $reverse
Definition: SpecialPagesWithProp.php:50
SpecialPagesWithProp\sortDescending
sortDescending()
Definition: SpecialPagesWithProp.php:193
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:717
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:561
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:734
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:140
SpecialPage\getLinkRenderer
getLinkRenderer()
Definition: SpecialPage.php:944
SpecialPagesWithProp\getQueryInfo
getQueryInfo()
Subclasses return an SQL query here, formatted as an array with the following keys: tables => Table(s...
Definition: SpecialPagesWithProp.php:154
SpecialPagesWithProp\formatResult
formatResult( $skin, $result)
Definition: SpecialPagesWithProp.php:202
SpecialPagesWithProp\getExistingPropNames
getExistingPropNames()
Definition: SpecialPagesWithProp.php:227
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
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:231
SpecialPagesWithProp\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialPagesWithProp.php:261
SpecialPagesWithProp\getOrderFields
getOrderFields()
Subclasses return an array of fields to order by here.
Definition: SpecialPagesWithProp.php:182
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:315
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:662