Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 37 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| CargoTemplateFormat | |
0.00% |
0 / 37 |
|
0.00% |
0 / 3 |
240 | |
0.00% |
0 / 1 |
| allowedParameters | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| displayRow | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
42 | |||
| display | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
72 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @author Yaron Koren |
| 4 | * @ingroup Cargo |
| 5 | */ |
| 6 | |
| 7 | class CargoTemplateFormat extends CargoDisplayFormat { |
| 8 | |
| 9 | public static function allowedParameters() { |
| 10 | return [ |
| 11 | 'template' => [ 'type' => 'string' ], |
| 12 | 'named args' => [ 'type' => 'boolean' ], |
| 13 | 'delimiter' => [ 'type' => 'string', 'label' => wfMessage( 'cargo-viewdata-delimiterparam' )->parse() ] |
| 14 | ]; |
| 15 | } |
| 16 | |
| 17 | protected function displayRow( $templateName, $row, $fieldDescriptions, $namedArgs ) { |
| 18 | $wikiText = '{{' . $templateName; |
| 19 | // If we're not using named arguments, we add the field number |
| 20 | // in to the template call, to not mess up values that contain '='. |
| 21 | $fieldNum = 1; |
| 22 | foreach ( $fieldDescriptions as $fieldName => $fieldDescription ) { |
| 23 | if ( array_key_exists( $fieldName, $row ) ) { |
| 24 | $paramName = $namedArgs ? $fieldName : $fieldNum; |
| 25 | // HTML-decode the Wikitext values, which were |
| 26 | // encoded in CargoSQLQuery::run(). |
| 27 | // We do this only for the "template" format |
| 28 | // because it's the only one that uses the |
| 29 | // unformatted values - the formatted values |
| 30 | // do this HTML-encoding on their own. |
| 31 | $value = $row[$fieldName] ?? ''; |
| 32 | if ( $fieldDescription->mType == 'Wikitext' || $fieldDescription->mType == 'Wikitext string' ) { |
| 33 | $value = htmlspecialchars_decode( $value ); |
| 34 | } |
| 35 | // Escape pipes within the values so that they |
| 36 | // aren't interpreted as template pipes. |
| 37 | $value = str_replace( '|', '{{!}}', $value ); |
| 38 | $wikiText .= '|' . $paramName . '=' . $value; |
| 39 | $fieldNum++; |
| 40 | } |
| 41 | } |
| 42 | $wikiText .= "\n}}"; |
| 43 | return $wikiText; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @param array $valuesTable |
| 48 | * @param array $formattedValuesTable Unused |
| 49 | * @param array $fieldDescriptions |
| 50 | * @param array $displayParams |
| 51 | * @return string |
| 52 | * @throws MWException |
| 53 | */ |
| 54 | public function display( $valuesTable, $formattedValuesTable, $fieldDescriptions, $displayParams ) { |
| 55 | if ( !array_key_exists( 'template', $displayParams ) ) { |
| 56 | throw new MWException( wfMessage( "cargo-query-missingparam", "template", "template" )->parse() ); |
| 57 | } |
| 58 | |
| 59 | $templateName = $displayParams['template']; |
| 60 | $namedArgs = false; |
| 61 | if ( array_key_exists( 'named args', $displayParams ) ) { |
| 62 | $namedArgs = strtolower( $displayParams['named args'] ) == 'yes'; |
| 63 | } |
| 64 | $delimiter = ( array_key_exists( 'delimiter', $displayParams ) ) ? |
| 65 | $displayParams['delimiter'] : ''; |
| 66 | $delimiter = str_replace( '\n', "\n", $delimiter ); |
| 67 | $text = ''; |
| 68 | if ( array_key_exists( 'intro', $displayParams ) ) { |
| 69 | $text .= "\n" . $displayParams['intro'] . "\n"; |
| 70 | } |
| 71 | foreach ( $valuesTable as $i => $row ) { |
| 72 | if ( $i > 0 ) { |
| 73 | $text .= $delimiter; |
| 74 | } |
| 75 | $text .= $this->displayRow( $templateName, $row, $fieldDescriptions, $namedArgs ); |
| 76 | } |
| 77 | if ( array_key_exists( 'outro', $displayParams ) ) { |
| 78 | $text .= "\n" . $displayParams['outro'] . "\n"; |
| 79 | } |
| 80 | return $text; |
| 81 | } |
| 82 | |
| 83 | } |