MediaWiki扩展:指定图片链接
在默认情况下,mediawiki系统中[]形式的图片会指向其自身的相关页面(即大图及图片附注页面),而这在很多时候不方便,无法通过图片引导读者的进一步阅读。使用本扩展,可以指定图片的链接,使其指向另外的位置。
本扩展适用于Mediawiki 1.5.x、1.6.x版本。
参数
参数类型说明举例
wikipage必备指定链接目标的条目名称wikipage=首页
tooltip可选当鼠标光标移到图片上时,弹出的气泡中提示文字tooltip=Sampletext
img_src必备指定要显示的图片名称img_src=Image:Sample.gif
img_width可选你可以任意设置图片的显示宽度
不使用本参数时,图片以实际宽度显示;反之以指定尺寸显示。
可使用##px或'10%'等方式
img_width=10px
img_height可选指定图片的显示高度
用法同上
img_height=10px
img_alt可选当用户的浏览器禁止图片时,显示的替代文本img_alt=Text
用法举例<linkedimage>
wikipage=Main_Page
tooltip=Main Page
img_src=Image:Sample.gif
img_width=10%
img_height=10px
img_alt=Sampletext
</linkedimage>
[*]复制LinkedImages.php到extensions文件夹
[*]在localsettings.php中增加include("extensions/LinkedImages.php");(建议放在文件末尾)
LinkedImages.php代码版本号: 0.2
<?php
/**
* This file contains the main include file for the LinkedImage extension of
* MediaWiki.
*
* Usage: require_once("path/to/LinkedImage.php"); in LocalSettings.php
*
* This extension requires MediaWiki 1.5 or higher.
*
* @author Alexander Kraus <kraus.alexander@gmail.com>
* @copyright Public domain
* @license Public domain
* @package MediaWikiExtensions
* @version 0.2
*/
/**
* Register the LinkedImage extension with MediaWiki
*/
$wgExtensionFunctions[] = 'wfLinkedImage';
$wgExtensionCredits['parserhook'][] = array(
'name' => 'LinkedImage',
'author' => 'Alexander Kraus',
'url' => 'http://meta.wikimedia.org/wiki/LinkedImage',
);
//renderLinkedImage();
/**
* Sets the tag that this extension looks for and the function by which it
* operates
*/
function wfLinkedImage()
{
global $wgParser, $wgMessageCache;
$wgMessageCache->addMessages( array(
'linkedimage_nowikipage'=> 'LinkedImage: No link target specified! e.g. \'wikipage=Main_page\'',
'linkedimage_noimg' => 'LinkedImage: No image specified! e.g. \'img_src=Image:LinkedImage.png\''
)
);
$wgParser->setHook('linkedimage', 'renderLinkedImage');
}
function renderLinkedImage($input)
{
$linkedimage=new LinkedImage();
$linkedimage->getBoxOption($linkedimage->wikipage, $input,'wikipage');
$linkedimage->getBoxOption($linkedimage->tooltip, $input,'tooltip');
$linkedimage->getBoxOption($linkedimage->img_src, $input,'img_src');
$linkedimage->getBoxOption($linkedimage->img_height, $input,'img_height');
$linkedimage->getBoxOption($linkedimage->img_width,$input,'img_width');
$linkedimage->getBoxOption($linkedimage->img_alt, $input,'img_alt');
$linkedimage->getBoxOption($linkedimage->img_border, $input,'img_border');
// render and return linked image ...
return $linkedimage->render();
}
class LinkedImage {
var $wikipage;
var $tooltip;
var $img_src;
var $img_alt;
var $img_height;
var $img_width;
var $img_border;
public function LinkedImage() {
$this->setWikipage('');
$this->setTooltip('');
$this->setImg_src('');
$this->setImg_alt('');
$this->setImg_height('');
$this->setImg_width('');
$this->setImg_border('');
}
private function setWikipage($value){ $this->wikipage=$value; }
private function getWikipage(){ return $this->wikipage; }
private function setTooltip($value){ $this->tooltip=$value; }
private function getTooltip(){ return $this->tooltip; }
private function getTooltipHTML(){
if ($this->tooltip != '') {
return 'title="'.$this->getTooltip().'" ';
} else {
return '';
}
}
private function setImg_src($value){ $this->img_src=$value; }
private function getImg_src(){ return $this->img_src; }
private function getImg_srcHTML($getImageUrl=false){
if ($this->img_src != '') {
if ($getImageUrl) {
return 'src="'.$this->image->getUrl().'" ';
} else {
return 'src="'.$this->img_src.'" ';
}
} else {
return '';
}
}
private function setImg_alt($value){ $this->img_alt=$value; }
private function getImg_alt(){ return $this->img_alt; }
private function getImg_altHTML(){
if ($this->img_alt != '') {
return 'alt="'.$this->img_alt.'" ';
} else {
return '';
}
}
private function setImg_height($value){ $this->img_heigth=$value;}
private function getImg_height(){ return $this->img_height;}
private function getImg_heightHTML(){
if ($this->img_height != '') {
return 'height="'.$this->img_height.'" ';
} else {
return 'height="'.$this->image->getHeight().'" ';
}
}
private function setImg_width($value){ $this->img_width=$value; }
private function getImg_width(){ return $this->img_width; }
private function getImg_widthHTML(){
if ($this->img_width != '') {
return 'width="'.$this->img_width.'" ';
} else {
return 'width="'.$this->image->getWidth().'" ';
}
}
private function setImg_border($value){ $this->img_border=$value; }
private function getImg_border(){ return $this->img_border; }
private function getImg_borderHTML(){
if ($this->img_border != '') {
return 'border="'.$this->img_border.'" ';
} else {
return $this->img_border;
}
}
public function render() {
global $wgArticlePath;
// check param wikipage existence
if ($this->getWikipage() == '') {
return htmlspecialchars( wfMsg( 'linkedimage_nowikipage' ) );
}
// check param img_src existence
if ($this->getImg_src() == '') {
return htmlspecialchars( wfMsg( 'linkedimage_noimg' ) );
}
// create mediawiki image object ...
$this->image = new Image( Title::newFromText( $this->img_src ) );
// return link ...
return '<a href="'.str_replace( "$1", $this->wikipage, $wgArticlePath ).'" '.$this->getTooltipHTML().'>
<img '. $this->getImg_srcHTML(true) . $this->getImg_altHTML() . $this->getImg_widthHTML() . $this->getImg_heightHTML() . $this->getImg_borderHTML() . '></a>';
} // End render()
public function getBoxOption(&$value,&$input,$name,$isNumber=false) {
if(preg_match("/^\s*$name\s*=\s*(.*)/mi",$input,$matches)) {
if($isNumber) {
$value=intval($matches);
} else {
$value=htmlspecialchars($matches);
}
}
} // End getBoxOption()
}
?>
非常感谢~~~~~~~~~~~
static/image/common/sigline.gif
http://img2081.poco.cn/mypoco/myphoto/20110719/00/6035253720110719001650069.png
页:
[1]