Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes issue add support for more datatypes in filters (3) #62

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
2acde68
add missing datatypes
thomas-topway-it Jun 21, 2023
6531b66
add datatypes
thomas-topway-it Jun 21, 2023
7648b4f
support monolingual_text
thomas-topway-it Jun 28, 2023
ddaa2b1
support monolingual_text
thomas-topway-it Jun 28, 2023
7b475d4
add propKey
thomas-topway-it Jun 28, 2023
35c34c6
add datavalues
thomas-topway-it Jun 28, 2023
deb12ae
refactor getPropertyType
thomas-topway-it Jun 28, 2023
21cd41f
fix committed tree
thomas-topway-it Jun 28, 2023
34e2d90
fix monolingual_text
thomas-topway-it Jun 30, 2023
b90f1d3
fix monolingual_text
thomas-topway-it Jun 30, 2023
ae9fcf1
fix monolingual_text
thomas-topway-it Jun 30, 2023
0b32b53
SemanticDrilldown/issues/10
thomas-topway-it Jun 30, 2023
6fc4f71
fix single option error
thomas-topway-it Jun 30, 2023
5e8ab72
fixes "record", "keyword" and "reference" datatypes
thomas-topway-it Jun 30, 2023
1a18939
fixes "record", "keyword" and "reference" datatype
thomas-topway-it Jun 30, 2023
c556446
fix lint errors
thomas-topway-it Jul 3, 2023
1fa7fdc
fix lint
thomas-topway-it Jul 3, 2023
1d3594a
fix lint error
thomas-topway-it Jul 3, 2023
7fc6edd
fix lint error
thomas-topway-it Jul 3, 2023
c964a78
fix phpstan error
thomas-topway-it Jul 3, 2023
4a4e839
unit test additional datatypes
thomas-topway-it Jul 12, 2023
8398d99
added applied filter
thomas-topway-it Jul 15, 2023
71a05be
Merge branch 'SemanticMediaWiki:master' into fixes-issue-Add-support-…
thomas-topway-it Jul 15, 2023
d111488
restore original
thomas-topway-it Nov 22, 2024
e641279
Merge branch 'SemanticMediaWiki:master' into fixes-issue-Add-support-…
thomas-topway-it Nov 22, 2024
4499476
restore sql monolingual_text
thomas-topway-it Nov 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions includes/AppliedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,13 @@
SQL;
}

if ( $this->filter->propertyType() === 'monolingual_text' ) {
$sql .= <<<SQL
JOIN $smw_ids o_ids ON p.o_id = o_ids.smw_id

Check warning on line 291 in includes/AppliedFilter.php

View check run for this annotation

Codecov / codecov/patch

includes/AppliedFilter.php#L290-L291

Added lines #L290 - L291 were not covered by tests
JOIN smw_fpt_text fpt_text ON p.o_id = fpt_text.s_id
SQL;

Check warning on line 293 in includes/AppliedFilter.php

View check run for this annotation

Codecov / codecov/patch

includes/AppliedFilter.php#L293

Added line #L293 was not covered by tests
}

