Completed
Push — master ( ea0e5d...10ff2a )
by mw
02:37
created

formats/gallery/Gallery.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SRF;
4
5
use SMW\ResultPrinter;
6
7
use SMWQueryResult;
8
use SMWPrintRequest;
9
use SMWDataItem;
10
use SMWOutputs;
11
use SRFUtils;
12
13
use Html;
14
use ImageGallery;
15
use Title;
16
17
/**
18
 * Result printer that outputs query results as a image gallery.
19
 *
20
 * @author Jeroen De Dauw < [email protected] >
21
 * @author mwjames
22
 * @author Rowan Rodrik van der Molen
23
 */
24
class Gallery extends ResultPrinter {
25
26
	/**
27
	 * @see SMWResultPrinter::getName
28
	 *
29
	 * @return string
30
	 */
31
	public function getName() {
32
		return $this->getContext()->msg( 'srf_printername_gallery' )->text();
33
	}
34
35
	/**
36
	 * @see SMWResultPrinter::buildResult
37
	 *
38
	 * @since 1.8
39
	 *
40
	 * @param SMWQueryResult $results
41
	 *
42
	 * @return string
43
	 */
44
	protected function buildResult( SMWQueryResult $results ) {
45
46
		// Intro/outro are not planned to work with the widget option
47
		if ( ( $this->params['intro'] !== '' || $this->params['outro'] !== '' ) && $this->params['widget'] !== '' ){
48
			return $results->addErrors( array(
49
				$this->getContext()->msg( 'srf-error-option-mix', 'widget' )->inContentLanguage()->text()
50
			) );
51
		};
52
53
		return $this->getResultText( $results, $this->outputMode );
54
	}
55
56
	/**
57
	 * @see SMWResultPrinter::getResultText
58
	 *
59
	 * @param $results SMWQueryResult
60
	 * @param $outputmode integer
61
	 *
62
	 * @return string
63
	 */
64
	public function getResultText( SMWQueryResult $results, $outputmode ) {
0 ignored issues
show
getResultText uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
65
66
		$ig = new ImageGallery();
67
		$ig->setShowBytes( false );
68
		$ig->setShowFilename( false );
69
		$ig->setCaption( $this->mIntro ); // set caption to IQ header
70
71
		// No need for a special page to use the parser but for the "normal" page
72
		// view we have to ensure caption text is parsed correctly through the parser
73
		if ( !$this->isSpecialPage() ) {
74
			$ig->setParser( $GLOBALS['wgParser'] );
75
		}
76
77
		// Initialize
78
		static $statNr = 0;
79
		$html          = '';
80
		$processing    = '';
81
82
		if ( $this->params['widget'] == 'carousel' ) {
83
			// Carousel widget
84
			$ig->setAttributes( $this->getCarouselWidget() );
85
		} elseif ( $this->params['widget'] == 'slideshow' ) {
86
			// Slideshow widget
87
			$ig->setAttributes( $this->getSlideshowWidget() );
88
		} else {
89
90
			// Standard gallery attributes
91
			$attribs = array(
92
				'id' => uniqid(),
93
				'class' => $this->getImageOverlay(),
94
			);
95
96
			$ig->setAttributes( $attribs );
97
		}
98
99
		// Only use redirects where the overlay option is not used and redirect
100
		// thumb images towards a different target
101
		if ( $this->params['redirects'] !== '' && !$this->params['overlay'] ){
102
			SMWOutputs::requireResource( 'ext.srf.gallery.redirect' );
103
		}
104
105
		// For the carousel widget, the perrow option should not be set
106
		if ( $this->params['perrow'] !== '' && $this->params['widget'] !== 'carousel' ) {
107
			$ig->setPerRow( $this->params['perrow'] );
108
		}
109
110
		if ( $this->params['widths'] !== '' ) {
111
			$ig->setWidths( $this->params['widths'] );
112
		}
113
114
		if ( $this->params['heights'] !== '' ) {
115
			$ig->setHeights( $this->params['heights'] );
116
		}
117
118
		$printReqLabels = array();
119
		$redirectType = '';
120
121
		/**
122
		 * @var SMWPrintRequest $printReq
123
		 */
124
		foreach ( $results->getPrintRequests() as $printReq ) {
125
			$printReqLabels[] = $printReq->getLabel();
126
127
			// Get redirect type
128
			if ( $this->params['redirects'] === $printReq->getLabel() ){
129
			 $redirectType = $printReq->getTypeID();
130
			}
131
		}
132
133
		if ( $this->params['imageproperty'] !== '' && in_array( $this->params['imageproperty'], $printReqLabels ) ||
134
			$this->params['redirects'] !== '' && in_array( $this->params['redirects'], $printReqLabels ) ) {
135
136
			$this->addImageProperties(
137
				$results,
138
				$ig,
139
				$this->params['imageproperty'],
140
				$this->params['captionproperty'],
141
				$this->params['redirects'],
142
				$outputmode
143
			);
144
		} else {
145
			$this->addImagePages( $results, $ig );
146
		}
147
148
		// SRF Global settings
149
		SRFUtils::addGlobalJSVariables();
150
151
		// Display a processing image as long as the DOM is no ready
152
		if ( $this->params['widget'] !== '' ) {
153
			$processing = SRFUtils::htmlProcessingElement();
154
		}
155
156
		// Beautify the class selector
157
		$class = $this->params['widget'] ?  '-' . $this->params['widget'] . ' ' : '';
158
		$class = $this->params['redirects'] !== '' && $this->params['overlay'] === false ? $class . ' srf-redirect' . ' ': $class;
159
		$class = $this->params['class'] ? $class . ' ' . $this->params['class'] : $class ;
160
161
		// Separate content from result output
162
		if ( !$ig->isEmpty() ) {
163
			$attribs = array (
164
				'class'  => 'srf-gallery' . $class,
165
				'align'  => 'justify',
166
				'data-redirect-type' => $redirectType,
167
				'data-ns-text' => $this->getFileNsTextForPageLanguage()
168
			);
169
170
			$html = Html::rawElement( 'div', $attribs, $processing . $ig->toHTML() );
171
		}
172
173
		// If available, create a link that points to further results
174
		if ( $this->linkFurtherResults( $results ) ) {
175
			$html .= $this->getLink( $results, SMW_OUTPUT_HTML )->getText( SMW_OUTPUT_HTML, $this->mLinker );
176
		}
177
178
		return array( $html, 'nowiki' => true, 'isHTML' => true );
179
	}
180
181
	/**
182
	 * Handles queries where the images (and optionally their captions) are specified as properties.
183
	 *
184
	 * @since 1.5.3
185
	 *
186
	 * @param SMWQueryResult $results
187
	 * @param ImageGallery $ig
188
	 * @param string $imageProperty
189
	 * @param string $captionProperty
190
	 * @param string $redirectProperty
191
	 * @param $outputMode
192
	 */
193
	protected function addImageProperties( SMWQueryResult $results, ImageGallery &$ig, $imageProperty, $captionProperty, $redirectProperty, $outputMode ) {
194
		while ( /* array of SMWResultArray */ $rows = $results->getNext() ) { // Objects (pages)
195
			$images = array();
196
			$captions = array();
197
			$redirects = array();
198
199
			for ( $i = 0, $n = count( $rows ); $i < $n; $i++ ) { // Properties
200
				/**
201
				 * @var SMWResultArray $resultArray
202
				 * @var SMWDataValue $dataValue
203
				 */
204
				$resultArray = $rows[$i];
205
206
				$label = $resultArray->getPrintRequest()->getMode() == SMWPrintRequest::PRINT_THIS
207
					? '-' : $resultArray->getPrintRequest()->getLabel();
208
209
				// Make sure always use real label here otherwise it results in an empty array
210
				if ( $resultArray->getPrintRequest()->getLabel() == $imageProperty ) {
211
					while ( ( $dataValue = $resultArray->getNextDataValue() ) !== false ) { // Property values
212
						if ( $dataValue->getTypeID() == '_wpg' ) {
213
							$images[] = $dataValue->getTitle();
214
						}
215
					}
216
				} elseif ( $label == $captionProperty ) {
217
					while ( ( $dataValue = $resultArray->getNextDataValue() ) !== false ) { // Property values
218
						$captions[] = $dataValue->getShortText( $outputMode, $this->getLinker( true ) );
219
					}
220
				} elseif ( $label == $redirectProperty ) {
221
					while ( ( $dataValue = $resultArray->getNextDataValue() ) !== false ) { // Property values
222
						if ( $dataValue->getDataItem()->getDIType() == SMWDataItem::TYPE_WIKIPAGE ) {
223
							$redirects[] = $dataValue->getTitle();
224
						} elseif ( $dataValue->getDataItem()->getDIType() == SMWDataItem::TYPE_URI  ) {
225
						  $redirects[] = $dataValue->getURL();
226
						}
227
					}
228
				}
229
			}
230
231
			// Check available matches against captions
232
			$amountMatches = count( $captions ) == count( $images );
233
			$hasCaption = $amountMatches || count( $captions ) > 0;
234
235
			// Check available matches against redirects
236
			$amountRedirects = count( $redirects ) == count( $images );
237
			$hasRedirect = $amountRedirects || count( $redirects ) > 0;
238
239
			/**
240
			 * @var Title $imgTitle
241
			 */
242
			foreach ( $images as $imgTitle ) {
243
				if ( $imgTitle->exists() ) {
244
					$imgCaption = $hasCaption ? ( $amountMatches ? array_shift( $captions ) : $captions[0] ) : '';
245
					$imgRedirect = $hasRedirect ? ( $amountRedirects ? array_shift( $redirects ) : $redirects[0] ) : '';
246
					$this->addImageToGallery( $ig, $imgTitle, $imgCaption, $imgRedirect );
247
				}
248
			}
249
		}
250
	}
251
252
	/**
253
	 * Handles queries where the result objects are image pages.
254
	 *
255
	 * @since 1.5.3
256
	 *
257
	 * @param SMWQueryResult $results
258
	 * @param ImageGallery $ig
259
	 */
260
	protected function addImagePages( SMWQueryResult $results, ImageGallery &$ig ) {
261
		while ( $row = $results->getNext() ) {
262
			/**
263
			 * @var SMWResultArray $firstField
264
			 */
265
			$firstField = $row[0];
266
			$nextObject = $firstField->getNextDataValue();
267
268
			if ( $nextObject !== false ) {
269
				$imgTitle = $nextObject->getTitle();
270
271
				// Ensure the title belongs to the image namespace
272
				if ( $imgTitle instanceof Title && $imgTitle->getNamespace() === NS_FILE ) {
273
					$imgCaption = '';
274
275
					// Is there a property queried for display with ?property
276
					if ( isset( $row[1] ) ) {
277
						$imgCaption = $row[1]->getNextDataValue();
278
						if ( is_object( $imgCaption ) ) {
279
							$imgCaption = $imgCaption->getShortText( $this->outputMode, $this->getLinker( true ) );
280
						}
281
					}
282
283
					$this->addImageToGallery( $ig, $imgTitle, $imgCaption );
284
				}
285
			}
286
		}
287
	}
288
289
	/**
290
	 * Adds a single image to the gallery.
291
	 * Takes care of automatically adding a caption when none is provided and parsing it's wikitext.
292
	 *
293
	 * @since 1.5.3
294
	 *
295
	 * @param ImageGallery $ig The gallery to add the image to
296
	 * @param Title $imgTitle The title object of the page of the image
297
	 * @param string $imgCaption An optional caption for the image
298
	 * @param string $imgRedirect
299
	 */
300
	protected function addImageToGallery( ImageGallery &$ig, Title $imgTitle, $imgCaption, $imgRedirect = '' ) {
301
302
		if ( empty( $imgCaption ) ) {
303
			if ( $this->m_params['autocaptions'] ) {
304
				$imgCaption = $imgTitle->getBaseText();
305
306
				if ( !$this->m_params['fileextensions'] ) {
307
					$imgCaption = preg_replace( '#\.[^.]+$#', '', $imgCaption );
308
				}
309
			} else {
310
				$imgCaption = '';
311
			}
312
		} else {
313
			if ( $imgTitle instanceof Title && $imgTitle->getNamespace() == NS_FILE && !$this->isSpecialPage() ) {
314
				$imgCaption = $ig->mParser->recursiveTagParse( $imgCaption );
315
			}
316
		}
317
		// Use image alt as helper for either text
318
		$imgAlt =  $this->params['redirects'] === '' ? $imgCaption : $imgRedirect !== '' ? $imgRedirect : '' ;
319
		$ig->add( $imgTitle, $imgCaption, $imgAlt );
320
	}
321
322
	/**
323
	 * Returns the overlay setting
324
	 *
325
	 * @since 1.8
326
	 *
327
	 * @return string
328
	 */
329
	private function getImageOverlay() {
330
		if ( array_key_exists( 'overlay', $this->params ) && $this->params['overlay'] == true ) {
331
			SMWOutputs::requireResource( 'ext.srf.gallery.overlay' );
332
			return ' srf-overlay';
333
		} else {
334
			return '';
335
		}
336
	}
337
338
	/**
339
	 * Init carousel widget
340
	 *
341
	 * @since 1.8
342
	 *
343
	 * @return string
344
	 */
345
	private function getCarouselWidget() {
346
347
		// Set attributes for jcarousel
348
		$dataAttribs = array(
349
			'wrap' => 'both', // Whether to wrap at the first/last item (or both) and jump back to the start/end.
350
			'vertical' => 'false', // Orientation: vertical = false means horizontal
351
			'rtl' => 'false', // Directionality: rtl = false means ltr
352
		);
353
354
		// Use the perrow parameter to determine the scroll sequence.
355
		if ( empty( $this->params['perrow'] ) ) {
356
			$dataAttribs['scroll'] = 1;  // default 1
357
		} else {
358
			$dataAttribs['scroll'] = $this->params['perrow'];
359
			$dataAttribs['visible'] = $this->params['perrow'];
360
		}
361
362
		$attribs = array(
363
			'id' => uniqid(),
364
			'class' => 'jcarousel jcarousel-skin-smw' . $this->getImageOverlay(),
365
			'style' => 'display:none;',
366
		);
367
368
		foreach ( $dataAttribs as $name => $value ) {
369
			$attribs['data-' . $name] = $value;
370
		}
371
372
		SMWOutputs::requireResource( 'ext.srf.gallery.carousel' );
373
374
		return $attribs;
375
	}
376
377
378
	/**
379
	 * Init slideshow widget
380
	 *
381
	 * @since 1.8
382
	 *
383
	 * @return string
384
	 */
385
	private function getSlideshowWidget() {
386
387
		$attribs = array(
388
			'id'    => uniqid(),
389
			'class' => $this->getImageOverlay(),
390
			'style' => 'display:none;',
391
			'data-nav-control' => $this->params['navigation']
392
		);
393
394
		SMWOutputs::requireResource( 'ext.srf.gallery.slideshow' );
395
396
		return $attribs;
397
	}
398
399
	/**
400
	 * @see SMWResultPrinter::getParamDefinitions
401
	 *
402
	 * @since 1.8
403
	 *
404
	 * @param $definitions array of IParamDefinition
405
	 *
406
	 * @return array of IParamDefinition|array
407
	 */
408
	public function getParamDefinitions( array $definitions ) {
409
		$params = parent::getParamDefinitions( $definitions );
410
411
		$params['class'] = array(
412
			'type' => 'string',
413
			'message' => 'srf-paramdesc-class',
414
			'default' => ''
415
		);
416
417
		$params['widget'] = array(
418
			'type' => 'string',
419
			'default' => '',
420
			'message' => 'srf-paramdesc-widget',
421
			'values' => array( 'carousel', 'slideshow', '' )
422
		);
423
424
		$params['navigation'] = array(
425
			'type' => 'string',
426
			'default' => 'nav',
427
			'message' => 'srf-paramdesc-navigation',
428
			'values' => array( 'nav', 'pager', 'auto' )
429
		);
430
431
		$params['overlay'] = array(
432
			'type' => 'boolean',
433
			'default' => false,
434
			'message' => 'srf-paramdesc-overlay'
435
		);
436
437
		$params['perrow'] = array(
438
			'type' => 'integer',
439
			'default' => '',
440
			'message' => 'srf_paramdesc_perrow'
441
		);
442
443
		$params['widths'] = array(
444
			'type' => 'integer',
445
			'default' => '',
446
			'message' => 'srf_paramdesc_widths'
447
		);
448
449
		$params['heights'] = array(
450
			'type' => 'integer',
451
			'default' => '',
452
			'message' => 'srf_paramdesc_heights'
453
		);
454
455
		$params['autocaptions'] = array(
456
			'type' => 'boolean',
457
			'default' => true,
458
			'message' => 'srf_paramdesc_autocaptions'
459
		);
460
461
		$params['fileextensions'] = array(
462
			'type' => 'boolean',
463
			'default' => false,
464
			'message' => 'srf_paramdesc_fileextensions'
465
		);
466
467
		$params['captionproperty'] = array(
468
			'type' => 'string',
469
			'default' => '',
470
			'message' => 'srf_paramdesc_captionproperty'
471
		);
472
473
		$params['imageproperty'] = array(
474
			'type' => 'string',
475
			'default' => '',
476
			'message' => 'srf_paramdesc_imageproperty'
477
		);
478
479
		$params['redirects'] = array(
480
			'type' => 'string',
481
			'default' => '',
482
			'message' => 'srf-paramdesc-redirects'
483
		);
484
485
		return $params;
486
	}
487
488
	private function isSpecialPage() {
489
		$title = $this->getContext()->getTitle();
490
		return $title instanceof Title && $title->isSpecialPage();
491
	}
492
493
	private function getFileNsTextForPageLanguage() {
494
		$title = $this->getContext()->getTitle();
495
		return $title instanceof Title ? $title->getPageLanguage()->getNsText( NS_FILE ) : null;
496
	}
497
498
}
499