<?php

$wgExtensionFunctions
[] = "wfCategoryGalleryExtension"
$wgExtensionCredits['parserhook'][] = array(
    
'name' => 'CategoryGallery',
    
'description' => 'Displays a gallery from a category. Parameters: category=The category you want to gallerize, '
                     
'refresh=Display refresh widget, resizewidth=If set, display thumbnails of this width, '
                     
'width=Cell width (default 210), height=Cell height (default 350), '
                     
'border=CSS Cell border (default solid 2px #999999), margin=Cell margin in px (default 5) '
                     
'padding=Cell padding in px (default 5), background=Cell background (default #dddddd), '
                     
'displayhelp=If true, display a line explaining how to add to the gallery (default false)',
    
'author' => 'Stefano Rivera: http://rivera.za.net/'
    
//'url' => 'http://meta.wikimedia.org/wiki/DynamicPageList'
    
);

function 
wfCategoryGalleryExtension() {
  global 
$wgParser;
  
$wgParser->setHook"category-gallery""printCategoryGallery" );
}

function 
printCategoryGallery($input$argv, &$parser) {
  global 
$wgUser$wgContLang;

  
// Parameters
  
$category $argv['category'];
  
$refresh = (isset($argv['refresh']) && in_array(strtolower($argv['refresh']), array("true""refresh"1)));
  
$resizeWidth = isset($argv['resizewidth']) ? $argv['resizewidth'] : false;
  
$width = isset($argv['width']) ? $argv['width'] : 210;
  
$height = isset($argv['height']) ? $argv['height'] : 350;
  
$border = isset($argv['border']) ? $argv['border'] : "solid 2px #999999";
  
$margin = isset($argv['margin']) ? $argv['margin'] : 5;
  
$padding = isset($argv['padding']) ? $argv['padding'] : 5;
  
$background = isset($argv['background']) ? $argv['background'] : "#dddddd";
  
$displayHelp = (isset($argv['displayhelp']) && in_array(strtolower($argv['refresh']), array("true""displayhelp"1)));

  
$sk =& $wgUser->getSkin();
  
$local_parser = new Parser;
  
$catKey Title::newFromText($local_parser->transformMsg($category$parser->mOptions))->getDbKey();
      
  
// Build the SQL query
  
$dbr =& wfGetDBDB_SLAVE );
  
$select "SELECT page_namespace, page_title, old_text, img_description "
            
"FROM " $dbr->tableName('page') . " "
            
"LEFT JOIN " $dbr->tableName('revision') . " ON page_latest = rev_id "
            
"LEFT JOIN " $dbr->tableName('text') . " ON old_id = rev_text_id "
            
"LEFT JOIN " $dbr->tableName('image') . " ON page_title = img_name "
            
"INNER JOIN " $dbr->tableName('categorylinks') . " "
            
"ON page_id = cl_from AND cl_to = " $dbr->addQuotes($catKey) . " ";
  
$where " WHERE page_namespace = " $wgContLang->getNsIndex('Image') . " "
           
"ORDER BY img_name ";
  
$res $dbr->query($select$where);

  
$output "<div style=\"width: 100%; clear: both\">\n";

  if (
$dbr->numRows($res) == 0)
    
$output .= "<p>Nothing to display in gallery</p>";
  else {
    if (
$refresh)
      
$output .= $sk->makeKnownLinkObj($parser->getTitle(), "Refresh Gallery""action=purge");
    
    while (
$row $dbr->fetchObject($res)) {
      
// Used for links:
      
$title Title::makeTitle($row->page_namespace$row->page_title);

      
// If there is no page text, but there is an image description, we read that instead.
      
$description trim($row->old_text);
      if (
$description == "")
        
$description trim($row->img_description);
      
$description explode("\n"$description);
      
      
// Get the Description (wikitext)
      
$text implode("\n"preg_grep('/^;\s*Description\s*:/'$description));
      
$text preg_replace('/^;\s*Description\s*:\s*/'''$text);
      
$text $local_parser->parse($text$parser->mTitle$parser->mOptionstruefalse);

      
// Get the alternative Link
      
$link implode("\n"preg_grep('/^;\s*Link\s*:\s*\[\[.*\]\]/'$description));
      
$link preg_replace('/^;\s*Link\s*:\s*\[\[(.*)\]\]/''$1'$link);
      
$nice_nick preg_replace('/\.[^.]*/'''$row->page_title);
      if (
$link == "")
        
$link $nice_nick;
      
$link $sk->makeLinkObj(Title::makeTitle(0$link), $nice_nick);
      
      
$image $sk->makeImageLinkObj($title"Label"$nice_nick''$resizeWidth);
      
      
// Output
      
$output .= "<div style=\"float: left; width:${width}px; height: ${height}px; "
                 
"border: ${border}; margin: ${margin}px; padding: ${padding}px; "
                 
"background: ${background}; text-align: center; overflow: hidden;\">";
      
$output .= $image;
      
$output .= "<div style=\"text-weight: bold\">" $link "</div>";
      
$output .= "<div style=\"text-align: left\">" $text->getText() . "</div>";
      
$output .= "</div>\n";
    }
  }
  
$output .= "</div>";
  
  
// End the floatie world
  
$output .= "<div style=\"clear: left\"></div>";

  if (
$displayHelp)
    
$output .= "<p style=\"font-size: x-small\">To add to this gallery, upload an image, and put it in <code>[[Category:${category}]]</code>.</p>";
  
  return 
$output;
}
# vim:tabstop=2:expandtab:
?>