Smugmug extension for MediaWiki: Difference between revisions
(→Syntax: reversed the defaults on names of sizes. basically the full names are preferred, but short names are allowed) |
(Added section for da code) |
||
Line 18: | Line 18: | ||
Caption text will default to the caption on Smugmug, or the keywords on Smugmug if the caption does not exist. The options given as attributes will override options between the tags. | Caption text will default to the caption on Smugmug, or the keywords on Smugmug if the caption does not exist. The options given as attributes will override options between the tags. | ||
== Da Code == | |||
Or [http://robnugen.com/downloads/php/Smugmug_wiki_extension/Smugmug.php.txt Smugmug.php.txt] | |||
<source lang="php"> | |||
<?php | |||
# Smugmug MediaWiki extension 0.04 | |||
# | |||
# This code is licensed under the Creative Commons Attribution-ShareAlike 3.0 License | |||
# | |||
# For more information or the latest version visit | |||
# http://robnugen.com/wiki/index.php?title=Smugmug_extension | |||
# | |||
$wgExtensionFunctions[] = 'wfSmugmug'; | |||
$wgExtensionCredits['parserhook'][] = array( | |||
'name' => 'Smugmug', | |||
'description' => 'Display Smugmug image', | |||
'author' => 'Rob Nugen', | |||
'url' => 'http://robnugen.com/wiki' | |||
); | |||
function wfSmugmug() { | |||
global $wgParser; | |||
$wgParser->setHook('smugmug', 'renderHTML'); | |||
} | |||
require_once("phpSmug-1.0.2/phpSmug.php"); // eventually this will not be required | |||
# The callback function for converting the input text to HTML output | |||
function renderHTML($input, $params) | |||
{ | |||
# $input is what's between the tags, and $params are the modifiers inside the first tag: | |||
# <smugmug param1="value1" param2="value2">input</smugmug> | |||
$smugmug_api_key = "COPY YOUR SMUGMUG API KEY HERE"; | |||
// these parameters have defaults if nothing is specified | |||
$smugmug_default_type = "none"; // override with <smugmug type="thumb|frame"> | |||
$smugmug_default_idtype = "image"; // override with <smugmug idtype="album"> | |||
$smugmug_default_location = ""; // override with <smugmug location="left|center|none|right"> | |||
$smugmug_default_size = "medium"; // override with <smugmug size="ti|th|s|l|o"> | |||
// these are allowable values for the various parameters. There should be *no repetition* of allowable values here, otherwise the parameter reading won't work correctly. | |||
// If there must be repetition here, then specify the parameter values via attributes in the tag, and you should have no problem | |||
$valid_smugmug_idtypes = array("album"); | |||
$valid_smugmug_types = array("thumbnail", "thumb", "frame"); | |||
$valid_smugmug_locations = array("right", "left", "center", "none"); | |||
$valid_smugmug_sizes = array("tiny", "thumb", "small", "medium", "large", "original"); | |||
// keys in these arrays should point to values that exist in the corresponding arrays above. | |||
$other_spellings_of_types = array ("thumbnail" => "thumb", "thumnail" => "thumb"); | |||
$other_spellings_of_sizes = array ("ti" => "tiny", "th" => "thumb", "s" => "small", "m" => "medium", "med" => "medium", "l" => "large", "o" => "original", "orig" => "original"); | |||
// allow the other spellings of the arrays to be found in the parameters. There should be a line here for each of the "other_spellings" arrays above | |||
$valid_smugmug_sizes = array_merge($valid_smugmug_sizes, array_keys($other_spellings_of_sizes)); | |||
$valid_smugmug_types = array_merge($valid_smugmug_types, array_keys($other_spellings_of_types)); | |||
/*************************************************************************************************************************************************************** | |||
The arrays above define the parameters that we'll be looking for. You should be able to tweak a lot without changing anything below this line. | |||
Now we start looking for parameters sent by the wiki | |||
****************************************************************************************************************************************************************/ | |||
// if they exist, the parameters sent as attributes override the parameters sent <smugmug>between the tags</smugmug>, so grab them first. | |||
// Then we must be sure not to overwrite existing values for these variables. | |||
$_smugmug_id = $params['id']; | |||
$_smugmug_type = $params['type']; | |||
$_smugmug_idtype = $params['idtype']; | |||
$_smugmug_location = $params['location']; | |||
$_smugmug_size = $params['size']; | |||
$_smugmug_caption = $params['caption']; | |||
# split the text sent <smugmug>between the tags</smugmug> on | | |||
$inputs = explode("|", $input); | |||
# process the inputs, basically comparing them to the arrays set above to determine for which parameter their value should be used. | |||
foreach($inputs as $value) { | |||
if ($_smugmug_id == "" && ctype_digit($value)) { $_smugmug_id = $value; /* if the value is all digits, it must be the id */ } | |||
elseif ($_smugmug_type == "" && in_array(strtolower($value), $valid_smugmug_types)) { $_smugmug_type = strtolower($value); } | |||
elseif ($_smugmug_idtype == "" && in_array(strtolower($value), $valid_smugmug_idtypes)) {$_smugmug_idtype = strtolower($value); } | |||
elseif ($_smugmug_location == "" && in_array(strtolower($value), $valid_smugmug_locations)) { $_smugmug_location = strtolower($value); } | |||
elseif ($_smugmug_size == "" && in_array(strtolower($value), $valid_smugmug_sizes)) { $_smugmug_size = strtolower($value); } | |||
elseif ($_smugmug_caption == "") { $_smugmug_caption = $value; } // everything else is caption | |||
else { $_smugmug_caption .= "|".$value; } // this allows caption to have | chars in it | |||
} | |||
// finally, if none of the above worked, fill in the defaults | |||
if($_smugmug_type == "") { $_smugmug_type = $smugmug_default_type; } | |||
if($_smugmug_idtype == "") { $_smugmug_idtype = $smugmug_default_idtype; } | |||
if($_smugmug_location == "") { $_smugmug_location = $smugmug_default_location; } | |||
if($_smugmug_size == "") { $_smugmug_size = $smugmug_default_size; } | |||
// We have all the parameters as sent by the user (actually they have been lowercased). | |||
// Now convert them to allowable values according to the other_spellings arrays | |||
if(is_array($other_spellings_of_types) && isset($other_spellings_of_types[$_smugmug_type])) { $_smugmug_type = $other_spellings_of_types[$_smugmug_type]; } | |||
if(is_array($other_spellings_of_idtypes) && isset($other_spellings_of_idtypes[$_smugmug_idtype])) { $_smugmug_idtype = $other_spellings_of_idtypes[$_smugmug_idtype]; } | |||
if(is_array($other_spellings_of_locations) && isset($other_spellings_of_locations[$_smugmug_location])) { $_smugmug_location = $other_spellings_of_locations[$_smugmug_location]; } | |||
if(is_array($other_spellings_of_sizes) && isset($other_spellings_of_sizes[$_smugmug_size])) { $_smugmug_size = $other_spellings_of_sizes[$_smugmug_size]; } | |||
# Now make sure we have the one required parameter: id | |||
if (!isset($_smugmug_id)) { | |||
$output = "<strong class='error'>Smugmug Error ( No ID ): Enter at least a PhotoID</strong>"; | |||
return $output; | |||
} | |||
$f = new phpSmug($smugmug_api_key, "robnugen.com/wiki/extensions/Smugmug.php v0.03wiki"); | |||
$f->login_anonymously(); | |||
$image_info = $f->images_getInfo($_smugmug_id); | |||
// It makes no sense to have a default caption for all pictures, so the default will be whatever is set on smugmug servers | |||
if ($_smugmug_caption == "") { $_smugmug_caption = $image_info['Caption']; } | |||
if ($_smugmug_caption == "") { $_smugmug_caption = $image_info['Keywords']; } // use the keywords if there was no caption | |||
/********************************************************************************************************************************************* | |||
From here, we are just basically mimicking the code in ../includes/Linker.php | |||
However, I have skipped the Right To Left languages, and skipped vertical alignment options | |||
*********************************************************************************************************************************************/ | |||
// Clean up our output strings | |||
$prefix = $postfix = $s = ''; | |||
if ($_smugmug_location == 'center') { | |||
$prefix = '<div class="center">'; | |||
$postfix = '</div>'; | |||
$_smugmug_location = 'none'; // This 'none' value will be used in subsequent div class (same as Linker.php) | |||
} | |||
if ($_smugmug_idtype == "image") | |||
{ | |||
// best I can tell, this matches Linker.php except for valign parameters | |||
if ($_smugmug_type == "none") | |||
{ | |||
$size_url_key = ucfirst($_smugmug_size) . "URL"; // for example "LargeURL" | |||
$s = "<a href=\"{$image_info['AlbumURL']}\" class=\"image\" title=\"{$_smugmug_caption}\"><img src=\"{$image_info[$size_url_key]}\" alt=\"{$_smugmug_caption}\"></a>"; | |||
if ($_smugmug_location != '') { | |||
$s = "<div class=\"float{$_smugmug_location}\"><span>{$s}</span></div>"; | |||
} | |||
return str_replace("\n", ' ',$prefix.$s.$postfix); | |||
} | |||
else | |||
{ | |||
// basically we are in $_smugmug_type = "frame" or "thumb" | |||
if($_smugmug_location == "") { $_smugmug_location = 'right';} // This 'right' value will be used in subsequent div classes (same as Linker.php) | |||
/********************************************************************************************************************/ | |||
/********************************************************************************************************************/ | |||
/********************************************************************************************************************/ | |||
/*********** WARNING!! THIS HARDCODED VALUE SHOULD BE THE WIDTH OF THE THUMBNAIL + 2 ****************************/ | |||
/********************************************************************************************************************/ | |||
/********************************************************************************************************************/ | |||
/********************************************************************************************************************/ | |||
$outerWidth = 402; | |||
/********************************************************************************************************************/ | |||
/********************************************************************************************************************/ | |||
/********************************************************************************************************************/ | |||
/*********** WARNING!! IN OTHER WORDS, THIS HARDCODED VALUE SHOULD NOT BE HARDCODED! ****************************/ | |||
/********************************************************************************************************************/ | |||
/********************************************************************************************************************/ | |||
/****************************************************************************** (but it works oh so nicely for now) */ | |||
/********************************************************************************************************************/ | |||
$s = "<div class=\"thumb t{$_smugmug_location}\"><div class=\"thumbinner\" style=\"width:{$outerWidth}px;\">"; | |||
// this basically matches Linker.php. Check that code for documentation. Oh wait! There is none! | |||
$size_url_key = ucfirst($_smugmug_size) . "URL"; | |||
$s .= "<a href=\"{$image_info['AlbumURL']}\" class=\"image\" title=\"{$_smugmug_caption}\">" . | |||
"<img src=\"{$image_info[$size_url_key]}\" alt=\"{$_smugmug_caption}\" class=\"thumbimage\"></a>"; | |||
if ($_smugmug_type == 'frame') { | |||
// type="frame" just gets a frame | |||
$zoomicon=""; | |||
} | |||
else | |||
{ | |||
// type="thumb" gets a cute little zoom icon | |||
global $wgStylePath; | |||
$zoomicon = '<div class="magnify" style="float:right">' . | |||
'<a href="' . $image_info[$size_url_key] . '" class="internal" title="Enlarge">'. | |||
'<img src="'.$wgStylePath.'/common/images/magnify-clip.png" width="15" height="11" alt="" /></a></div>'; | |||
} | |||
$s .= '<div class="thumbcaption">'.$zoomicon.$_smugmug_caption."</div></div></div>"; // closes the divs for class="thumbcaption", class="thumb t..", and class="thumbinner" | |||
return $s; | |||
} // close $_smugmug_type != "" | |||
} // Matches if ($_smugmug_idtype == "image") | |||
error_reporting(E_ALL); | |||
// output=php means that the request will return serialized PHP | |||
$request = 'http://api.search.yahoo.com/ImageSearchService/V1/imageSearch?appid=YahooDemo&query=Madonna&results=4&output=php'; | |||
# use cURL if we can't use file_get_contents | |||
if(ini_get('allow_url_fopen')) { | |||
$response = file_get_contents($request); | |||
} | |||
else | |||
{ | |||
$ch = curl_init(); | |||
$timeout = 5; // set to zero for no timeout | |||
curl_setopt ($ch, CURLOPT_URL, $request); | |||
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); | |||
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); | |||
$response = curl_exec($ch); | |||
curl_close($ch); | |||
} | |||
if ($response === false) { | |||
return "request failed"; | |||
} | |||
$phpobj = unserialize($response); | |||
return print_r($phpobj,true); // print_r prints human readable format. true means return, don't echo | |||
} | |||
</source> |
Revision as of 05:35, 1 November 2007
Syntax
Based on the basic syntax for images in a wiki, the full syntax for displaying an image from smugmug is:
<smugmug>{id}|{type}|{location}|{size}|{caption}</smugmug>
OR
<smugmug id="{id}" type="{type}" location="{location}" size="{size}" caption="{caption}"></smugmug>
{id}
is required.{type}
can bethumb
,thumbnail
, orframe
. This controls how the image is formatted.{idtype}
can beimage
, oralbum
:album
means{id}
is the id of an album.{location}
can beleft
,right
,center
,none
: Controls the alignment of the image(s) on the page.{size}
can beTiny
,Thumbnail
,Square
,Medium
,Large
,Original
, or their short name counterparts:Ti
,Th
,S
,M
,L
,O
.{caption text}
is the caption text
The options can be given in any order. If a given option does not match any of the other possibilities, it is assumed to be the caption text. Caption text can contain wiki links or other formatting, I hope.
Caption text will default to the caption on Smugmug, or the keywords on Smugmug if the caption does not exist. The options given as attributes will override options between the tags.
Da Code
<source lang="php"> <?php
- Smugmug MediaWiki extension 0.04
- This code is licensed under the Creative Commons Attribution-ShareAlike 3.0 License
- For more information or the latest version visit
- http://robnugen.com/wiki/index.php?title=Smugmug_extension
$wgExtensionFunctions[] = 'wfSmugmug'; $wgExtensionCredits['parserhook'][] = array(
'name' => 'Smugmug', 'description' => 'Display Smugmug image', 'author' => 'Rob Nugen', 'url' => 'http://robnugen.com/wiki'
);
function wfSmugmug() {
global $wgParser; $wgParser->setHook('smugmug', 'renderHTML');
}
require_once("phpSmug-1.0.2/phpSmug.php"); // eventually this will not be required
- The callback function for converting the input text to HTML output
function renderHTML($input, $params) { # $input is what's between the tags, and $params are the modifiers inside the first tag: # <smugmug param1="value1" param2="value2">input</smugmug>
$smugmug_api_key = "COPY YOUR SMUGMUG API KEY HERE";
// these parameters have defaults if nothing is specified $smugmug_default_type = "none"; // override with <smugmug type="thumb|frame"> $smugmug_default_idtype = "image"; // override with <smugmug idtype="album"> $smugmug_default_location = ""; // override with <smugmug location="left|center|none|right"> $smugmug_default_size = "medium"; // override with <smugmug size="ti|th|s|l|o">
// these are allowable values for the various parameters. There should be *no repetition* of allowable values here, otherwise the parameter reading won't work correctly. // If there must be repetition here, then specify the parameter values via attributes in the tag, and you should have no problem $valid_smugmug_idtypes = array("album"); $valid_smugmug_types = array("thumbnail", "thumb", "frame"); $valid_smugmug_locations = array("right", "left", "center", "none"); $valid_smugmug_sizes = array("tiny", "thumb", "small", "medium", "large", "original");
// keys in these arrays should point to values that exist in the corresponding arrays above. $other_spellings_of_types = array ("thumbnail" => "thumb", "thumnail" => "thumb"); $other_spellings_of_sizes = array ("ti" => "tiny", "th" => "thumb", "s" => "small", "m" => "medium", "med" => "medium", "l" => "large", "o" => "original", "orig" => "original");
// allow the other spellings of the arrays to be found in the parameters. There should be a line here for each of the "other_spellings" arrays above $valid_smugmug_sizes = array_merge($valid_smugmug_sizes, array_keys($other_spellings_of_sizes)); $valid_smugmug_types = array_merge($valid_smugmug_types, array_keys($other_spellings_of_types));
/***************************************************************************************************************************************************************
The arrays above define the parameters that we'll be looking for. You should be able to tweak a lot without changing anything below this line.
Now we start looking for parameters sent by the wiki
****************************************************************************************************************************************************************/
// if they exist, the parameters sent as attributes override the parameters sent <smugmug>between the tags</smugmug>, so grab them first. // Then we must be sure not to overwrite existing values for these variables. $_smugmug_id = $params['id']; $_smugmug_type = $params['type']; $_smugmug_idtype = $params['idtype']; $_smugmug_location = $params['location']; $_smugmug_size = $params['size']; $_smugmug_caption = $params['caption'];
# split the text sent <smugmug>between the tags</smugmug> on | $inputs = explode("|", $input);
# process the inputs, basically comparing them to the arrays set above to determine for which parameter their value should be used. foreach($inputs as $value) { if ($_smugmug_id == "" && ctype_digit($value)) { $_smugmug_id = $value; /* if the value is all digits, it must be the id */ } elseif ($_smugmug_type == "" && in_array(strtolower($value), $valid_smugmug_types)) { $_smugmug_type = strtolower($value); } elseif ($_smugmug_idtype == "" && in_array(strtolower($value), $valid_smugmug_idtypes)) {$_smugmug_idtype = strtolower($value); } elseif ($_smugmug_location == "" && in_array(strtolower($value), $valid_smugmug_locations)) { $_smugmug_location = strtolower($value); } elseif ($_smugmug_size == "" && in_array(strtolower($value), $valid_smugmug_sizes)) { $_smugmug_size = strtolower($value); } elseif ($_smugmug_caption == "") { $_smugmug_caption = $value; } // everything else is caption else { $_smugmug_caption .= "|".$value; } // this allows caption to have | chars in it }
// finally, if none of the above worked, fill in the defaults if($_smugmug_type == "") { $_smugmug_type = $smugmug_default_type; } if($_smugmug_idtype == "") { $_smugmug_idtype = $smugmug_default_idtype; } if($_smugmug_location == "") { $_smugmug_location = $smugmug_default_location; } if($_smugmug_size == "") { $_smugmug_size = $smugmug_default_size; }
// We have all the parameters as sent by the user (actually they have been lowercased). // Now convert them to allowable values according to the other_spellings arrays if(is_array($other_spellings_of_types) && isset($other_spellings_of_types[$_smugmug_type])) { $_smugmug_type = $other_spellings_of_types[$_smugmug_type]; } if(is_array($other_spellings_of_idtypes) && isset($other_spellings_of_idtypes[$_smugmug_idtype])) { $_smugmug_idtype = $other_spellings_of_idtypes[$_smugmug_idtype]; } if(is_array($other_spellings_of_locations) && isset($other_spellings_of_locations[$_smugmug_location])) { $_smugmug_location = $other_spellings_of_locations[$_smugmug_location]; } if(is_array($other_spellings_of_sizes) && isset($other_spellings_of_sizes[$_smugmug_size])) { $_smugmug_size = $other_spellings_of_sizes[$_smugmug_size]; }
# Now make sure we have the one required parameter: id if (!isset($_smugmug_id)) { $output = "Smugmug Error ( No ID ): Enter at least a PhotoID"; return $output; }
$f = new phpSmug($smugmug_api_key, "robnugen.com/wiki/extensions/Smugmug.php v0.03wiki");
$f->login_anonymously();
$image_info = $f->images_getInfo($_smugmug_id);
// It makes no sense to have a default caption for all pictures, so the default will be whatever is set on smugmug servers if ($_smugmug_caption == "") { $_smugmug_caption = $image_info['Caption']; } if ($_smugmug_caption == "") { $_smugmug_caption = $image_info['Keywords']; } // use the keywords if there was no caption
/*********************************************************************************************************************************************
From here, we are just basically mimicking the code in ../includes/Linker.php
However, I have skipped the Right To Left languages, and skipped vertical alignment options
*********************************************************************************************************************************************/ // Clean up our output strings $prefix = $postfix = $s = ;
if ($_smugmug_location == 'center') {
$prefix = '
';
$_smugmug_location = 'none'; // This 'none' value will be used in subsequent div class (same as Linker.php) }
if ($_smugmug_idtype == "image") {
// best I can tell, this matches Linker.php except for valign parameters if ($_smugmug_type == "none") { $size_url_key = ucfirst($_smugmug_size) . "URL"; // for example "LargeURL" $s = "<a href=\"{$image_info['AlbumURL']}\" class=\"image\" title=\"{$_smugmug_caption}\"><img src=\"{$image_info[$size_url_key]}\" alt=\"{$_smugmug_caption}\"></a>"; if ($_smugmug_location != ) {
$s = "
";
} return str_replace("\n", ' ',$prefix.$s.$postfix); } else { // basically we are in $_smugmug_type = "frame" or "thumb" if($_smugmug_location == "") { $_smugmug_location = 'right';} // This 'right' value will be used in subsequent div classes (same as Linker.php)
/********************************************************************************************************************/ /********************************************************************************************************************/ /********************************************************************************************************************/ /*********** WARNING!! THIS HARDCODED VALUE SHOULD BE THE WIDTH OF THE THUMBNAIL + 2 ****************************/ /********************************************************************************************************************/ /********************************************************************************************************************/ /********************************************************************************************************************/ $outerWidth = 402; /********************************************************************************************************************/ /********************************************************************************************************************/ /********************************************************************************************************************/ /*********** WARNING!! IN OTHER WORDS, THIS HARDCODED VALUE SHOULD NOT BE HARDCODED! ****************************/ /********************************************************************************************************************/ /********************************************************************************************************************/ /****************************************************************************** (but it works oh so nicely for now) */ /********************************************************************************************************************/
$s = "
// this basically matches Linker.php. Check that code for documentation. Oh wait! There is none! $size_url_key = ucfirst($_smugmug_size) . "URL"; $s .= "<a href=\"{$image_info['AlbumURL']}\" class=\"image\" title=\"{$_smugmug_caption}\">" . "<img src=\"{$image_info[$size_url_key]}\" alt=\"{$_smugmug_caption}\" class=\"thumbimage\"></a>";
if ($_smugmug_type == 'frame') { // type="frame" just gets a frame $zoomicon=""; } else { // type="thumb" gets a cute little zoom icon global $wgStylePath;
$zoomicon = ''<a href="' . $image_info[$size_url_key] . '" class="internal" title="Enlarge">'.
'<img src="'.$wgStylePath.'/common/images/magnify-clip.png" width="15" height="11" alt="" /></a>}
$s .= '"; // closes the divs for class="thumbcaption", class="thumb t..", and class="thumbinner"
return $s; } // close $_smugmug_type != "" } // Matches if ($_smugmug_idtype == "image")
error_reporting(E_ALL);
// output=php means that the request will return serialized PHP $request = 'http://api.search.yahoo.com/ImageSearchService/V1/imageSearch?appid=YahooDemo&query=Madonna&results=4&output=php'; # use cURL if we can't use file_get_contents
if(ini_get('allow_url_fopen')) {
$response = file_get_contents($request);
} else { $ch = curl_init(); $timeout = 5; // set to zero for no timeout curl_setopt ($ch, CURLOPT_URL, $request); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $response = curl_exec($ch); curl_close($ch); }
if ($response === false) {
return "request failed";
}
$phpobj = unserialize($response);
return print_r($phpobj,true); // print_r prints human readable format. true means return, don't echo
}
</source>