Extension:CopperminePhotoGallery

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg
CopperminePhotoGallery
Release status: stable
2008-10-10 coppermine photo gallery.png
Implementation Tag
Description This extension allows you to insert thumbnails (plus hyperlink to the larger image) from your Coppermine Photo Gallery (CPG) into a MediaWiki page.
Author(s) Claus van Beek (ClausVBtalk)
Latest version 1.0 (2008-10-05)
License GPL (latest version)
Download CopperminePhotoGallery.zip
Translate the CopperminePhotoGallery extension if it is available at translatewiki.net
Check usage and version matrix.

Detailed description[edit]

This extension allows you to insert pictures from your Coppermine Photo Gallery (CPG) into a mediawiki page. You can choose all three different sizes created by CPG. If you click on a thumbnails the large image will open in a new window (JavaScript). PHP5 and "mysqli" (see MySQL improved extension) are required.

Installation[edit]

OPTIONAL: You might take a look at Installing an extension, if you never installed an extension before.

  1. Download CopperminePhotoGallery.zip
  2. Extract it to your directory, e.g. "mediawiki-1.13.x/extensions"
  3. Edit your "LocalSettings.php" (see below)

Copy the following line at the end of your "LocalSettings.php":

require_once 'extensions/cpg.php';

Afterwards continue with your MySQL settings.

MySQL and other settings[edit]

Your CPG installation should contain a file like

include/config.inc.php

with all your MySQL settings. Please copy your values to "cpg.php".

// MySQL settings
define('CPG_HOST', 'localhost');
define('CPG_USER', 'mysql_user');
define('CPG_PASSWORD', 'mysql_password');
define('CPG_DATABASE', 'coppermine_mysql_database');
define('CPG_PREFIX', 'cpg_');

You must set these constants, otherwise this extension will not work. "cpg.php" connects the database to get the needed values.

Your next step should be to change "CPG_URI". Your CPG probably is installed in a subdirectory (e.g. "cpg"). You should find "albums" in this directory.

define('CPG_URI', 'http://www.yourdomain.tld/cpg/albums/'); // trailing "/" is IMPORTANT

There is a CPG demo. The right "CPG_URI" for this demo would be:

http://coppermine-gallery.net/demo/cpg14x/albums/

"CPG_URI" is needed to display your thumbnails and images. This extension will not work without it.

// e.g. "pic.jpg" (1792x1200), "normal_pic.jpg" (448x300), "thumb_pic.jpg" (224x150)
define('PREFIX_NORMAL', 'normal_');
define('PREFIX_THUMBNAILS', 'thumb_');

These are default settings, which should work right away. If you change those values in CPG, you must change them here as well.

Get help[edit]

First of all you can use

<cpg>help</cpg>

to get help. This will display all albums and all pictures, which are stored in your CPG database.

If you have further questions, please post them here (discussion).

Usage[edit]

To get help and general informations about your CPG, please use

<cpg>help</cpg>

If your know the subdirectory to your album and which pictures you want to show, please use

<cpg album="subdir_to_your_album">pic1.jpg;pic2.jpg;pic3.jpg</cpg>

Please remember "subdir_to_your_album" is not the complete path. Example: Your CPG_URI is "http://www.yourdomain.tld/cpg/albums/" and your images are stored in "beach". All images would be accessible with a browser in "http://www.yourdomain.tld/cpg/albums/beach/", but your album ("subdir_to_your_album") would be just "beach".

There is also the option "size". It has two possible values:

  • big
  • normal
<cpg album="beach" size="normal">

That way you are able to choose if you want to use thumbnails or larger versions of your images.

Note: The <img> tag embeds an image in an HTML page. The <img> tag has three required attributes in this extension:

  • src,
  • alt and
  • title.

Take a look a the code:

$select_pictures = 'SELECT pid, filepath, filename, title, caption
   FROM ' . CPG_PREFIX . 'pictures
   WHERE filename IN(' . $search_string . ')
   ORDER BY aid, pid';

(...)

$output .= '<a href="' . $biggest_image . '" onclick="window.open(\'' . $biggest_image
   . '\', \'popup\', \'scrollbars=yes, resizable=yes, width=1024,
     height=768\'); return false;"><img src="'
   . $filename_uri . '" alt="'
   . $row_pictures['title'] . '" title="'
   . $row_pictures['caption'] . '" /></a>' . "\n";

All these attributes are filled with values from the database, not the extension.

Code[edit]

I tried to comment my code. If you have any questions or need more comments on a special line, please let me know.

