Skip to content

Commit

Permalink
Simplify registration for custom fixed property tables
Browse files Browse the repository at this point in the history
The current approach in `'SMW::SQLStore::updatePropertyTableDefinitions'`
requires internal SMW knowledge [0] therefore adding `'SMW::SQLStore::AddCustomFixedPropertyTables'`
where only property keys are being assigned and have the builder to figure out
all necessary attributes to finalize the table definition.

Just add properties like:

```
$properties = array(
	$propertyRegistry::SCI_CITE_KEY,
	$propertyRegistry::SCI_CITE_REFERENCE,
	$propertyRegistry::SCI_CITE_TEXT,
	$propertyRegistry::SCI_CITE,
	$propertyRegistry::SCI_DOI
);

foreach ( $properties as $property ) {
	$customFixedProperties[$property] = str_replace( '__', '_', $property );
}
```

Whether `'SMW::SQLStore::updatePropertyTableDefinitions'` should be deprecated in
favour of `'SMW::SQLStore::AddCustomFixedPropertyTables'` is not part of this PR.

[0] https://github.com/SemanticMediaWiki/SemanticExtraSpecialProperties/blob/master/src/PropertyRegistry.php#L111-L126
  • Loading branch information
mwjames committed Jun 19, 2015
1 parent b35d9e2 commit ad1e0f2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 23 deletions.
1 change: 1 addition & 0 deletions docs/technical/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Implementing a hook should be made in consideration of the expected performance
### 2.3

- `SMW::SQLStore::BeforeDataRebuildJobInsert` to add update jobs while running the rebuild process.<sup>Use of `smwRefreshDataJobs` was deprecated with 2.3</sup>
- `SMW::SQLStore::AddCustomFixedPropertyTables` to add fixed property table definitions

