Fz-29 has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/365091 )
Change subject: Work in progress Add support for hierarchy fields in Drilldown
......................................................................
Work in progress
Add support for hierarchy fields in Drilldown
Change-Id: Ic465b978a1f40f00c66fbdf1b29f8ebe71f2fc55
---
M CargoHierarchy.php
M CargoUtils.php
M drilldown/CargoSpecialDrilldown.php
3 files changed, 172 insertions(+), 6 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Cargo
refs/changes/91/365091/1
diff --git a/CargoHierarchy.php b/CargoHierarchy.php
index 983008c..8a41eba 100644
--- a/CargoHierarchy.php
+++ b/CargoHierarchy.php
@@ -13,6 +13,10 @@
public $mLeft = 0;
public $mRight = 0;
+ // Helper class member for Drilldown
+ public $mPageCount = 0;
+ public $mExclusivePageCount = 0;
+
function __construct( $curTitle = '__pseudo_root__' ) {
$this->mTitle = $curTitle;
$this->mChildren = array();
@@ -42,6 +46,7 @@
$curParentNode = $fullTree->getLastNodeForLevel(
$numBullets );
$curParentNode->addChild( new CargoHierarchy( $lineText
) );
}
+ $fullTree->computeLeftRight();
return $fullTree;
}
@@ -55,7 +60,6 @@
function generateHierarchyStructureTableData() {
$tableData = array();
- $this->computeLeftRight();
// Preorder traversal using Stack data structure
$stack = new SplStack();
$stack->push( $this );
@@ -83,4 +87,63 @@
$this->mRight = $counter;
$counter += 1;
}
+
+ function computeNodeCountByFilter( $f, $fullTextSearchTerm,
$appliedFilters ) {
+ $cdb = CargoUtils::getDB();
+ $countArg = "";
+
+ list( $tableNames, $conds, $joinConds ) = $f->getQueryParts(
$fullTextSearchTerm, $appliedFilters );
+ if ( $f->fieldDescription->mIsList ) {
+ $countArg = "_rowID";
+ $fieldTableName = $f->tableName . '__' . $f->name;
+ $tableNames[] = $fieldTableName;
+ // $fieldName = CargoUtils::escapedFieldName( $cdb,
$fieldTableName, '_value' );
+ $fieldName = '_value';
+ $joinConds[$fieldTableName] =
CargoUtils::joinOfMainAndFieldTable( $cdb, $f->tableName, $fieldTableName );
+ } else {
+ $countArg = "_ID";
+ $fieldName = $f->name;
+ $fieldTableName = $f->tableName;
+ }
+
+ // Join hierarchy structure table __hierarchy with the main
table
+ $hierarchyTableName = $f->tableName . '__' . $f->name .
'__hierarchy';
+ $tableNames[] = $hierarchyTableName;
+
+ $joinConds[$hierarchyTableName] =
CargoUtils::joinOfSingleFieldAndHierarchyTable( $cdb,
+ $fieldTableName, $fieldName, $hierarchyTableName );
+
+ // Need of following?
+ // if ( $f->searchableFiles ) {
+ // $countClause = "COUNT(DISTINCT
cargo__{$f->tableName}._pageID) AS total";
+ // } else {
+ // $countClause = "COUNT(*) AS total";
+ // }
+
+ $countClause = "COUNT(DISTINCT($countArg)) AS total";
+ $hierarchyConds = array();
+ $exclusiveHierarchyConds = array();
+ $hierarchyConds[] = "_left >= $this->mLeft";
+ $hierarchyConds[] = "_right <= $this->mRight";
+ $exclusiveHierarchyConds[] = "_left = $this->mLeft";
+
+ // Hierarchy Node Count
+ $res = $cdb->select( $tableNames, array( $countClause ),
array_merge( $conds, $hierarchyConds ),
+ null, null, $joinConds );
+ $count = 0;
+ while ( $row = $cdb->fetchRow( $res ) ) {
+ $count = $row['total'];
+ }
+ $this->mPageCount = $count;
+ $cdb->freeResult( $res );
+ // Exclusive Node Count
+ $res = $cdb->select( $tableNames, array( $countClause ),
array_merge( $conds, $exclusiveHierarchyConds ),
+ null, null, $joinConds );
+ $count = 0;
+ while ( $row = $cdb->fetchRow( $res ) ) {
+ $count = $row['total'];
+ }
+ $this->mExclusivePageCount = $count;
+ $cdb->freeResult( $res );
+ }
}
\ No newline at end of file
diff --git a/CargoUtils.php b/CargoUtils.php
index eaf1392..a1686be 100644
--- a/CargoUtils.php
+++ b/CargoUtils.php
@@ -908,6 +908,15 @@
);
}
+ public static function joinOfSingleFieldAndHierarchyTable( $cdb,
$singleFieldTableName, $fieldColumnName, $hierarchyTableName ) {
+ return array(
+ 'LEFT OUTER JOIN',
+ self::escapedFieldName( $cdb, $singleFieldTableName,
$fieldColumnName ) .
+ ' = ' .
+ self::escapedFieldName( $cdb,
$hierarchyTableName, '_value' )
+ );
+ }
+
public static function escapedInsert( $db, $tableName, $fieldValues ) {
// Put quotes around the field names - needed for Postgres,
// which otherwise lowercases all field names.
diff --git a/drilldown/CargoSpecialDrilldown.php
b/drilldown/CargoSpecialDrilldown.php
index 5cc2126..7891829 100644
--- a/drilldown/CargoSpecialDrilldown.php
+++ b/drilldown/CargoSpecialDrilldown.php
@@ -528,6 +528,92 @@
return $results_line;
}
+ function printUnappliedFilterValuesForHierarachy( $cur_url, $f,
$fullTextSearchTerm, $applied_filters ) {
+ $results_line = "";
+ // construct the CargoHierarchyTree for drilldown
+ $hierarchyTreeRoot = CargoHierarchy::newFromWikiText(
$f->fieldDescription->mHierarchyStructure );
+ $num_printed_values = 0;
+ // count and print side by side
+ // TODO implement for list of hierarchy type
+ if( true || $f->fieldDescription->mIsList === false ){
+ $stack = new SplStack();
+ // preorder traversal of the tree
+ $stack->push( $hierarchyTreeRoot );
+ while( !$stack->isEmpty() ) {
+ $node = $stack->pop();
+ if( $node != ")" ) {
+ if( $node->mLeft !== 1 ) {
+ // check if its not
__pseudo_root__ node, then only print
+
$node->computeNodeCountByFilter( $f, $fullTextSearchTerm, $applied_filters );
+ if ( $num_printed_values++ > 0 ) {
+ $results_line .= " · ";
+ }
+ $results_line .=
$this->getFilterByValueHrefElements( $f, $node->mTitle, $node->mPageCount,
$cur_url );
+
+ }
+ if( count( $node->mChildren ) > 0 ) {
+ if( $node->mLeft !== 1 ) {
+ $results_line .= " · ";
+ $results_line .= "(" .
$this->getFilterByValueHrefElements( $f, $node->mTitle . " only" ,
$node->mExclusivePageCount, $cur_url );
+ $stack->push( ")" );
+ }
+ $i = count( $node->mChildren )
- 1;
+ while( $i >= 0 )
+ {
+ $stack->push($node->mChildren[$i]);
+ $i = $i - 1;
+ }
+ }
+ }
+ else {
+ $results_line .= ") ";
+ }
+ }
+ }
+ return $results_line;
+ }
+
+ function getFilterByValueHrefElements( $f, $value_str, $num_results,
$cur_url ) {
+
+ global $wgCargoDrilldownSmallestFontSize,
$wgCargoDrilldownLargestFontSize;
+ // set font-size values for filter "tag cloud", if the
+ // appropriate global variables are set
+ if ( $wgCargoDrilldownSmallestFontSize > 0 &&
$wgCargoDrilldownLargestFontSize > 0 ) {
+ $lowest_num_results = min( $filter_values );
+ $highest_num_results = max( $filter_values );
+ if ( $lowest_num_results != $highest_num_results ) {
+ $scale_factor = (
$wgCargoDrilldownLargestFontSize - $wgCargoDrilldownSmallestFontSize ) /
+ ( log( $highest_num_results ) - log(
$lowest_num_results ) );
+ }
+ }
+
+ $result_line_part = "";
+ $filter_text = "";
+ $filter_text = $this->printFilterValue( $f, $value_str );
+ $filter_text .= " ($num_results)";
+ $filter_url = $cur_url . urlencode( str_replace( ' ', '_',
$f->name ) ) . '=' .
+ urlencode( str_replace( ' ', '_', $value_str ) );
+ if ( $wgCargoDrilldownSmallestFontSize > 0 &&
$wgCargoDrilldownLargestFontSize > 0 ) {
+ if ( $lowest_num_results != $highest_num_results ) {
+ $font_size = round( ((log( $num_results ) -
log( $lowest_num_results )) * $scale_factor ) +
+ $wgCargoDrilldownSmallestFontSize );
+ } else {
+ $font_size = (
$wgCargoDrilldownSmallestFontSize + $wgCargoDrilldownLargestFontSize ) / 2;
+ }
+ $result_line_part .= "\n\t\t\t\t\t\t" .
Html::rawElement( 'a',
+ array( 'href' => $filter_url,
+ 'title' => $this->msg(
'cargo-drilldown-filterbyvalue' )->text(),
+ 'style' => "font-size: {$font_size}px"
+ ), $filter_text );
+ } else {
+ $result_line_part .= "\n\t\t\t\t\t\t" .
Html::rawElement( 'a',
+ array( 'href' => $filter_url,
+ 'title' => $this->msg(
'cargo-drilldown-filterbyvalue' )->text()
+ ), $filter_text );
+ }
+ return $result_line_part;
+ }
+
/**
* Copied from Miga, also written by Yaron Koren
* (https://github.com/yaronkoren/miga/blob/master/NumberUtils.js)
@@ -936,7 +1022,18 @@
global $wgCargoDrilldownMinValuesForComboBox;
$fieldType = $f->fieldDescription->mType;
- if ( $fieldType == 'Date' || $fieldType == 'Datetime' ) {
+ $isHierarchy = $f->fieldDescription->mIsHierarchy;
+ if( $cur_url === null ) {
+ // If $cur_url wasn't passed in, we have to create it.
+ $cur_url = $this->makeBrowseURL( $this->tableName,
$this->fullTextSearchTerm, $this->applied_filters, $f->name );
+ $cur_url .= ( strpos( $cur_url, '?' ) ) ? '&' : '?';
+ }
+
+ if( $isHierarchy ) {
+ $results_line =
$this->printUnappliedFilterValuesForHierarachy( $cur_url, $f,
+ $this->fullTextSearchTerm,
$this->applied_filters );
+ return $this->printFilterLine( $f->name, false, true,
$results_line );
+ } else if ( $fieldType == 'Date' || $fieldType == 'Datetime' ) {
$filter_values = $f->getTimePeriodValues(
$this->fullTextSearchTerm, $this->applied_filters );
} else {
$filter_values = $f->getAllValues(
$this->fullTextSearchTerm, $this->applied_filters );
@@ -958,10 +1055,7 @@
} elseif ( count( $filter_values ) >=
$wgCargoDrilldownMinValuesForComboBox ) {
$results_line = $this->printComboBoxInput(
$filter_name, 0, $filter_values );
$normal_filter = false;
- } else {
- // If $cur_url wasn't passed in, we have to create it.
- $cur_url = $this->makeBrowseURL( $this->tableName,
$this->fullTextSearchTerm, $this->applied_filters, $f->name );
- $cur_url .= ( strpos( $cur_url, '?' ) ) ? '&' : '?';
+ } else {
$results_line = $this->printUnappliedFilterValues(
$cur_url, $f, $filter_values );
}
--
To view, visit https://gerrit.wikimedia.org/r/365091
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic465b978a1f40f00c66fbdf1b29f8ebe71f2fc55
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Cargo
Gerrit-Branch: master
Gerrit-Owner: Fz-29 <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits