Open main menu

Extension:MediaList

MediaWiki extensions manual
OOjs UI icon advanced.svg
MediaList
Release status: beta
Implementation Tag
Description Displays a list of all files used by the current article
Author(s) Mick Schmidt (Wolverinetalk)
Latest version 1.0.1
MediaWiki tested with 1.11
License No license specified
Download No link
Translate the MediaList extension if it is available at translatewiki.net
Check usage and version matrix.

Contents

What can this extension do?Edit

This extension displays a listing of all files (images and other files) that have been used via Image or Media links in an article. It's meant to be an addition to the general Imagelist, to show only those files used by one article. It is intended to give an overview of the articles files, especially for articles that make heavy use of images and other files. It implements a new tag, <medialist>, to add the list in the chosen spot.

UsageEdit

The tag can be used in two ways:

<medialist [options]/>

or

<medialist [options]>Heading line above list</medialist>

The tag takes the following parameters, all of which are optional:

style=<style>
  • 'simple' to show only the linked filenames as text (default)
  • 'table' as a table with additional information (type, user, date)

ExamplesEdit

For most information with a heading line, use this:

<medialist style="table">Informations about all files used by this article</medialist>

For a brief overview as flowtext without a heading just use:

<medialist/>

InstallationEdit

  1. Save the code in your wiki's extensions directory as extensions/MediaList/MediaList.php and extensions/MediaList/MediaList.i18n.php.
  2. Add
    require_once("$IP/extensions/MediaList/MediaList.php");
    to the end of your LocalSettings.php

CodeEdit

MediaList.phpEdit

<?php

/*
 * Media List extension.
 *
 * This extension implements a new tag, <medialist>, which generates a list of
 * all images or other media that is referenced by the article in which the
 * tag is used.
 *
 * Usage:
 *   Just the plain list: <medialist [options]/>
 *   With heading line:   <medialist [options]>Heading text here</medialist>
 *
 *   Possible options:
 *     style=<style>
 *       'simple' to show only the linked filenames as text(default); 
 *       'table' as a table with additional information (type, user, date) 
 *
 */

if (!defined('MEDIAWIKI')) die();

$wgExtensionFunctions[] = 'wfMediaList';
$wgExtensionCredits['parserhook'][] = array(
  'name' =>        'MediaList (version 1.0.1)',
  'author' =>      'Mick Schmidt',
  'url' =>         'http://www.mediawiki.org/wiki/Extension:MediaList',
  'description' => 'Generates a list of all image/media files used by an article',
);

// Internationalization file
require_once( dirname(__FILE__) . '/MediaList.i18n.php' );

/*
 * Setup Medialist extension.
 * Sets a parser hook for <medialist/>.
 */
function wfMediaList()
{
  // Add messages
  global $wgMessageCache, $MediaListMessages;
  foreach( $MediaListMessages as $key => $value )
  {
    $wgMessageCache->addMessages($MediaListMessages[$key], $key);
  }

  new MediaList();
}


class MediaList
{

  ////////////////////////////////////////////////////////////////////////
  // Configuration
  ////////////////////////////////////////////////////////////////////////

  /*
   * Default settings for the media list.
   */
    private $settings = array(
        'style'     => 'simple'      // Default style: simple list
    );


  ////////////////////////////////////////////////////////////////////////
  // Constructor
  ////////////////////////////////////////////////////////////////////////

  /*
   * Setup Medialist extension.
   * Sets a parser hook for <medialist/>.
   */
  public function __construct()
  {
    global $wgParser;
    $wgParser->setHook('medialist', array(&$this, 'hookML'));
  } // end of constructor


  ////////////////////////////////////////////////////////////////////////
  // Hook
  ////////////////////////////////////////////////////////////////////////

  /*
   * The hook function.  Handles <medialist/>.
   *     $headline   The tag's text content;
   *                 (between <medialist> and </medialist>);
   *                 if not empty used as headline.
   *     $argv       List of tag parameters.
   *                 [style=simple|table] ; default: simple
   *     $parser     Parser handle.
   */
  public function hookML($headline, $argv, &$parser)
  {
    // Get any user-specified parameters, and save them in $this->settings.
    foreach ($argv as $key => $value)
      $this->settings[$key] = $value;

    if (isset($headline) & $headline != "")
    {
      // Replace variables in $headline by calling the parser on it.  This
      // allows it to use {{PAGENAME}}, etc.
      $localParser = new Parser();
      $headline = $localParser->parse($headline, $parser->mTitle, $parser->mOptions, false);
      $headline = $headline->getText();
      $output = "<h3>$headline</h3>";
    }
    else
      $output = "";

    // Get the database handle,
    $dbr =& wfGetDB(DB_SLAVE);

    // Retrieve the page_id of this article
    $artID = $this->getArticleID($dbr, $parser->mTitle->getDBKey());

    // Get all files for this article
    $articleFiles = $this->searchFiles($dbr, $artID);

    // Generate the media listing.
    $output .= $this->outputMedia($articleFiles);

    // Convert the listing wikitext into HTML and return it.
    $localParser = new Parser();
    $output = $localParser->parse($output, $parser->mTitle, $parser->mOptions);
    return $output->getText();
  } // end of hookML

  ////////////////////////////////////////////////////////////////////////
  // Database Access
  ////////////////////////////////////////////////////////////////////////

