MediaWiki REL1_31
SpecialGadgetUsage.php
Go to the documentation of this file.
1<?php
29
35 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
48
49 public function isExpensive() {
50 return true;
51 }
52
72 public function getQueryInfo() {
74 if ( !$this->activeUsers ) {
75 return [
76 'tables' => [ 'user_properties' ],
77 'fields' => [
78 'title' => 'up_property',
79 'value' => 'SUM( up_value )',
80 'namespace' => NS_GADGET
81 ],
82 'conds' => [
83 'up_property' . $dbr->buildLike( 'gadget-', $dbr->anyString() )
84 ],
85 'options' => [
86 'GROUP BY' => [ 'up_property' ]
87 ]
88 ];
89 } else {
90 return [
91 'tables' => [ 'user_properties', 'user', 'querycachetwo' ],
92 'fields' => [
93 'title' => 'up_property',
94 'value' => 'SUM( up_value )',
95 // Need to pick fields existing in the querycache table so that the results are cachable
96 'namespace' => 'COUNT( qcc_title )'
97 ],
98 'conds' => [
99 'up_property' . $dbr->buildLike( 'gadget-', $dbr->anyString() )
100 ],
101 'options' => [
102 'GROUP BY' => [ 'up_property' ]
103 ],
104 'join_conds' => [
105 'user' => [
106 'LEFT JOIN', [
107 'up_user = user_id'
108 ]
109 ],
110 'querycachetwo' => [
111 'LEFT JOIN', [
112 'user_name = qcc_title',
113 'qcc_type = "activeusers"',
114 'up_value = 1'
115 ]
116 ]
117 ]
118 ];
119 }
120 }
121
122 public function getOrderFields() {
123 return [ 'value' ];
124 }
125
130 protected function outputTableStart() {
131 $html = Html::openElement( 'table', [ 'class' => [ 'sortable', 'wikitable' ] ] );
132 $html .= Html::openElement( 'tr', [] );
133 $headers = [ 'gadgetusage-gadget', 'gadgetusage-usercount' ];
134 if ( $this->activeUsers ) {
135 $headers[] = 'gadgetusage-activeusers';
136 }
137 foreach ( $headers as $h ) {
138 if ( $h == 'gadgetusage-gadget' ) {
139 $html .= Html::element( 'th', [], $this->msg( $h )->text() );
140 } else {
141 $html .= Html::element( 'th', [ 'data-sort-type' => 'number' ],
142 $this->msg( $h )->text() );
143 }
144 }
145 $html .= Html::closeElement( 'tr' );
146 $this->getOutput()->addHTML( $html );
147 }
148
154 public function formatResult( $skin, $result ) {
155 $gadgetTitle = substr( $result->title, 7 );
156 $gadgetUserCount = $this->getLanguage()->formatNum( $result->value );
157 if ( $gadgetTitle ) {
158 $html = Html::openElement( 'tr', [] );
159 $html .= Html::element( 'td', [], $gadgetTitle );
160 $html .= Html::element( 'td', [], $gadgetUserCount );
161 if ( $this->activeUsers == true ) {
162 $activeUserCount = $this->getLanguage()->formatNum( $result->namespace );
163 $html .= Html::element( 'td', [], $activeUserCount );
164 }
165 $html .= Html::closeElement( 'tr' );
166 return $html;
167 }
168 return false;
169 }
170
177 protected function getDefaultGadgets( $gadgetRepo, $gadgetIds ) {
178 $gadgetsList = [];
179 foreach ( $gadgetIds as $g ) {
180 $gadget = $gadgetRepo->getGadget( $g );
181 if ( $gadget->isOnByDefault() ) {
182 $gadgetsList[] = $gadget->getName();
183 }
184 }
185 asort( $gadgetsList, SORT_STRING | SORT_FLAG_CASE );
186 return $gadgetsList;
187 }
188
200 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
201 $gadgetRepo = GadgetRepo::singleton();
202 $gadgetIds = $gadgetRepo->getGadgetIds();
203 $defaultGadgets = $this->getDefaultGadgets( $gadgetRepo, $gadgetIds );
204 if ( $this->activeUsers ) {
205 $out->addHtml(
206 $this->msg( 'gadgetusage-intro' )
207 ->numParams( $this->getConfig()->get( 'ActiveUserDays' ) )->parseAsBlock()
208 );
209 } else {
210 $out->addHtml(
211 $this->msg( 'gadgetusage-intro-noactive' )->parseAsBlock()
212 );
213 }
214 if ( $num > 0 ) {
215 $this->outputTableStart();
216 // Append default gadgets to the table with 'default' in the total and active user fields
217 foreach ( $defaultGadgets as $default ) {
218 $html = Html::openElement( 'tr', [] );
219 $html .= Html::element( 'td', [], $default );
220 $html .= Html::element( 'td', [], $this->msg( 'gadgetusage-default' )->text() );
221 if ( $this->activeUsers ) {
222 $html .= Html::element( 'td', [], $this->msg( 'gadgetusage-default' )->text() );
223 }
224 $html .= Html::closeElement( 'tr' );
225 $out->addHTML( $html );
226 }
227 foreach ( $res as $row ) {
228 // Remove the 'gadget-' part of the result string and compare if it's present
229 // in $defaultGadgets, if not we format it and add it to the output
230 if ( !in_array( substr( $row->title, 7 ), $defaultGadgets ) ) {
231 // Only pick gadgets which are in the list $gadgetIds to make sure they exist
232 if ( in_array( substr( $row->title, 7 ), $gadgetIds ) ) {
233 $line = $this->formatResult( $skin, $row );
234 if ( $line ) {
235 $out->addHTML( $line );
236 }
237 }
238 }
239 }
240 // Close table element
241 $out->addHtml( Html::closeElement( 'table' ) );
242 } else {
243 $out->addHtml(
244 $this->msg( 'gadgetusage-noresults' )->parseAsBlock()
245 );
246 }
247 }
248
249 protected function getGroupName() {
250 return 'wiki';
251 }
252}
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
$line
Definition cdb.php:59
static singleton()
Get the configured default GadgetRepo.
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:34
int $offset
The offset and limit in use, as passed to the query() function.
Definition QueryPage.php:39
Special:GadgetUsage - Lists all the gadgets on the wiki along with number of users.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
$activeUsers
Flag for holding the value of config variable SpecialGadgetUsageActiveUsers.
getQueryInfo()
Define the database query that is used to generate the stats table.
formatResult( $skin, $result)
getDefaultGadgets( $gadgetRepo, $gadgetIds)
Get a list of default gadgets.
outputTableStart()
Output the start of the table Including opening.
__construct( $name='GadgetUsage')
isExpensive()
Is this query expensive (for some definition of expensive)? Then we don't let it run in miser mode.
getOrderFields()
Subclasses return an array of fields to order by here.
outputResults( $out, $skin, $dbr, $res, $num, $offset)
Format and output report results using the given information plus OutputPage.
getOutput()
Get the OutputPage being used for this instance.
msg( $key)
Wrapper around wfMessage that sets the current context.
getConfig()
Shortcut to get main config object.
getLanguage()
Shortcut to get user's language.
$res
Definition database.txt:21
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition design.txt:18
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:864
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition hooks.txt:2013
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
Result wrapper for grabbing data queried from an IDatabase object.
const DB_REPLICA
Definition defines.php:25