Hello,
I am developing a mysql database where data about IP-networks, MAC-addresses, IT-devices (PCs, Server, Access-Points) is stored.
So, we have one table for the IPs in our Networks (WAN, LAN, Management etc), one table for our managed switches (Ports and connected devices), one table for our devices etc.
I wrote some syntax plugins which generates tables like
a) Which devices are connected to one switch (switch-overview)
b) Which devices have an IP from a subnet (subnet-overview)
c) For each device, which IPs, MACs and switch-connection it has.
a) is called via
<sqltables:Switch name="foo"/>
b) is called via
<sqltables:IPs net="192.168.1.0"/>
c) is called via
<sqltables:Interfaces name="testserver"/>
The tables are rendered very well, it works.
But unfortunately, I could not get a WYSISWG-Editor to work, so that the user can insert a new device into the SQL database from DokuWiki. Right now, we do it with LibreOffice Base. I followed
https://www.dokuwiki.org/devel:section_editor and created the files with the Plugin Wizard.
DokuWiki version: 2013-05-10a "Weatherwax", running on Ubuntu 14.04.2 with OMD/check_mk (see
http://omdistro.org/).
Plugin file structure (in lib/plugins):
root@sql:/opt/omd/versions/1.20/share/dokuwiki/htdocs/lib/plugins# ls -R sqltables/
sqltables/:
action conf plugin.info.txt README syntax
sqltables/action:
DSLKundeEdit.php InterfacesEdit.php IPsEdit.php SwitchEdit.php
sqltables/conf:
default.php metadata.php
sqltables/syntax:
AP.php DSLKunde.php Interfaces.php IPs.php Server.php Switch.php UebersichtDSLKunden.php UebersichtModems.php UebersichtSwitche.php VMGuest.php VMHost.php
Let's have a look at Interfaces.php
// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();
class syntax_plugin_sqltables_Interfaces extends DokuWiki_Syntax_Plugin {
public function getType() {
return 'substition';
}
public function connectTo($mode) {
$this->Lexer->addSpecialPattern('<sqltables:Interfaces [^>]+/>',$mode,'plugin_sqltables_Interfaces');
}
public function handle($match, $state, $pos, Doku_Handler &$handler){
if($state==DOKU_LEXER_SPECIAL) {
if(preg_match('<sqltables:Interfaces name="(?P<name>[a-zA-Z.\d_-]+)"/>',$match,$out)){
return array('state'=>$state,'name'=>$out['name'],'bytepos_start' => $pos, 'bytepos_end' => $pos + strlen($match));
}
return array('state'=>$state,'name'=>'','bytepos_start' => $pos, 'bytepos_end' => ($pos + strlen($match)));
}
return array();
}
public function render($mode, Doku_Renderer &$renderer, $data) {
if(($mode != 'xhtml') or (count($data)<>4)) return false;
$class = '';
if (method_exists($renderer, 'startSectionEdit')) {
/* start edit section*/
$class = $renderer->startSectionEdit($data['bytepos_start'],
'plugin_sqltables_InterfacesEdit');
}
$renderer->doc .= '<div class="' . $class . '">';
$renderer->doc .= "<table class=\"inline\">\n";
/* query mySQL here and populate table (pseudo-code, real code would be too long)
$out = array('Interface','IP','MAC-Addresse','Switch','Port');
make_tableheader($out)
foreach($out as $i) {
$renderer->doc .= add_cell(mysql_result[$i]);
} */
$renderer->doc .= "</table>";
$renderer->doc .= '</div>';
if (method_exists($renderer, 'finishSectionEdit')) {
/*end edit section*/
$renderer->finishSectionEdit($data['bytepos_end']);
}
$renderer->doc .= "<br/>\n";
return true;
} }
A picture of the produced table is attached.
Now, with the help of an action plugin, I would like to make a form where you can edit the config.
action/InterfacesEdit.php
class action_plugin_sqltables_InterfacesEdit extends DokuWiki_Action_Plugin {
public function register(Doku_Event_Handler $controller) {
$controller->register_hook('HTML_EDIT_FORMSELECTION', 'BEFORE', $this, 'handle_html_edit_formselection');
$controller->register_hook('HTML_SECEDIT_BUTTON', 'BEFORE', $this, 'handle_html_secedit_button');
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_action_act_preprocess');
}
public function handle_html_edit_formselection(Doku_Event &$event, $param) {
global $TEXT;
if ($event->data['target'] !== 'plugin_sqltables_InterfacesEdit') {
error_log("int_edit editform ".$event->data['target']);
return;
}
$event->preventDefault();
//unset($event->data['intro_locale']);
$event->data['media_manager'] = false;
echo $this->locale_xhtml('edit_intro');
// FIXME: Add real edit form
$attr = array();
if (!$event->data['wr']) $attr['readonly'] = 'readonly';
//$event->data['form']->addElement(form_makeWikiText($TEXT, $attr));
$event->data['form'] = new Doku_Form();
$form =& $event->data['form'];
$form->addElement('<div id="sqltablesedit_Interfaces"></div>');
$form->form_makeTextField('teeeeeeeeeest');
$form->addElement('</div>');
}
public function handle_html_secedit_button(Doku_Event &$event, $param) {
if ($event->data['target'] !== 'plugin_sqltables_InterfacesEdit') {
error_log($event->data['target']);
return;
}
$event->data['name'] = 'Data-Entry';
}
public function handle_action_act_preprocess(Doku_Event &$event, $param) {
}
}
No Edit-button is shown at the generated table. When you manually specify the target at
StartSectionEdit[/m] and click on "Edit", it says that [m]No editor for edit target plugin_sqltables_interfacesedit found.
With this database, every information about a device is stored at one place. So, there is exacty one entry which says that computer foo has IP a.b.c.d, and this pile of information can be structured. So, at the switch' page, a table is generated (containing the connected device), at the IP-net's page, another table with the issued IPs is generated and at one's device page, another table with the switch and the IP is generated. So this data is always consistent.
When you use traditional Dokuwiki pages, data corruption will be only a matter of time (you set up a new server, create a new page for this server and add the IPs but forget to update the IP-net table => boom!)
Please help me fix this. I'm shure that other persons may find this solution interesting, so maybe I can publish it.
Thank you very much for your help
Bjarne23
P.S. I also don't know how to make forms in DokuWiki. Any help is welcome :)