Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| CargoTreeFormat | |
0.00% |
0 / 38 |
|
0.00% |
0 / 4 |
240 | |
0.00% |
0 / 1 |
| allowedParameters | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| display | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
72 | |||
| printNode | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| printTree | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class to print query results in a "tree" display, using a field that |
| 5 | * defines a "parent" relationship between rows. |
| 6 | * |
| 7 | * @author Yaron Koren |
| 8 | * @ingroup Cargo |
| 9 | */ |
| 10 | |
| 11 | class CargoTreeFormat extends CargoListFormat { |
| 12 | /** @var string|null */ |
| 13 | protected $mParentField = null; |
| 14 | /** @var array|null */ |
| 15 | public $mFieldDescriptions; |
| 16 | |
| 17 | public static function allowedParameters() { |
| 18 | return [ 'parent field' => [ 'type' => 'string' ] ]; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * @param array $valuesTable Unused |
| 23 | * @param array $formattedValuesTable |
| 24 | * @param array $fieldDescriptions |
| 25 | * @param array $displayParams |
| 26 | * @return string HTML |
| 27 | * @throws MWException |
| 28 | */ |
| 29 | public function display( $valuesTable, $formattedValuesTable, $fieldDescriptions, $displayParams ) { |
| 30 | if ( !array_key_exists( 'parent field', $displayParams ) ) { |
| 31 | throw new MWException( wfMessage( "cargo-query-missingparam", "parent field", "tree" )->parse() ); |
| 32 | } |
| 33 | $this->mParentField = str_replace( '_', ' ', trim( $displayParams['parent field'] ) ); |
| 34 | $this->mFieldDescriptions = $fieldDescriptions; |
| 35 | |
| 36 | // Early error-checking. |
| 37 | if ( !array_key_exists( $this->mParentField, $fieldDescriptions ) ) { |
| 38 | throw new MWException( wfMessage( "cargo-query-specifiedfieldmissing", $this->mParentField, "parent field" )->parse() ); |
| 39 | } |
| 40 | if ( $fieldDescriptions[$this->mParentField]->mIsList ) { |
| 41 | throw new MWException( "Error: 'parent field' is declared to hold a list of values; " |
| 42 | . "only one parent value is allowed for the 'tree' format." ); |
| 43 | } |
| 44 | |
| 45 | // For each result row, get the main name, the parent value, |
| 46 | // and all additional display values, and add it to the tree. |
| 47 | $tree = new CargoTreeFormatTree(); |
| 48 | foreach ( $valuesTable as $queryResultsRow ) { |
| 49 | $name = null; |
| 50 | $parentName = null; |
| 51 | $values = []; |
| 52 | foreach ( $queryResultsRow as $fieldName => $value ) { |
| 53 | $value = "[[$value]]"; |
| 54 | if ( $name == null ) { |
| 55 | $name = $value; |
| 56 | } |
| 57 | if ( $fieldName == $this->mParentField ) { |
| 58 | $parentName = $value; |
| 59 | } else { |
| 60 | $values[$fieldName] = $value; |
| 61 | } |
| 62 | } |
| 63 | $tree->addNode( $name, $parentName, $values ); |
| 64 | } |
| 65 | |
| 66 | $result = self::printTree( $tree ); |
| 67 | return CargoUtils::smartParse( $result, $this->mParser ); |
| 68 | } |
| 69 | |
| 70 | protected function printNode( $tree, $nodeName, $level ) { |
| 71 | $node = $tree->getNode( $nodeName ); |
| 72 | $text = str_repeat( '*', $level ); |
| 73 | if ( $level == 1 ) { |
| 74 | $text .= "$nodeName\n"; |
| 75 | } else { |
| 76 | $text .= $this->displayRow( $node->getValues(), $this->mFieldDescriptions ) . "\n"; |
| 77 | } |
| 78 | foreach ( $node->getChildren() as $childName ) { |
| 79 | $text .= $this->printNode( $tree, $childName, $level + 1 ); |
| 80 | } |
| 81 | return $text; |
| 82 | } |
| 83 | |
| 84 | protected function printTree( $tree ) { |
| 85 | // Print subtree for each top-level node. |
| 86 | $text = ''; |
| 87 | foreach ( $tree->getNodes() as $nodeName => $node ) { |
| 88 | if ( $node->getParent() == null ) { |
| 89 | $text .= $this->printNode( $tree, $nodeName, 1 ); |
| 90 | } |
| 91 | } |
| 92 | return $text; |
| 93 | } |
| 94 | } |