$sql .= <<<SQL
JOIN $smwCategoryInstances insts ON p.s_id = insts.s_id
JOIN $smw_ids cat_ids ON insts.o_id = cat_ids.smw_id
Expand Down
11 changes: 8 additions & 3 deletions includes/DbService.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function createTempTable( $category, $subcategory, $subcategories, $appli
* both for speeding up later queries (at least, that's the hope)
* and for getting the set of 'None' values.
*/
public function createFilterValuesTempTable( $propertyType, $escaped_property ) {
public function createFilterValuesTempTable( $propertyType, $escaped_property, $propKey ) {
$smw_ids = $this->dbr->tableName( Utils::getIDsTableName() );

$valuesTable = $this->dbr->tableName( PropertyTypeDbInfo::tableName( $propertyType ) );
Expand All @@ -66,15 +66,19 @@ public function createFilterValuesTempTable( $propertyType, $escaped_property )

$sql = <<<END
CREATE TEMPORARY TABLE semantic_drilldown_filter_values
AS SELECT s_id AS id, $value_field AS value
AS SELECT $valuesTable.s_id AS id, $value_field AS value
FROM $valuesTable
JOIN $smw_ids p_ids ON $valuesTable.p_id = p_ids.smw_id

END;
if ( $propertyType === 'page' ) {
$sql .= " JOIN $smw_ids o_ids ON $valuesTable.o_id = o_ids.smw_id\n";
}
$sql .= " WHERE p_ids.smw_title = '$query_property'";

if ( $propertyType === 'monolingual_text' ) {
$sql .= " JOIN smw_fpt_text fpt_text ON $valuesTable.o_id = fpt_text.s_id\n";
}
$sql .= " WHERE ( p_ids.smw_title = '$query_property' OR p_ids.smw_title = '$propKey' )";

$temporaryTableManager = new TemporaryTableManager( $this->dbw );
$temporaryTableManager->queryWithAutoCommit( $sql, __METHOD__ );
Expand Down Expand Up @@ -154,6 +158,7 @@ public function getCategoryChildren( $category_name, $get_categories, $levels )
foreach ( $subcategories as $subcategory ) {
$pages = array_merge( $pages, $this->getCategoryChildren( $subcategory, $get_categories, $levels - 1 ) );
}

return $pages;
}

Expand Down
114 changes: 80 additions & 34 deletions includes/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* @var array
*/
public $possible_applied_filters = [];
private $propKey;

public function __construct(
DbService $db,
Expand Down Expand Up @@ -95,6 +96,10 @@
return SqlProvider::escapedProperty( $this->property() );
}

public function propKey() {
return $this->propKey;
}

/**
* Gets an array of the possible time period values (e.g., years,
* months) for this filter, and, for each one,
Expand Down Expand Up @@ -252,11 +257,16 @@
JOIN $smw_ids o_ids ON p.o_id = o_ids.smw_id
LEFT JOIN $revision_table_name ON $revision_table_name.rev_id = o_ids.smw_rev
LEFT JOIN $page_props_table_name displaytitle ON $revision_table_name.rev_page = displaytitle.pp_page AND displaytitle.pp_propname = 'displaytitle'
END;
}
if ( $this->propertyType === 'monolingual_text' ) {
$sql .= <<<END
JOIN smw_fpt_text fpt_text ON p.o_id = fpt_text.s_id
END;
}
$sql .= <<<END
JOIN $smw_ids p_ids ON p.p_id = p_ids.smw_id
WHERE p_ids.smw_title = '$property_value'
WHERE ( p_ids.smw_title = '$property_value' OR p_ids.smw_title = '$this->propKey' )
AND p_ids.smw_namespace = $prop_ns
GROUP BY $value_field
ORDER BY $value_field
Expand Down Expand Up @@ -340,44 +350,80 @@
$store = Utils::getSMWStore();
$escapedProperty = $this->escapedProperty();
$propPage = new SMWDIWikiPage( $escapedProperty, SMW_NS_PROPERTY, '' );
$types = $store->getPropertyValues( $propPage, new SMWDIProperty( '_TYPE' ) );
$datatypeLabels = Utils::getSMWContLang()->getDatatypeLabels();
if ( count( $types ) > 0 ) {
if ( $types[0] instanceof SMWDIWikiPage ) {
$typeValue = $types[0]->getDBkey();
} elseif ( $types[0] instanceof SMWDIURI ) {
// A bit inefficient, but it's the
// simplest approach.
$typeID = $types[0]->getFragment();
if ( $typeID == '_str' && !array_key_exists( '_str', $datatypeLabels ) ) {
$typeID = '_txt';
$property = $this->property();

// KnownTypeLabels
$isKnownLabel = false;
$typeValue = null;
if ( in_array( $property, $datatypeLabels ) ) {
$typeValue = $property;
$isKnownLabel = true;

Check warning on line 361 in includes/Filter.php

View check run for this annotation

Codecov / codecov/patch

includes/Filter.php#L360-L361

Added lines #L360 - L361 were not covered by tests
} else {
$types = $store->getPropertyValues( $propPage, new SMWDIProperty( '_TYPE' ) );

if ( count( $types ) > 0 ) {
if ( $types[0] instanceof SMWDIWikiPage ) {
$typeValue = $types[0]->getDBkey();

Check warning on line 367 in includes/Filter.php

View check run for this annotation

Codecov / codecov/patch

includes/Filter.php#L367

Added line #L367 was not covered by tests
} elseif ( $types[0] instanceof SMWDIURI ) {
// A bit inefficient, but it's the
// simplest approach.
$typeID = $types[0]->getFragment();
if ( $typeID == '_str' && !array_key_exists( '_str', $datatypeLabels ) ) {
$typeID = '_txt';

Check warning on line 373 in includes/Filter.php

View check run for this annotation

Codecov / codecov/patch

includes/Filter.php#L373

Added line #L373 was not covered by tests
}
$typeValue = $datatypeLabels[$typeID];
} else {
$typeValue = $types[0]->getWikiValue();

Check warning on line 377 in includes/Filter.php

View check run for this annotation

Codecov / codecov/patch

includes/Filter.php#L377

Added line #L377 was not covered by tests
}
$typeValue = $datatypeLabels[$typeID];
} else {
$typeValue = $types[0]->getWikiValue();
}
if ( $typeValue == $datatypeLabels['_wpg'] ) {
$propertyType = 'page';
// _str stopped existing in SMW 1.9
} elseif ( array_key_exists( '_str', $datatypeLabels ) && $typeValue == $datatypeLabels['_str'] ) {
$propertyType = 'string';
} elseif ( !array_key_exists( '_str', $datatypeLabels ) && $typeValue == $datatypeLabels['_txt'] ) {
$propertyType = 'string';
} elseif ( $typeValue == $datatypeLabels['_num'] ) {
$propertyType = 'number';
} elseif ( $typeValue == $datatypeLabels['_boo'] ) {
$propertyType = 'boolean';
} elseif ( $typeValue == $datatypeLabels['_dat'] ) {
$propertyType = 'date';
} elseif ( $typeValue == $datatypeLabels['_eid'] ) {
$propertyType = 'external_id';
} else {
// This should hopefully never get called.
print "Error! Unsupported property type ($typeValue) for filter {$this->name}.";
}
}

return $propertyType;
$propKey = array_search( $typeValue, $datatypeLabels );

if ( $propKey === false ) {
// This should hopefully never get called.
print "Error! Unsupported property type ($typeValue) for filter {$this->name}.";
$this->propKey = '_wpg';
return $propertyType;
}

// used to retrieve the value of
// properties set with the label of a
// predefined property, e.g. Email::[email protected]
$this->propKey = $propKey;

// @see https://github.com/SemanticMediaWiki/SemanticDrilldown/pull/62
if ( $isKnownLabel && ( $propKey === '_rec' || $propKey === '_keyw' || $propKey === '_ref_rec' ) ) {
return $propertyType;

Check warning on line 398 in includes/Filter.php

View check run for this annotation

Codecov / codecov/patch

includes/Filter.php#L398

Added line #L398 was not covered by tests
}

// normalize as before
$map = [
'_wpg' => 'page',
'_txt' => 'string',
// _str stopped existing in SMW 1.9
'_str' => 'string',
'_cod' => 'code',
'_boo' => 'boolean',
'_num' => 'number',
'_geo' => 'geographic_coord',
'_tem' => 'temperature',
'_dat' => 'date',
'_ema' => 'email',
'_uri' => 'URL',
'_anu' => 'annotation_uri',
'_tel' => 'telephone_number',
'_rec' => 'record',
'_qty' => 'quantity',
'_mlt_rec' => 'monolingual_text',
'_eid' => 'external_id',
'_keyw' => 'keyword',
'_ref_rec' => 'reference'
];

// propertyType
return $map[$propKey];
}

}
2 changes: 1 addition & 1 deletion includes/Specials/BrowseData/GetApplicableFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ private function getUnappliedFilterLine( Filter $f ): string {
}

private function getPossibleValues( Filter $f ): PossibleFilterValues {
$this->db->createFilterValuesTempTable( $f->propertyType(), $f->escapedProperty() );
$this->db->createFilterValuesTempTable( $f->propertyType(), $f->escapedProperty(), $f->propKey() );
if ( empty( $f->allowedValues() ) ) {
$possibleFilterValues = $f->propertyType() == 'date'
? $f->getTimePeriodValues()
Expand Down
2 changes: 1 addition & 1 deletion includes/Specials/BrowseData/GetCategories.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

public function __invoke( $selectedCategory ): ?array {
if ( $this->urlService->showSingleCat() ) {
return null;
return [];

Check warning on line 21 in includes/Specials/BrowseData/GetCategories.php

View check run for this annotation

Codecov / codecov/patch

includes/Specials/BrowseData/GetCategories.php#L21

Added line #L21 was not covered by tests
}

$toCategoryViewModel = function ( $category ) use ( $selectedCategory ) {
Expand Down
23 changes: 23 additions & 0 deletions includes/Sql/PropertyTypeDbInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@ class PropertyTypeDbInfo {
public static function tableName( string $propertyType ): ?string {
switch ( $propertyType ) {
case 'page':
case 'monolingual_text':
return 'smw_di_wikipage';
case 'boolean':
return 'smw_di_bool';
case 'date':
return 'smw_di_time';
case 'telephone_number':
case 'email':
case 'annotation_uri':
case 'URL':
return 'smw_di_uri';
case 'geographic_coord':
return 'smw_di_coords';
case 'number':
case 'quantity':
case 'temperature':
return 'smw_di_number';
// case 'monolingual_text':
// return 'smw_fpt_text';
default:
return 'smw_di_blob';
}
Expand All @@ -27,8 +39,19 @@ public static function valueField( string $propertyType ): ?string {
case 'boolean':
return 'o_value';
case 'date':
case 'quantity':
case 'temperature':
case 'number':
case 'telephone_number':
case 'URL':
case 'annotation_uri':
case 'email':
case 'geographic_coord':
return 'o_serialized';
case 'keyword':
return 'o_blob';

// case 'monolingual_text':
default:
// CONVERT() is also supported in PostgreSQL,
// but it doesn't seem to work the same way.
Expand Down
31 changes: 26 additions & 5 deletions includes/Sql/SqlProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@
if ( $includes_none ) {
$property_table_name = $dbr->tableName(
PropertyTypeDbInfo::tableName( $af->filter->propertyType() ) );
if ( $af->filter->propertyType() === 'page' ) {

if ( $af->filter->propertyType() === 'page' || $af->filter->propertyType() === 'monolingual_text' ) {

Check warning on line 105 in includes/Sql/SqlProvider.php

View check run for this annotation

Codecov / codecov/patch

includes/Sql/SqlProvider.php#L105

Added line #L105 was not covered by tests
$property_table_nickname = "nr$i";
$property_field = 'p_id';
} else {
Expand All @@ -116,18 +117,21 @@
// in SMW where the same page gets two
// different SMW IDs.

$propKey = $af->filter->propKey();

Check warning on line 120 in includes/Sql/SqlProvider.php

View check run for this annotation

Codecov / codecov/patch

includes/Sql/SqlProvider.php#L120

Added line #L120 was not covered by tests

$sql .= "LEFT OUTER JOIN
(SELECT s_id
FROM $property_table_name
WHERE $property_field = (SELECT MIN(smw_id) FROM $smwIDs WHERE smw_title = '$property_value' AND smw_namespace = $prop_ns)) $property_table_nickname
WHERE $property_field = (SELECT MIN(smw_id) FROM $smwIDs WHERE ( smw_title = '$property_value' OR smw_title = '$propKey' ) AND smw_namespace = $prop_ns)) $property_table_nickname

Check warning on line 125 in includes/Sql/SqlProvider.php

View check run for this annotation

Codecov / codecov/patch

includes/Sql/SqlProvider.php#L125

Added line #L125 was not covered by tests
ON ids.smw_id = $property_table_nickname.s_id ";
}
}
foreach ( $applied_filters as $i => $af ) {
$sql .= "\n ";
$property_table_name = $dbr->tableName(
PropertyTypeDbInfo::tableName( $af->filter->propertyType() ) );
if ( $af->filter->propertyType() === 'page' ) {

if ( $af->filter->propertyType() === 'page' || $af->filter->propertyType() === 'monolingual_text' ) {
if ( $includes_none ) {
$sql .= "LEFT OUTER ";
}
Expand All @@ -136,6 +140,11 @@
$sql .= "LEFT OUTER ";
}
$sql .= "JOIN $smwIDs o_ids$i ON r$i.o_id = o_ids$i.smw_id ";

if ( $af->filter->propertyType() === 'monolingual_text' ) {
$sql .= "JOIN smw_fpt_text fpt_text$i ON r$i.o_id = fpt_text$i.s_id ";
}

} else {
$sql .= "JOIN $property_table_name a$i ON ids.smw_id = a$i.s_id ";
}
Expand All @@ -155,19 +164,30 @@
}
$sql .= ")) ";
foreach ( $applied_filters as $i => $af ) {
$propKey = $af->filter->propKey();
$property_value = $af->filter->escapedProperty();
$value_field = PropertyTypeDbInfo::valueField( $af->filter->propertyType() );

if ( $af->filter->propertyType() === 'page' ) {
$property_field = "r$i.p_id";
$sql .= "\n AND ($property_field = (SELECT MIN(smw_id) FROM $smwIDs WHERE smw_title = '$property_value' AND smw_namespace = $prop_ns)";
$sql .= "\n AND ($property_field = (SELECT MIN(smw_id) FROM $smwIDs WHERE ( smw_title = '$property_value' OR smw_title = '$propKey' ) AND smw_namespace = $prop_ns)";
if ( $includes_none ) {
$sql .= " OR $property_field IS NULL";
}
$sql .= ")\n AND ";
$value_field = "o_ids$i.smw_title";
} elseif ( $af->filter->propertyType() === 'monolingual_text' ) {
$property_field = "r$i.p_id";
$sql .= "\n AND $property_field = (SELECT MIN(smw_id) FROM $smwIDs WHERE ( smw_title = '$property_value' OR smw_title = '$propKey' ) AND smw_namespace = $prop_ns) AND ";
if ( strncmp( $value_field, '(IF(o_blob IS NULL', 18 ) === 0 ) {
$value_field = str_replace( 'o_', "fpt_text$i.o_", $value_field );

Check warning on line 183 in includes/Sql/SqlProvider.php

View check run for this annotation

Codecov / codecov/patch

includes/Sql/SqlProvider.php#L180-L183

Added lines #L180 - L183 were not covered by tests
} else {
$value_field = "fpt_text$i.$value_field";

Check warning on line 185 in includes/Sql/SqlProvider.php

View check run for this annotation

Codecov / codecov/patch

includes/Sql/SqlProvider.php#L185

Added line #L185 was not covered by tests
}

} else {
$property_field = "a$i.p_id";
$sql .= "\n AND $property_field = (SELECT MIN(smw_id) FROM $smwIDs WHERE smw_title = '$property_value' AND smw_namespace = $prop_ns) AND ";
$sql .= "\n AND $property_field = (SELECT MIN(smw_id) FROM $smwIDs WHERE ( smw_title = '$property_value' OR smw_title = '$propKey' ) AND smw_namespace = $prop_ns) AND ";
if ( strncmp( $value_field, '(IF(o_blob IS NULL', 18 ) === 0 ) {
$value_field = str_replace( 'o_', "a$i.o_", $value_field );
} else {
Expand All @@ -176,6 +196,7 @@
}
$sql .= $af->checkSQL( $value_field );
}

return $sql;
}

Expand Down
Loading
Loading