Completed
Push — master ( eadb3e...25e675 )
by
unknown
10:12
created

CarouselGallery::toHTML()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 15
cts 15
cp 1
rs 9.552
c 0
b 0
f 0
cc 4
nc 6
nop 1
crap 4
1
<?php
2
/**
3
 * Contains the class providing and rendering the carousel gallery mode.
4
 *
5
 * @copyright (C) 2018, Tobias Oetterer, Paderborn University
6
 * @license       https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License, version 3 (or later)
7
 *
8
 * This file is part of the MediaWiki extension BootstrapComponents.
9
 * The BootstrapComponents extension is free software: you can redistribute it
10
 * and/or modify it under the terms of the GNU General Public License as published
11
 * by the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * The BootstrapComponents extension is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
21
 *
22
 * @file
23
 * @ingroup       BootstrapComponents
24
 * @author        Tobias Oetterer
25
 */
26
27
namespace BootstrapComponents;
28
29
use BootstrapComponents\Components\Carousel;
30
use \ImageGalleryBase;
31
32
/**
33
 * Class CarouselGallery
34
 *
35
 * @since 1.0
36
 */
37
class CarouselGallery extends ImageGalleryBase {
38
39
	/**
40
	 * Renders the carousel gallery.
41
	 *
42
	 * @param ParserOutputHelper $parserOutputHelper used for unit tests
43
	 *
44
	 * @throws \MWException cascading {@see CarouselGallery::constructCarouselParserRequest} and  {@see AbstractComponent::parseComponent}
45
	 * @return string
46
	 */
47 5
	public function toHTML( $parserOutputHelper = null ) {
48 5
		$parserOutputHelper = is_null( $parserOutputHelper )
49 5
			? ApplicationFactory::getInstance()->getParserOutputHelper( $this->mParser )
50 5
			: $parserOutputHelper;
51
52
		// if there were no images registered with the gallery, display error message and exit.
53 5
		if ( $this->isEmpty() ) {
54 2
			return $parserOutputHelper->renderErrorMessage( 'bootstrap-components-carousel-images-missing' );
55
		}
56
57 4
		$carousel = new Carousel(
58 4
			ApplicationFactory::getInstance()->getComponentLibrary(),
59 4
			$parserOutputHelper,
60 4
			ApplicationFactory::getInstance()->getNestingController()
61 4
		);
62 4
		$carouselParserRequest = $this->constructCarouselParserRequest();
63
64
		// if there were no valid images found in the list of registered images, display error message and exit.
65 4
		if ( $carouselParserRequest === false ) {
66 2
			return $parserOutputHelper->renderErrorMessage( 'bootstrap-components-carousel-images-missing' );
67
		}
68 3
		return $carousel->parseComponent( $carouselParserRequest );
69
	}
70
71
	/**
72
	 * This merges two different kind of attributes, so that the merger can later be treated as parser function attributes.
73
	 *
74
	 * @param array $origAttributes
75
	 * @param array $newAttributes
76
	 *
77
	 * @return array
78
	 */
79 3
	private function addParserFunctionAttributes( $origAttributes, $newAttributes ) {
80 3
		if ( empty( $newAttributes ) ) {
81 2
			return $origAttributes;
82
		}
83 2
		return array_merge( $origAttributes, $newAttributes );
84
	}
85
86
	/**
87
	 * Builds an image tag like it is normally used in wiki text.
88
	 *
89
	 * @param array $imageData
90
	 *
91
	 * @return string
92
	 */
93 3
	private function buildImageStringFromData( $imageData ) {
94
95
		/** @var \Title $imageTitle */
96 3
		list( $imageTitle, $imageCaption, $imageAlt, $imageLink, $imageParams ) = $imageData;
97 3
		$imageParams['alt'] = $imageAlt;
98
		# @note: this is a local link. has to be an article name :(
99
		# @note: assuming here, that the correct link processing is done in image processing
100 3
		$imageParams['link'] = $imageLink;
101
102
		// note that imageCaption, imageAlt and imageLink are strings. the latter is a local link or empty
103
		// imageParams is an associative array param => value
104 3
		$carouselImage = '[[' . $imageTitle->getPrefixedText();
105 3
		if ( !empty( $imageCaption ) ) {
106 3
			$carouselImage .= '|' . $imageCaption;
107 3
		}
108 3
		if ( empty( $imageParams['class'] ) ) {
109 3
			$imageParams['class'] = 'img-responsive';
110 3
		} else {
111
			$imageParams['class'] .= ' img-responsive';
112
		}
113 3
		foreach ( $imageParams as $key => $val ) {
114 3
			if ( !empty( $val ) ) {
115 3
				$carouselImage .= '|' . $key . '=' . $val;
116 3
			}
117 3
		}
118 3
		$carouselImage .= ']]';
119
120 3
		return $carouselImage;
121
	}
122
123
	/**
124
	 * Extracts the gallery images and builds image tags for every valid image.
125
	 *
126
	 * @param         $imageList
127
	 * @param \Parser $parser
128
	 * @param bool    $hideBadImages
129
	 * @param bool    $contextTitle
130
	 *
131
	 * @return array
132
	 */
133 4
	private function convertImages( $imageList, $parser = null, $hideBadImages = true, $contextTitle = false ) {
134 4
		$newImageList = [];
135 4
		foreach ( $imageList as $imageData ) {
136
			/** @var \Title $imageTitle */
137 4
			$imageTitle = $imageData[0];
138
139 4
			if ( $imageTitle->getNamespace() !== NS_FILE ) {
140 2
				if ( is_a( $parser, 'Parser' ) ) {
141 2
					$parser->addTrackingCategory( 'broken-file-category' );
142 2
				}
143 2
				continue;
144 3
			} elseif ( $hideBadImages && wfIsBadImage( $imageTitle->getDBkey(), $contextTitle ) ) {
145
				continue;
146
			}
147
148 3
			$carouselImage = $this->buildImageStringFromData( $imageData );
149 3
			$newImageList[] = $carouselImage;
150 4
		}
151 4
		return $newImageList;
152
	}
153
154
	/**
155
	 * From array of supplies images and some other object properties, this constructs a parser request object,
156
	 * to be used in the carousel component.
157
	 *
158
	 * @throws \MWException cascading {@see ApplicationFactory::getNewParserRequest}
159
	 *
160
	 * @return false|ParserRequest  returns false, if no valid images were detected
161
	 */
162 4
	private function constructCarouselParserRequest() {
163 4
		$carouselAttributes = $this->convertImages(
164 4
			$this->getImages(),
165 4
			$this->mParser,
166 4
			$this->mHideBadImages,
167 4
			$this->getContextTitle()
168 4
		);
169 4
		if ( !count( $carouselAttributes ) ) {
170 2
			return false;
171
		}
172 3
		$carouselAttributes = $this->addParserFunctionAttributes( $carouselAttributes, $this->mAttribs );
173 3
		$input = array_shift( $carouselAttributes );
174
175 3
		return ApplicationFactory::getInstance()->getNewParserRequest(
176 3
			[ $input, $carouselAttributes, $this->mParser, null ],
177 3
			false,
178
			'gallery carousel'
179 3
		);
180
	}
181
}