wiki
[ class tree: wiki ] [ index: wiki ] [ all elements ]

Source for file plugins_lib.php

Documentation is available at plugins_lib.php

  1. <?php
  2.     /**
  3.     * Plugin Lib
  4.     *
  5.     * A port of PhpWiki WikiPlugin class
  6.     * Principal use is port PhpWiki plugins, but can be used to make new ones.
  7.     * Use:
  8.     * - Extends PluginsLib with your class
  9.     * - add the lines
  10.     * <code>
  11.     * include "pluginslib.php";
  12.     *
  13.     * function wikiplugin_backlinks($data, $params) {
  14.     *    $plugin = new BackLinks();
  15.     *    return $plugin->run($data, $params);
  16.     * }
  17.     * function wikiplugin_backlinks_help() {
  18.     *    $plugin = new BackLinks();
  19.     *    return $plugin->getDescription();
  20.     * }    * </code>
  21.     * @package wiki
  22.     */
  23.  
  24.     /**    
  25.     * @package wiki
  26.     * @subpackage PluginsLib
  27.     * @author Claudio Bustos
  28.     * @version $Revision$
  29.     */
  30.     class PluginsLib extends BitBase {
  31.         var $_errors;
  32.         var $_data;
  33.         var $_params;
  34.         /**
  35.         * Array of params to be expanded as arrays. Explode the string with {@link $separator}
  36.         * @var array 
  37.         */
  38.         var $expanded_params = array();
  39.         /**
  40.         * Separator used to explote params listed on {@link $expanded_params}
  41.         * @var string 
  42.         */
  43.         var $separator = "|";
  44.         /**
  45.         * List of fields retrieved from {@link BitBase::list_pages()}
  46.         * Keys are the name of the fields and values the names for tra();
  47.         * @var array 
  48.         */
  49.         var $aInfoPresetNames = array(
  50.         "hits" => "Hits""last_modified" => "Last mod""user" => "Last author""len" => "Size""comment" => "Com""creator" => "Creator""version" => "Last ver""flag" => "Status""versions" => "Vers""links" => "Links""backlinks" => "Backlinks");
  51.  
  52.         /**
  53.         * Process the params, in this order:
  54.         * - default values, asigned on {@link PluginsLib::getDefaultArguments()}
  55.         * - request values, sended by GET or POST method, if $request is put to true
  56.         * - explicit values, asigned on the Wiki
  57.         * @param array sended to wikiplugin_name($data, $params)
  58.         * @param bool if set to true, accept values from $_REQUEST
  59.         * @param bool if set to true, assign default values from {@link PluginsLib::getDefaultArguments()}
  60.         * @return array list of params
  61.         */
  62.         function getParams($params$request false$defaults false{
  63.             if ($defaults === false{
  64.                 $defaults $this->getDefaultArguments();
  65.             }
  66.             $args array();
  67.             foreach ($defaults as $arg => $default_val{
  68.                 if (isset($params[$arg])) {
  69.                     $args[$arg$params[$arg];
  70.                 elseif(isset($_REQUEST[$arg])) {
  71.                     $args[$arg$_REQUEST[$arg];
  72.                 else {
  73.                     // maybe this kind of transformation can be grouped on a external function
  74.                     if ($default_val==="[pagename]"{
  75.                         $default_val=$_REQUEST["page"];
  76.                     }
  77.                     $args[$arg$default_val;
  78.                 }
  79.                 if (in_array($arg$this->expanded_params)) {
  80.                     if ($args[$arg]{
  81.                     $args[$argexplode($this->separator$args[$arg]);
  82.                     foreach($args[$argas $id=>$value{
  83.                         $args[$arg][$id]=trim($value);
  84.                     }
  85.                     else {
  86.                     $args[$arg]=array();
  87.                     }
  88.                 }
  89.             }
  90.             return $args;
  91.         }
  92.         /**
  93.         * Returns the name of the Plugin
  94.         * By default, erase the first 'WikiPlugin'
  95.         * Made for overload it.
  96.         * @return string 
  97.         */
  98.         function getName({
  99.             return preg_replace('/^WikiPlugin/'''get_class($this));
  100.         }
  101.         /**
  102.         * Returns a description of the Plugin
  103.         * Made for overload it.
  104.         * @return string 
  105.         */
  106.         function getDescription({
  107.             return $this->getName();
  108.         }
  109.         /**
  110.         * Returns the version of the version
  111.         * Made for overload it.
  112.         * @return string 
  113.         */
  114.         function getVersion({
  115.             return tra("No version indicated");
  116.             //return preg_replace("/[Revision: $]/", '',
  117.             //                    "\$Revision$");
  118.         }
  119.         /**
  120.         * Returns the default arguments for the plugin
  121.         * Use keys as the arguments and values as ... the default values
  122.         * @return array 
  123.         */
  124.         function getDefaultArguments({
  125.             return array('description' => $this->getDescription());
  126.         }
  127.         /**
  128.         * Run the plugin
  129.         * For sake of God, overload it!
  130.         * @param string 
  131.         * @param array 
  132.         */
  133.         function run ($data$params{
  134.             /**
  135.             * UGLY ERROR!.
  136.             */
  137.             return $this->error("PluginsLib::run: pure virtual function. Don't be so lazy!");
  138.         }
  139.         function error ($message{
  140.             return "~np~<span class='warn'>Plugin ".$this->getName()." ".tra("failed")." : ".tra($message)."</span>~/np~";
  141.         }
  142.         function getErrorDetail({
  143.             return $this->_errors;
  144.         }
  145.         function _error($message{
  146.             $this->_errors = $message;
  147.             return false;
  148.         }
  149.     }
  150.     /**
  151.     * Class with utilities for Plugins
  152.     *
  153.     * @package wiki
  154.     * @subpackage PluginsLib
  155.     * @author Claudio Bustos
  156.     * @version $Revision$
  157.     */
  158.     class PluginsLibUtil {
  159.         /**
  160.         * Create a table with information from pages
  161.         * @param array 
  162.         * @param array list of keys to show.
  163.         * @param array definition of the principal field. By default:
  164.         *               array("field"=>"title","name"=>"Page")
  165.         * @return string 
  166.         */
  167.         function createTable($aData,$aInfo=false,$aPrincipalField=false{
  168.             // contract
  169.             if (!$aPrincipalField or !is_array($aPrincipalField)) {
  170.                 $aPrincipalField=array("field"=>"title","name"=>"Page");
  171.             }
  172.             if (!is_array($aInfo)) {
  173.                 $aInfo=false;
  174.             }
  175.             // ~contract
  176.             $sOutput="";
  177.             if ($aInfo{
  178.                 $iNumCol=count($aInfo)+1;
  179.                 $sStyle=" style='width:".(floor(100/$iNumCol))."%' ";
  180.                 // Header for info
  181.                 $sOutput  .= "<table class='normal'><tr><td class='heading' $sStyle>".tra($aPrincipalField["name"])."</td>";
  182.                 foreach($aInfo as $iInfo => $sHeader{
  183.                     $sOutput  .= "<td class='heading' $sStyle >".tra($sHeader)."</td>";
  184.                 }
  185.                 $sOutput  .= "</tr>";
  186.             }
  187.             $iCounter=1;
  188.             foreach($aData as $aPage{
  189.                 $sClass=($iCounter%2)?"odd":"even";
  190.                 if (!$aInfo{
  191.                     $sOutput  .= "*((".$aPage[$aPrincipalField["field"]]."))\n";
  192.                 else {
  193.                     $sOutput  .= "<tr><td class='$sClass'>((".$aPage[$aPrincipalField["field"]]."))</td>";
  194.                     foreach($aInfo as $sInfo{
  195.                         if (isset($aPage[$sInfo])) {
  196.                             $sOutput  .= "<td class='$sClass'>".$aPage[$sInfo]."</td>";
  197.                         }
  198.                     }
  199.                 }
  200.             $iCounter++;
  201.             }
  202.                 if ($aInfo{
  203.                     $sOutput  .= "</table>";
  204.                 }
  205.         return $sOutput;
  206.         }
  207.     }
  208. ?>

Documentation generated on Wed, 29 Jul 2015 13:57:15 +0000 by phpDocumentor 1.5.0-lsces