如何将 PHP 脚本从 MediaWiki 更改为 Wordpress 插件?
How do I change PHP Scripts from MediaWiki to Wordpress Plugin?
我正在做一个项目,我有 MediaWiki PHP 脚本 将出版物信息从数据库导入 出版物页面.
我需要将此脚本转换为 Wordpress 插件,但我真的不知道最好的方法。我现在很迷茫,我尝试做 Tutorial: Writing a simple WordPress plugin from scratch 但我没有成功,我仍然没有这个工作。
原始 MediaWiki 代码
在这里你会看到我原来的 MediaWiki 代码:
<?php
# the function registered by the extension gets the text between the
# tags as input and can transform it into arbitrary HTML code.
# Note: The output is not interpreted as WikiText but directly
# included in the HTML output. So Wiki markup is not supported.
# To activate the extension, include it from your LocalSettings.php
# with: include("extensions/YourExtensionName.php");
$wgExtensionFunctions[] = "wfExampleExtension";
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//
// Register the extension with the WikiText parser.
// The first parameter is the name of the new tag. In this case it defines
// the tag:
// <server-id> ... </server-id>
// The second parameter is the callback function for processing the text
// between the tags.
//
function wfExampleExtension() {
global $wgParser; // MediaWiki global variable
$wgParser->setHook("server-id", "renderSERVERID");
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//
// The callback function for converting the input text to HTML output.
// The function registered by the extension gets the text between the
// tags as $input and transforms it into arbitrary HTML code.
// Note: the output is not interpreted as WikiText but directly included in
// the HTML output. So Wiki markup is not supported.
//
// To activate the extension, include it from your LocalSettings.php
// with: include("extensions/YourExtensionName.php");
//
// $argv is an array containing any arguments passed to the extension like:
// <server-id what="foo" bar>..
//
// According to the metawiki, this works in MediaWiki 1.5.5.
// <server-id what="person" id="62">This text is not actually used</server-id>
//
// Personal information:
// <server-id what='person' id='62'></server-id>
//
// Information for a group:
// <server-id what='publications' cc='IP02'></server-id>
//
function renderSERVERID($input, $argv) {
// connect to the database
$idDBLink = odbc_connect('SERVER ID', 'some_db', 'some_db_pw');
if (!$idDBLink) { exit("Connection to database failed! Please contact root@server-id.org."); }
$html = "";
if ($argv['what'] == 'person') {
$id = split(",", trim($argv["id"]));
if ($id != '') {
// information about someone:
// 1. personal contacts and summary
// 2. publications by person
// 3. advisory work by person
//
$html .= personById($idDBLink, $id[0]);
$internalIds = authorIdByNumber($idDBLink, $id); // use all Ids
$html .= pubsById($idDBLink, $internalIds);
$html .= advisingById($idDBLink, $internalIds);
}
}
else if ($argv['what'] == 'advising') {
$id = split(",", trim($argv["id"]));
if ($id != '') {
$internalIds = authorIdByNumber($idDBLink, $id); // use all Ids
$html .= iconv('latin1', 'UTF-8', advisingById($idDBLink, $internalIds));
}
}
else if ($argv['what'] == 'publications') {
// information about some "centro de custo":
// 1. currently, only a list of publications
//
$cc = trim($argv["cc"]);
$id = trim($argv["id"]);
if ($cc != '') {
$html .= iconv('latin1', 'UTF-8', pubsByCC($idDBLink, $cc));
}
else if ($id != '') {
$html .= iconv('latin1', 'UTF-8', pubsById($idDBLink, authorIdByNumber($idDBLink, array($id))));
}
}
/*else if ($argv['what'] == 'publications') {
// information about some "centro de custo":
// 1. currently, only a list of publications
//
$cc = trim($argv["cc"]);
if ($cc != '') {
$html .= pubsByCC($idDBLink, $cc);
}
}*/
else if ($argv['what'] == 'calls') {
// information about some "centro de custo":
// 1. currently, only a list of publications
//
$cc = trim($argv["cc"]);
$showClosed = isset($argv['showclosed']) ? trim($argv['showclosed']) : "";
if ($cc != '') {
$html .= iconv('latin1', 'UTF-8', callsByCC($idDBLink, $cc, $showClosed == "yes"));
}
}
else {
// oops! no text...
}
odbc_close($idDBLink);
return $html;
}
?>
我的 WordPress 试用版
在这里你会看到我尝试使用 WordPress 代码:
<?php
// ==================================================
// WordPress Plugin
// ==================================================
/*
Plugin Name: Publications Importer
Plugin URI: http://someperson.me/downloads/publications-importer
Description: Integrates the Publications Importer plugin into your WordPress install.
Version: 0.0.1
Author: Someone
Author URI: http://someperson.me/
*/
require_once 'server-id-config.php';
require_once 'server-id-util.php';
require_once 'server-id-people.php';
require_once 'server-id-pubs.php';
require_once 'server-id-advising.php';
defined( 'ABSPATH' ) or die( 'Plugin file cannot be accessed directly.' );
if ( ! class_exists( 'Publication' ) ) {
class Publication
{
/**
* Tag identifier used by file includes and selector attributes.
* @var string
*/
protected $tag = 'publications-importer';
/**
* User friendly name used to identify the plugin.
* @var string
*/
protected $name = 'Publications Importer';
/**
* Current version of the plugin.
* @var string
*/
protected $version = '0.0.1';
public function __construct()
{
add_shortcode( $this->tag, array( &$this, 'shortcode' ) );
}
public function shortcode( $atts, $content = null )
{
extract( shortcode_atts( array(
'what' => false,
'cc' => false
), $atts ) );
$styles = array();
if ( is_numeric( $what ) ) {
$styles[] = esc_attr( 'what: ' . $what );
}
$classes = array(
$this->tag
);
if ( !empty( $cc ) ) {
$classes[] = esc_attr( $cc );
}
ob_start();
?><pre cc="<?php esc_attr_e( implode( ' ', $classes ) ); ?>"<?php
echo ( count( $styles ) > 0 ? ' style="' . implode( ' ', $styles ) . '"' : '' );
?>><p><?php echo $content; ?></p></pre><?php
return ob_get_clean();
}
}
new Publication;
}
// ==================================================
// END WordPress Plugin
// ==================================================
# the function registered by the extension gets the text between the
# tags as input and can transform it into arbitrary HTML code.
# Note: The output is not interpreted as WikiText but directly
# included in the HTML output. So Wiki markup is not supported.
# To activate the extension, include it from your LocalSettings.php
# with: include("extensions/YourExtensionName.php");
$wgExtensionFunctions[] = "wfExampleExtension";
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//
// Register the extension with the WikiText parser.
// The first parameter is the name of the new tag. In this case it defines
// the tag:
// <server-id> ... </server-id>
// The second parameter is the callback function for processing the text
// between the tags.
//
function wfExampleExtension() {
global $wgParser; // MediaWiki global variable
$wgParser->setHook("server-id", "renderSERVERID");
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//
// The callback function for converting the input text to HTML output.
// The function registered by the extension gets the text between the
// tags as $input and transforms it into arbitrary HTML code.
// Note: the output is not interpreted as WikiText but directly included in
// the HTML output. So Wiki markup is not supported.
//
// To activate the extension, include it from your LocalSettings.php
// with: include("extensions/YourExtensionName.php");
//
// $argv is an array containing any arguments passed to the extension like:
// <server-id what="foo" bar>..
//
// According to the metawiki, this works in MediaWiki 1.5.5.
// <server-id what="person" id="62">This text is not actually used</server-id>
//
// Personal information:
// <server-id what='person' id='62'></server-id>
//
// Information for a group:
// <server-id what='publications' cc='IP02'></server-id>
//
function renderSERVERID($input, $argv) {
// connect to the database
$idDBLink = odbc_connect('SERVER ID', 'some_db', 'some_db_pw');
if (!$idDBLink) { exit("Connection to database failed! Please contact root@server-id.org."); }
$html = "";
if ($argv['what'] == 'person') {
$id = split(",", trim($argv["id"]));
if ($id != '') {
// information about someone:
// 1. personal contacts and summary
// 2. publications by person
// 3. advisory work by person
//
$html .= personById($idDBLink, $id[0]);
$internalIds = authorIdByNumber($idDBLink, $id); // use all Ids
$html .= pubsById($idDBLink, $internalIds);
$html .= advisingById($idDBLink, $internalIds);
}
}
else if ($argv['what'] == 'advising') {
$id = split(",", trim($argv["id"]));
if ($id != '') {
$internalIds = authorIdByNumber($idDBLink, $id); // use all Ids
$html .= iconv('latin1', 'UTF-8', advisingById($idDBLink, $internalIds));
}
}
else if ($argv['what'] == 'publications') {
// information about some "centro de custo":
// 1. currently, only a list of publications
//
$cc = trim($argv["cc"]);
$id = trim($argv["id"]);
if ($cc != '') {
$html .= iconv('latin1', 'UTF-8', pubsByCC($idDBLink, $cc));
}
else if ($id != '') {
$html .= iconv('latin1', 'UTF-8', pubsById($idDBLink, authorIdByNumber($idDBLink, array($id))));
}
}
/*else if ($argv['what'] == 'publications') {
// information about some "centro de custo":
// 1. currently, only a list of publications
//
$cc = trim($argv["cc"]);
if ($cc != '') {
$html .= pubsByCC($idDBLink, $cc);
}
}*/
else if ($argv['what'] == 'calls') {
// information about some "centro de custo":
// 1. currently, only a list of publications
//
$cc = trim($argv["cc"]);
$showClosed = isset($argv['showclosed']) ? trim($argv['showclosed']) : "";
if ($cc != '') {
$html .= iconv('latin1', 'UTF-8', callsByCC($idDBLink, $cc, $showClosed == "yes"));
}
}
else {
// oops! no text...
}
odbc_close($idDBLink);
return $html;
}
?>
所以我真正需要知道的是:
1) 不应该 WordPress 能够解释 MediaWiki 标签(例如:<server-id what='publications' cc='IP02'></server-id>
)并执行此操作自动?
2) 在哪里可以找到有关此类迁移的更多文档?
3) 我是不是做错了?
WordPress 和 MediaWiki 是独立的应用程序,不能指望为一个应用程序编写的插件可以直接移植到另一个应用程序。如果你幸运的话,一些代码可能是可重用的,但它不会像剪切和粘贴那样简单。这两个应用程序有不同的做事方式。
1) 不,WordPress 将无法理解此类标签。 WordPress 可以通过附加插件理解 MediaWiki 风格的降价标签,但我不认识你突出显示的标签示例。
2) 我认为您当前的方法是合理的,您需要了解 MediaWiki 代码的作用并在 WordPress 插件中重新创建它。除了花一些时间来掌握 WP 插件之外,我怀疑是否有捷径可走。如果您喜欢轻量级编码和编写插件,那么这段时间是值得的。能够自定义 WordPress 非常有用。
3) 除了自己重新编码之外,另一种选择是查看是否有 WordPress 插件可以满足您的需求。您的问题没有详细说明您要添加的功能到底是什么。
我已经为 MediaWiki 和 WordPress 编写了插件,我发现使用起来更容易也更愉快。
我正在做一个项目,我有 MediaWiki PHP 脚本 将出版物信息从数据库导入 出版物页面.
我需要将此脚本转换为 Wordpress 插件,但我真的不知道最好的方法。我现在很迷茫,我尝试做 Tutorial: Writing a simple WordPress plugin from scratch 但我没有成功,我仍然没有这个工作。
原始 MediaWiki 代码
在这里你会看到我原来的 MediaWiki 代码:
<?php
# the function registered by the extension gets the text between the
# tags as input and can transform it into arbitrary HTML code.
# Note: The output is not interpreted as WikiText but directly
# included in the HTML output. So Wiki markup is not supported.
# To activate the extension, include it from your LocalSettings.php
# with: include("extensions/YourExtensionName.php");
$wgExtensionFunctions[] = "wfExampleExtension";
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//
// Register the extension with the WikiText parser.
// The first parameter is the name of the new tag. In this case it defines
// the tag:
// <server-id> ... </server-id>
// The second parameter is the callback function for processing the text
// between the tags.
//
function wfExampleExtension() {
global $wgParser; // MediaWiki global variable
$wgParser->setHook("server-id", "renderSERVERID");
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//
// The callback function for converting the input text to HTML output.
// The function registered by the extension gets the text between the
// tags as $input and transforms it into arbitrary HTML code.
// Note: the output is not interpreted as WikiText but directly included in
// the HTML output. So Wiki markup is not supported.
//
// To activate the extension, include it from your LocalSettings.php
// with: include("extensions/YourExtensionName.php");
//
// $argv is an array containing any arguments passed to the extension like:
// <server-id what="foo" bar>..
//
// According to the metawiki, this works in MediaWiki 1.5.5.
// <server-id what="person" id="62">This text is not actually used</server-id>
//
// Personal information:
// <server-id what='person' id='62'></server-id>
//
// Information for a group:
// <server-id what='publications' cc='IP02'></server-id>
//
function renderSERVERID($input, $argv) {
// connect to the database
$idDBLink = odbc_connect('SERVER ID', 'some_db', 'some_db_pw');
if (!$idDBLink) { exit("Connection to database failed! Please contact root@server-id.org."); }
$html = "";
if ($argv['what'] == 'person') {
$id = split(",", trim($argv["id"]));
if ($id != '') {
// information about someone:
// 1. personal contacts and summary
// 2. publications by person
// 3. advisory work by person
//
$html .= personById($idDBLink, $id[0]);
$internalIds = authorIdByNumber($idDBLink, $id); // use all Ids
$html .= pubsById($idDBLink, $internalIds);
$html .= advisingById($idDBLink, $internalIds);
}
}
else if ($argv['what'] == 'advising') {
$id = split(",", trim($argv["id"]));
if ($id != '') {
$internalIds = authorIdByNumber($idDBLink, $id); // use all Ids
$html .= iconv('latin1', 'UTF-8', advisingById($idDBLink, $internalIds));
}
}
else if ($argv['what'] == 'publications') {
// information about some "centro de custo":
// 1. currently, only a list of publications
//
$cc = trim($argv["cc"]);
$id = trim($argv["id"]);
if ($cc != '') {
$html .= iconv('latin1', 'UTF-8', pubsByCC($idDBLink, $cc));
}
else if ($id != '') {
$html .= iconv('latin1', 'UTF-8', pubsById($idDBLink, authorIdByNumber($idDBLink, array($id))));
}
}
/*else if ($argv['what'] == 'publications') {
// information about some "centro de custo":
// 1. currently, only a list of publications
//
$cc = trim($argv["cc"]);
if ($cc != '') {
$html .= pubsByCC($idDBLink, $cc);
}
}*/
else if ($argv['what'] == 'calls') {
// information about some "centro de custo":
// 1. currently, only a list of publications
//
$cc = trim($argv["cc"]);
$showClosed = isset($argv['showclosed']) ? trim($argv['showclosed']) : "";
if ($cc != '') {
$html .= iconv('latin1', 'UTF-8', callsByCC($idDBLink, $cc, $showClosed == "yes"));
}
}
else {
// oops! no text...
}
odbc_close($idDBLink);
return $html;
}
?>
我的 WordPress 试用版
在这里你会看到我尝试使用 WordPress 代码:
<?php
// ==================================================
// WordPress Plugin
// ==================================================
/*
Plugin Name: Publications Importer
Plugin URI: http://someperson.me/downloads/publications-importer
Description: Integrates the Publications Importer plugin into your WordPress install.
Version: 0.0.1
Author: Someone
Author URI: http://someperson.me/
*/
require_once 'server-id-config.php';
require_once 'server-id-util.php';
require_once 'server-id-people.php';
require_once 'server-id-pubs.php';
require_once 'server-id-advising.php';
defined( 'ABSPATH' ) or die( 'Plugin file cannot be accessed directly.' );
if ( ! class_exists( 'Publication' ) ) {
class Publication
{
/**
* Tag identifier used by file includes and selector attributes.
* @var string
*/
protected $tag = 'publications-importer';
/**
* User friendly name used to identify the plugin.
* @var string
*/
protected $name = 'Publications Importer';
/**
* Current version of the plugin.
* @var string
*/
protected $version = '0.0.1';
public function __construct()
{
add_shortcode( $this->tag, array( &$this, 'shortcode' ) );
}
public function shortcode( $atts, $content = null )
{
extract( shortcode_atts( array(
'what' => false,
'cc' => false
), $atts ) );
$styles = array();
if ( is_numeric( $what ) ) {
$styles[] = esc_attr( 'what: ' . $what );
}
$classes = array(
$this->tag
);
if ( !empty( $cc ) ) {
$classes[] = esc_attr( $cc );
}
ob_start();
?><pre cc="<?php esc_attr_e( implode( ' ', $classes ) ); ?>"<?php
echo ( count( $styles ) > 0 ? ' style="' . implode( ' ', $styles ) . '"' : '' );
?>><p><?php echo $content; ?></p></pre><?php
return ob_get_clean();
}
}
new Publication;
}
// ==================================================
// END WordPress Plugin
// ==================================================
# the function registered by the extension gets the text between the
# tags as input and can transform it into arbitrary HTML code.
# Note: The output is not interpreted as WikiText but directly
# included in the HTML output. So Wiki markup is not supported.
# To activate the extension, include it from your LocalSettings.php
# with: include("extensions/YourExtensionName.php");
$wgExtensionFunctions[] = "wfExampleExtension";
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//
// Register the extension with the WikiText parser.
// The first parameter is the name of the new tag. In this case it defines
// the tag:
// <server-id> ... </server-id>
// The second parameter is the callback function for processing the text
// between the tags.
//
function wfExampleExtension() {
global $wgParser; // MediaWiki global variable
$wgParser->setHook("server-id", "renderSERVERID");
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//
// The callback function for converting the input text to HTML output.
// The function registered by the extension gets the text between the
// tags as $input and transforms it into arbitrary HTML code.
// Note: the output is not interpreted as WikiText but directly included in
// the HTML output. So Wiki markup is not supported.
//
// To activate the extension, include it from your LocalSettings.php
// with: include("extensions/YourExtensionName.php");
//
// $argv is an array containing any arguments passed to the extension like:
// <server-id what="foo" bar>..
//
// According to the metawiki, this works in MediaWiki 1.5.5.
// <server-id what="person" id="62">This text is not actually used</server-id>
//
// Personal information:
// <server-id what='person' id='62'></server-id>
//
// Information for a group:
// <server-id what='publications' cc='IP02'></server-id>
//
function renderSERVERID($input, $argv) {
// connect to the database
$idDBLink = odbc_connect('SERVER ID', 'some_db', 'some_db_pw');
if (!$idDBLink) { exit("Connection to database failed! Please contact root@server-id.org."); }
$html = "";
if ($argv['what'] == 'person') {
$id = split(",", trim($argv["id"]));
if ($id != '') {
// information about someone:
// 1. personal contacts and summary
// 2. publications by person
// 3. advisory work by person
//
$html .= personById($idDBLink, $id[0]);
$internalIds = authorIdByNumber($idDBLink, $id); // use all Ids
$html .= pubsById($idDBLink, $internalIds);
$html .= advisingById($idDBLink, $internalIds);
}
}
else if ($argv['what'] == 'advising') {
$id = split(",", trim($argv["id"]));
if ($id != '') {
$internalIds = authorIdByNumber($idDBLink, $id); // use all Ids
$html .= iconv('latin1', 'UTF-8', advisingById($idDBLink, $internalIds));
}
}
else if ($argv['what'] == 'publications') {
// information about some "centro de custo":
// 1. currently, only a list of publications
//
$cc = trim($argv["cc"]);
$id = trim($argv["id"]);
if ($cc != '') {
$html .= iconv('latin1', 'UTF-8', pubsByCC($idDBLink, $cc));
}
else if ($id != '') {
$html .= iconv('latin1', 'UTF-8', pubsById($idDBLink, authorIdByNumber($idDBLink, array($id))));
}
}
/*else if ($argv['what'] == 'publications') {
// information about some "centro de custo":
// 1. currently, only a list of publications
//
$cc = trim($argv["cc"]);
if ($cc != '') {
$html .= pubsByCC($idDBLink, $cc);
}
}*/
else if ($argv['what'] == 'calls') {
// information about some "centro de custo":
// 1. currently, only a list of publications
//
$cc = trim($argv["cc"]);
$showClosed = isset($argv['showclosed']) ? trim($argv['showclosed']) : "";
if ($cc != '') {
$html .= iconv('latin1', 'UTF-8', callsByCC($idDBLink, $cc, $showClosed == "yes"));
}
}
else {
// oops! no text...
}
odbc_close($idDBLink);
return $html;
}
?>
所以我真正需要知道的是:
1) 不应该 WordPress 能够解释 MediaWiki 标签(例如:<server-id what='publications' cc='IP02'></server-id>
)并执行此操作自动?
2) 在哪里可以找到有关此类迁移的更多文档?
3) 我是不是做错了?
WordPress 和 MediaWiki 是独立的应用程序,不能指望为一个应用程序编写的插件可以直接移植到另一个应用程序。如果你幸运的话,一些代码可能是可重用的,但它不会像剪切和粘贴那样简单。这两个应用程序有不同的做事方式。
1) 不,WordPress 将无法理解此类标签。 WordPress 可以通过附加插件理解 MediaWiki 风格的降价标签,但我不认识你突出显示的标签示例。
2) 我认为您当前的方法是合理的,您需要了解 MediaWiki 代码的作用并在 WordPress 插件中重新创建它。除了花一些时间来掌握 WP 插件之外,我怀疑是否有捷径可走。如果您喜欢轻量级编码和编写插件,那么这段时间是值得的。能够自定义 WordPress 非常有用。
3) 除了自己重新编码之外,另一种选择是查看是否有 WordPress 插件可以满足您的需求。您的问题没有详细说明您要添加的功能到底是什么。
我已经为 MediaWiki 和 WordPress 编写了插件,我发现使用起来更容易也更愉快。