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