跳转到内容

MediaWiki 开发人员手册/添加按钮

来自维基教科书,开放世界中的开放书籍
if( !defined( 'MEDIAWIKI' ) ) {
  die( "This file is part of MediaWiki and is not a valid entry point\n" );
  }
$myRedirectPng = "extensions/myRedirect/myRedirect.png";

$wgHooks['EditPage::showEditForm:initial'][] = 'myRedirectButton';
$wgExtensionCredits['myRedirectButton'][]= array(
  'name'         => 'my Redirect Button Extension', 
  'version'      => '1.0.0', 
  'author'       => 'Your Name', 
  'url'          => 'http://www.your.com/myredirectbutton/', 
  'description'  => 'This extension is for demonstration only.');

// Add a button to the internal editor
function myRedirectButton ($editPage) { 
  global $wgOut, $wgScriptPath, $myRedirectPng;
  
  // Insert javascript script that hooks up to create button.
  $wgOut->addScript("<script type=\"text/javascript\">\n".
       "function myAddButton(){\n".
       "  addButton('$myRedirectPng','Redirect','#REDIRECT [[',']]','Insert text');".
       "  }\n".
       "addOnloadHook(myAddButton);\n".
       "</script>");
        return true;
  }

安装

  • 创建一个目录 mediawiki/extensions/myRedirect
  • 将上面的脚本保存为 myRedirect.php
  • 将以下图片保存到目录中作为 myRedirect.png。 .
  • 将以下行附加到 LocalSettings.php
require_once("$IP/extensions/myRedirect/myRedirect.php");


添加调用自定义 JavaScript 函数的按钮

[编辑 | 编辑源代码]
<?php

// to install, upload this file as /extensions/myEditbarButton/myEditbarButton.php
// then, copy this button image as /extensions/myEditbarButton/myButtonCard.png
//
// then add the following line to end of LocalSettings.php
//
//       require_once("$IP/extensions/myEditbarButton/myEditbarButton.php");
//
// This is a modification of 
// https://wikibooks.cn/wiki/MediaWiki_Developer%27s_Handbook/Add_Button
//
// Sorry that extension can only add a new button before the other buttons
// of the edit bar, instead of after them. I am also not sure if this will work
// on other installations of mediawiki ( mine is MediaWiki: 1.16.5, PHP: 5.2.17
// MySQL: 5.1.69 )

if( !defined( 'MEDIAWIKI' ) ) {
  die( "This file is part of MediaWiki and is not a valid entry point\n" );
  }
$myButtonPng = "extensions/myEditbarButton/myButtonCard.png";

$wgHooks['EditPage::showEditForm:initial'][] = 'myEditbarButton';

// for $wgExtensionsCredits[$type][]
//       $type must be api, media, parserhook, skin
//                     specialpage, variable, other

$wgExtensionCredits['other'][] = array(
        'name'         => 'my Editbar Button', 
        'version'      => '1.0.0', 
        'author'       => 'Your Name', 
        'url'          => 'http://www.your.com/myredirectbutton/', 
        'description'  => 'Adds a new button to the old edit bar that calls a custom javascript function.'
        );

// Add a button to the internal editor
function myEditbarButton ($editPage) { 
        global $wgOut, $wgScriptPath, $myButtonPng;
  
        // Insert javascript script that hooks up to create button.
        $wgOut->addScript("<script type=\"text/javascript\">\n".
                "function myAddButton(){ \n".
                        "var toolbar = document.getElementById( 'toolbar' ); \n".
                        "var image = document.createElement( 'img' ); \n".
                        "image.className = 'mw-toolbar-editbutton'; \n".
                        "image.src = '$myButtonPng'; \n".
                        "image.border = 0; \n".
                        "image.alt = 'speedTip'; \n".
                        "image.title = 'speedTip'; \n".
                        "image.style.cursor = 'pointer'; \n".
                        "image.onclick = function() { \n".
                                "window.alert('HelloWorld'); \n".
                        "} \n".
                        "toolbar.appendChild( image ); \n".
                "} \n".

                "addOnloadHook(myAddButton); \n".
        "</script>");
        return true;
}

?>

http://commons.wikimedia.org/wiki/Category:Button_link

华夏公益教科书