2015-09-04 73 views
0

我正在從事一個項目,其中我有MediaWiki PHP Scripts,它將DB的出版物信息導入出版物頁面如何將PHP腳本從MediaWiki更改爲Wordpress插件?

我需要將這些腳本轉換爲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 [email protected]"); } 

    $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 [email protected]"); } 

    $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)我做錯了嗎?

回答

2

WordPress和MediaWiki是獨立的應用程序,不能指望爲其中一個插件直接移植到另一個插件上。如果你幸運的話,一些代碼可能是可重用的,但它不會像剪切和粘貼一樣簡單。這兩個應用程序有不同的做事方式。 1)不,WordPress不能理解這些標籤。 WordPress可以通過額外的插件瞭解MediaWiki樣式markdown標籤,但我不認識你突出顯示的標籤示例。

2)我認爲你目前的方法是合理的,你需要了解MediaWiki代碼在做什麼,並在WordPress插件中重新創建。我懷疑除了花些時間來掌握WP插件之外,還有其他捷徑。如果你喜歡編碼和編寫插件,這是花費時間。能夠自定義WordPress是非常有用的。

3)除了自己重新編碼它的另一種選擇將是看看是否有一個WordPress插件,做你正在尋找。你的問題沒有詳細說明你想要添加的功能是什麼。

爲MediaWiki和WordPress編寫了插件,我發現使用起來更輕鬆愉快。

+0

謝謝你的回答。我真正需要的是從數據庫中提取信息,並使用科學出版物訪問並將其打印到WordPress頁面中。我的問題是,我已經完成了PHP腳本和MediaWiki一樣簡單,我希望它們也可以使用WordPress,但我不知道如何。 –

+1

現有PHP腳本的有用部分可能是對數據庫的查詢。其餘的對我來說恐怕沒什麼用處。但是,這是可以的,因爲WordPress提供了一個插件框架,可以爲你做很多工作。首先開始處理一個簡單的插件,然後你可以使用[wp db](https://codex.wordpress.org/Class_Reference/wpdb)對象來執行你的查詢並獲得你需要的數據。 – jezza101

+1

你有幾個選擇如何在你的文章中顯示它,一個[簡碼](https://codex.wordpress.org/Shortcode_API)可能是一個很好的選擇,否則對[內容](https: //codex.wordpress.org/Plugin_API/Filter_Reference/the_content)可以用來自動將內容注入到您的文章中,也許您也會添加一個自定義元屬性來控制您要顯示的內容。我認爲在這一點上,你將不得不離開並閱讀,WP文檔非常棒! – jezza101