Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
coverage: false
experimental: false
- mediawiki_version: '1.43.1'
smw_version: dev-master
smw_version: 5.0.2
pf_version: 5.9
sfs_version: dev-master
php_version: 8.1
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ EXTENSION=SemanticResultFormats
MW_VERSION?=1.39
PHP_VERSION?=8.1
DB_TYPE?=mysql
DB_IMAGE?="mariadb:10"
DB_IMAGE?="mariadb:11.2"

# extensions
SMW_VERSION?=dev-master
SMW_VERSION ?= 5.0.2
PF_VERSION ?= 5.5.1
SFS_VERSION ?= 4.0.0-beta
MM_VERSION ?= 6.0.1
Expand Down
29 changes: 22 additions & 7 deletions src/Graph/GraphFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,29 @@ public function buildGraph( $nodes ) {
$alignment = in_array( $field['type'], [ '_num', '_qty', '_dat', '_tem' ] )
? 'right'
: 'left';
$valueLink = $field['valueLink'];
if ( $valueLink !== null ) {
$valueLink = $field['valueLink'];
} else {
$valueLink = $field['value'];
}
return '<tr><td align="left" href="[[Property:' . $field['page'] . ']]">'
. $field['name'] . '</td>'
. '<td align="' . $alignment . '">'
. $instance->getWordWrappedText(
$field['value'],
$instance->options->getWordWrapLimit()
. '<td align="' . $alignment . '"'
. (
$field['type'] === '_wpg'
? ' href="[[' . htmlspecialchars( $field['valueLink'] ) . ']]">'
. $instance->getWordWrappedText(
htmlspecialchars( $field['value'] ),
$instance->options->getWordWrapLimit()
)
: '>'
. $instance->getWordWrappedText(
htmlspecialchars( $field['value'] ),
$instance->options->getWordWrapLimit()
)
)
. '</td></tr>';
. '</td></tr>';
}, $fields ) ) . "\n</table>\n>";
$nodeLinkURL = null;
// the value at the top is already hyperlinked.
Expand Down Expand Up @@ -182,8 +197,8 @@ public function buildGraph( $nodes ) {

// handle parent/child switch (parentRelation)
$this->add( $this->options->getParentRelation()
? '"' . $parentNode['object'] . '" -> "' . $node->getID() . '"'
: '"' . $node->getID() . '" -> "' . $parentNode['object'] . '"' );
? '"' . htmlspecialchars( $parentNode['object'] ) . '" -> "' . htmlspecialchars( $node->getID() ) . '"'
: '"' . htmlspecialchars( $node->getID() ) . '" -> "' . htmlspecialchars( $parentNode['object'] ) . '"' );