For implementation details and examples, see the [integration test](https://github.com/SemanticMediaWiki/SemanticMediaWiki/blob/master/tests/phpunit/Integration/SemanticMediaWikiProvidedHookInterfaceIntegrationTest.php).

Expand Down
70 changes: 48 additions & 22 deletions src/SQLStore/PropertyTableDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use SMW\DataTypeRegistry;
use SMW\DIProperty;
use Hooks;

/**
* Class that generates property table definitions
Expand All @@ -25,31 +26,52 @@ class PropertyTableDefinitionBuilder {
*/
protected $fixedPropertyTableIds = array();

/**
* @var string
*/
private $fixedPropertyTablePrefix = 'smw_fpt';

/**
* @since 1.9
*
* @param array $diType
* @param array $specialProperties
* @param array $fixedProperties
* @param array $userDefinedFixedProperties
*/
public function __construct( array $diTypes, array $specialProperties, array $fixedProperties ) {
public function __construct( array $diTypes, array $specialProperties, array $userDefinedFixedProperties ) {
$this->diTypes = $diTypes;
$this->specialProperties = $specialProperties;
$this->fixedProperties = $fixedProperties;
$this->userDefinedFixedProperties = $userDefinedFixedProperties;
}

/**
* @since 1.9
*/
public function doBuild() {

$this->buildPropertyTablesForDiTypes( $this->diTypes );
$this->buildPropertyTablesForSpecialProperties( $this->specialProperties );
$this->buildPropertyTablesForFixedProperties( $this->fixedProperties );
$this->addTableDefinitionForDiTypes( $this->diTypes );

$this->addTableDefinitionForFixedProperties(
$this->specialProperties
);

$customFixedProperties = array();

Hooks::run( 'SMW::SQLStore::AddCustomFixedPropertyTables', array( &$customFixedProperties ) );

$this->addTableDefinitionForFixedProperties(
$customFixedProperties
);

wfRunHooks( 'SMW::SQLStore::updatePropertyTableDefinitions', array( &$this->propertyTables ) );
$this->addRedirectTableDefinition();

$this->buildFixedPropertyTableIdIndex( $this->propertyTables );
$this->addTableDefinitionForUserDefinedFixedProperties(
$this->userDefinedFixedProperties
);

Hooks::run( 'SMW::SQLStore::updatePropertyTableDefinitions', array( &$this->propertyTables ) );

$this->createFixedPropertyTableIdIndex();
}

/**
Expand All @@ -60,7 +82,7 @@ public function doBuild() {
* @return string
*/
public function getTablePrefix() {
return 'smw_fpt';
return $this->fixedPropertyTablePrefix;
}

/**
Expand Down Expand Up @@ -116,27 +138,34 @@ protected function addPropertyTable( $diType, $tableName, $fixedProperty = false
/**
* @param array $diTypes
*/
private function buildPropertyTablesForDiTypes( array $diTypes ) {
private function addTableDefinitionForDiTypes( array $diTypes ) {
foreach( $diTypes as $tableDIType => $tableName ) {
$this->addPropertyTable( $tableDIType, $tableName );
}
}

/**
* @param array $specialProperties
* @param array $properties
*/
private function buildPropertyTablesForSpecialProperties( array $specialProperties ) {
foreach( $specialProperties as $propertyKey ) {
private function addTableDefinitionForFixedProperties( array $properties ) {
foreach( $properties as $propertyKey => $propetyTableSuffix ) {

// Either as plain index array containing the property key or as associated
// array with property key => tableSuffix
$propertyKey = is_int( $propertyKey ) ? $propetyTableSuffix : $propertyKey;

$this->addPropertyTable(
DataTypeRegistry::getInstance()->getDataItemId( DIProperty::getPredefinedPropertyTypeId( $propertyKey ) ),
$this->getTablePrefix() . strtolower( $propertyKey ),
$this->fixedPropertyTablePrefix . strtolower( $propetyTableSuffix ),
$propertyKey
);
}
}

private function addRedirectTableDefinition() {
// Redirect table uses another subject scheme for historic reasons
// TODO This should be changed if possible
$redirectTableName = $this->getTablePrefix() . '_redi';
$redirectTableName = $this->fixedPropertyTablePrefix . '_redi';
if ( isset( $this->propertyTables[$redirectTableName]) ) {
$this->propertyTables[$redirectTableName]->setUsesIdSubject( false );
}
Expand All @@ -148,22 +177,19 @@ private function buildPropertyTablesForSpecialProperties( array $specialProperti
*
* @param array $fixedProperties
*/
private function buildPropertyTablesForFixedProperties( array $fixedProperties ) {
private function addTableDefinitionForUserDefinedFixedProperties( array $fixedProperties ) {
foreach( $fixedProperties as $propertyKey => $tableDIType ) {
$this->addPropertyTable(
$tableDIType,
$this->getTablePrefix() . '_' . md5( $propertyKey ),
$this->fixedPropertyTablePrefix . '_' . md5( $propertyKey ),
$propertyKey
);
}
}

/**
* @param array $propertyTables
*/
private function buildFixedPropertyTableIdIndex( array $propertyTables ) {
private function createFixedPropertyTableIdIndex() {

foreach ( $propertyTables as $tid => $propTable ) {
foreach ( $this->propertyTables as $tid => $propTable ) {
if ( $propTable->isFixedPropertyTable() ) {
$this->fixedPropertyTableIds[$propTable->getFixedProperty()] = $tid;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,25 @@ public function testRegisteredParserBeforeMagicWordsFinder() {
$inTextAnnotationParser->parse( $text );
}

public function testRegisteredAddCustomFixedPropertyTables() {

$store = $this->getMockBuilder( '\SMW\SQLStore\SQLStore' )
->disableOriginalConstructor()
->setMethods( null )
->getMock();

$this->mwHooksHandler->register( 'SMW::SQLStore::AddCustomFixedPropertyTables', function( &$customFixedProperties ) {
$customFixedProperties['Foo'] = '_Bar';

return true;
} );

$this->assertEquals(
'smw_fpt_bar',
$store->findPropertyTableID( new \SMW\DIProperty( 'Foo' ) )
);
}

public function storeClassProvider() {

$provider[] = array( '\SMWSQLStore3' );
Expand Down
3 changes: 2 additions & 1 deletion tests/phpunit/Utils/MwHooksHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class MwHooksHandler {
'SMW::SQLStore::BeforeDeleteSubjectComplete',
'SMW::SQLStore::AfterDeleteSubjectComplete',
'SMW::Parser::BeforeMagicWordsFinder',
'SMW::SQLStore::BeforeDataRebuildJobInsert'
'SMW::SQLStore::BeforeDataRebuildJobInsert',
'SMW::SQLStore::AddCustomFixedPropertyTables'
);

/**
Expand Down

0 comments on commit ad1e0f2

Please sign in to comment.