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