if ( $this->options->isGraphLabel() || $this->options->isGraphColor() ) {
$this->add( ' [' );
Expand Down
5 changes: 3 additions & 2 deletions src/Graph/GraphNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ public function addParentNode( $predicate, $object ) {
* @param string $value : Field value
* @param string $type : Type of the field, for aligning
* @param string $page : Property page
* @param string|null $valueLink : The page to link the value to
*/
public function addField( $name, $value, $type, $page ) {
$this->fields[] = [ 'name' => $name ?: $page, 'value' => $value, 'type' => $type, 'page' => $page ];
public function addField( $name, $value, $type, $page, $valueLink = null ) {
$this->fields[] = [ 'name' => $name ?: $page, 'value' => $value, 'type' => $type, 'page' => $page, 'valueLink' => $valueLink ];
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/Graph/GraphOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class GraphOptions {
private $showGraphLegend;
/** @var bool Show non-Page properties as fields within nodes rather than edges. */
private $showGraphFields;
private $showGraphFieldsPages;

public function __construct( $options ) {
$this->graphName = trim( $options['graphname'] );
Expand All @@ -45,6 +46,7 @@ public function __construct( $options ) {
$this->showGraphColor = trim( $options['graphcolor'] );
$this->showGraphLegend = trim( $options['graphlegend'] );
$this->showGraphFields = trim( $options['graphfields'] );
$this->showGraphFieldsPages = trim( $options['graphfieldspages'] );
}

public function getGraphName(): string {
Expand Down Expand Up @@ -104,4 +106,8 @@ public function isGraphLegend(): bool {
public function showGraphFields(): bool {
return $this->showGraphFields;
}

public function showGraphFieldsPages(): string {
return $this->showGraphFieldsPages;
}
}
121 changes: 96 additions & 25 deletions src/Graph/GraphPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,51 +192,116 @@ protected function processResultRow( array $row ) {
$node = null;
$fields = [];
$parents = [];
// loop through all row fields
$pageTypeSeen = 0;

foreach ( $row as $result_array ) {
$request = $result_array->getPrintRequest();
$type = $request->getTypeID();
// Whether this printout should be shown as an edge.
// no fields at all.
$show_as_edge = !$this->options->showGraphFields()
|| in_array( $type, self::PAGETYPES )
$isPageType = in_array( $type, self::PAGETYPES );
$canonicalLabel = $request->getCanonicalLabel();
$label = $request->getLabel() ?: $canonicalLabel ?: '?';

if ( $isPageType ) {
$pageTypeSeen++;
}

$showAsEdge = !$this->options->showGraphFields()
|| $isPageType
|| $request->isMode( PrintRequest::PRINT_CHAIN );

// Loop through all values of a multivalue field.
$showGraphFieldsPages = $this->options->showGraphFieldsPages() === 'yes';
$showGraphFields = $this->options->showGraphFields();

while ( ( $object = $result_array->getNextDataValue() ) !== false ) {
if ( $show_as_edge ) {
if ( !$node && !$object->getProperty() ) {
// The graph node for the current record has not been created,
// and this is the printout '?'. So, create it now.
$node = new GraphNode( $object->getShortWikiText() );
$hasProperty = $object->getProperty();
if ( $object instanceof \SMW\DataValues\StringValue ) {
$objectText = $object->getShortWikiText();
} else {
$objectText = $object->getDisplayTitle();
}

$includeAsEdge = !$showGraphFields || $isPageType || $request->isMode( PrintRequest::PRINT_CHAIN );
$includeAsField = $showGraphFields && ( !$isPageType || $showGraphFieldsPages );

$skipNode = $isPageType && $pageTypeSeen > 1;

// Create node if not yet created
if ( !$node && !$hasProperty && !$skipNode ) {
$node = new GraphNode( $objectText );
$node->setLabel( $object->getPreferredCaption() ?: $object->getText() );
}

// Handle edge
if ( $showGraphFieldsPages ) {
if ( $includeAsEdge && $node ) {
if ( $objectText !== $node->getId() && $pageTypeSeen === 2 ) {
$parents[] = [
'predicate' => $label,
'object' => $objectText,
];
}
}
} elseif ( $showAsEdge ) {
if ( !$node && !$hasProperty ) {
$node = new GraphNode( $objectText );
$node->setLabel( $object->getPreferredCaption() ?: $object->getText() );
} else {
// Remember a parent node to add after the row is processed.
} elseif ( $node && $objectText !== $node->getId() ) {
$parents[] = [
'predicate' => $request->getLabel(),
'object' => $object->getShortWikiText()
'predicate' => $label,
'object' => $objectText,
];
}
} else {
// A non-Page property and 'graphfields' is set,
// so display it as a field after the row has been processed.
continue;
}

// Handle field
if ( $showGraphFieldsPages && $includeAsField ) {
if ( $hasProperty || !$isPageType ) {
// non-page or property field
if ( $pageTypeSeen !== 2 && !$isPageType ) {
$fields[] = [
'name' => $label,
'value' => $objectText,
'type' => $type,
'page' => $canonicalLabel,
];
} elseif ( $pageTypeSeen !== 2 && $isPageType ) {
$fields[] = [
'name' => $label,
'value' => $object->getDisplayTitle(),
'valueLink' => $object->getShortWikiText(),
'type' => $type,
'page' => $canonicalLabel,
];
}
}
} elseif ( !$showGraphFieldsPages && !$showAsEdge ) {
$fields[] = [
'name' => $request->getLabel(),
'value' => $object->getShortWikiText(),
'name' => $label,
'value' => $objectText,
'type' => $type,
'page' => $request->getCanonicalLabel()
'page' => $canonicalLabel,
];
}
}
}
// Add the node, if any, its parent nodes and fields for non-Page properties to the current edge.

if ( $node ) {
foreach ( $parents as $parent ) {
$node->addParentNode( $parent['predicate'], $parent['object'] );
// @TODO: add explicit nodes with hyperlinks to every parent node not added as '?', but only once.
if ( !empty( $parent['object'] ) ) {
$node->addParentNode( $parent['predicate'], $parent['object'] );
}
}
foreach ( $fields as $field ) {
$node->addField( $field['name'], $field['value'], $field['type'], $field['page'] );
if ( $field['value'] !== '' ) {
$node->addField(
$field['name'],
$field['value'],
$field['type'],
$field['page'],
$field['valueLink'] ?? null
);
}
}
$this->nodes[] = $node;
}
Expand Down Expand Up @@ -344,6 +409,12 @@ public function getParamDefinitions( array $definitions ) {
'type' => 'boolean'
];

$params['graphfieldspages'] = [
'default' => 'no',
'message' => 'srf-paramdesc-graphfieldspages',
'type' => 'string'
];

return $params;
}
}
Loading