How to change PHP scripts from MediaWiki to Wordpress Plugin?

1

I'm working on a project where I have PHP MediaWiki Scripts that import information from publications of a database into a Publications page.

I need to convert these scripts to Wordpress Plugin but I really do not know the best way to do it. I find myself a bit lost, I've already tried to follow the Tutorial: Writing a simple WordPress plugin from scratch , but without success.

Original Code MediaWiki

Here you will see my original MediaWiki source code:

<?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;
}

?>

My WordPress Try

Here you will see what I tried to do in WordPress Code:

<?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;
}

?>

So what I need to know exactly is:

1) Should not WordPress be able to interpret the MediaWiki tags (for example, <server-id = 'que publicações' cc = "IP02"> </ server-id> ) and do this automatically?

2) Where can I find more documentation on this type of migration?

3) Am I doing this the wrong way?

    
asked by anonymous 04.09.2015 / 14:04

0 answers