美国主机Lunarpages

Mediawiki中文技术论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6046|回复: 1

MediaWiki扩展:指定图片链接

[复制链接]
发表于 2010-12-27 16:42:59 | 显示全部楼层 |阅读模式
HostEase主机优惠
在默认情况下,mediawiki系统中[[Image:XXX]]形式的图片会指向其自身的相关页面(即大图及图片附注页面),而这在很多时候不方便,无法通过图片引导读者的进一步阅读。

使用本扩展,可以指定图片的链接,使其指向另外的位置。

本扩展适用于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
  1. <?php

  2. /**
  3. * This file contains the main include file for the LinkedImage extension of
  4. * MediaWiki.
  5. *
  6. * Usage: require_once("path/to/LinkedImage.php"); in LocalSettings.php
  7. *
  8. * This extension requires MediaWiki 1.5 or higher.
  9. *
  10. * @author Alexander Kraus <kraus.alexander@gmail.com>
  11. * @copyright Public domain
  12. * @license Public domain
  13. * @package MediaWikiExtensions
  14. * @version 0.2
  15. */

  16. /**
  17. * Register the LinkedImage extension with MediaWiki
  18. */
  19. $wgExtensionFunctions[] = 'wfLinkedImage';
  20. $wgExtensionCredits['parserhook'][] = array(
  21. 'name' => 'LinkedImage',
  22. 'author' => 'Alexander Kraus',
  23. 'url' => 'http://meta.wikimedia.org/wiki/LinkedImage',
  24. );

  25. //renderLinkedImage();

  26. /**
  27. * Sets the tag that this extension looks for and the function by which it
  28. * operates
  29. */
  30. function wfLinkedImage()
  31. {
  32.     global $wgParser, $wgMessageCache;
  33.    
  34.     $wgMessageCache->addMessages( array(
  35.                 'linkedimage_nowikipage'=> 'LinkedImage: No link target specified! e.g. \'wikipage=Main_page\'',
  36.                 'linkedimage_noimg'         => 'LinkedImage: No image specified! e.g. \'img_src=Image:LinkedImage.png\''
  37.                                         )
  38.         );
  39.                                   
  40.     $wgParser->setHook('linkedimage', 'renderLinkedImage');
  41. }

  42. function renderLinkedImage($input)
  43. {
  44.     $linkedimage=new LinkedImage();
  45.        
  46.         $linkedimage->getBoxOption($linkedimage->wikipage,   $input,'wikipage');
  47.     $linkedimage->getBoxOption($linkedimage->tooltip,    $input,'tooltip');
  48.     $linkedimage->getBoxOption($linkedimage->img_src,    $input,'img_src');
  49.     $linkedimage->getBoxOption($linkedimage->img_height, $input,'img_height');
  50.     $linkedimage->getBoxOption($linkedimage->img_width,  $input,'img_width');
  51.     $linkedimage->getBoxOption($linkedimage->img_alt,    $input,'img_alt');
  52.     $linkedimage->getBoxOption($linkedimage->img_border, $input,'img_border');
  53.    
  54.     // render and return linked image ...
  55.     return $linkedimage->render();
  56. }

  57. class LinkedImage {
  58.    var $wikipage;
  59.    var $tooltip;
  60.    var $img_src;
  61.    var $img_alt;
  62.    var $img_height;
  63.    var $img_width;
  64.    var $img_border;
  65.    
  66.    public function LinkedImage() {
  67.     $this->setWikipage('');
  68.     $this->setTooltip('');
  69.     $this->setImg_src('');
  70.     $this->setImg_alt('');
  71.     $this->setImg_height('');
  72.     $this->setImg_width('');
  73.     $this->setImg_border('');
  74.    }
  75.    
  76.    private function setWikipage($value){         $this->wikipage=$value; }
  77.    private function getWikipage(){               return $this->wikipage; }
  78.    
  79.    private function setTooltip($value){          $this->tooltip=$value; }
  80.    private function getTooltip(){                         return $this->tooltip; }
  81.    private function getTooltipHTML(){
  82.                    if ($this->tooltip != '') {
  83.                         return 'title="'.$this->getTooltip().'" ';
  84.                    } else {
  85.                            return '';
  86.                    }
  87.    }
  88.    
  89.    private function setImg_src($value){          $this->img_src=$value; }
  90.    private function getImg_src(){                         return $this->img_src; }
  91.    private function getImg_srcHTML($getImageUrl=false){
  92.                    if ($this->img_src != '') {
  93.                            if ($getImageUrl) {
  94.                                 return 'src="'.$this->image->getUrl().'" ';
  95.                            } else {
  96.                                    return 'src="'.$this->img_src.'" ';
  97.                            }
  98.                    } else {
  99.                            return '';
  100.                    }
  101.    }
  102.    
  103.    
  104.    private function setImg_alt($value){          $this->img_alt=$value; }
  105.    private function getImg_alt(){                         return $this->img_alt; }
  106.    private function getImg_altHTML(){
  107.                    if ($this->img_alt != '') {
  108.                            return 'alt="'.$this->img_alt.'" ';
  109.                    } else {
  110.                            return '';
  111.                    }
  112.    }
  113.    
  114.    private function setImg_height($value){         $this->img_heigth=$value;}
  115.    private function getImg_height(){                return $this->img_height;}
  116.    private function getImg_heightHTML(){
  117.                    if ($this->img_height != '') {
  118.                            return 'height="'.$this->img_height.'" ';
  119.                    } else {
  120.                            return 'height="'.$this->image->getHeight().'" ';
  121.                    }
  122.    }
  123.    
  124.    private function setImg_width($value){        $this->img_width=$value; }
  125.    private function getImg_width(){                        return $this->img_width; }
  126.    private function getImg_widthHTML(){
  127.                    if ($this->img_width != '') {
  128.                            return 'width="'.$this->img_width.'" ';
  129.                    } else {
  130.                            return 'width="'.$this->image->getWidth().'" ';
  131.                    }
  132.    }
  133.       
  134.    private function setImg_border($value){        $this->img_border=$value; }
  135.    private function getImg_border(){                return $this->img_border; }
  136.    private function getImg_borderHTML(){
  137.                    if ($this->img_border != '') {
  138.                            return 'border="'.$this->img_border.'" ';
  139.                    } else {
  140.                            return $this->img_border;
  141.                    }
  142.    }

  143.    public function render() {
  144.                 global $wgArticlePath;
  145.        
  146.                 // check param wikipage existence
  147.                 if ($this->getWikipage() == '') {
  148.                         return htmlspecialchars( wfMsg( 'linkedimage_nowikipage' ) );
  149.                 }

  150.                 // check param img_src existence
  151.                 if ($this->getImg_src() == '') {
  152.                         return htmlspecialchars( wfMsg( 'linkedimage_noimg' ) );
  153.                 }
  154.         
  155.         // create mediawiki image object ...
  156.         $this->image = new Image( Title::newFromText( $this->img_src ) );
  157.                
  158.                 // return link ...
  159.                 return '<a href="'.str_replace( "$1", $this->wikipage, $wgArticlePath ).'" '.$this->getTooltipHTML().'>
  160. <img '. $this->getImg_srcHTML(true) . $this->getImg_altHTML() . $this->getImg_widthHTML() . $this->getImg_heightHTML() . $this->getImg_borderHTML() . '></a>';
  161.           
  162.    } // End render()
  163.    
  164.    public function getBoxOption(&$value,&$input,$name,$isNumber=false) {
  165.       if(preg_match("/^\s*$name\s*=\s*(.*)/mi",$input,$matches)) {
  166.                 if($isNumber) {
  167.                         $value=intval($matches[1]);
  168.                 } else {
  169.                         $value=htmlspecialchars($matches[1]);
  170.                 }
  171.           }
  172.    } // End getBoxOption()
  173.    
  174. }

  175. ?>
复制代码



回复

使用道具 举报

发表于 2011-8-25 09:47:28 | 显示全部楼层
RAKsmart美国服务器
非常感谢~~~~~~~~~~~













回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

美国主机评测网站

Archiver|手机版|小黑屋|Mediawiki中文技术论坛

GMT+8, 2024-4-25 08:44 , Processed in 0.048137 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.

快速回复 返回顶部 返回列表