FOSSology Project Logo FOSSology
Advancing open source analysis and development
 

A Simple Plugin - "Hello World"

In this tutorial example, we will extend FOSSology through the addition of a “Hello World” plugin, written in php. Our new plugin will be accessed and executed through the user interface (Fossology UI) by clicking on the top level Help menu and selecting “Hello World” from its pulldown menu.

Every plugin, including this “Hello World” example, extends a common FOSSology plugin template called FO_Plugin. In this example, we use several variables defined by FO_Plugin. FO_Plugin will be discussed in more detail in the following tutorial section.

Click here to see the complete code for the “Hello World” example.

Creating the "Hello World" Plugin

To start, every plugin must always include the following code to check for global readiness. The GlobalReady variable is set during FOSSology UI initialization and must be checked by plugins to ensure code is being executed on the same system where the UI is running (this prevents hacking attempts).

/*************************************************
 Restrict usage: Every PHP file should have this
 at the very beginning.
 This prevents hacking attempts.
 *************************************************/
global $GlobalReady;                    /* Always include these two lines at the start to insure the plugin */
if (!isset($GlobalReady)) { exit; }     /* is running on the same system on which the UI is running */

...

The class and variable declaration section defines the class name; it should look like this:

class ui_hello extends FO_Plugin            /* This is the class name (ui_hello) and */
                                            /* it extends functionality of the FO_Plugin */
  {
  public $Name      = "hello";                /* This is the name by which FOSSology identifies the plugin */
  public $Title     = "Hello World Example";  /* This is the title that will be displayed in the UI */
  public $MenuList  = "Help::Hello World";    /* This is the description that will be displayed in the pulldown menu */ 
  public $LoginFlag = 0;                      /* you do not need to be logged into the UI to execute this plugin */

  protected $_Text="Hello World";             /* This is the output message that will be displayed in the UI */

Next, we include an Output() function to display our “Hello World” string. The function is written to handle 3 different output types, XML, HTML and text.

  function Output()
    {
    if ($this->State != PLUGIN_STATE_READY) { return; }     /* State is set by FO_Plugin */
    $V="";
    switch($this->OutputType)                               /* OutputType is set by FO_Plugin */
      {
      case "XML":
        $V .= "<text>$this->_Text</text>\n";
        break;
      case "HTML":
        $V .= "<b>$this->_Text</b>\n";
        break;
      case "Text":
        $V .= "$this->_Text\n";
        break;
      default:
        break;
      }
    if (!$this->OutputToStdout) { return($V); }            /* OutputToStdout is a function defined by FO_Plugin */  
    print($V);
    return;
    }

Finally, you'll need to:

  1. Include code to add the “Hello World” plugin to the “Plugins” array. (The “Plugins array is a data structure used by the FOSSology UI to manage plugins.)
  2. Initialize your new plugin.
$NewPlugin = new ui_hello;
$NewPlugin->Initialize();                                 /* Initialize is a function defined by FO_Plugin */
Testing the "Hello World" Plugin

In order to test your new plugin, copy it to the UI plugin directory.

sudo cp hello.php /usr/local/share/fossology/www/plugins/hello.php

Open a browser to the FOSSology UI (http://<your_server>/repo). Place your cursor on the Help tab at the top of the UI window to display the Help pulldown menu. You should see an option in the pulldown menu for “Hello World”. Click on this option to run your plugin.

 
hello_world.txt · Last modified: 2009/04/02 10:16 by markd

Copyright (C) 2007-2009 Hewlett-Packard Development Company, L.P.
FOSSology Project documentation is licensed under the GNU Free Documentation License Version 1.2
Recent changes RSS feed Valid XHTML 1.0 Valid CSS3 Driven by DokuWiki