  /*
   * Get the unique database id for a given article.
   *     $dbr     The database handle
   *     $title   The article name for which the corresponing ID is retrieved
   *
   * Returns the articles unique page_id
   */
  private function getArticleID($dbr, $title)
  {
    // Query the database.
    $res = $dbr->select(
            array('page'),
            array('page_id'),
            array('page_title' => $title),
            '',
            ''
            );
    if ($res === false)
      return 0;

    $x = $dbr->fetchObject($res);

    // Free the results.
    $dbr->freeResult($res);

    return $x->page_id;
  } // end of getArticleID

  /*
   * Get all files that are referenced in the given article.
   *     $dbr        The database handle
   *     $artID      The article ID for which files are to be retrieved
   *
   * Returns an array of objects, each representing one member of the files
   * contained in the article.
   * Each object contains the following fields from the database:
   *      img_name
   *      img_media_type
   *      img_user_text
   *      img_timestamp
   *      img_major_mime
   *      img_minor_mime
   */
  private function searchFiles($dbr, $artID)
  {
    // Query the database.
    $res = $dbr->select(
            array('image', 'imagelinks'),
            array('img_name','img_media_type','img_user_text',
                  'img_timestamp','img_major_mime','img_minor_mime'),
            array('il_to = img_name', 'il_from' => $artID),
            '',
            array('ORDER BY' => 'img_name')
            );
    if ($res === false)
      return array();

    // Convert the results list into an array.
    $list = array();
    while ($x = $dbr->fetchObject($res))
    {
      $list[] = $x;
    }

    // Free the results.
    $dbr->freeResult($res);

    return $list;
  } // end of searchFiles

  ////////////////////////////////////////////////////////////////////////
  // Output
  ////////////////////////////////////////////////////////////////////////

  /*
   * Generate output for the list.
   */
  function outputMedia($filelist)
  {
    global $wgUser;

    $output = '';

    if ($this->settings['style'] == "table")
    {
      $output .= '<table width="100%" border="1" cellspacing="0">';
      $output .= '<tr><th>' . wfMsgForContent('ml_heading_name') .
                 '</th><th>' . wfMsgForContent('ml_heading_type') .
                 '</th><th>' . wfMsgForContent('ml_heading_user') .
                 '</th><th>' . wfMsgForContent('ml_heading_datetime') .
                 '</th></tr>';
    }

    $imglnk = wfMsg('ml_imagefile_link');
    $usrlnk = wfMsg('ml_user_link');
    $othrdsc = wfMsgForContent('ml_non_imagefile_description');

    foreach ($filelist as $dataobject)
    {
      if ($this->settings['style'] == "table")
      {
        $output .= "<tr>";
        $output .= "<td>[[:$imglnk:" . $dataobject->img_name . "|" . $dataobject->img_name . "]]</td>";
        $output .= '<td align="center">' . $dataobject->img_major_mime . "/" . $dataobject->img_minor_mime . "</td>";
        $output .= '<td align="center">' . "[[$usrlnk:" . $dataobject->img_user_text . "|" . $dataobject->img_user_text . "]]</td>";
        $jahr=substr($dataobject->img_timestamp, 0, 4);
        $monat=substr($dataobject->img_timestamp, 4, 2);
        $tag=substr($dataobject->img_timestamp, 6, 2);
        $stunde=substr($dataobject->img_timestamp, 8, 2);
        $minute=substr($dataobject->img_timestamp, 10, 2);
        $sekunde=substr($dataobject->img_timestamp, 12, 2);
        $timestamp=mktime($stunde,$minute,$sekunde,$monat,$tag,$jahr);
        $timecorrection=$wgUser->getOption("timecorrection", "00:00");
        $tc_part=explode(":", $timecorrection);
        $timestamp += (($tc_part[0] * 60) + $tc_part[1]) * 60;
        $output .= '<td align="center">' . date("d. M. Y, H:i:s", $timestamp) . "</td>";
        $output .= "</tr>";
      }
      else
      {
        if ($dataobject->img_media_type != "BITMAP")
          $output .= "[[:$imglnk:" . $dataobject->img_name . "|$othrdsc:" . $dataobject->img_name . "]]&nbsp;&nbsp; ";
        else
          $output .= "[[:$imglnk:" . $dataobject->img_name . "]]&nbsp;&nbsp; ";
      }
    }

    if ($this->settings['style'] == "table")
    {
      $output .= '</table>';
    }

    return $output;
  } // end of outputMedia

} // end of class MediaList

MediaList.i18n.phpEdit

<?php
#coding: utf-8

/*
 * Internationalization file for MediaList extension.
 */

$MediaListMessages = array();

$MediaListMessages['en'] = array(
  'ml_imagefile_link'                 => 'Image',
  'ml_user_link'                      => 'User',
  'ml_non_imagefile_description'      => 'File',

  // Table column headings
  'ml_heading_name'                   => 'Name',
  'ml_heading_type'                   => 'Type',
  'ml_heading_user'                   => 'User',
  'ml_heading_datetime'               => 'Last Change',
);

$MediaListMessages['de'] = array(
  'ml_imagefile_link'                 => 'Bild',
  'ml_user_link'                      => 'Benutzer',
  'ml_non_imagefile_description'      => 'Datei',

  // Table column headings
  'ml_heading_name'                   => 'Name',
  'ml_heading_type'                   => 'Typ',
  'ml_heading_user'                   => 'Benutzer',
  'ml_heading_datetime'               => 'Letzte Ă„nderung',
);