Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 93 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
CargoGalleryFormat | |
0.00% |
0 / 93 |
|
0.00% |
0 / 3 |
1482 | |
0.00% |
0 / 1 |
allowedParameters | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
getFileTitles | |
0.00% |
0 / 37 |
|
0.00% |
0 / 1 |
240 | |||
display | |
0.00% |
0 / 47 |
|
0.00% |
0 / 1 |
506 |
1 | <?php |
2 | /** |
3 | * @author Yaron Koren |
4 | * @ingroup Cargo |
5 | * |
6 | * Defines the 'gallery' format, which matches the output of MediaWiki's |
7 | * <gallery> tag. |
8 | */ |
9 | |
10 | class CargoGalleryFormat extends CargoDisplayFormat { |
11 | |
12 | public static function allowedParameters() { |
13 | return [ |
14 | 'mode' => [ 'values' => [ 'traditional', 'nolines', 'packed', 'packed-overlay', 'packed-hover' ] ], |
15 | 'show bytes' => [ 'type' => 'boolean' ], |
16 | 'show filename' => [ 'type' => 'boolean' ], |
17 | 'show dimensions' => [ 'type' => 'boolean' ], |
18 | 'per row' => [ 'type' => 'int' ], |
19 | 'image width' => [ 'type' => 'int' ], |
20 | 'image height' => [ 'type' => 'int' ] |
21 | ]; |
22 | } |
23 | |
24 | protected function getFileTitles( $valuesTable, $fieldDescriptions, $captionField, $altField, $linkField ) { |
25 | $fileField = null; |
26 | foreach ( $fieldDescriptions as $field => $fieldDesc ) { |
27 | if ( $fieldDesc->mType == 'File' ) { |
28 | $fileField = $field; |
29 | break; |
30 | } |
31 | } |
32 | |
33 | // If there's no 'File' field in the schema, just use the |
34 | // page name. |
35 | if ( $fileField == null ) { |
36 | $usingPageName = true; |
37 | $fileField = '_pageName'; |
38 | } else { |
39 | $usingPageName = false; |
40 | } |
41 | |
42 | $fileNames = []; |
43 | foreach ( $valuesTable as $row ) { |
44 | if ( array_key_exists( $fileField, $row ) ) { |
45 | $caption = ( $captionField == null ) ? null : $row[$captionField]; |
46 | $alt = ( $altField == null ) ? null : $row[$altField]; |
47 | $link = ( $linkField == null ) ? null : Title::newFromText( $row[$linkField] ); |
48 | $fileNames[] = [ |
49 | 'title' => $row[$fileField], |
50 | 'caption' => $caption, |
51 | 'alt' => $alt, |
52 | 'link' => $link |
53 | ]; |
54 | } |
55 | } |
56 | |
57 | $files = []; |
58 | foreach ( $fileNames as $f ) { |
59 | if ( $usingPageName ) { |
60 | $title = Title::newFromText( $f['title'] ); |
61 | if ( $title == null || $title->getNamespace() != NS_FILE ) { |
62 | continue; |
63 | } |
64 | } else { |
65 | $title = Title::makeTitleSafe( NS_FILE, $f['title'] ); |
66 | if ( $title == null ) { |
67 | continue; |
68 | } |
69 | } |
70 | |
71 | $files[] = [ |
72 | 'title' => $title, |
73 | 'caption' => CargoUtils::smartParse( $f['caption'], null ), |
74 | 'alt' => $f['alt'], |
75 | 'link' => ( $f['link'] !== null ) ? $f['link']->getLinkURL() : null |
76 | ]; |
77 | |
78 | } |
79 | |
80 | return $files; |
81 | } |
82 | |
83 | /** |
84 | * @param array $valuesTable Unused |
85 | * @param array $formattedValuesTable |
86 | * @param array $fieldDescriptions |
87 | * @param array $displayParams Unused |
88 | * @return string HTML |
89 | */ |
90 | public function display( $valuesTable, $formattedValuesTable, $fieldDescriptions, $displayParams ) { |
91 | $this->mOutput->addModules( [ 'mediawiki.page.gallery' ] ); |
92 | $this->mOutput->addModuleStyles( [ 'mediawiki.page.gallery.styles' ] ); |
93 | |
94 | if ( array_key_exists( 'caption field', $displayParams ) ) { |
95 | $captionField = str_replace( '_', ' ', $displayParams['caption field'] ); |
96 | if ( $captionField[0] == ' ' ) { |
97 | $captionField[0] = '_'; |
98 | } |
99 | if ( count( $valuesTable ) > 0 && !array_key_exists( $captionField, $valuesTable[0] ) ) { |
100 | throw new MWException( wfMessage( "cargo-query-specifiedfieldmissing", $captionField, "caption field" )->parse() ); |
101 | } |
102 | } else { |
103 | $captionField = null; |
104 | } |
105 | if ( array_key_exists( 'alt field', $displayParams ) ) { |
106 | $altField = str_replace( '_', ' ', $displayParams['alt field'] ); |
107 | if ( $altField[0] == ' ' ) { |
108 | $altField[0] = '_'; |
109 | } |
110 | if ( count( $valuesTable ) > 0 && !array_key_exists( $altField, $valuesTable[0] ) ) { |
111 | throw new MWException( wfMessage( "cargo-query-specifiedfieldmissing", $altField, "alt field" )->parse() ); |
112 | } |
113 | } else { |
114 | $altField = null; |
115 | } |
116 | if ( array_key_exists( 'link field', $displayParams ) ) { |
117 | $linkField = str_replace( '_', ' ', $displayParams['link field'] ); |
118 | if ( $linkField[0] == ' ' ) { |
119 | $linkField[0] = '_'; |
120 | } |
121 | if ( count( $valuesTable ) > 0 && !array_key_exists( $linkField, $valuesTable[0] ) ) { |
122 | throw new MWException( wfMessage( "cargo-query-specifiedfieldmissing", $linkField, "link field" )->parse() ); |
123 | } |
124 | } else { |
125 | $linkField = null; |
126 | } |
127 | |
128 | $files = self::getFileTitles( $valuesTable, $fieldDescriptions, $captionField, $altField, $linkField ); |
129 | // Display mode - can be 'traditional'/null, 'nolines', |
130 | // 'packed', 'packed-overlay' or 'packed-hover'; see |
131 | // https://www.mediawiki.org/wiki/Help:Images#Mode_parameter |
132 | $mode = ( array_key_exists( 'mode', $displayParams ) ) ? |
133 | $displayParams['mode'] : null; |
134 | |
135 | try { |
136 | // @TODO - it would be nice to pass in a context here, |
137 | // if that's possible. |
138 | $gallery = ImageGalleryBase::factory( $mode ); |
139 | } catch ( MWException $e ) { |
140 | // User specified something invalid, fallback to default. |
141 | $gallery = ImageGalleryBase::factory( false ); |
142 | } |
143 | if ( array_key_exists( 'show bytes', $displayParams ) ) { |
144 | $gallery->setShowBytes( $displayParams['show bytes'] ); |
145 | } |
146 | if ( array_key_exists( 'show dimensions', $displayParams ) ) { |
147 | $gallery->setShowDimensions( $displayParams['show dimensions'] ); |
148 | } |
149 | if ( array_key_exists( 'show filename', $displayParams ) ) { |
150 | $gallery->setShowFilename( $displayParams['show filename'] ); |
151 | } |
152 | if ( array_key_exists( 'per row', $displayParams ) ) { |
153 | $gallery->setPerRow( $displayParams['per row'] ); |
154 | } |
155 | if ( array_key_exists( 'image width', $displayParams ) ) { |
156 | $gallery->setWidths( $displayParams['image width'] ); |
157 | } |
158 | if ( array_key_exists( 'image height', $displayParams ) ) { |
159 | $gallery->setHeights( $displayParams['image height'] ); |
160 | } |
161 | |
162 | foreach ( $files as $file ) { |
163 | $gallery->add( $file['title'], $file['caption'], $file['alt'], $file['link'] ); |
164 | } |
165 | |
166 | $text = "<div id=\"mw-category-media\">\n"; |
167 | $text .= $gallery->toHTML(); |
168 | $text .= "\n</div>"; |
169 | |
170 | return $text; |
171 | } |
172 | |
173 | } |