Fix #218: Redirects do not support named anchors
[mediawiki.git] / includes / ImageGallery.php
blob561cfb15c36adee059cd2e5af37ea88396e0f511
1 <?php
2 if ( ! defined( 'MEDIAWIKI' ) )
3 die( -1 );
5 /**
6 * @package MediaWiki
7 */
9 /**
10 * Image gallery
12 * Add images to the gallery using add(), then render that list to HTML using toHTML().
14 * @package MediaWiki
16 class ImageGallery
18 var $mImages, $mShowBytes, $mShowFilename;
20 /**
21 * Create a new image gallery object.
23 function ImageGallery( ) {
24 $this->mImages = array();
25 $this->mShowBytes = true;
26 $this->mShowFilename = true;
29 /**
30 * Add an image to the gallery.
32 * @param $image Image object that is added to the gallery
33 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
35 function add( $image, $html='' ) {
36 $this->mImages[] = array( &$image, $html );
39 /**
40 * Add an image at the beginning of the gallery.
42 * @param $image Image object that is added to the gallery
43 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
45 function insert( $image, $html='' ) {
46 array_unshift( $this->mImages, array( &$image, $html ) );
50 /**
51 * isEmpty() returns true if the gallery contains no images
53 function isEmpty() {
54 return empty( $this->mImages );
57 /**
58 * Enable/Disable showing of the file size of an image in the gallery.
59 * Enabled by default.
61 * @param $f Boolean: set to false to disable.
63 function setShowBytes( $f ) {
64 $this->mShowBytes = ( $f == true);
67 /**
68 * Enable/Disable showing of the filename of an image in the gallery.
69 * Enabled by default.
71 * @param $f Boolean: set to false to disable.
73 function setShowFilename( $f ) {
74 $this->mShowFilename = ( $f == true);
77 /**
78 * Return a HTML representation of the image gallery
80 * For each image in the gallery, display
81 * - a thumbnail
82 * - the image name
83 * - the additional text provided when adding the image
84 * - the size of the image
87 function toHTML() {
88 global $wgLang, $wgUser;
90 $sk = $wgUser->getSkin();
92 $s = '<table class="gallery" cellspacing="0" cellpadding="0">';
93 $i = 0;
94 foreach ( $this->mImages as $pair ) {
95 $img =& $pair[0];
96 $text = $pair[1];
98 $name = $img->getName();
99 $nt = $img->getTitle();
101 // Not an image. Just print the name and skip.
102 if ( $nt->getNamespace() != NS_IMAGE ) {
103 $s .=
104 (($i%4==0) ? "<tr>\n" : '') .
105 '<td><div class="gallerybox" style="height: 152px;">' .
106 htmlspecialchars( $nt->getText() ) . '</div></td>' .
107 (($i%4==3) ? "</tr>\n" : '');
108 $i++;
110 continue;
113 //TODO
114 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
116 if( $this->mShowBytes ) {
117 if( $img->exists() ) {
118 $nb = wfMsgHtml( 'nbytes', $wgLang->formatNum( $img->getSize() ) );
119 } else {
120 $nb = wfMsgHtml( 'filemissing' );
122 $nb = "$nb<br />\n";
123 } else {
124 $nb = '';
127 $textlink = $this->mShowFilename ?
128 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
129 '' ;
131 $s .= ($i%4==0) ? '<tr>' : '';
132 $thumb = $img->getThumbnail( 120, 120 );
133 $vpad = floor( ( 150 - $thumb->height ) /2 ) - 2;
134 $s .= '<td><div class="gallerybox">' . '<div class="thumb" style="padding: ' . $vpad . 'px 0;">';
136 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
137 # in version 4.8.6 generated crackpot html in its absence, see:
138 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
139 $s .= $sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</div><div class="gallerytext">' . "\n" .
140 $textlink . $text . $nb .
141 '</div>';
142 $s .= "</div></td>\n";
143 $s .= ($i%4==3) ? '</tr>' : '';
144 $i++;
146 if( $i %4 != 0 ) {
147 $s .= "</tr>\n";
149 $s .= '</table>';
151 return $s;
154 } //class