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