2015-10-20 06:27:23 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Implements Special:GadgetUsage
|
|
|
|
*
|
|
|
|
* Copyright © 2015 Niharika Kohli
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup SpecialPage
|
|
|
|
* @author Niharika Kohli <niharika@wikimedia.org>
|
|
|
|
*/
|
|
|
|
|
2018-02-25 02:41:50 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2018-04-07 08:35:58 +00:00
|
|
|
use Wikimedia\Rdbms\IResultWrapper;
|
2018-02-25 02:41:50 +00:00
|
|
|
|
2015-10-20 06:27:23 +00:00
|
|
|
/**
|
|
|
|
* Special:GadgetUsage - Lists all the gadgets on the wiki along with number of users.
|
|
|
|
* @ingroup SpecialPage
|
|
|
|
*/
|
|
|
|
class SpecialGadgetUsage extends QueryPage {
|
|
|
|
function __construct( $name = 'GadgetUsage' ) {
|
|
|
|
parent::__construct( $name );
|
|
|
|
$this->limit = 1000; // Show all gadgets
|
|
|
|
$this->shownavigation = false;
|
2015-12-17 14:03:21 +00:00
|
|
|
$this->activeUsers = $this->getConfig()->get( 'SpecialGadgetUsageActiveUsers' );
|
2015-10-20 06:27:23 +00:00
|
|
|
}
|
|
|
|
|
2015-12-17 14:03:21 +00:00
|
|
|
/**
|
|
|
|
* Flag for holding the value of config variable SpecialGadgetUsageActiveUsers
|
|
|
|
*
|
|
|
|
* @var bool $activeUsers
|
|
|
|
*/
|
|
|
|
public $activeUsers;
|
|
|
|
|
2015-10-20 06:27:23 +00:00
|
|
|
public function isExpensive() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-30 16:13:18 +00:00
|
|
|
/**
|
2015-12-18 21:48:40 +00:00
|
|
|
* Define the database query that is used to generate the stats table.
|
|
|
|
* This uses 1 of 2 possible queries, depending on $wgSpecialGadgetUsageActiveUsers.
|
|
|
|
*
|
|
|
|
* The simple query is essentially:
|
|
|
|
* SELECT up_property, SUM(up_value)
|
|
|
|
* FROM user_properties
|
|
|
|
* WHERE up_property LIKE 'gadget-%'
|
|
|
|
* GROUP BY up_property;
|
|
|
|
*
|
|
|
|
* The more expensive query is:
|
2015-11-30 16:13:18 +00:00
|
|
|
* SELECT up_property, SUM(up_value), count(qcc_title)
|
|
|
|
* FROM user_properties
|
|
|
|
* LEFT JOIN user ON up_user = user_id
|
|
|
|
* LEFT JOIN querycachetwo ON user_name = qcc_title AND qcc_type = 'activeusers' AND up_value = 1
|
|
|
|
* WHERE up_property LIKE 'gadget-%'
|
|
|
|
* GROUP BY up_property;
|
2017-08-27 14:07:08 +00:00
|
|
|
* @return array
|
2015-11-30 16:13:18 +00:00
|
|
|
*/
|
2015-10-20 06:27:23 +00:00
|
|
|
public function getQueryInfo() {
|
2017-09-24 05:26:03 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2015-12-17 14:03:21 +00:00
|
|
|
if ( !$this->activeUsers ) {
|
2016-12-28 10:25:47 +00:00
|
|
|
return [
|
|
|
|
'tables' => [ 'user_properties' ],
|
|
|
|
'fields' => [
|
2015-12-17 14:03:21 +00:00
|
|
|
'title' => 'up_property',
|
|
|
|
'value' => 'SUM( up_value )',
|
|
|
|
'namespace' => NS_GADGET
|
2016-12-28 10:25:47 +00:00
|
|
|
],
|
|
|
|
'conds' => [
|
2015-12-17 14:03:21 +00:00
|
|
|
'up_property' . $dbr->buildLike( 'gadget-', $dbr->anyString() )
|
2016-12-28 10:25:47 +00:00
|
|
|
],
|
|
|
|
'options' => [
|
|
|
|
'GROUP BY' => [ 'up_property' ]
|
|
|
|
]
|
|
|
|
];
|
2015-12-17 14:03:21 +00:00
|
|
|
} else {
|
2016-12-28 10:25:47 +00:00
|
|
|
return [
|
|
|
|
'tables' => [ 'user_properties', 'user', 'querycachetwo' ],
|
|
|
|
'fields' => [
|
2015-12-17 14:03:21 +00:00
|
|
|
'title' => 'up_property',
|
|
|
|
'value' => 'SUM( up_value )',
|
|
|
|
// Need to pick fields existing in the querycache table so that the results are cachable
|
|
|
|
'namespace' => 'COUNT( qcc_title )'
|
2016-12-28 10:25:47 +00:00
|
|
|
],
|
|
|
|
'conds' => [
|
2015-12-17 14:03:21 +00:00
|
|
|
'up_property' . $dbr->buildLike( 'gadget-', $dbr->anyString() )
|
2016-12-28 10:25:47 +00:00
|
|
|
],
|
|
|
|
'options' => [
|
|
|
|
'GROUP BY' => [ 'up_property' ]
|
|
|
|
],
|
|
|
|
'join_conds' => [
|
|
|
|
'user' => [
|
|
|
|
'LEFT JOIN', [
|
2015-12-17 14:03:21 +00:00
|
|
|
'up_user = user_id'
|
2016-12-28 10:25:47 +00:00
|
|
|
]
|
|
|
|
],
|
|
|
|
'querycachetwo' => [
|
|
|
|
'LEFT JOIN', [
|
2015-12-17 14:03:21 +00:00
|
|
|
'user_name = qcc_title',
|
|
|
|
'qcc_type = "activeusers"',
|
|
|
|
'up_value = 1'
|
2016-12-28 10:25:47 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
2015-12-17 14:03:21 +00:00
|
|
|
}
|
2015-10-20 06:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getOrderFields() {
|
2016-12-28 10:25:47 +00:00
|
|
|
return [ 'value' ];
|
2015-10-20 06:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output the start of the table
|
|
|
|
* Including opening <table>, and first <tr> with column headers.
|
|
|
|
*/
|
|
|
|
protected function outputTableStart() {
|
2016-12-28 10:25:47 +00:00
|
|
|
$html = Html::openElement( 'table', [ 'class' => [ 'sortable', 'wikitable' ] ] );
|
|
|
|
$html .= Html::openElement( 'tr', [] );
|
|
|
|
$headers = [ 'gadgetusage-gadget', 'gadgetusage-usercount' ];
|
2015-12-17 14:03:21 +00:00
|
|
|
if ( $this->activeUsers ) {
|
|
|
|
$headers[] = 'gadgetusage-activeusers';
|
|
|
|
}
|
2017-05-30 18:38:47 +00:00
|
|
|
foreach ( $headers as $h ) {
|
2016-01-22 15:17:30 +00:00
|
|
|
if ( $h == 'gadgetusage-gadget' ) {
|
2016-12-28 10:25:47 +00:00
|
|
|
$html .= Html::element( 'th', [], $this->msg( $h )->text() );
|
2016-01-22 15:17:30 +00:00
|
|
|
} else {
|
2016-12-28 10:25:47 +00:00
|
|
|
$html .= Html::element( 'th', [ 'data-sort-type' => 'number' ],
|
2016-01-22 15:17:30 +00:00
|
|
|
$this->msg( $h )->text() );
|
|
|
|
}
|
2015-10-20 06:27:23 +00:00
|
|
|
}
|
|
|
|
$html .= Html::closeElement( 'tr' );
|
|
|
|
$this->getOutput()->addHTML( $html );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Skin $skin
|
|
|
|
* @param object $result Result row
|
2015-10-27 09:23:42 +00:00
|
|
|
* @return string|bool String of HTML
|
2015-10-20 06:27:23 +00:00
|
|
|
*/
|
|
|
|
public function formatResult( $skin, $result ) {
|
2015-10-27 09:23:42 +00:00
|
|
|
$gadgetTitle = substr( $result->title, 7 );
|
2015-10-20 06:27:23 +00:00
|
|
|
$gadgetUserCount = $this->getLanguage()->formatNum( $result->value );
|
|
|
|
if ( $gadgetTitle ) {
|
2016-12-28 10:25:47 +00:00
|
|
|
$html = Html::openElement( 'tr', [] );
|
|
|
|
$html .= Html::element( 'td', [], $gadgetTitle );
|
|
|
|
$html .= Html::element( 'td', [], $gadgetUserCount );
|
2015-12-17 14:03:21 +00:00
|
|
|
if ( $this->activeUsers == true ) {
|
|
|
|
$activeUserCount = $this->getLanguage()->formatNum( $result->namespace );
|
2016-12-28 10:25:47 +00:00
|
|
|
$html .= Html::element( 'td', [], $activeUserCount );
|
2015-12-17 14:03:21 +00:00
|
|
|
}
|
2015-10-20 06:27:23 +00:00
|
|
|
$html .= Html::closeElement( 'tr' );
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-29 21:24:49 +00:00
|
|
|
/**
|
|
|
|
* Get a list of default gadgets
|
2015-11-03 00:57:37 +00:00
|
|
|
* @param GadgetRepo $gadgetRepo
|
|
|
|
* @param array $gadgetIds list of gagdet ids registered in the wiki
|
|
|
|
* @return array
|
2017-10-21 04:22:47 +00:00
|
|
|
*/
|
2015-11-03 00:57:37 +00:00
|
|
|
protected function getDefaultGadgets( $gadgetRepo, $gadgetIds ) {
|
2016-12-28 10:25:47 +00:00
|
|
|
$gadgetsList = [];
|
2015-10-29 21:24:49 +00:00
|
|
|
foreach ( $gadgetIds as $g ) {
|
|
|
|
$gadget = $gadgetRepo->getGadget( $g );
|
|
|
|
if ( $gadget->isOnByDefault() ) {
|
|
|
|
$gadgetsList[] = $gadget->getName();
|
|
|
|
}
|
|
|
|
}
|
2015-12-14 05:22:02 +00:00
|
|
|
asort( $gadgetsList, SORT_STRING | SORT_FLAG_CASE );
|
2015-10-29 21:24:49 +00:00
|
|
|
return $gadgetsList;
|
|
|
|
}
|
|
|
|
|
2015-10-20 06:27:23 +00:00
|
|
|
/**
|
|
|
|
* Format and output report results using the given information plus
|
|
|
|
* OutputPage
|
|
|
|
*
|
|
|
|
* @param OutputPage $out OutputPage to print to
|
|
|
|
* @param Skin $skin User skin to use
|
|
|
|
* @param IDatabase $dbr Database (read) connection to use
|
2018-04-07 08:35:58 +00:00
|
|
|
* @param IResultWrapper $res Result pointer
|
2015-10-20 06:27:23 +00:00
|
|
|
* @param int $num Number of available result rows
|
|
|
|
* @param int $offset Paging offset
|
|
|
|
*/
|
|
|
|
protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
|
2015-11-03 00:57:37 +00:00
|
|
|
$gadgetRepo = GadgetRepo::singleton();
|
|
|
|
$gadgetIds = $gadgetRepo->getGadgetIds();
|
|
|
|
$defaultGadgets = $this->getDefaultGadgets( $gadgetRepo, $gadgetIds );
|
2015-12-17 14:03:21 +00:00
|
|
|
if ( $this->activeUsers ) {
|
|
|
|
$out->addHtml(
|
2017-05-30 18:38:47 +00:00
|
|
|
$this->msg( 'gadgetusage-intro' )
|
|
|
|
->numParams( $this->getConfig()->get( 'ActiveUserDays' ) )->parseAsBlock()
|
2015-12-17 14:03:21 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$out->addHtml(
|
|
|
|
$this->msg( 'gadgetusage-intro-noactive' )->parseAsBlock()
|
|
|
|
);
|
|
|
|
}
|
2015-10-20 06:27:23 +00:00
|
|
|
if ( $num > 0 ) {
|
|
|
|
$this->outputTableStart();
|
2015-12-14 05:22:02 +00:00
|
|
|
// Append default gadgets to the table with 'default' in the total and active user fields
|
|
|
|
foreach ( $defaultGadgets as $default ) {
|
2016-12-28 10:25:47 +00:00
|
|
|
$html = Html::openElement( 'tr', [] );
|
|
|
|
$html .= Html::element( 'td', [], $default );
|
2016-12-28 12:49:53 +00:00
|
|
|
$html .= Html::element( 'td', [], $this->msg( 'gadgetusage-default' )->text() );
|
2015-12-17 14:03:21 +00:00
|
|
|
if ( $this->activeUsers ) {
|
2016-12-28 12:49:53 +00:00
|
|
|
$html .= Html::element( 'td', [], $this->msg( 'gadgetusage-default' )->text() );
|
2015-12-17 14:03:21 +00:00
|
|
|
}
|
2015-12-14 05:22:02 +00:00
|
|
|
$html .= Html::closeElement( 'tr' );
|
|
|
|
$out->addHTML( $html );
|
|
|
|
}
|
2015-10-20 06:27:23 +00:00
|
|
|
foreach ( $res as $row ) {
|
2015-10-29 21:24:49 +00:00
|
|
|
// Remove the 'gadget-' part of the result string and compare if it's present
|
|
|
|
// in $defaultGadgets, if not we format it and add it to the output
|
|
|
|
if ( !in_array( substr( $row->title, 7 ), $defaultGadgets ) ) {
|
2015-11-03 00:57:37 +00:00
|
|
|
// Only pick gadgets which are in the list $gadgetIds to make sure they exist
|
|
|
|
if ( in_array( substr( $row->title, 7 ), $gadgetIds ) ) {
|
|
|
|
$line = $this->formatResult( $skin, $row );
|
|
|
|
if ( $line ) {
|
|
|
|
$out->addHTML( $line );
|
|
|
|
}
|
2015-10-29 21:24:49 +00:00
|
|
|
}
|
2015-10-20 06:27:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Close table element
|
|
|
|
$out->addHtml( Html::closeElement( 'table' ) );
|
|
|
|
} else {
|
|
|
|
$out->addHtml(
|
|
|
|
$this->msg( 'gadgetusage-noresults' )->parseAsBlock()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getGroupName() {
|
|
|
|
return 'wiki';
|
|
|
|
}
|
|
|
|
}
|