From e34d357e9da096a9a98ee5988cf49e3af2b35aa5 Mon Sep 17 00:00:00 2001 From: Jens Frank Date: Sat, 13 Nov 2004 10:53:46 +0000 Subject: [PATCH] New tag "" to generate a table of image thumbnails --- RELEASE-NOTES | 1 + includes/ImageGallery.php | 67 ++++++++++++++++++++++++++++++++++++--- includes/Parser.php | 15 +++++++++ 3 files changed, 78 insertions(+), 5 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index b01f1218d9..98d9fad3f8 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -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 "" to generate a table of image thumbnails * ... and more! === Caveats === diff --git a/includes/ImageGallery.php b/includes/ImageGallery.php index 7c6c153354..b652e49131 100644 --- a/includes/ImageGallery.php +++ b/includes/ImageGallery.php @@ -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() ) ) . '
' : + '' ; + $textlink = $this->mShowFilename ? + $sk->makeKnownLinkObj( $nt, Language::truncate( $nt->getText(), 20, '...' ) ) . '
' : + '' ; $s .= ($i%4==0) ? '' : ''; $s .= '' . ''. '
' . $sk->makeKnownLinkObj( $nt, '' ) . '
' . - $sk->makeKnownLinkObj( $nt, Language::truncate( $nt->getText(), 20, '...' ) ) . - "
{$text}{$nb}
" ; + $textlink . $text . $nb; $s .= '' . (($i%4==3) ? '' : ''); @@ -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 diff --git a/includes/Parser.php b/includes/Parser.php index 5f115a8a48..645ad7cd8f 100644 --- a/includes/Parser.php +++ b/includes/Parser.php @@ -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] = ''.$content.''; + } + } + # 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; -- 2.20.1