Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 57 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| CargoTagCloudFormat | |
0.00% |
0 / 57 |
|
0.00% |
0 / 2 |
210 | |
0.00% |
0 / 1 |
| allowedParameters | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| display | |
0.00% |
0 / 52 |
|
0.00% |
0 / 1 |
182 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @author Cindy Cicalese |
| 4 | * @ingroup Cargo |
| 5 | */ |
| 6 | |
| 7 | use MediaWiki\Html\Html; |
| 8 | |
| 9 | class CargoTagCloudFormat extends CargoDisplayFormat { |
| 10 | |
| 11 | public static function allowedParameters() { |
| 12 | return [ |
| 13 | 'template' => [ 'type' => 'string' ], |
| 14 | 'min size' => [ 'type' => 'int' ], |
| 15 | 'max size' => [ 'type' => 'int' ] |
| 16 | ]; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * @param array $valuesTagCloud Unused |
| 21 | * @param array $formattedValuesTagCloud |
| 22 | * @param array $fieldDescriptions |
| 23 | * @param array $displayParams |
| 24 | * @return string HTML |
| 25 | */ |
| 26 | public function display( $valuesTagCloud, $formattedValuesTagCloud, $fieldDescriptions, $displayParams ) { |
| 27 | $this->mOutput->addModuleStyles( [ 'ext.cargo.main' ] ); |
| 28 | |
| 29 | if ( count( $fieldDescriptions ) < 2 ) { |
| 30 | return ''; |
| 31 | } |
| 32 | |
| 33 | $fieldNames = array_keys( $fieldDescriptions ); |
| 34 | $tagFieldName = $fieldNames[0]; |
| 35 | $countFieldName = $fieldNames[1]; |
| 36 | |
| 37 | if ( $fieldDescriptions[$countFieldName]->mType != 'Integer' ) { |
| 38 | return ''; |
| 39 | } |
| 40 | |
| 41 | $tags = []; |
| 42 | |
| 43 | foreach ( $formattedValuesTagCloud as $row ) { |
| 44 | |
| 45 | $tag = $row[$tagFieldName]; |
| 46 | $count = $row[$countFieldName]; |
| 47 | |
| 48 | if ( strlen( $tag ) > 0 && is_numeric( $count ) && $count > 0 ) { |
| 49 | |
| 50 | $tags[$tag] = $count; |
| 51 | |
| 52 | } |
| 53 | |
| 54 | } |
| 55 | |
| 56 | if ( $tags == [] ) { |
| 57 | return ''; |
| 58 | } |
| 59 | |
| 60 | $maxSize = $displayParams['max size'] ?? 200; |
| 61 | $minSize = $displayParams['min size'] ?? 80; |
| 62 | |
| 63 | $maxSizeIncrease = $maxSize - $minSize; |
| 64 | |
| 65 | $minCount = min( $tags ); |
| 66 | $maxCount = max( $tags ); |
| 67 | |
| 68 | if ( $maxCount == $minCount ) { |
| 69 | $size = $minSize + $maxSizeIncrease / 2; |
| 70 | } else { |
| 71 | $denominator = log( $maxCount ) - log( $minCount ); |
| 72 | $sizes = []; |
| 73 | } |
| 74 | |
| 75 | $attrs = [ |
| 76 | 'class' => 'cargoTagCloud', |
| 77 | 'align' => 'justify' |
| 78 | ]; |
| 79 | |
| 80 | $text = Html::openElement( 'div', $attrs ); |
| 81 | |
| 82 | foreach ( $tags as $tag => $count ) { |
| 83 | if ( isset( $displayParams['template'] ) ) { |
| 84 | $tagstring = '{{' . $displayParams['template'] . |
| 85 | '|' . $tag . '|' . $count . '}}'; |
| 86 | $tagstring = CargoUtils::smartParse( $tagstring, null ); |
| 87 | } else { |
| 88 | $tagstring = $tag; |
| 89 | } |
| 90 | if ( $maxCount != $minCount ) { |
| 91 | $countstr = strval( $count ); |
| 92 | if ( !isset( $sizes[$countstr] ) ) { |
| 93 | $sizes[$countstr] = |
| 94 | $minSize + $maxSizeIncrease * |
| 95 | ( log( $count ) - log( $minCount ) ) / |
| 96 | $denominator; |
| 97 | } |
| 98 | $size = $sizes[$countstr]; |
| 99 | } |
| 100 | $text .= Html::rawElement( 'span', |
| 101 | [ |
| 102 | 'style' => 'font-size:' . $size . '%;white-space:nowrap;' |
| 103 | ], |
| 104 | $tagstring ); |
| 105 | $text .= ' '; |
| 106 | } |
| 107 | |
| 108 | $text .= Html::closeElement( 'div' ); |
| 109 | |
| 110 | return $text; |
| 111 | } |
| 112 | |
| 113 | } |