<?php
   // MySQL settings
   define('CPG_HOST', 'localhost');
   define('CPG_USER', 'mysql_user');
   define('CPG_PASSWORD', 'mysql_password');
   define('CPG_DATABASE', 'coppermine_mysql_database');
   define('CPG_PREFIX', 'cpg_');

   // CPG = Coppermine Picture Gallery
   define('CPG_URI', 'http://clausvb.de/coppermine/albums/'); // trailing "/" is IMPORTANT

   // e.g. "pic.jpg" (1792x1200), "normal_pic.jpg" (448x300), "thumb_pic.jpg" (224x150)
   define('PREFIX_NORMAL', 'normal_');
   define('PREFIX_THUMBNAILS', 'thumb_');


   $wgExtensionFunctions[] = 'cpg';

   function cpg()
   {
      global $wgParser;
      $wgParser->setHook('cpg', 'process_cpg');
   }

   function process_cpg($input, $args, $parser)
   {
      $db = new mysqli(CPG_HOST, CPG_USER, CPG_PASSWORD, CPG_DATABASE);

      // If MySQL cannot be connected, do nothing and leave function
      if (mysqli_connect_errno())
      {
         return '<h2>MySQL settings are wrong</h2> <p><strong>Error: "' . mysqli_connect_error() . '"</strong>.
         Please open</p> <pre>' .  __FILE__ . '</pre> <p>and check CPG_HOST, CPG_USER, CPG_PASSWORD, CPG_DATABASE
         and CPG_PREFIX, please.</p>';
      }

      // SET all encodings from an to RDBMS to UTF-8
      $db->query('SET character_set_results = utf8');
      $db->query('SET character_set_client = utf8');

      /**
       * help, e.g. <cpg>help</cpg>
      **/
      if ($input == 'help')
      {
         $select_all_pictures = 'SELECT filepath, filename FROM ' . CPG_PREFIX . 'pictures ORDER BY aid, pid';
         $result_all_pictures = $db->query($select_all_pictures)
            or die('ERROR MySQL: ' . $db->error . '<pre>' . $select_all_pictures . '</pre>');

         // read one time to get $compare and first row
         $row_all_pictures = $result_all_pictures->fetch_assoc();

         $compare = $row_all_pictures['filepath'];

         $output = '<h2>&lt;cpg&gt;help&lt;/cpg&gt;</h2> <p>This help will try to teach a few basics about this
         extension. CPG (Coppermine Picture Gallery) stores data in a MySQL database. To access this data you will
         have to provide a few things like user and password. Take a look at</p> <pre>' .  __FILE__ . '</pre> <p>and
         change it to your needs.</p> <h2>Your albums</h2>';

         $output .= '<fieldset style="font-size: 80%;">' . "\n";
         $output .= '<legend>' . $row_all_pictures['filepath'] . '</legend>' . "\n";

         do
         {
            if ($compare == $row_all_pictures['filepath'])
            {
               $output .= $row_all_pictures['filename'] . ", \n";
            }
            else
            {
               $compare = $row_all_pictures['filepath']; // set "$compare" to filepath

               $output .= '</fieldset>' . "\n";
               $output .= '<fieldset style="font-size: 80%;">' . "\n";
               $output .= '<legend>' . $row_all_pictures['filepath'] . '</legend>' . "\n";
            }
         } while ($row_all_pictures = $result_all_pictures->fetch_assoc());

         $output .= '</fieldset>' . "\n";

         return $output;
      }
      elseif (preg_match('/, /', $input))
      {
         $output = '<h2>&lt;cpg&gt;ERROR&lt;/cpg&gt;</h2> <p>ERROR: You may not use &quot;pic6.jpg, pic2.jpg&quot;! Please
         use &quot;pic6.jpg;pic2.jpg&quot; instead, thank you!</p>';

         return $output;
      }
      else
      {
         $filename_array = explode(';', $input); // split all filenames to an array
         $last_element = end($filename_array); // e.g. "pic6.jpg;pic2.jpg" => "pic2.jpg"

         // get TITLE and CAPTION for all files
         foreach ($filename_array as $filename)
         {
            /**
             * $search_string is important for WHERE clause, e.g. WHERE IN("pic6.jpg", "pic2.jpg")
            **/
            $search_string .= ($filename == $last_element)
               ? '"' . $filename . '"'
               : '"' . $filename . '", ';
         }

         $select_pictures = 'SELECT pid, filepath, filename, title, caption
            FROM ' . CPG_PREFIX . 'pictures
            WHERE filename IN(' . $search_string . ')
            ORDER BY aid, pid';
         $result_pictures = $db->query($select_pictures)
            or die('ERROR MySQL: ' . $db->error . '<pre>' . $select_pictures . '</pre>');

         while ($row_pictures = $result_pictures->fetch_assoc())
         {
            if ($args['size'] == 'big')
            {
               $filename_uri = CPG_URI . $args['album'] . '/' . $row_pictures['filename'];
            }
            elseif ($args['size'] == 'normal')
            {
               $filename_uri = CPG_URI . $args['album'] . '/' . PREFIX_NORMAL . $row_pictures['filename'];
            }
            else
            {
               $filename_uri = CPG_URI . $args['album'] . '/' . PREFIX_THUMBNAILS . $row_pictures['filename'];
            }

            $biggest_image = CPG_URI . $args['album'] . '/' . $row_pictures['filename'];

            $output .= '<a href="' . $biggest_image . '" onclick="window.open(\'' . $biggest_image
               . '\', \'popup\', \'scrollbars=yes, resizable=yes, width=1024, height=768\'); return false;"><img src="'
               . $filename_uri . '" alt="'
               . $row_pictures['title'] . '" title="'
               . $row_pictures['caption'] . '" /></a>' . "\n";
         }

         return $output;
      }

      $db->close();
   }
?>

Screenshots[edit]

Screenshot of some pictures in my MediaWiki using this extension Screenshot of getting help with "<cpg>help</cpg>"