Complex classes like Gallery often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Gallery, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Gallery extends ResultPrinter { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @see SMWResultPrinter::getName |
||
| 26 | * |
||
| 27 | * @return string |
||
| 28 | */ |
||
| 29 | 1 | public function getName() { |
|
| 30 | 1 | return $this->msg( 'srf_printername_gallery' )->text(); |
|
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @see SMWResultPrinter::buildResult |
||
| 35 | * |
||
| 36 | * @since 1.8 |
||
| 37 | * |
||
| 38 | * @param SMWQueryResult $results |
||
| 39 | * |
||
| 40 | * @return string |
||
| 41 | */ |
||
| 42 | 4 | protected function buildResult( SMWQueryResult $results ) { |
|
| 43 | |||
| 44 | // Intro/outro are not planned to work with the widget option |
||
| 45 | 4 | if ( ( $this->params['intro'] !== '' || $this->params['outro'] !== '' ) && $this->params['widget'] !== '' ) { |
|
| 46 | 1 | $results->addErrors( |
|
| 47 | [ |
||
| 48 | 1 | $this->msg( 'srf-error-option-mix', 'widget' )->inContentLanguage()->text() |
|
| 49 | ] |
||
| 50 | ); |
||
| 51 | |||
| 52 | 1 | return ''; |
|
| 53 | }; |
||
| 54 | |||
| 55 | 3 | return $this->getResultText( $results, $this->outputMode ); |
|
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @see SMWResultPrinter::getResultText |
||
| 60 | * |
||
| 61 | * @param $results SMWQueryResult |
||
| 62 | * @param $outputmode integer |
||
| 63 | * |
||
| 64 | * @return string | array |
||
| 65 | */ |
||
| 66 | 3 | public function getResultText( SMWQueryResult $results, $outputmode ) { |
|
| 67 | |||
| 68 | 3 | $ig = new TraditionalImageGallery(); |
|
| 69 | |||
| 70 | 3 | $ig->setShowBytes( false ); |
|
| 71 | 3 | $ig->setShowFilename( false ); |
|
| 72 | |||
| 73 | 3 | if ( method_exists( $ig, 'setShowDimensions' ) ) { |
|
| 74 | 3 | $ig->setShowDimensions( false ); |
|
| 75 | } |
||
| 76 | |||
| 77 | 3 | $ig->setCaption( $this->mIntro ); // set caption to IQ header |
|
| 78 | |||
| 79 | // No need for a special page to use the parser but for the "normal" page |
||
| 80 | // view we have to ensure caption text is parsed correctly through the parser |
||
| 81 | 3 | if ( !$this->isSpecialPage() ) { |
|
| 82 | 3 | $ig->setParser( $GLOBALS['wgParser'] ); |
|
| 83 | } |
||
| 84 | |||
| 85 | 3 | $html = ''; |
|
| 86 | 3 | $processing = ''; |
|
| 87 | |||
| 88 | 3 | if ( $this->params['widget'] == 'carousel' ) { |
|
| 89 | // Carousel widget |
||
| 90 | 1 | $ig->setAttributes( $this->getCarouselWidget() ); |
|
| 91 | 3 | } elseif ( $this->params['widget'] == 'slideshow' ) { |
|
| 92 | // Slideshow widget |
||
| 93 | 1 | $ig->setAttributes( $this->getSlideshowWidget() ); |
|
| 94 | } else { |
||
| 95 | |||
| 96 | // Standard gallery attributes |
||
| 97 | $attribs = [ |
||
| 98 | 3 | 'id' => uniqid(), |
|
| 99 | 3 | 'class' => $this->getImageOverlay(), |
|
| 100 | ]; |
||
| 101 | |||
| 102 | 3 | $ig->setAttributes( $attribs ); |
|
| 103 | } |
||
| 104 | |||
| 105 | // Only use redirects where the overlay option is not used and redirect |
||
| 106 | // thumb images towards a different target |
||
| 107 | 3 | if ( $this->params['redirects'] !== '' && !$this->params['overlay'] ) { |
|
| 108 | 1 | SMWOutputs::requireResource( 'ext.srf.gallery.redirect' ); |
|
| 109 | } |
||
| 110 | |||
| 111 | // For the carousel widget, the perrow option should not be set |
||
| 112 | 3 | if ( $this->params['perrow'] !== '' && $this->params['widget'] !== 'carousel' ) { |
|
| 113 | $ig->setPerRow( $this->params['perrow'] ); |
||
| 114 | } |
||
| 115 | |||
| 116 | 3 | if ( $this->params['widths'] !== '' ) { |
|
| 117 | 1 | $ig->setWidths( $this->params['widths'] ); |
|
| 118 | } |
||
| 119 | |||
| 120 | 3 | if ( $this->params['heights'] !== '' ) { |
|
| 121 | 1 | $ig->setHeights( $this->params['heights'] ); |
|
| 122 | } |
||
| 123 | |||
| 124 | 3 | $printReqLabels = []; |
|
| 125 | 3 | $redirectType = ''; |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @var SMWPrintRequest $printReq |
||
| 129 | */ |
||
| 130 | 3 | foreach ( $results->getPrintRequests() as $printReq ) { |
|
| 131 | 3 | $printReqLabels[] = $printReq->getLabel(); |
|
| 132 | |||
| 133 | // Get redirect type |
||
| 134 | 3 | if ( $this->params['redirects'] === $printReq->getLabel() ) { |
|
| 135 | 3 | $redirectType = $printReq->getTypeID(); |
|
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | 3 | if ( $this->params['imageproperty'] !== '' && in_array( $this->params['imageproperty'], $printReqLabels ) || |
|
| 140 | 3 | $this->params['redirects'] !== '' && in_array( $this->params['redirects'], $printReqLabels ) ) { |
|
| 141 | |||
| 142 | 1 | $this->addImageProperties( |
|
| 143 | 1 | $results, |
|
| 144 | $ig, |
||
| 145 | 1 | $this->params['imageproperty'], |
|
| 146 | 1 | $this->params['captionproperty'], |
|
| 147 | 1 | $this->params['redirects'], |
|
| 148 | $outputmode |
||
| 149 | ); |
||
| 150 | } else { |
||
| 151 | 3 | $this->addImagePages( $results, $ig ); |
|
| 152 | } |
||
| 153 | |||
| 154 | // SRF Global settings |
||
| 155 | 3 | SRFUtils::addGlobalJSVariables(); |
|
| 156 | |||
| 157 | // Display a processing image as long as the DOM is no ready |
||
| 158 | 3 | if ( $this->params['widget'] !== '' ) { |
|
| 159 | 1 | $processing = SRFUtils::htmlProcessingElement(); |
|
| 160 | } |
||
| 161 | |||
| 162 | // Beautify the class selector |
||
| 163 | 3 | $class = $this->params['widget'] ? '-' . $this->params['widget'] . ' ' : ''; |
|
| 164 | 3 | $class = $this->params['redirects'] !== '' && $this->params['overlay'] === false ? $class . ' srf-redirect' . ' ' : $class; |
|
| 165 | 3 | $class = $this->params['class'] ? $class . ' ' . $this->params['class'] : $class; |
|
| 166 | |||
| 167 | // Separate content from result output |
||
| 168 | 3 | if ( !$ig->isEmpty() ) { |
|
| 169 | $attribs = [ |
||
| 170 | 3 | 'class' => 'srf-gallery' . $class, |
|
| 171 | 3 | 'data-redirect-type' => $redirectType, |
|
| 172 | 3 | 'data-ns-text' => $this->getFileNsTextForPageLanguage() |
|
| 173 | ]; |
||
| 174 | |||
| 175 | 3 | $html = Html::rawElement( 'div', $attribs, $processing . $ig->toHTML() ); |
|
| 176 | } |
||
| 177 | |||
| 178 | // If available, create a link that points to further results |
||
| 179 | 3 | if ( $this->linkFurtherResults( $results ) ) { |
|
| 180 | $html .= $this->getLink( $results, SMW_OUTPUT_HTML )->getText( SMW_OUTPUT_HTML, $this->mLinker ); |
||
| 181 | } |
||
| 182 | |||
| 183 | // If available and no results, return default message |
||
| 184 | |||
| 185 | 3 | if ( $results == '' && $this->params[ 'default' ] !== '' ) { |
|
| 186 | $html = $this->params[ 'default' ]; |
||
| 187 | } |
||
| 188 | |||
| 189 | |||
| 190 | 3 | return [ $html, 'nowiki' => true, 'isHTML' => true ]; |
|
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Handles queries where the images (and optionally their captions) are specified as properties. |
||
| 195 | * |
||
| 196 | * @since 1.5.3 |
||
| 197 | * |
||
| 198 | * @param SMWQueryResult $results |
||
| 199 | * @param TraditionalImageGallery $ig |
||
| 200 | * @param string $imageProperty |
||
| 201 | * @param string $captionProperty |
||
| 202 | * @param string $redirectProperty |
||
| 203 | * @param $outputMode |
||
| 204 | */ |
||
| 205 | 1 | protected function addImageProperties( SMWQueryResult $results, &$ig, $imageProperty, $captionProperty, $redirectProperty, $outputMode ) { |
|
| 206 | while ( /* array of SMWResultArray */ |
||
| 207 | 1 | $rows = $results->getNext() ) { // Objects (pages) |
|
| 208 | 1 | $images = []; |
|
| 209 | 1 | $captions = []; |
|
| 210 | 1 | $redirects = []; |
|
| 211 | |||
| 212 | 1 | for ( $i = 0, $n = count( $rows ); $i < $n; $i++ ) { // Properties |
|
| 213 | /** |
||
| 214 | * @var \SMWResultArray $resultArray |
||
| 215 | * @var \SMWDataValue $dataValue |
||
| 216 | */ |
||
| 217 | 1 | $resultArray = $rows[$i]; |
|
| 218 | |||
| 219 | 1 | $label = $resultArray->getPrintRequest()->getMode() == SMWPrintRequest::PRINT_THIS |
|
| 220 | 1 | ? '-' : $resultArray->getPrintRequest()->getLabel(); |
|
| 221 | |||
| 222 | // Make sure always use real label here otherwise it results in an empty array |
||
| 223 | 1 | if ( $resultArray->getPrintRequest()->getLabel() == $imageProperty ) { |
|
| 224 | 1 | while ( ( $dataValue = $resultArray->getNextDataValue() ) !== false ) { // Property values |
|
| 225 | 1 | if ( $dataValue->getTypeID() == '_wpg' ) { |
|
| 226 | 1 | $images[] = $dataValue->getDataItem()->getTitle(); |
|
| 227 | } |
||
| 228 | } |
||
| 229 | 1 | } elseif ( $label == $captionProperty ) { |
|
| 230 | while ( ( $dataValue = $resultArray->getNextDataValue() ) !== false ) { // Property values |
||
| 231 | $captions[] = $dataValue->getShortText( $outputMode, $this->getLinker( true ) ); |
||
| 232 | } |
||
| 233 | 1 | } elseif ( $label == $redirectProperty ) { |
|
| 234 | 1 | while ( ( $dataValue = $resultArray->getNextDataValue() ) !== false ) { // Property values |
|
| 235 | 1 | if ( $dataValue->getDataItem()->getDIType() == SMWDataItem::TYPE_WIKIPAGE ) { |
|
| 236 | 1 | $redirects[] = $dataValue->getTitle(); |
|
| 237 | } elseif ( $dataValue->getDataItem()->getDIType() == SMWDataItem::TYPE_URI ) { |
||
| 238 | $redirects[] = $dataValue->getURL(); |
||
| 239 | } |
||
| 240 | } |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | // Check available matches against captions |
||
| 245 | 1 | $amountMatches = count( $captions ) == count( $images ); |
|
| 246 | 1 | $hasCaption = $amountMatches || count( $captions ) > 0; |
|
| 247 | |||
| 248 | // Check available matches against redirects |
||
| 249 | 1 | $amountRedirects = count( $redirects ) == count( $images ); |
|
| 250 | 1 | $hasRedirect = $amountRedirects || count( $redirects ) > 0; |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @var Title $imgTitle |
||
| 254 | */ |
||
| 255 | 1 | foreach ( $images as $imgTitle ) { |
|
| 256 | 1 | if ( $imgTitle->exists() ) { |
|
| 257 | 1 | $imgCaption = $hasCaption ? ( $amountMatches ? array_shift( $captions ) : $captions[0] ) : ''; |
|
| 258 | 1 | $imgRedirect = $hasRedirect ? ( $amountRedirects ? array_shift( $redirects ) : $redirects[0] ) : ''; |
|
| 259 | 1 | $this->addImageToGallery( $ig, $imgTitle, $imgCaption, $imgRedirect ); |
|
| 260 | } |
||
| 261 | } |
||
| 262 | } |
||
| 263 | 1 | } |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Handles queries where the result objects are image pages. |
||
| 267 | * |
||
| 268 | * @since 1.5.3 |
||
| 269 | * |
||
| 270 | * @param SMWQueryResult $results |
||
| 271 | * @param TraditionalImageGallery $ig |
||
| 272 | */ |
||
| 273 | 3 | protected function addImagePages( SMWQueryResult $results, &$ig ) { |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Adds a single image to the gallery. |
||
| 307 | * Takes care of automatically adding a caption when none is provided and parsing it's wikitext. |
||
| 308 | * |
||
| 309 | * @since 1.5.3 |
||
| 310 | * |
||
| 311 | * @param TraditionalImageGallery $ig The gallery to add the image to |
||
| 312 | * @param Title $imgTitle The title object of the page of the image |
||
| 313 | * @param string $imgCaption An optional caption for the image |
||
| 314 | * @param string $imgRedirect |
||
| 315 | */ |
||
| 316 | 3 | protected function addImageToGallery( &$ig, Title $imgTitle, $imgCaption, $imgRedirect = '' ) { |
|
| 317 | |||
| 318 | 3 | if ( empty( $imgCaption ) ) { |
|
| 319 | 1 | if ( $this->params['autocaptions'] ) { |
|
| 320 | 1 | $imgCaption = $imgTitle->getBaseText(); |
|
| 321 | |||
| 322 | 1 | if ( !$this->params['fileextensions'] ) { |
|
| 323 | 1 | $imgCaption = preg_replace( '#\.[^.]+$#', '', $imgCaption ); |
|
| 324 | } |
||
| 325 | } else { |
||
| 326 | 1 | $imgCaption = ''; |
|
| 327 | } |
||
| 328 | } else { |
||
| 329 | 3 | if ( $imgTitle instanceof Title && $imgTitle->getNamespace() == NS_FILE && !$this->isSpecialPage() ) { |
|
| 330 | 3 | $imgCaption = $ig->mParser->recursiveTagParse( $imgCaption ); |
|
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | 3 | if ( $this->params['captiontemplate'] !== '' ) { |
|
| 335 | 1 | $templateCode = "{{" . $this->params['captiontemplate'] . |
|
| 336 | 1 | "|imageraw=".$imgTitle->getPrefixedText()."|imagecaption=$imgCaption|imageredirect=$imgRedirect}}"; |
|
| 337 | |||
| 338 | 1 | $imgCaption = $ig->mParser->recursiveTagParse( $templateCode ); |
|
| 339 | |||
| 340 | } |
||
| 341 | |||
| 342 | // Use image alt as helper for either text |
||
| 343 | 3 | $imgAlt = $this->params['redirects'] === '' ? $imgCaption : ( $imgRedirect !== '' ? $imgRedirect : '' ); |
|
| 344 | 3 | $ig->add( $imgTitle, $imgCaption, $imgAlt ); |
|
| 345 | 3 | } |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Returns the overlay setting |
||
| 349 | * |
||
| 350 | * @since 1.8 |
||
| 351 | * |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | 3 | private function getImageOverlay() { |
|
| 355 | 3 | if ( array_key_exists( 'overlay', $this->params ) && $this->params['overlay'] == true ) { |
|
| 356 | 1 | SMWOutputs::requireResource( 'ext.srf.gallery.overlay' ); |
|
| 357 | 1 | return ' srf-overlay'; |
|
| 358 | } else { |
||
| 359 | 3 | return ''; |
|
| 360 | } |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Init carousel widget |
||
| 365 | * |
||
| 366 | * @since 1.8 |
||
| 367 | * |
||
| 368 | * @return string[] |
||
| 369 | */ |
||
| 370 | 1 | private function getCarouselWidget() { |
|
| 371 | |||
| 372 | // Set attributes for jcarousel |
||
| 373 | $dataAttribs = [ |
||
| 374 | 1 | 'wrap' => 'both', // Whether to wrap at the first/last item (or both) and jump back to the start/end. |
|
| 375 | 'vertical' => 'false', // Orientation: vertical = false means horizontal |
||
| 376 | 'rtl' => 'false', // Directionality: rtl = false means ltr |
||
| 377 | ]; |
||
| 378 | |||
| 379 | // Use the perrow parameter to determine the scroll sequence. |
||
| 380 | 1 | if ( empty( $this->params['perrow'] ) ) { |
|
| 381 | 1 | $dataAttribs['scroll'] = 1; // default 1 |
|
| 382 | } else { |
||
| 383 | $dataAttribs['scroll'] = $this->params['perrow']; |
||
| 384 | $dataAttribs['visible'] = $this->params['perrow']; |
||
| 385 | } |
||
| 386 | |||
| 387 | $attribs = [ |
||
| 388 | 1 | 'id' => uniqid(), |
|
| 389 | 1 | 'class' => 'jcarousel jcarousel-skin-smw' . $this->getImageOverlay(), |
|
| 390 | 1 | 'style' => 'display:none;', |
|
| 391 | ]; |
||
| 392 | |||
| 393 | 1 | foreach ( $dataAttribs as $name => $value ) { |
|
| 394 | 1 | $attribs['data-' . $name] = $value; |
|
| 395 | } |
||
| 396 | |||
| 397 | 1 | SMWOutputs::requireResource( 'ext.srf.gallery.carousel' ); |
|
| 398 | |||
| 399 | 1 | return $attribs; |
|
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Init slideshow widget |
||
| 404 | * |
||
| 405 | * @since 1.8 |
||
| 406 | * |
||
| 407 | * @return string[] |
||
| 408 | */ |
||
| 409 | 1 | private function getSlideshowWidget() { |
|
| 410 | |||
| 411 | $attribs = [ |
||
| 412 | 1 | 'id' => uniqid(), |
|
| 413 | 1 | 'class' => $this->getImageOverlay(), |
|
| 414 | 1 | 'style' => 'display:none;', |
|
| 415 | 1 | 'data-nav-control' => $this->params['navigation'] |
|
| 416 | ]; |
||
| 417 | |||
| 418 | 1 | SMWOutputs::requireResource( 'ext.srf.gallery.slideshow' ); |
|
| 419 | |||
| 420 | 1 | return $attribs; |
|
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @see SMWResultPrinter::getParamDefinitions |
||
| 425 | * |
||
| 426 | * @since 1.8 |
||
| 427 | * |
||
| 428 | * @param $definitions array of IParamDefinition |
||
| 429 | * |
||
| 430 | * @return array of IParamDefinition|array |
||
| 431 | */ |
||
| 432 | 3 | public function getParamDefinitions( array $definitions ) { |
|
| 517 | |||
| 518 | /** |
||
| 519 | * @return bool |
||
| 520 | */ |
||
| 521 | 3 | private function isSpecialPage() { |
|
| 525 | |||
| 526 | /** |
||
| 527 | * @return bool|null|string |
||
| 528 | */ |
||
| 529 | 3 | private function getFileNsTextForPageLanguage() { |
|
| 533 | |||
| 534 | } |
||
| 535 |