Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
CargoDisplayFormat
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 5
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 allowedParameters
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isDeferred
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 formatArray
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
30
 getParser
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3use MediaWiki\MediaWikiServices;
4
5/**
6 * @author Yaron Koren
7 * @ingroup Cargo
8 */
9
10class CargoDisplayFormat {
11
12    /** @var OutputPage|ParserOutput */
13    protected $mOutput;
14    /** @var Parser|null */
15    protected $mParser;
16
17    /**
18     * @param OutputPage|ParserOutput $output
19     * @param Parser|null $parser
20     */
21    public function __construct( $output, $parser = null ) {
22        $this->mOutput = $output;
23        $this->mParser = $parser;
24    }
25
26    public static function allowedParameters() {
27        return [];
28    }
29
30    public static function isDeferred() {
31        return false;
32    }
33
34    /**
35     * Apply a Cargo format to a 2D row-based array of values of any origin.
36     *
37     * @author Alexander Mashin
38     * @param Parser $parser
39     * @param array $values A 2D row-based array of values.
40     * @param array $mappings A mapping from ED to Cargo variables.
41     * @param array $params An array of params for {{#cargo_query:}}.
42     *
43     * @return array [ string, 'noparse' => bool, 'isHTML' => bool ].
44     */
45    public static function formatArray( Parser $parser, array $values, array $mappings, array $params ): array {
46        $format = $params['format'] ?? 'list';
47        $classes = CargoQueryDisplayer::getAllFormatClasses();
48        /** @var CargoDisplayFormat $class */
49        $class = $classes[ $format ] ?? CargoListFormat::class;
50        $formatter = new $class( $parser->getOutput(), $parser );
51
52        // This cannot yet be called for "deferred" formats, where the
53        // processing is done via JavaScript and not PHP - in theory,
54        // it could be done, but it would require being able to pass
55        // in a data set to the JavaScript, instead of query
56        // information.
57        if ( $formatter->isDeferred() ) {
58            throw new MWException( "formatArray() cannot be called for the $format format because it is a \"deferred format\"." );
59        }
60
61        $query_displayer = new CargoQueryDisplayer();
62        $field_descriptions = [];
63        foreach ( $mappings as $local => $external ) {
64            $description = new CargoFieldDescription();
65            $description->mType = 'String';
66            $field_descriptions[$local] = $description;
67        }
68        $query_displayer->mFieldDescriptions = $field_descriptions;
69        $query_displayer->mFieldTables = [];
70
71        $html = $formatter->display(
72            $values,
73            $query_displayer->getFormattedQueryResults( $values ),
74            $query_displayer->mFieldDescriptions,
75            $params
76        );
77        $no_html = $params['no html'] ?? false;
78        return !$no_html && $format !== 'template'
79            ? [ $html, 'noparse' => true, 'isHTML' => true ]
80            : [ $html, 'noparse' => false ];
81    }
82
83    public function getParser() {
84        return $this->mParser ?? MediaWikiServices::getInstance()->getParser();
85    }
86}