7e6307eff469c1d915a0f1157ac9d10e88d8ecde
[lhc/web/wiklou.git] / includes / ImageGallery.php
1 <?php
2 if ( ! defined( 'MEDIAWIKI' ) )
3 die( 1 );
4
5 /**
6 * @package MediaWiki
7 */
8
9 /**
10 * Image gallery
11 *
12 * Add images to the gallery using add(), then render that list to HTML using toHTML().
13 *
14 * @package MediaWiki
15 */
16 class ImageGallery
17 {
18 var $mImages, $mShowBytes, $mShowFilename;
19 var $mCaption = false;
20
21 /**
22 * Is the gallery on a wiki page (i.e. not a special page)
23 */
24 var $mParsing;
25
26 /**
27 * Create a new image gallery object.
28 */
29 function ImageGallery( ) {
30 $this->mImages = array();
31 $this->mShowBytes = true;
32 $this->mShowFilename = true;
33 $this->mParsing = false;
34 }
35
36 /**
37 * Set the "parse" bit so we know to hide "bad" images
38 */
39 function setParsing( $val = true ) {
40 $this->mParsing = $val;
41 }
42
43 /**
44 * Set the caption
45 *
46 * @param $caption Caption
47 */
48 function setCaption( $caption ) {
49 $this->mCaption = $caption;
50 }
51
52 /**
53 * Add an image to the gallery.
54 *
55 * @param $image Image object that is added to the gallery
56 * @param $html String: additional HTML text to be shown. The name and size of the image are always shown.
57 */
58 function add( $image, $html='' ) {
59 $this->mImages[] = array( &$image, $html );
60 wfDebug( "ImageGallery::add " . $image->getName() . "\n" );
61 }
62
63 /**
64 * Add an image at the beginning of the gallery.
65 *
66 * @param $image Image object that is added to the gallery
67 * @param $html String: Additional HTML text to be shown. The name and size of the image are always shown.
68 */
69 function insert( $image, $html='' ) {
70 array_unshift( $this->mImages, array( &$image, $html ) );
71 }
72
73
74 /**
75 * isEmpty() returns true if the gallery contains no images
76 */
77 function isEmpty() {
78 return empty( $this->mImages );
79 }
80
81 /**
82 * Enable/Disable showing of the file size of an image in the gallery.
83 * Enabled by default.
84 *
85 * @param $f Boolean: set to false to disable.
86 */
87 function setShowBytes( $f ) {
88 $this->mShowBytes = ( $f == true);
89 }
90
91 /**
92 * Enable/Disable showing of the filename of an image in the gallery.
93 * Enabled by default.
94 *
95 * @param $f Boolean: set to false to disable.
96 */
97 function setShowFilename( $f ) {
98 $this->mShowFilename = ( $f == true);
99 }
100
101 /**
102 * Return a HTML representation of the image gallery
103 *
104 * For each image in the gallery, display
105 * - a thumbnail
106 * - the image name
107 * - the additional text provided when adding the image
108 * - the size of the image
109 *
110 */
111 function toHTML() {
112 global $wgLang, $wgIgnoreImageErrors, $wgGenerateThumbnailOnParse;
113
114 $s = '<table class="gallery" cellspacing="0" cellpadding="0">';
115 if( $this->mCaption )
116 $s .= '<td class="galleryheader" colspan="4"><big>' . htmlspecialchars( $this->mCaption ) . '</big></td>';
117
118 $i = 0;
119 foreach ( $this->mImages as $pair ) {
120 $img =& $pair[0];
121 $text = $pair[1];
122
123 $name = $img->getName();
124 $nt = $img->getTitle();
125
126 if( $nt->getNamespace() != NS_IMAGE ) {
127 # We're dealing with a non-image, spit out the name and be done with it.
128 $thumbhtml = '<div style="height: 152px;">' . htmlspecialchars( $nt->getText() ) . '</div>';
129 }
130 else if( $this->mParsing && wfIsBadImage( $nt->getDBkey() ) ) {
131 # The image is blacklisted, just show it as a text link.
132 $thumbhtml = '<div style="height: 152px;">'
133 . Linker::makeKnownLinkObj( $nt, htmlspecialchars( $nt->getText() ) ) . '</div>';
134 } else if( !( $thumb = $img->getThumbnail( 120, 120, $wgGenerateThumbnailOnParse ) ) ) {
135 # Error generating thumbnail.
136 $thumbhtml = '<div style="height: 152px;">'
137 . htmlspecialchars( $img->getLastError() ) . '</div>';
138 }
139 else {
140 $vpad = floor( ( 150 - $thumb->height ) /2 ) - 2;
141 $thumbhtml = '<div class="thumb" style="padding: ' . $vpad . 'px 0;">'
142 . Linker::makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</div>';
143 }
144
145 //TODO
146 //$ul = Linker::makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
147
148 if( $this->mShowBytes ) {
149 if( $img->exists() ) {
150 $nb = wfMsgExt( 'nbytes', array( 'parsemag', 'escape'),
151 $wgLang->formatNum( $img->getSize() ) );
152 } else {
153 $nb = wfMsgHtml( 'filemissing' );
154 }
155 $nb = "$nb<br />\n";
156 } else {
157 $nb = '';
158 }
159
160 $textlink = $this->mShowFilename ?
161 Linker::makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
162 '' ;
163
164 # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltidy which
165 # in version 4.8.6 generated crackpot html in its absence, see:
166 # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
167
168 $s .= ($i%4==0) ? '<tr>' : '';
169 $s .= '<td><div class="gallerybox">' . $thumbhtml
170 . '<div class="gallerytext">' . "\n" . $textlink . $text . $nb
171 . "</div></div></td>\n";
172 $s .= ($i%4==3) ? '</tr>' : '';
173 $i++;
174 }
175 if( $i %4 != 0 ) {
176 $s .= "</tr>\n";
177 }
178 $s .= '</table>';
179
180 return $s;
181 }
182
183 } //class
184 ?>