(bug 1016) Fix handling of lines omitting Image: in a <gallery> tag
[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 $text Additional text to be shown. The name and size of the image are always shown.
37 */
38 function add( $image, $text='' ) {
39 $this->mImages[] = array( &$image, $text );
40 }
41
42 /**
43 * isEmpty() returns false iff the gallery doesn't contain any images
44 */
45 function isEmpty() {
46 return empty( $this->mImages );
47 }
48
49 /**
50 * Enable/Disable showing of the file size of an image in the gallery.
51 * Enabled by default.
52 *
53 * @param boolean $f set to false to disable
54 */
55 function setShowBytes( $f ) {
56 $this->mShowBytes = ( $f == true);
57 }
58
59 /**
60 * Enable/Disable showing of the filename of an image in the gallery.
61 * Enabled by default.
62 *
63 * @param boolean $f set to false to disable
64 */
65 function setShowFilename( $f ) {
66 $this->mShowFilename = ( $f == true);
67 }
68
69 /**
70 * Return a HTML representation of the image gallery
71 *
72 * For each image in the gallery, display
73 * - a thumbnail
74 * - the image name
75 * - the additional text provided when adding the image
76 * - the size of the image
77 *
78 */
79 function toHTML() {
80 global $wgLang, $wgContLang, $wgUser;
81
82 $sk = $wgUser->getSkin();
83
84 $s = '<table style="border:solid 1px #DDDDDD; cellspacing:0; cellpadding:0; margin:1em;">';
85 $i = 0;
86 foreach ( $this->mImages as $pair ) {
87 $img =& $pair[0];
88 $text = $pair[1];
89
90 $name = $img->getName();
91 $nt = $img->getTitle();
92
93 // Not an image. Just print the name and skip.
94 if ( $nt->getNamespace() != NS_IMAGE ) {
95 $s .= '<td valign="top" width="150px" style="background-color:#F0F0F0;">' .
96 $nt->getText() . '</td>' . (($i%4==3) ? "</tr>\n" : '');
97 $i++;
98
99 continue;
100 }
101
102 //TODO
103 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
104
105 if( $this->mShowBytes ) {
106 if( $img->exists() ) {
107 $nb = wfMsg( 'nbytes', $wgLang->formatNum( $img->getSize() ) );
108 } else {
109 $nb = wfMsg( 'filemissing' );
110 }
111 $nb = htmlspecialchars( $nb ) . '<br />';
112 } else {
113 $nb = '';
114 }
115
116 '' ;
117 $textlink = $this->mShowFilename ?
118 $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20, '...' ) ) ) . '<br />' :
119 '' ;
120
121 $s .= ($i%4==0) ? '<tr>' : '';
122 $s .= '<td valign="top" width="150px" style="background-color:#F0F0F0;">' .
123 '<table width="100%" height="150px">'.
124 '<tr><td align="center" valign="center" style="background-color:#F8F8F8;border:solid 1px #888888;">' .
125 $sk->makeKnownLinkObj( $nt, '<img src="'.$img->createThumb(120,120).'" alt="" />' ) . '</td></tr></table> ' .
126 $textlink . $text . $nb;
127
128 $s .= "</td>\n" . (($i%4==3) ? "</tr>\n" : '');
129
130 $i++;
131 }
132 if( $i %4 != 0 ) {
133 $s .= "</tr>\n";
134 }
135 $s .= '</table>';
136
137 return $s;
138 }
139
140 } //class
141
142
143
144
145 } // defined( 'MEDIAWIKI' )
146 ?>