New tag "<gallery>" to generate a table of image thumbnails
[lhc/web/wiklou.git] / includes / ImageGallery.php
1
2 <?php
3 /**
4 * @package MediaWiki
5 */
6
7 /**
8 * This is not a valid entry point, perform no further processing unless MEDIAWIKI is defined
9 */
10 if( defined( 'MEDIAWIKI' ) ) {
11
12
13 /**
14 * Image gallery
15 *
16 * Add images to the gallery using add(), then render that list to HTML using toHTML().
17 *
18 * @package MediaWiki
19 */
20 class ImageGallery
21 {
22 var $mImages, $mShowBytes, $mShowFilename;
23
24 /**
25 * Create a new image gallery object.
26 */
27 function ImageGallery( ) {
28 $this->mImages = array();
29 $this->mShowBytes = true;
30 $this->mShowFilename = true;
31 }
32
33 /**
34 * Add an image to the gallery.
35 *
36 * @param Image $image Image object that is added to the gallery
37 * @param string $text Additional text to be shown. The name and size of the image are always shown.
38 */
39 function add( $image, $text='' ) {
40 $this->mImages[] = array( &$image, $text );
41 }
42
43 /**
44 * isEmpty() returns false iff the gallery doesn't contain any images
45 */
46 function isEmpty() {
47 return empty( $this->mImages );
48 }
49
50 /**
51 * Enable/Disable showing of the file size of an image in the gallery.
52 * Enabled by default.
53 *
54 * @param boolean $f set to false to disable
55 */
56 function setShowBytes( $f ) {
57 $this->mShowBytes = ( $f == true);
58 }
59
60 /**
61 * Enable/Disable showing of the filename of an image in the gallery.
62 * Enabled by default.
63 *
64 * @param boolean $f set to false to disable
65 */
66 function setShowFilename( $f ) {
67 $this->mShowFilename = ( $f == true);
68 }
69
70 /**
71 * Return a HTML representation of the image gallery
72 *
73 * For each image in the gallery, display
74 * - a thumbnail
75 * - the image name
76 * - the additional text provided when adding the image
77 * - the size of the image
78 *
79 */
80 function toHTML() {
81 global $wgLang, $wgContLang, $wgUser;
82
83 $sk = $wgUser->getSkin();
84
85 $s = '<table style="border:solid 1px #DDDDDD; cellspacing:0; cellpadding:0; margin:1em;">';
86 $i = 0;
87 foreach ( $this->mImages as $pair ) {
88 $img =& $pair[0];
89 $text = $pair[1];
90
91 $name = $img->getName();
92 $nt = $img->getTitle();
93
94 //TODO
95 //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
96
97 $nb = $this->mShowBytes ?
98 wfMsg( "nbytes", $wgLang->formatNum( $img->getSize() ) ) . '<br />' :
99 '' ;
100 $textlink = $this->mShowFilename ?
101 $sk->makeKnownLinkObj( $nt, Language::truncate( $nt->getText(), 20, '...' ) ) . '<br />' :
102 '' ;
103
104 $s .= ($i%4==0) ? '<tr>' : '';
105 $s .= '<td valign="top" width="150px" style="background-color:#F0F0F0;">' .
106 '<table width="100%" height="150px">'.
107 '<tr><td align="center" valign="center" style="background-color:#F8F8F8;border:solid 1px #888888;">' .
108 $sk->makeKnownLinkObj( $nt, '<img src="'.$img->createThumb(120,120).'" alt="">' ) . '</td></tr></table> ' .
109 $textlink . $text . $nb;
110
111 $s .= '</td>' . (($i%4==3) ? '</tr>' : '');
112
113 $i++;
114 }
115 $s .= '</table>';
116
117 return $s;
118 }
119
120 /**
121 * Transparently generates an image gallery from a text with one line per image.
122 * text labels may be given by using |-style alternative text. E.g.
123 * Image:one.jpg|The number "1"
124 * Image:tree.jpg|A tree
125 * given as text will return a gallery with two images, labeled 'The number "1"' and
126 * 'A tree'.
127 */
128 function newFromTextList( $text ) {
129 $ig = new ImageGallery();
130 $ig->setShowBytes( false );
131 $ig->setShowFilename( false );
132 $lines = explode( "\n", $text );
133 foreach ( $lines as $line ) {
134 preg_match( "/^([^|]+)(\\|(.*))?$/", $line, $matches );
135 # Skip empty lines
136 if ( count( $matches ) == 0 ) {
137 continue;
138 }
139 $nt = Title::newFromURL( $matches[1] );
140 if ( isset( $matches[3] ) ) {
141 $label = $matches[3];
142 } else {
143 $label = '';
144 }
145 $ig->add( Image::newFromTitle( $nt ), $label );
146 }
147 return $ig;
148 }
149
150
151 } //class
152
153
154
155
156 } // defined( 'MEDIAWIKI' )
157 ?>