Difference between revisions of "Extension:Download"

From Organic Design wiki
(make a start)
(Done :-))
Line 12: Line 12:
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
  
define('DOWNLOAD_VERSION', '1.0.0, 2008-08-22');
+
define('DOWNLOAD_VERSION', '1.0.1, 2008-08-25');
  
 
$egDownloadTag          = "download";
 
$egDownloadTag          = "download";
Line 34: Line 34:
  
 
function tagDownload($text,$argv,&$parser) {
 
function tagDownload($text,$argv,&$parser) {
global $egDownloadTag;
+
global $wgScriptPath, $egDownloadImages;
$args = '';
+
$dir = preg_replace('|^.+(?=[/\\\\]extensions)|', $wgScriptPath, $egDownloadImages);
foreach ($argv as $k => $v) {
+
preg_match_all('|^\s*(.+?)\s*(\|(.+?))?\s*$|m', $text, $links);
$k = htmlspecialchars($k);
+
$text = "<ul class='download-links'>\n";
$v = htmlspecialchars($v);
+
foreach ($links[3] as $i => $link) {
$args .= "<li><b>$k:</b> <i>$v</i></li>\n";
+
$page = $links[1][$i];
 +
$img = Image::newFromTitle(Title::newFromText($page));
 +
$src = $img && $img->exists() ? $img->getURL() : false;
 +
if ($src) {
 +
$ext = preg_match('|^.+\.(.+?)$|', $src, $m) ? $m[1] : 'default';
 +
$icon = glob("$egDownloadImages/$ext.*");
 +
if (count($icon) == 0) $icon = glob("$egDownloadImages/default.*");
 +
$icon = "$dir/".basename($icon[0]);
 +
$style = "background: transparent url($icon) no-repeat scroll right center; padding-right: 16px;";
 +
$item = "<a style=\"$style\" href=\"$src\">$link</a>";
 +
} else $item = "<b>No file associated with \"$page\"</b>";
 +
$text .=  "<li>$item</li>\n";
 
}
 
}
$text = htmlspecialchars($text);
+
$text .= "</ul>\n";
return "<h3>Tag $egDownloadTag():</h3>\n<ul>$args<li>Content:</b>$text</ul>\n";
+
return $text;
 
}
 
}
 
   }
 
   }

Revision as of 03:43, 25 August 2008

<?php /**

* Download extension - Provides a tag which renders a list of image titles as downloadable links
*
Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.

Template:PhpCategory:Extensions created with Template:Extension

* See http://www.organicdesign.co.nz/Extension:Download for installation and usage details
*
* @package MediaWiki
* @subpackage Extensions
* @author User:Nad
* @licence GNU General Public Licence 2.0 or later
*/

if (!defined('MEDIAWIKI')) die('Not an entry point.');

define('DOWNLOAD_VERSION', '1.0.1, 2008-08-25');

$egDownloadTag = "download"; $egDownloadImages = dirname(__FILE__)."/images"; $wgExtensionFunctions[] = 'efSetupDownload';

$wgExtensionCredits['parserhook'][] = array( 'name' => 'Download', 'author' => 'User:Nad', 'description' => 'Provides a tag which renders a list of image titles as downloadable links', 'url' => 'http://www.organicdesign.co.nz/Extension:Download', 'version' => DOWNLOAD_VERSION );

class Download {

function __construct() { global $wgParser, $egDownloadTag;

		$wgParser->setHook($egDownloadTag, array($this, 'tagDownload'));
	}

function tagDownload($text,$argv,&$parser) { global $wgScriptPath, $egDownloadImages; $dir = preg_replace('|^.+(?=[/\\\\]extensions)|', $wgScriptPath, $egDownloadImages); preg_match_all('|^\s*(.+?)\s*(\|(.+?))?\s*$|m', $text, $links);

$text = "

\n";

return $text; }

 }

function efSetupDownload() { global $egDownload; $egDownload = new Download(); }