Skip to content

Commit

Permalink
Add category filtering in page stats
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmontalvo3 committed Dec 12, 2014
1 parent 628e267 commit 673414e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
5 changes: 4 additions & 1 deletion i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
"watchanalytics-special-wikihistory-pagetitle": "Watch Analytics: Wiki historical watch statistics",
"watchanalytics-special-header-user": "User",
"watchanalytics-special-header-page-title": "page",
"watchanalytics-user-group-filter-label": "Filter by user group",
"watchanalytics-user-group-filter-label": "Filter by user group:",
"watchanalytics-user-group-no-filter": "(no group filter)",
"watchanalytics-category-filter-label": "Filter by page category:",
"watchanalytics-category-no-filter": "(no category filter)",
"watchanalytics-special-header-watches": "# watches",
"watchanalytics-special-header-pending-watches": "# pending watches",
"watchanalytics-special-header-reviewed-watches": "# reviewed watches",
Expand Down
9 changes: 8 additions & 1 deletion includes/PageWatchesQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function getQueryInfo( $conds = null ) {
$this->sqlAvgPendingMins,
);

$this->conds = $conds ? $conds : array();
$this->conds = $conds ? $conds : array( 'p.page_namespace IS NOT NULL' );

$this->tables = array( 'w' => 'watchlist' );

Expand All @@ -80,6 +80,13 @@ function getQueryInfo( $conds = null ) {
'RIGHT JOIN', 'p.page_namespace=w.wl_namespace AND p.page_title=w.wl_title'
);

// optionally join the 'categorylinks' table to filter by page category
if ( $this->categoryFilter ) {
$this->tables['cat'] = 'categorylinks';
$this->join_conds['cat'] = array(
'RIGHT JOIN', "cat.cl_from = p.page_id AND cat.cl_to = \"{$this->categoryFilter}\""
);
}

$this->options = array(
// 'GROUP BY' => 'w.wl_title, w.wl_namespace'
Expand Down
43 changes: 40 additions & 3 deletions includes/WatchAnalyticsTablePager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ function __construct( $page, $conds, $filters=array() ) {
trim( $filters['groupfilter'] )
);
}

if ( $filters['categoryfilter'] ) {
$this->watchQuery->setCategoryFilter(
trim( $filters['categoryfilter'] )
);
}

// $this->mIndexField = 'am_title';
// $this->mPage = $page;
Expand Down Expand Up @@ -179,9 +185,9 @@ function getPagingQueries() {
public function buildForm() {
global $wgScript;

$groupFilter = new XmlSelect( 'groupfilter', false, $this->filters['groupfilter'] );
// user group filter
$groups = array( $this->msg( 'watchanalytics-user-group-no-filter' )->text() => '');
$rawGroups = User::getAllGroups();
$groups = array();
foreach( $rawGroups as $group ) {
$labelMsg = $this->msg( 'group-' . $group );
if ( $labelMsg->exists() ) {
Expand All @@ -192,8 +198,27 @@ public function buildForm() {
}
$groups[ $label ] = $group;
}
$groupFilter = new XmlSelect( 'groupfilter', false, $this->filters['groupfilter'] );
$groupFilter->addOptions( $groups );

// category filter
$dbr = wfGetDB( DB_SLAVE );
$result = $dbr->select(
'categorylinks',
'cl_to',
'',
__METHOD__,
array( 'DISTINCT' )
);
$categories = array( $this->msg( 'watchanalytics-category-no-filter' )->text() => '');
while ( $row = $result->fetchRow() ) {
$category = Category::newFromName( $row['cl_to'] );
$label = $category->getTitle()->getText();

$categories[ $label ] = $row['cl_to'];
}
$categoryFilter = new XmlSelect( 'categoryfilter', false, $this->filters['categoryfilter'] );
$categoryFilter->addOptions( $categories );

$out =
// create the form element
Expand Down Expand Up @@ -222,6 +247,18 @@ public function buildForm() {
'</td>
</tr>' .

// filter results by page category
'<tr>
<td class="mw-label">' .
Xml::label(
$this->msg( 'watchanalytics-category-filter-label' )->text(),
'ext-watchanalytics-category-filter'
) .
'</td>
<td class="mw-input">' .
$categoryFilter->getHTML() .
'</td>
</tr>' .

// limit results returned
'<tr>
Expand All @@ -246,7 +283,7 @@ public function buildForm() {

// FIXME: are all of these needed? are additional need to support
// WatchAnalytics fields?
$this->getHiddenFields( array( 'title', 'prefix', 'filter', 'lang', 'limit', 'groupfilter' ) ) .
$this->getHiddenFields( array( 'title', 'prefix', 'filter', 'lang', 'limit', 'groupfilter', 'categoryfilter' ) ) .

// close fieldset and form elements
Xml::closeElement( 'fieldset' ) .
Expand Down
7 changes: 7 additions & 0 deletions includes/WatchesQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,11 @@ public function setUserGroupFilter ( $ugf ) {
$this->userGroupFilter = $ugf;
}
}

public function setCategoryFilter ( $cf ) {
if ( $cf ) {
$this->categoryFilter = $cf;
}
}

}
8 changes: 7 additions & 1 deletion specials/SpecialWatchAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,14 @@ function execute( $parser = null ) {
}

$filters = array(
'groupfilter' => $wgRequest->getVal( 'groupfilter', false ),
'groupfilter' => $wgRequest->getVal( 'groupfilter', '' ),
'categoryfilter' => $wgRequest->getVal( 'categoryfilter', '' ),
);
foreach( $filters as &$filter ) {
if ( $filter === '' ) {
$filter = false;
}
}

$wgOut->addHTML( $this->getPageHeader() );
if ($this->mMode == 'users') {
Expand Down

0 comments on commit 673414e

Please sign in to comment.