Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 96 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| CargoGalleryFormat | |
0.00% |
0 / 96 |
|
0.00% |
0 / 3 |
1560 | |
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 / 50 |
|
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 | |
| 10 | use MediaWiki\Title\Title; |
| 11 | |
| 12 | class 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 | if ( $usingPageName ) { |
| 62 | $title = Title::newFromText( $f['title'] ); |
| 63 | if ( $title == null || $title->getNamespace() != NS_FILE ) { |
| 64 | continue; |
| 65 | } |
| 66 | } else { |
| 67 | $title = Title::makeTitleSafe( NS_FILE, $f['title'] ); |
| 68 | if ( $title == null ) { |
| 69 | continue; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | $files[] = [ |
| 74 | 'title' => $title, |
| 75 | 'caption' => CargoUtils::smartParse( $f['caption'], null ), |
| 76 | 'alt' => $f['alt'], |
| 77 | 'link' => ( $f['link'] !== null ) ? $f['link']->getLinkURL() : null |
| 78 | ]; |
| 79 | |
| 80 | } |
| 81 | |
| 82 | return $files; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @param array $valuesTable Unused |
| 87 | * @param array $formattedValuesTable |
| 88 | * @param array $fieldDescriptions |
| 89 | * @param array $displayParams Unused |
| 90 | * @return string HTML |
| 91 | */ |
| 92 | public function display( $valuesTable, $formattedValuesTable, $fieldDescriptions, $displayParams ) { |
| 93 | $this->mOutput->addModules( [ 'mediawiki.page.gallery' ] ); |
| 94 | $this->mOutput->addModuleStyles( [ 'mediawiki.page.gallery.styles' ] ); |
| 95 | |
| 96 | if ( array_key_exists( 'caption field', $displayParams ) ) { |
| 97 | $captionField = str_replace( '_', ' ', $displayParams['caption field'] ); |
| 98 | if ( $captionField[0] == ' ' ) { |
| 99 | $captionField[0] = '_'; |
| 100 | } |
| 101 | if ( count( $valuesTable ) > 0 && !array_key_exists( $captionField, $valuesTable[0] ) ) { |
| 102 | throw new MWException( wfMessage( "cargo-query-specifiedfieldmissing", $captionField, "caption field" )->parse() ); |
| 103 | } |
| 104 | } else { |
| 105 | $captionField = null; |
| 106 | } |
| 107 | if ( array_key_exists( 'alt field', $displayParams ) ) { |
| 108 | $altField = str_replace( '_', ' ', $displayParams['alt field'] ); |
| 109 | if ( $altField[0] == ' ' ) { |
| 110 | $altField[0] = '_'; |
| 111 | } |
| 112 | if ( count( $valuesTable ) > 0 && !array_key_exists( $altField, $valuesTable[0] ) ) { |
| 113 | throw new MWException( wfMessage( "cargo-query-specifiedfieldmissing", $altField, "alt field" )->parse() ); |
| 114 | } |
| 115 | } else { |
| 116 | $altField = null; |
| 117 | } |
| 118 | if ( array_key_exists( 'link field', $displayParams ) ) { |
| 119 | $linkField = str_replace( '_', ' ', $displayParams['link field'] ); |
| 120 | if ( $linkField[0] == ' ' ) { |
| 121 | $linkField[0] = '_'; |
| 122 | } |
| 123 | if ( count( $valuesTable ) > 0 && !array_key_exists( $linkField, $valuesTable[0] ) ) { |
| 124 | throw new MWException( wfMessage( "cargo-query-specifiedfieldmissing", $linkField, "link field" )->parse() ); |
| 125 | } |
| 126 | } else { |
| 127 | $linkField = null; |
| 128 | } |
| 129 | |
| 130 | $files = self::getFileTitles( $valuesTable, $fieldDescriptions, $captionField, $altField, $linkField ); |
| 131 | // Display mode - can be 'traditional'/null, 'nolines', |
| 132 | // 'packed', 'packed-overlay' or 'packed-hover'; see |
| 133 | // https://www.mediawiki.org/wiki/Help:Images#Mode_parameter |
| 134 | $mode = ( array_key_exists( 'mode', $displayParams ) ) ? |
| 135 | $displayParams['mode'] : null; |
| 136 | |
| 137 | try { |
| 138 | // @TODO - it would be nice to pass in a context here, |
| 139 | // if that's possible. |
| 140 | $gallery = ImageGalleryBase::factory( $mode ); |
| 141 | } catch ( MWException $e ) { |
| 142 | // User specified something invalid, fallback to default. |
| 143 | $gallery = ImageGalleryBase::factory( false ); |
| 144 | } |
| 145 | |
| 146 | $parseWidthParamChecker = new ReflectionMethod( Parser::class, 'parseWidthParam' ); |
| 147 | if ( !$parseWidthParamChecker->isStatic() ) { |
| 148 | // MW >= 1.43 |
| 149 | // This is all due to the change at |
| 150 | // https://phabricator.wikimedia.org/rMW0450b5e4d58387a0d57dc699c2c58f8e780ca44e |
| 151 | $gallery->setParser( $this->getParser() ); |
| 152 | } |
| 153 | if ( array_key_exists( 'show bytes', $displayParams ) ) { |
| 154 | $gallery->setShowBytes( $displayParams['show bytes'] ); |
| 155 | } |
| 156 | if ( array_key_exists( 'show dimensions', $displayParams ) ) { |
| 157 | $gallery->setShowDimensions( $displayParams['show dimensions'] ); |
| 158 | } |
| 159 | if ( array_key_exists( 'show filename', $displayParams ) ) { |
| 160 | $gallery->setShowFilename( $displayParams['show filename'] ); |
| 161 | } |
| 162 | if ( array_key_exists( 'per row', $displayParams ) ) { |
| 163 | $gallery->setPerRow( $displayParams['per row'] ); |
| 164 | } |
| 165 | if ( array_key_exists( 'image width', $displayParams ) ) { |
| 166 | $gallery->setWidths( $displayParams['image width'] ); |
| 167 | } |
| 168 | if ( array_key_exists( 'image height', $displayParams ) ) { |
| 169 | $gallery->setHeights( $displayParams['image height'] ); |
| 170 | } |
| 171 | |
| 172 | foreach ( $files as $file ) { |
| 173 | $gallery->add( $file['title'], $file['caption'], $file['alt'], $file['link'] ); |
| 174 | } |
| 175 | |
| 176 | $text = "<div id=\"mw-category-media\">\n"; |
| 177 | $text .= $gallery->toHTML(); |
| 178 | $text .= "\n</div>"; |
| 179 | |
| 180 | return $text; |
| 181 | } |
| 182 | |
| 183 | } |