Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CargoOutlineFormat
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 3
462
0.00% covered (danger)
0.00%
0 / 1
 allowedParameters
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 printTree
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
132
 display
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
90
1<?php
2
3/**
4 * A class to print query results in an outline format, along with some
5 * helper classes to handle the aggregation
6 *
7 * Code is based heavily on the code for the 'outline' format in the
8 * Semantic Result Formats extension.
9 *
10 * @author Yaron Koren
11 */
12
13use MediaWiki\Html\Html;
14
15class CargoOutlineFormat extends CargoListFormat {
16
17    /** @var string[] */
18    protected $mOutlineFields = [];
19    /** @var array|null */
20    public $mFieldDescriptions;
21
22    public static function allowedParameters() {
23        return [ 'outline fields' => [ 'type' => 'string' ] ];
24    }
25
26    protected function printTree( $outlineTree, $level = 0 ) {
27        $text = "";
28        if ( $outlineTree->mUnsortedRows !== null ) {
29            $text .= "<ul>\n";
30            foreach ( $outlineTree->mUnsortedRows as $row ) {
31                $text .= Html::rawElement( 'li', [],
32                    $this->displayRow( $row->mDisplayFields, $this->mFieldDescriptions ) ) . "\n";
33            }
34            $text .= "</ul>\n";
35        }
36        if ( $level > 0 ) {
37            $text .= "<ul>\n";
38        }
39        $numLevels = count( $this->mOutlineFields );
40        // Set font size and weight depending on level we're at.
41        $fontLevel = $level;
42        if ( $numLevels < 4 ) {
43            $fontLevel += ( 4 - $numLevels );
44        }
45        if ( $fontLevel == 0 ) {
46            $fontSize = 'x-large';
47        } elseif ( $fontLevel == 1 ) {
48            $fontSize = 'large';
49        } elseif ( $fontLevel == 2 ) {
50            $fontSize = 'medium';
51        } else {
52            $fontSize = 'small';
53        }
54        if ( $fontLevel == 3 ) {
55            $fontWeight = 'bold';
56        } else {
57            $fontWeight = 'regular';
58        }
59        foreach ( $outlineTree->mTree as $node ) {
60            $text .= Html::rawElement( 'p',
61                [ 'style' =>
62                "font-size: $fontSize; font-weight: $fontWeight;" ], $node->mFormattedValue ) . "\n";
63            $text .= $this->printTree( $node, $level + 1 );
64        }
65        if ( $level > 0 ) {
66            $text .= "</ul>\n";
67        }
68        return $text;
69    }
70
71    public function display( $valuesTable, $formattedValuesTable, $fieldDescriptions, $displayParams ) {
72        if ( !array_key_exists( 'outline fields', $displayParams ) ) {
73            throw new MWException( wfMessage( "cargo-query-missingparam", "outline fields", "outline" )->parse() );
74        }
75        $outlineFields = explode( ',', $displayParams['outline fields'] );
76        $this->mOutlineFields = [];
77        foreach ( $outlineFields as $outlineField ) {
78            $modifiedOutlineField = trim( $outlineField );
79            if ( $modifiedOutlineField[0] != '_' ) {
80                $modifiedOutlineField = str_replace( '_', ' ', $modifiedOutlineField );
81            }
82            $this->mOutlineFields[] = $modifiedOutlineField;
83        }
84        $this->mFieldDescriptions = $fieldDescriptions;
85
86        // For each result row, create an array of the row itself
87        // and all its sorted-on fields, and add it to the initial
88        // 'tree'.
89        $outlineTree = new CargoOutlineTree();
90        foreach ( $valuesTable as $rowNum => $queryResultsRow ) {
91            $coRow = new CargoOutlineRow();
92            foreach ( $queryResultsRow as $fieldName => $value ) {
93                $formattedValue = $formattedValuesTable[$rowNum][$fieldName];
94                if ( in_array( $fieldName, $this->mOutlineFields ) ) {
95                    if ( property_exists( $fieldDescriptions[$fieldName], 'isList' ) ) {
96                        $delimiter = $fieldDescriptions[$fieldName]['delimiter'];
97                        $coRow->addOutlineFieldValues( $fieldName, array_map( 'trim', explode( $delimiter, $value ) ),
98                            array_map( 'trim', explode( $delimiter, $formattedValue ) ) );
99                    } else {
100                        $coRow->addOutlineFieldValue( $fieldName, $value, $formattedValue );
101                    }
102                } else {
103                    $coRow->addDisplayFieldValue( $fieldName, $formattedValue );
104                }
105            }
106            $outlineTree->addRow( $coRow );
107        }
108
109        // Now, cycle through the outline fields, creating the tree.
110        foreach ( $this->mOutlineFields as $outlineField ) {
111            $outlineTree->addField( $outlineField );
112        }
113        $result = $this->printTree( $outlineTree );
114
115        return $result;
116    }
117
118}