Completed
Pull Request — master (#538)
by
unknown
13:29
created

GraphPrinter::getWordWrappedText()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 17
cp 0
rs 9.504
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 20
1
<?php
2
3
namespace SRF\Graph;
4
5
use SMW\ResultPrinter;
6
use SMWQueryResult;
7
use SMWWikiPageValue;
8
use GraphViz;
9
use Html;
10
11
/**
12
 * SMW result printer for graphs using graphViz.
13
 * In order to use this printer you need to have both
14
 * the graphViz library installed on your system and
15
 * have the graphViz MediaWiki extension installed.
16
 *
17
 * @file SRF_Graph.php
18
 * @ingroup SemanticResultFormats
19
 *
20
 * @licence GNU GPL v2+
21
 * @author Frank Dengler
22
 * @author Jeroen De Dauw < [email protected] >
23
 * @author Sebastian Schmid
24
 */
25
class GraphPrinter extends ResultPrinter {
26
27
	const NODELABEL_DISPLAYTITLE = 'displaytitle';
28
	public static $NODE_LABELS = [
29
		self::NODELABEL_DISPLAYTITLE,
30
	];
31
32
	public static $NODE_SHAPES = [
33
		'box',
34
		'box3d',
35
		'circle',
36
		'component',
37
		'diamond',
38
		'doublecircle',
39
		'doubleoctagon',
40
		'egg',
41
		'ellipse',
42
		'folder',
43
		'hexagon',
44
		'house',
45
		'invhouse',
46
		'invtrapezium',
47
		'invtriangle',
48
		'Mcircle',
49
		'Mdiamond',
50
		'Msquare',
51
		'none',
52
		'note',
53
		'octagon',
54
		'parallelogram',
55
		'pentagon ',
56
		'plaintext',
57
		'point',
58
		'polygon',
59
		'rect',
60
		'rectangle',
61
		'septagon',
62
		'square',
63
		'tab',
64
		'trapezium',
65
		'triangle',
66
		'tripleoctagon',
67
	];
68
	private $nodes = [];
69
	private $options = [];
70
71
	public function getName() {
72
		return $this->msg( 'srf-printername-graph' )->text();
73
	}
74
75
	/**
76
	 * @see SMWResultPrinter::handleParameters()
77
	 */
78
	protected function handleParameters( array $params, $outputmode ) {
79
		parent::handleParameters( $params, $outputmode );
80
81
		$this->options = [
82
			'graphName' => trim( $params['graphname'] ),
83
			'graphSize' => trim( $params['graphsize'] ),
84
			'nodeShape' => $params['nodeshape'],
85
			'nodeLabel' => $params['nodelabel'],
86
			'rankDir' => strtoupper( trim( $params['arrowdirection'] ) ),
87
			'wordWrapLimit' => $params['wordwraplimit'],
88
			'parentRelation' => strtolower( trim( $params['relation'] ) ) == 'parent',
89
			'enableGraphLink' => $params['graphlink'],
90
			'showGraphLabel' => $params['graphlabel'],
91
			'showGraphColor' => $params['graphcolor'],
92
			'showGraphLegend' => $params['graphlegend']
93
		];
94
	}
95
96
	/**
97
	 * @param SMWQueryResult $res
98
	 * @param $outputmode
99
	 *
100
	 * @return string
101
	 */
102
	protected function getResultText( SMWQueryResult $res, $outputmode ) {
103
		if ( !class_exists( 'GraphViz' )
104
			&& !class_exists( '\\MediaWiki\\Extension\\GraphViz\\GraphViz' )
105
		) {
106
			wfWarn( 'The SRF Graph printer needs the GraphViz extension to be installed.' );
107
			return '';
108
		}
109
110
		// iterate query result and create SRF\GraphNodes
111
		while ( $row = $res->getNext() ) {
112
			$this->processResultRow( $row );
113
		}
114
115
		// use GraphFormater to build the graph
116
		$graphFormatter = new GraphFormatter( $this->options );
117
		$graphFormatter->buildGraph( $this->nodes );
118
119
		// Calls graphvizParserHook function from MediaWiki GraphViz extension
120
		$result = $GLOBALS['wgParser']->recursiveTagParse( "<graphviz>" . $graphFormatter->getGraph
121
				() . "</graphviz>" );
122
123
		// append legend
124
		$result .= $graphFormatter->getGraphLegend();
125
126
		return $result;
127
	}
128
129
	/**
130
	 * Process a result row and create SRF\GraphNodes
131
	 *
132
	 * @since 3.1
133
	 *
134
	 * @param array $row
135
	 *
136
	 */
137
	protected function processResultRow( array /* of SMWResultArray */ $row ) {
138
139
		// loop through all row fields
140
		foreach ( $row as $i => $resultArray ) {
141
142
			// loop through all values of a multivalue field
143
			while ( ( /* SMWWikiPageValue */
144
				$object = $resultArray->getNextDataValue() ) !== false ) {
145
146
				// create SRF\GraphNode for column 0
147
				if ( $i == 0 ) {
148
					$node = new GraphNode( $object->getShortWikiText() );
149
					$node->setLabel( $object->getDisplayTitle() );
150
					$this->nodes[] = $node;
151
				} else {
152
					$node->addParentNode( $resultArray->getPrintRequest()->getLabel(), $object->getShortWikiText() );
0 ignored issues
show
Bug introduced by
The variable $node does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
153
				}
154
			}
155
		}
156
	}
157
158
	/**
159
	 * @see SMWResultPrinter::getParamDefinitions
160
	 *
161
	 * @since 1.8
162
	 *
163
	 * @param $definitions array of IParamDefinition
164
	 *
165
	 * @return array of IParamDefinition|array
166
	 */
167
	public function getParamDefinitions( array $definitions ) {
168
		$params = parent::getParamDefinitions( $definitions );
169
170
		$params['graphname'] = [
171
			'default' => 'QueryResult',
172
			'message' => 'srf-paramdesc-graphname',
173
		];
174
175
		$params['graphsize'] = [
176
			'type' => 'string',
177
			'default' => '',
178
			'message' => 'srf-paramdesc-graphsize',
179
			'manipulatedefault' => false,
180
		];
181
182
		$params['graphlegend'] = [
183
			'type' => 'boolean',
184
			'default' => false,
185
			'message' => 'srf-paramdesc-graphlegend',
186
		];
187
188
		$params['graphlabel'] = [
189
			'type' => 'boolean',
190
			'default' => false,
191
			'message' => 'srf-paramdesc-graphlabel',
192
		];
193
194
		$params['graphlink'] = [
195
			'type' => 'boolean',
196
			'default' => false,
197
			'message' => 'srf-paramdesc-graphlink',
198
		];
199
200
		$params['graphcolor'] = [
201
			'type' => 'boolean',
202
			'default' => false,
203
			'message' => 'srf-paramdesc-graphcolor',
204
		];
205
206
		$params['arrowdirection'] = [
207
			'aliases' => 'rankdir',
208
			'default' => 'LR',
209
			'message' => 'srf-paramdesc-rankdir',
210
			'values'  => [ 'LR', 'RL', 'TB', 'BT' ],
211
		];
212
213
		$params['nodeshape'] = [
214
			'default' => false,
215
			'message' => 'srf-paramdesc-graph-nodeshape',
216
			'manipulatedefault' => false,
217
			'values' => self::$NODE_SHAPES,
218
		];
219
220
		$params['relation'] = [
221
			'default' => 'child',
222
			'message' => 'srf-paramdesc-graph-relation',
223
			'manipulatedefault' => false,
224
			'values' => [ 'parent', 'child' ],
225
		];
226
227
		$params['wordwraplimit'] = [
228
			'type' => 'integer',
229
			'default' => 25,
230
			'message' => 'srf-paramdesc-graph-wwl',
231
			'manipulatedefault' => false,
232
		];
233
234
		$params['nodelabel'] = [
235
			'default' => '',
236
			'message' => 'srf-paramdesc-nodelabel',
237
			'values' => self::$NODE_LABELS,
238
		];
239
240
		return $params;
241
	}
242
}
243