* Removed messages in English, they'll be inherited from the parent.
[lhc/web/wiklou.git] / includes / ImageGallery.php
1 <?php
2 /**
3 * @package MediaWiki
4 */
5
6 /**
7 * This is not a valid entry point, perform no further processing unless MEDIAWIKI is defined
8 */
9 if( defined( 'MEDIAWIKI' ) ) {
10
11
12 /**
13 * Image gallery
14 *
15 * Add images to the gallery using add(), then render that list to HTML using toHTML().
16 *
17 * @package MediaWiki
18 */
19 class ImageGallery
20 {
21 var $mImages, $mShowBytes, $mShowFilename;
22
23 /**
24 * Create a new image gallery object.
25 */
26 function ImageGallery( ) {
27 $this->mImages = array();
28 $this->mShowBytes = true;
29 $this->mShowFilename = true;
30 }
31
32 /**
33 * Add an image to the gallery.
34 *
35 * @param Image $image Image object that is added to the gallery
36 * @param string $html Additional HTML text to be shown. The name and size of the image are always shown.
37 */
38 function add( $image, $html='' ) {
39 $this->mImages[] = array( &$image, $html );
40 }
41
42 /**
43 * Add an image at the beginning of the gallery.
44 *
45 * @param Image $image Image object that is added to the gallery
46 * @param string $html Additional HTML text to be shown. The name and size of the image are always shown.
47 */
48 function insert( $image, $html='' ) {
49 array_unshift( $this->mImages, array( &$image, $html ) );
50 }
51
52
53 /**
54 * isEmpty() returns true if the gallery contains no images
55 */
56 function isEmpty() {
57 return empty( $this->mImages );
58 }
59
60 /**
61 * Enable/Disable showing of the file size of an image in the gallery.
62 * Enabled by default.
63 *
64 * @param boolean $f set to false to disable
65 */
66 function setShowBytes( $f ) {
67 $this->mShowBytes = ( $f == true);
68 }
69
70 /**
71 * Enable/Disable showing of the filename of an image in the gallery.
72 * Enabled by default.
73 *
74 * @param boolean $f set to false to disable
75 */
76 function setShowFilename( $f ) {
77 $this->mShowFilename = ( $f == true);
78 }
79
80 /**
81 * Return a HTML representation of the image gallery
82 *
83 * For each image in the gallery, display
84 * - a thumbnail
85 * - the image name
86 * - the additional text provided when adding the image
87 * - the size of the image
88 *
89 */
90 function toHTML() {
91 global $wgLang, $wgContLang, $wgUser;
92
93 $sk = $wgUser->getSkin();
94
95 $s = '<table class="gallery" cellspacing="0" cellpadding="0">';
96 $i = 0;
97 foreach ( $this->mImages as $pair ) {
98 $img =& $pair[0];
99 $text = $pair[1];
100
101 $name = $img->getName();
102 $nt = $img->getTitle();
103
104 // Not an image. Just print the name and skip.
105 if ( $nt->getNamespace() != NS_IMAGE ) {
106 $s .= '<td><div class="gallerybox" style="height: 152px;">' .
107 htmlspecialchars( $nt->getText() ) . '</div></td>' . (($i%4==3) ? "</tr>\n" : '');
108 $i++;
109
110 continue;
111 }
112
113 //TODO
114 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
115
116 if( $this->mShowBytes ) {
117 if( $img->exists() ) {
118 $nb = wfMsg( 'nbytes', $wgLang->formatNum( $img->getSize() ) );
119 } else {
120 $nb = wfMsg( 'filemissing' );
121 }
122 $nb = htmlspecialchars( $nb ) . "<br />\n";
123 } else {
124 $nb = '';
125 }
126
127 $textlink = $this->mShowFilename ?
128 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . "<br />\n" :
129 '' ;
130
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;">';
135
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
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++;
145 }
146 if( $i %4 != 0 ) {
147 $s .= "</tr>\n";
148 }
149 $s .= '</table>';
150
151 return $s;
152 }
153
154 } //class
155
156
157
158
159 } // defined( 'MEDIAWIKI' )
160 ?>