Fix #5005: XHTML <gallery> output.
[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
20 /**
21 * Create a new image gallery object.
22 */
23 function ImageGallery( ) {
24 $this->mImages = array();
25 $this->mShowBytes = true;
26 $this->mShowFilename = true;
27 }
28
29 /**
30 * Add an image to the gallery.
31 *
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.
34 */
35 function add( $image, $html='' ) {
36 $this->mImages[] = array( &$image, $html );
37 }
38
39 /**
40 * Add an image at the beginning of the gallery.
41 *
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.
44 */
45 function insert( $image, $html='' ) {
46 array_unshift( $this->mImages, array( &$image, $html ) );
47 }
48
49
50 /**
51 * isEmpty() returns true if the gallery contains no images
52 */
53 function isEmpty() {
54 return empty( $this->mImages );
55 }
56
57 /**
58 * Enable/Disable showing of the file size of an image in the gallery.
59 * Enabled by default.
60 *
61 * @param $f Boolean: set to false to disable.
62 */
63 function setShowBytes( $f ) {
64 $this->mShowBytes = ( $f == true);
65 }
66
67 /**
68 * Enable/Disable showing of the filename of an image in the gallery.
69 * Enabled by default.
70 *
71 * @param $f Boolean: set to false to disable.
72 */
73 function setShowFilename( $f ) {
74 $this->mShowFilename = ( $f == true);
75 }
76
77 /**
78 * Return a HTML representation of the image gallery
79 *
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
85 *
86 */
87 function toHTML() {
88 global $wgLang, $wgUser;
89
90 $sk = $wgUser->getSkin();
91
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];
97
98 $name = $img->getName();
99 $nt = $img->getTitle();
100
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++;
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 = wfMsgHtml( 'nbytes', $wgLang->formatNum( $img->getSize() ) );
119 } else {
120 $nb = wfMsgHtml( 'filemissing' );
121 }
122 $nb = "$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 -Æ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++;
145 }
146 if( $i %4 != 0 ) {
147 $s .= "</tr>\n";
148 }
149 $s .= '</table>';
150
151 return $s;
152 }
153
154 } //class
155 ?>