How to change PHP scripts from Mediawiki to Wordpress Plugin?

Asked

Viewed 47 times

1

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

I need to convert these scripts to Wordpress Plugin but I don’t really know the best way to do it. I find myself a little lost, 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 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 Attempt at Wordpress

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) You must not Wordpress be able to interpret tags Mediawiki (for example: <server-id = 'que publicações' cc = "IP02"> </ server-id>) and do it automatically?

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

3) I am doing this the wrong way?

  • 1

    I imagine the class Publication is being instantiated inside one of those includes, is that it? Where exactly do you need this class to be available? Your code generates some error?

  • Yes she’s being instantiated in one of the includes, I need it to be available for the Plugin. The code is no mistake, it’s simply written to Mediawiki and not to Wordpress.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.