[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/gallery/ -> TraditionalImageGallery.php (source)

   1  <?php
   2  /**
   3   * Image gallery.
   4   *
   5   * This program is free software; you can redistribute it and/or modify
   6   * it under the terms of the GNU General Public License as published by
   7   * the Free Software Foundation; either version 2 of the License, or
   8   * (at your option) any later version.
   9   *
  10   * This program is distributed in the hope that it will be useful,
  11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13   * GNU General Public License for more details.
  14   *
  15   * You should have received a copy of the GNU General Public License along
  16   * with this program; if not, write to the Free Software Foundation, Inc.,
  17   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18   * http://www.gnu.org/copyleft/gpl.html
  19   *
  20   * @file
  21   */
  22  
  23  class TraditionalImageGallery extends ImageGalleryBase {
  24      /**
  25       * Return a HTML representation of the image gallery
  26       *
  27       * For each image in the gallery, display
  28       * - a thumbnail
  29       * - the image name
  30       * - the additional text provided when adding the image
  31       * - the size of the image
  32       *
  33       * @return string
  34       */
  35  	function toHTML() {
  36          if ( $this->mPerRow > 0 ) {
  37              $maxwidth = $this->mPerRow * ( $this->mWidths + $this->getAllPadding() );
  38              $oldStyle = isset( $this->mAttribs['style'] ) ? $this->mAttribs['style'] : '';
  39              # _width is ignored by any sane browser. IE6 doesn't know max-width
  40              # so it uses _width instead
  41              $this->mAttribs['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" .
  42                  $oldStyle;
  43          }
  44  
  45          $attribs = Sanitizer::mergeAttributes(
  46              array( 'class' => 'gallery mw-gallery-' . $this->mMode ), $this->mAttribs );
  47  
  48          $modules = $this->getModules();
  49  
  50          if ( $this->mParser ) {
  51              $this->mParser->getOutput()->addModules( $modules );
  52          } else {
  53              $this->getOutput()->addModules( $modules );
  54          }
  55          $output = Xml::openElement( 'ul', $attribs );
  56          if ( $this->mCaption ) {
  57              $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>";
  58          }
  59  
  60          $lang = $this->getRenderLang();
  61          # Output each image...
  62          foreach ( $this->mImages as $pair ) {
  63              /** @var Title $nt */
  64              $nt = $pair[0];
  65              $text = $pair[1]; # "text" means "caption" here
  66              $alt = $pair[2];
  67              $link = $pair[3];
  68  
  69              $descQuery = false;
  70              if ( $nt->getNamespace() === NS_FILE ) {
  71                  # Get the file...
  72                  if ( $this->mParser instanceof Parser ) {
  73                      # Give extensions a chance to select the file revision for us
  74                      $options = array();
  75                      wfRunHooks( 'BeforeParserFetchFileAndTitle',
  76                          array( $this->mParser, $nt, &$options, &$descQuery ) );
  77                      # Fetch and register the file (file title may be different via hooks)
  78                      list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $options );
  79                  } else {
  80                      $img = wfFindFile( $nt );
  81                  }
  82              } else {
  83                  $img = false;
  84              }
  85  
  86              $params = $this->getThumbParams( $img );
  87              // $pair[4] is per image handler options
  88              $transformOptions = $params + $pair[4];
  89  
  90              $thumb = false;
  91  
  92              if ( !$img ) {
  93                  # We're dealing with a non-image, spit out the name and be done with it.
  94                  $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
  95                      . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
  96                      . htmlspecialchars( $nt->getText() ) . '</div>';
  97  
  98                  if ( $this->mParser instanceof Parser ) {
  99                      $this->mParser->addTrackingCategory( 'broken-file-category' );
 100                  }
 101              } elseif ( $this->mHideBadImages
 102                  && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() )
 103              ) {
 104                  # The image is blacklisted, just show it as a text link.
 105                  $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' .
 106                      ( $this->getThumbPadding() + $this->mHeights ) . 'px;">' .
 107                      Linker::linkKnown(
 108                          $nt,
 109                          htmlspecialchars( $nt->getText() )
 110                      ) .
 111                      '</div>';
 112              } elseif ( !( $thumb = $img->transform( $transformOptions ) ) ) {
 113                  # Error generating thumbnail.
 114                  $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: '
 115                      . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">'
 116                      . htmlspecialchars( $img->getLastError() ) . '</div>';
 117              } else {
 118                  /** @var MediaTransformOutput $thumb */
 119                  $vpad = $this->getVPad( $this->mHeights, $thumb->getHeight() );
 120  
 121                  $imageParameters = array(
 122                      'desc-link' => true,
 123                      'desc-query' => $descQuery,
 124                      'alt' => $alt,
 125                      'custom-url-link' => $link
 126                  );
 127  
 128                  // In the absence of both alt text and caption, fall back on
 129                  // providing screen readers with the filename as alt text
 130                  if ( $alt == '' && $text == '' ) {
 131                      $imageParameters['alt'] = $nt->getText();
 132                  }
 133  
 134                  $this->adjustImageParameters( $thumb, $imageParameters );
 135  
 136                  # Set both fixed width and min-height.
 137                  $thumbhtml = "\n\t\t\t"
 138                      . '<div class="thumb" style="width: '
 139                      . $this->getThumbDivWidth( $thumb->getWidth() ) . 'px;">'
 140                      # Auto-margin centering for block-level elements. Needed
 141                      # now that we have video handlers since they may emit block-
 142                      # level elements as opposed to simple <img> tags. ref
 143                      # http://css-discuss.incutio.com/?page=CenteringBlockElement
 144                      . '<div style="margin:' . $vpad . 'px auto;">'
 145                      . $thumb->toHtml( $imageParameters ) . '</div></div>';
 146  
 147                  // Call parser transform hook
 148                  /** @var MediaHandler $handler */
 149                  $handler = $img->getHandler();
 150                  if ( $this->mParser && $handler ) {
 151                      $handler->parserTransformHook( $this->mParser, $img );
 152                  }
 153              }
 154  
 155              // @todo Code is incomplete.
 156              // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) .
 157              // ":{$ut}" );
 158              // $ul = Linker::link( $linkTarget, $ut );
 159  
 160              if ( $this->mShowBytes ) {
 161                  if ( $img ) {
 162                      $fileSize = htmlspecialchars( $lang->formatSize( $img->getSize() ) );
 163                  } else {
 164                      $fileSize = $this->msg( 'filemissing' )->escaped();
 165                  }
 166                  $fileSize = "$fileSize<br />\n";
 167              } else {
 168                  $fileSize = '';
 169              }
 170  
 171              $textlink = $this->mShowFilename ?
 172                  Linker::linkKnown(
 173                      $nt,
 174                      htmlspecialchars( $lang->truncate( $nt->getText(), $this->mCaptionLength ) )
 175                  ) . "<br />\n" :
 176                  '';
 177  
 178              $galleryText = $textlink . $text . $fileSize;
 179              $galleryText = $this->wrapGalleryText( $galleryText, $thumb );
 180  
 181              # Weird double wrapping (the extra div inside the li) needed due to FF2 bug
 182              # Can be safely removed if FF2 falls completely out of existence
 183              $output .= "\n\t\t" . '<li class="gallerybox" style="width: '
 184                  . $this->getGBWidth( $thumb ) . 'px">'
 185                  . '<div style="width: ' . $this->getGBWidth( $thumb ) . 'px">'
 186                  . $thumbhtml
 187                  . $galleryText
 188                  . "\n\t\t</div></li>";
 189          }
 190          $output .= "\n</ul>";
 191  
 192          return $output;
 193      }
 194  
 195      /**
 196       * Add the wrapper html around the thumb's caption
 197       *
 198       * @param string $galleryText The caption
 199       * @param MediaTransformOutput|bool $thumb The thumb this caption is for
 200       *   or false for bad image.
 201       * @return string
 202       */
 203  	protected function wrapGalleryText( $galleryText, $thumb ) {
 204          # ATTENTION: The newline after <div class="gallerytext"> is needed to
 205          # accommodate htmltidy which in version 4.8.6 generated crackpot html in
 206          # its absence, see: http://bugzilla.wikimedia.org/show_bug.cgi?id=1765
 207          # -Ævar
 208  
 209          return "\n\t\t\t" . '<div class="gallerytext">' . "\n"
 210              . $galleryText
 211              . "\n\t\t\t</div>";
 212      }
 213  
 214      /**
 215       * How much padding such the thumb have between image and inner div that
 216       * that contains the border. This is both for verical and horizontal
 217       * padding. (However, it is cut in half in the vertical direction).
 218       * @return int
 219       */
 220  	protected function getThumbPadding() {
 221          return 30;
 222      }
 223  
 224      /**
 225       * @note GB stands for gallerybox (as in the <li class="gallerybox"> element)
 226       *
 227       * @return int
 228       */
 229  	protected function getGBPadding() {
 230          return 5;
 231      }
 232  
 233      /**
 234       * Get how much extra space the borders around the image takes up.
 235       *
 236       * For this mode, it is 2px borders on each side + 2px implied padding on
 237       * each side from the stylesheet, giving us 2*2+2*2 = 8.
 238       * @return int
 239       */
 240  	protected function getGBBorders() {
 241          return 8;
 242      }
 243  
 244      /**
 245       * Get total padding.
 246       *
 247       * @return int Number of pixels of whitespace surrounding the thumbnail.
 248       */
 249  	protected function getAllPadding() {
 250          return $this->getThumbPadding() + $this->getGBPadding() + $this->getGBBorders();
 251      }
 252  
 253      /**
 254       * Get vertical padding for a thumbnail
 255       *
 256       * Generally this is the total height minus how high the thumb is.
 257       *
 258       * @param int $boxHeight How high we want the box to be.
 259       * @param int $thumbHeight How high the thumbnail is.
 260       * @return int Vertical padding to add on each side.
 261       */
 262  	protected function getVPad( $boxHeight, $thumbHeight ) {
 263          return ( $this->getThumbPadding() + $boxHeight - $thumbHeight ) / 2;
 264      }
 265  
 266      /**
 267       * Get the transform parameters for a thumbnail.
 268       *
 269       * @param File $img The file in question. May be false for invalid image
 270       * @return array
 271       */
 272  	protected function getThumbParams( $img ) {
 273          return array(
 274              'width' => $this->mWidths,
 275              'height' => $this->mHeights
 276          );
 277      }
 278  
 279      /**
 280       * Get the width of the inner div that contains the thumbnail in
 281       * question. This is the div with the class of "thumb".
 282       *
 283       * @param int $thumbWidth The width of the thumbnail.
 284       * @return int Width of inner thumb div.
 285       */
 286  	protected function getThumbDivWidth( $thumbWidth ) {
 287          return $this->mWidths + $this->getThumbPadding();
 288      }
 289  
 290      /**
 291       * Width of gallerybox <li>.
 292       *
 293       * Generally is the width of the image, plus padding on image
 294       * plus padding on gallerybox.
 295       *
 296       * @note Important: parameter will be false if no thumb used.
 297       * @param MediaTransformOutput|bool $thumb MediaTransformObject object or false.
 298       * @return int Width of gallerybox element
 299       */
 300  	protected function getGBWidth( $thumb ) {
 301          return $this->mWidths + $this->getThumbPadding() + $this->getGBPadding();
 302      }
 303  
 304      /**
 305       * Get a list of modules to include in the page.
 306       *
 307       * Primarily intended for subclasses.
 308       *
 309       * @return array Modules to include
 310       */
 311  	protected function getModules() {
 312          return array();
 313      }
 314  
 315      /**
 316       * Adjust the image parameters for a thumbnail.
 317       *
 318       * Used by a subclass to insert extra high resolution images.
 319       * @param MediaTransformOutput $thumb The thumbnail
 320       * @param array $imageParameters Array of options
 321       */
 322  	protected function adjustImageParameters( $thumb, &$imageParameters ) {
 323      }
 324  }
 325  
 326  /**
 327   * Backwards compatibility. This always uses traditional mode
 328   * if called the old way, for extensions that may expect traditional
 329   * mode.
 330   *
 331   * @deprecated since 1.22 Use ImageGalleryBase::factory instead.
 332   */
 333  class ImageGallery extends TraditionalImageGallery {
 334  	function __construct( $mode = 'traditional' ) {
 335          wfDeprecated( __METHOD__, '1.22' );
 336          parent::__construct( $mode );
 337      }
 338  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1