Plugins Directory

page title: [Hack: Plugins Directory]


bugs: Probably
Compatible with: R6.1
Current version:
Credits: brianko, ported over from WikkaWiki

class/wacko.php <-- add to end of file

<?php
/**
 * Build a (possibly valid) filepath from a delimited list of paths
 *
 * This function takes a list of paths delimited by ":"
 * and attempts to construct a fully-qualified pathname to a specific
 * file.  By default, this function checks to see if the file
 * pointed to by the fully-qualified pathname exists.  First valid
 * match wins.  Disabling this feature will return the first valid
 * constructed path (i.e, a path containing a valid directory, but
 * not necessarily pointing to an existant file).
 *
 * @param string $filename mandatory: filename to be used in
 *          construction of fully-qualified filepath
 * @param string $pathlist mandatory: list of
 *          paths (delimited by ":")
 * @param  boolean $checkIfFileExists optional: if TRUE, returns
 *          only a pathname that points to a file that exists
 *          (default)
 * @return string A fully-qualified pathname or NULL if none found
 */
function BuildFullpathFromMultipath($filename, $pathlist, $checkIfFileExists=TRUE): ?string
{
    $paths = preg_split('/:/', $pathlist);
    if(empty($paths[0])) return NULL;
    if(FALSE === $checkIfFileExists)
    {
        // Just return first directory that exists
        foreach($paths as $path)
        {
            $path = trim($path);
            if(file_exists($path))
            {
                return Ut::join_path($path, $filename);
            }
        }
        return NULL;
    }
    foreach($paths as $path)
    {
        $path = trim($path);
        $fqfn = Ut::join_path($path, $filename);
        if(file_exists($fqfn)) return $fqfn;
    }
    return NULL;
}
?>

1. Documentation

Introduces a preliminary plugin architecture that allows Wacko users to override core functionality with custom actions, handlers, formatters, and associated templates. The advantages of such a setup are numerous:

  • A component that "misbehaves" or does not perform to your expectations can simply be removed.
  • Future Wacko upgrades are guaranteed not to change or modify anything in the /plugins folders.
  • Testing of a new action, handler, formatter, or template is greatly enhanced: There is no need to modify core functionality, and it's easy to revert back to the core if necessary.

2. How to

To use the new plugin folders for your add-ons, you will have to create the /plugins tree and add the corresponding paths in your config/constants.php file. Components will be loaded following the path order specified in the config file (so for instance a custom action called /plugins/action/contact.php will override the default action/contact.php). The only supported path separator is the colon (:). For example:


const FORMATTER_DIR = 'plugins/formatter:formatter',
const HANDLER_DIR = 'plugins/handler:handler',
const ACTION_DIR = 'plugins/action:action',	

For some formatters with associated class files (pre_wacko.php, wackoformatter.php) manual changes are required in the config/autoload.conf file:


plugins/formatter/class/pre_wacko.php                       PreFormatter
plugins/formatter/class/wackoformatter.php                 WackoFormatter	

Other modifications are necessary to fully implement:


1. class/wacko.php
In the _format function, the two include_buffered calls that have Ut:join_path calls as the first argument need to be modified:

#$text      = $this->include_buffered(Ut::join_path(FORMATTER_DIR, $formatter . '.php'), $error, compact('text', 'options'));
$text       = $this->include_buffered($this->BuildFullpathFromMultipath($formatter . '.php', FORMATTER_DIR), $error, compact('text', 'options'));

#$text = $this->include_buffered(Ut::join_path(FORMATTER_DIR, 'typografica.php'), $error, compact('text'));
$text = $this->include_buffered($this->BuildFullpathFromMultipath('typografica.php', FORMATTER_DIR), $error, compact('text'));

2. formatter/wiki.php, formatter/highlight/notypo.php, formatter/highlight/wacko.php, formatter/highlight/info.php, formatter/highlight/noinclude.php, formatter/highlight/details.php, formatter/highlight/noautolinks.php, formatter/highlight/typografica.php
The Ut::join_path call(s) in each file need to be modified. For example:

#include Ut::join_path(FORMATTER_DIR, 'post_wacko.php');
include $this->BuildFullpathFromMultipath('post_wacko.php', FORMATTER_DIR);

2.1. Localization (optional)

put this at the end of your language file


lang/custom.<lang>.php
%%

3. Changelog

0.1 Intitial version

4. To Do


Referring pages:

  1. Dev/PatchesHacks

Comments

  1. Re: Plugins Directory

    Not sure why it needs $checkIfFileExists at all, if it checks only for the path and not the file.

    • explode() consumes less resources.
    • The function itself should be placed in Ut class, e.g. Ut::join_multi_path()
    • Perhaps it is better to mirror the folder structure by just set the 'plugin' folder as prefix in the function
      • const PLUGIN_DIR = 'plugin',
      • $paths = [PLUGIN_DIR . '/' . $path, $path]

    <?php
    
    function BuildFullpathFromMultipath($filename, $pathlist, $checkIfFileExists = true): ?string
    {
        $paths = explode(':', $pathlist);
    
        if(empty($paths[0])) return null;
    
        if (!$checkIfFileExists)
        {
            // Just return first directory that exists
            foreach ($paths as $path)
            {
                $path = trim($path);
    
                if(file_exists($path)) // check for file
                {
                    return Ut::join_path($path, $filename);
                }
            }
    
            return null;
        }
    
        foreach ($paths as $path)
        {
            $path = trim($path);
            $fqfn = Ut::join_path($path, $filename);
    
            if (file_exists($fqfn))
            {
                return $fqfn;
            }
        }
    
        return null;
    }
    • WikiAdmin
    • 14.02.2024 10:41 edited
  2. Comment 4401

    Yep, all good suggestions, will implement.

    File existence is checked here:

    if (file_exists($fqfn))
    {
    return $fqfn;
    }
    Otherwise, it will check the next path/file until it finds a file, or it will exit with NULL.
  3. Re: Meaning of "Ut"

    Ut: Utility - assorted utility functions used throughout WackoWiki

    class/ut.php

    I haven't thought about the plugin API in depth yet, just attempted to read and understand the patch.
    • WikiAdmin
    • 14.02.2024 16:00 edited
  4. Re: Meaning of "Ut"



    No worries, wasn't suggesting it should be adopted. I just find it useful when testing. I see a similar effort is already being done in include_buffered(), so someone was thinking about it!