MediaWiki  1.34.0
SpecialGadgetUsage.php
Go to the documentation of this file.
1 <?php
29 
35  public function __construct( $name = 'GadgetUsage' ) {
36  parent::__construct( $name );
37  $this->limit = 1000; // Show all gadgets
38  $this->shownavigation = false;
39  $this->activeUsers = $this->getConfig()->get( 'SpecialGadgetUsageActiveUsers' );
40  }
41 
45  public function execute( $par ) {
46  parent::execute( $par );
47  $this->addHelpLink( 'Extension:Gadgets' );
48  }
49 
55  public $activeUsers;
56 
57  public function isExpensive() {
58  return true;
59  }
60 
80  public function getQueryInfo() {
81  $dbr = wfGetDB( DB_REPLICA );
82  if ( !$this->activeUsers ) {
83  return [
84  'tables' => [ 'user_properties' ],
85  'fields' => [
86  'title' => 'up_property',
87  'value' => 'SUM( up_value )',
88  'namespace' => NS_GADGET
89  ],
90  'conds' => [
91  'up_property' . $dbr->buildLike( 'gadget-', $dbr->anyString() )
92  ],
93  'options' => [
94  'GROUP BY' => [ 'up_property' ]
95  ]
96  ];
97  } else {
98  return [
99  'tables' => [ 'user_properties', 'user', 'querycachetwo' ],
100  'fields' => [
101  'title' => 'up_property',
102  'value' => 'SUM( up_value )',
103  // Need to pick fields existing in the querycache table so that the results are cachable
104  'namespace' => 'COUNT( qcc_title )'
105  ],
106  'conds' => [
107  'up_property' . $dbr->buildLike( 'gadget-', $dbr->anyString() )
108  ],
109  'options' => [
110  'GROUP BY' => [ 'up_property' ]
111  ],
112  'join_conds' => [
113  'user' => [
114  'LEFT JOIN', [
115  'up_user = user_id'
116  ]
117  ],
118  'querycachetwo' => [
119  'LEFT JOIN', [
120  'user_name = qcc_title',
121  'qcc_type = "activeusers"',
122  'up_value = 1'
123  ]
124  ]
125  ]
126  ];
127  }
128  }
129 
130  public function getOrderFields() {
131  return [ 'value' ];
132  }
133 
139  protected function outputTableStart() {
140  $html = Html::openElement( 'table', [ 'class' => [ 'sortable', 'wikitable' ] ] );
141  $html .= Html::openElement( 'thead', [] );
142  $html .= Html::openElement( 'tr', [] );
143  $headers = [ 'gadgetusage-gadget', 'gadgetusage-usercount' ];
144  if ( $this->activeUsers ) {
145  $headers[] = 'gadgetusage-activeusers';
146  }
147  foreach ( $headers as $h ) {
148  if ( $h == 'gadgetusage-gadget' ) {
149  $html .= Html::element( 'th', [], $this->msg( $h )->text() );
150  } else {
151  $html .= Html::element( 'th', [ 'data-sort-type' => 'number' ],
152  $this->msg( $h )->text() );
153  }
154  }
155  $html .= Html::closeElement( 'tr' );
156  $html .= Html::closeElement( 'thead' );
157  $html .= Html::openElement( 'tbody', [] );
158  $this->getOutput()->addHTML( $html );
159  $this->getOutput()->addModuleStyles( 'jquery.tablesorter.styles' );
160  $this->getOutput()->addModules( 'jquery.tablesorter' );
161  }
162 
167  protected function outputTableEnd() {
168  $this->getOutput()->addHTML(
169  Html::closeElement( 'tbody' ) .
170  Html::closeElement( 'table' )
171  );
172  }
173 
179  public function formatResult( $skin, $result ) {
180  $gadgetTitle = substr( $result->title, 7 );
181  $gadgetUserCount = $this->getLanguage()->formatNum( $result->value );
182  if ( $gadgetTitle ) {
183  $html = Html::openElement( 'tr', [] );
184  $html .= Html::element( 'td', [], $gadgetTitle );
185  $html .= Html::element( 'td', [], $gadgetUserCount );
186  if ( $this->activeUsers == true ) {
187  $activeUserCount = $this->getLanguage()->formatNum( $result->namespace );
188  $html .= Html::element( 'td', [], $activeUserCount );
189  }
190  $html .= Html::closeElement( 'tr' );
191  return $html;
192  }
193  return false;
194  }
195 
202  protected function getDefaultGadgets( $gadgetRepo, $gadgetIds ) {
203  $gadgetsList = [];
204  foreach ( $gadgetIds as $g ) {
205  $gadget = $gadgetRepo->getGadget( $g );
206  if ( $gadget->isOnByDefault() ) {
207  $gadgetsList[] = $gadget->getName();
208  }
209  }
210  asort( $gadgetsList, SORT_STRING | SORT_FLAG_CASE );
211  return $gadgetsList;
212  }
213 
225  protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
226  $gadgetRepo = GadgetRepo::singleton();
227  $gadgetIds = $gadgetRepo->getGadgetIds();
228  $defaultGadgets = $this->getDefaultGadgets( $gadgetRepo, $gadgetIds );
229  if ( $this->activeUsers ) {
230  $out->addHtml(
231  $this->msg( 'gadgetusage-intro' )
232  ->numParams( $this->getConfig()->get( 'ActiveUserDays' ) )->parseAsBlock()
233  );
234  } else {
235  $out->addHtml(
236  $this->msg( 'gadgetusage-intro-noactive' )->parseAsBlock()
237  );
238  }
239  if ( $num > 0 ) {
240  $this->outputTableStart();
241  // Append default gadgets to the table with 'default' in the total and active user fields
242  foreach ( $defaultGadgets as $default ) {
243  $html = Html::openElement( 'tr', [] );
244  $html .= Html::element( 'td', [], $default );
245  $html .= Html::element( 'td', [ 'data-sort-value' => 'Infinity' ],
246  $this->msg( 'gadgetusage-default' )->text() );
247  if ( $this->activeUsers ) {
248  $html .= Html::element( 'td', [ 'data-sort-value' => 'Infinity' ],
249  $this->msg( 'gadgetusage-default' )->text() );
250  }
251  $html .= Html::closeElement( 'tr' );
252  $out->addHTML( $html );
253  }
254  foreach ( $res as $row ) {
255  // Remove the 'gadget-' part of the result string and compare if it's present
256  // in $defaultGadgets, if not we format it and add it to the output
257  if ( !in_array( substr( $row->title, 7 ), $defaultGadgets ) ) {
258  // Only pick gadgets which are in the list $gadgetIds to make sure they exist
259  if ( in_array( substr( $row->title, 7 ), $gadgetIds ) ) {
260  $line = $this->formatResult( $skin, $row );
261  if ( $line ) {
262  $out->addHTML( $line );
263  }
264  }
265  }
266  }
267  // Close table element
268  $this->outputTableEnd();
269  } else {
270  $out->addHtml(
271  $this->msg( 'gadgetusage-noresults' )->parseAsBlock()
272  );
273  }
274  }
275 
276  protected function getGroupName() {
277  return 'wiki';
278  }
279 }
SpecialGadgetUsage\getOrderFields
getOrderFields()
Subclasses return an array of fields to order by here.
Definition: SpecialGadgetUsage.php:130
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:792
SpecialGadgetUsage\__construct
__construct( $name='GadgetUsage')
Definition: SpecialGadgetUsage.php:35
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:719
SpecialGadgetUsage\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialGadgetUsage.php:276
SpecialGadgetUsage\outputTableEnd
outputTableEnd()
Output the end of the table </tbody>
Definition: SpecialGadgetUsage.php:167
SpecialGadgetUsage\outputResults
outputResults( $out, $skin, $dbr, $res, $num, $offset)
Format and output report results using the given information plus OutputPage.
Definition: SpecialGadgetUsage.php:225
SpecialGadgetUsage\outputTableStart
outputTableStart()
Output the start of the table Including opening.
Definition: SpecialGadgetUsage.php:139
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
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
SpecialGadgetUsage\getQueryInfo
getQueryInfo()
Define the database query that is used to generate the stats table.
Definition: SpecialGadgetUsage.php:80
$dbr
$dbr
Definition: testCompression.php:50
SpecialGadgetUsage\$activeUsers
$activeUsers
Flag for holding the value of config variable SpecialGadgetUsageActiveUsers.
Definition: SpecialGadgetUsage.php:55
SpecialPage\addHelpLink
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: SpecialPage.php:828
SpecialPage\getConfig
getConfig()
Shortcut to get main config object.
Definition: SpecialPage.php:758
Wikimedia\Rdbms\IResultWrapper
Result wrapper for grabbing data queried from an IDatabase object.
Definition: IResultWrapper.php:24
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2575
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
GadgetRepo\singleton
static singleton()
Get the configured default GadgetRepo.
Definition: GadgetRepo.php:88
$line
$line
Definition: cdb.php:59
SpecialGadgetUsage\isExpensive
isExpensive()
Is this query expensive (for some definition of expensive)? Then we don't let it run in miser mode.
Definition: SpecialGadgetUsage.php:57
SpecialGadgetUsage
Special:GadgetUsage - Lists all the gadgets on the wiki along with number of users.
Definition: SpecialGadgetUsage.php:34
SpecialGadgetUsage\getDefaultGadgets
getDefaultGadgets( $gadgetRepo, $gadgetIds)
Get a list of default gadgets.
Definition: SpecialGadgetUsage.php:202
SpecialGadgetUsage\formatResult
formatResult( $skin, $result)
Definition: SpecialGadgetUsage.php:179
SpecialGadgetUsage\execute
execute( $par)
This is the actual workhorse.It does everything needed to make a real, honest-to-gosh query page.
Definition: SpecialGadgetUsage.php:45