New tag "<gallery>" to generate a table of image thumbnails
authorJens Frank <jeluf@users.mediawiki.org>
Sat, 13 Nov 2004 10:53:46 +0000 (10:53 +0000)
committerJens Frank <jeluf@users.mediawiki.org>
Sat, 13 Nov 2004 10:53:46 +0000 (10:53 +0000)
RELEASE-NOTES
includes/ImageGallery.php
includes/Parser.php

index b01f121..98d9fad 100644 (file)
@@ -30,6 +30,7 @@ Major changes from 1.3.x:
 * Adding filter and username exact search match for Special:Listusers (bug #770)
 * Special:Listadmins outdated, use Special:Listusers instead (bug #857)
 * Traditional/Simplified Chinese conversion
+* New tag "<gallery>" to generate a table of image thumbnails
 * ... and more!
 
 === Caveats ===
index 7c6c153..b652e49 100644 (file)
@@ -19,13 +19,15 @@ if( defined( 'MEDIAWIKI' ) ) {
  */
 class ImageGallery
 {
-       var $mImages;
+       var $mImages, $mShowBytes, $mShowFilename;
 
        /** 
         * Create a new image gallery object.
         */
        function ImageGallery( ) {
-               $this->mImages=array();
+               $this->mImages = array();
+               $this->mShowBytes = true;
+               $this->mShowFilename = true;
        }
 
        /**
@@ -45,6 +47,26 @@ class ImageGallery
                return empty( $this->mImages );
        }
 
+       /**
+        * Enable/Disable showing of the file size of an image in the gallery.
+        * Enabled by default.
+        * 
+        * @param boolean $f    set to false to disable
+        */
+       function setShowBytes( $f ) {
+               $this->mShowBytes = ( $f == true);
+       }
+
+       /**
+        * Enable/Disable showing of the filename of an image in the gallery.
+        * Enabled by default.
+        * 
+        * @param boolean $f    set to false to disable
+        */
+       function setShowFilename( $f ) {
+               $this->mShowFilename = ( $f == true);
+       }
+
        /**
         * Return a HTML representation of the image gallery
         * 
@@ -72,15 +94,19 @@ class ImageGallery
                        //TODO
                        //$ul = $sk->makeLink( $wgContLang->getNsText( Namespace::getUser() ) . ":{$ut}", $ut );
 
-                       $nb = wfMsg( "nbytes", $wgLang->formatNum( $img->getSize() ) );
+                       $nb = $this->mShowBytes ? 
+                               wfMsg( "nbytes", $wgLang->formatNum( $img->getSize() ) )  . '<br />' :
+                               '' ;
+                       $textlink = $this->mShowFilename ?
+                               $sk->makeKnownLinkObj( $nt, Language::truncate( $nt->getText(), 20, '...' ) ) . '<br />' :
+                               '' ;
 
                        $s .= ($i%4==0) ? '<tr>' : '';
                        $s .= '<td valign="top" width="150px" style="background-color:#F0F0F0;">' .
                                '<table width="100%" height="150px">'.
                                '<tr><td align="center" valign="center" style="background-color:#F8F8F8;border:solid 1px #888888;">' .
                                $sk->makeKnownLinkObj( $nt, '<img  src="'.$img->createThumb(120,120).'" alt="">' ) . '</td></tr></table> ' .
-                               $sk->makeKnownLinkObj( $nt, Language::truncate( $nt->getText(), 20, '...' ) ) .
-                               "<br />{$text}{$nb}<br />" ;
+                               $textlink . $text . $nb; 
 
                        $s .= '</td>' .  (($i%4==3) ? '</tr>' : '');
 
@@ -91,6 +117,37 @@ class ImageGallery
                return $s;
        }
 
+       /**
+        * Transparently generates an image gallery from a text with one line per image.
+        * text labels may be given by using |-style alternative text. E.g.
+        *   Image:one.jpg|The number "1"
+        *   Image:tree.jpg|A tree
+        * given as text will return a gallery with two images, labeled 'The number "1"' and
+        * 'A tree'.
+        */
+       function newFromTextList( $text ) {
+               $ig = new ImageGallery();
+               $ig->setShowBytes( false );
+               $ig->setShowFilename( false );
+               $lines = explode( "\n", $text );
+               foreach ( $lines as $line ) {
+                       preg_match( "/^([^|]+)(\\|(.*))?$/", $line, $matches );
+                       # Skip empty lines
+                       if ( count( $matches ) == 0 ) {
+                               continue;
+                       }
+                       $nt = Title::newFromURL( $matches[1] );
+                       if ( isset( $matches[3] ) ) {
+                               $label = $matches[3];
+                       } else {
+                               $label = '';
+                       }
+                       $ig->add( Image::newFromTitle( $nt ), $label );
+               }
+               return $ig;
+       }
+               
+
 } //class
 
 
index 5f115a8..645ad7c 100644 (file)
@@ -277,6 +277,7 @@ class Parser
                $pre_content = array();
                $comment_content = array();
                $ext_content = array();
+               $gallery_content = array();
 
                # Replace any instances of the placeholders
                $uniq_prefix = UNIQ_PREFIX;
@@ -330,6 +331,18 @@ class Parser
                        }
                }
 
+               # gallery
+               $text = Parser::extractTags('gallery', $text, $gallery_content, $uniq_prefix);
+               foreach( $gallery_content as $marker => $content ) {
+                       require_once( 'ImageGallery.php' );
+                       if ( $render ) {
+                               $ig = ImageGallery::newFromTextList( $content );
+                               $gallery_content[$marker] = $ig->toHTML();
+                       } else {
+                               $gallery_content[$marker] = '<gallery>'.$content.'</gallery>';
+                       }
+               }
+
                # Comments
                if($stripcomments) {
                        $text = Parser::extractTags(STRIP_COMMENTS, $text, $comment_content, $uniq_prefix);
@@ -358,6 +371,7 @@ class Parser
                        $state['math'] = $state['math'] + $math_content;
                        $state['pre'] = $state['pre'] + $pre_content;
                        $state['comment'] = $state['comment'] + $comment_content;
+                       $state['gallery'] = $state['gallery'] + $gallery_content;
 
                        foreach( $ext_content as $tag => $array ) {
                                if ( array_key_exists( $tag, $state ) ) {
@@ -371,6 +385,7 @@ class Parser
                          'math' => $math_content,
                          'pre' => $pre_content,
                          'comment' => $comment_content,
+                         'gallery' => $gallery_content,
                        ) + $ext_content;
                }
                return $text;