xref: /plugin/struct/action/output.php (revision 881e940c5447b444534e39519dc2fdb881587526)
182c064c1SAndreas Gohr<?php
282c064c1SAndreas Gohr/**
382c064c1SAndreas Gohr * DokuWiki Plugin struct (Action Component)
482c064c1SAndreas Gohr *
582c064c1SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
682c064c1SAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
782c064c1SAndreas Gohr */
882c064c1SAndreas Gohr
982c064c1SAndreas Gohr// must be run within Dokuwiki
1082c064c1SAndreas Gohrif(!defined('DOKU_INC')) die();
1182c064c1SAndreas Gohr
1282c064c1SAndreas Gohr/**
1382c064c1SAndreas Gohr * Class action_plugin_struct_output
1482c064c1SAndreas Gohr *
1582c064c1SAndreas Gohr * This action component handles the automatic output of all schema data that has been assigned
1682c064c1SAndreas Gohr * to the current page by appending the appropriate instruction to the handler calls.
1782c064c1SAndreas Gohr *
1882c064c1SAndreas Gohr * The real output creation is done within the syntax component
1982c064c1SAndreas Gohr * @see syntax_plugin_struct_output
2082c064c1SAndreas Gohr */
2182c064c1SAndreas Gohrclass action_plugin_struct_output extends DokuWiki_Action_Plugin {
2282c064c1SAndreas Gohr
23*881e940cSAndreas Gohr    protected $lastread = '';
24*881e940cSAndreas Gohr
2582c064c1SAndreas Gohr    /**
2682c064c1SAndreas Gohr     * Registers a callback function for a given event
2782c064c1SAndreas Gohr     *
2882c064c1SAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
2982c064c1SAndreas Gohr     * @return void
3082c064c1SAndreas Gohr     */
3182c064c1SAndreas Gohr    public function register(Doku_Event_Handler $controller) {
3282c064c1SAndreas Gohr        $controller->register_hook('PARSER_HANDLER_DONE', 'AFTER', $this, 'handle_output');
33*881e940cSAndreas Gohr        $controller->register_hook('IO_WIKIPAGE_READ', 'AFTER', $this, 'handle_read');
34*881e940cSAndreas Gohr    }
3582c064c1SAndreas Gohr
36*881e940cSAndreas Gohr    /**
37*881e940cSAndreas Gohr     * This is kind of a hack. We want to be sure our instruction is only added when the
38*881e940cSAndreas Gohr     * instructions of the main page are created. There is no clear way to figure that out
39*881e940cSAndreas Gohr     * though. Thus we only act on when the appropriate wiki page was read from disk
40*881e940cSAndreas Gohr     * immediately before our call.
41*881e940cSAndreas Gohr     *
42*881e940cSAndreas Gohr     * @param Doku_Event $event
43*881e940cSAndreas Gohr     * @param $param
44*881e940cSAndreas Gohr     */
45*881e940cSAndreas Gohr    public function handle_read(Doku_Event $event, $param) {
46*881e940cSAndreas Gohr        $this->lastread = $event->data[2];
4782c064c1SAndreas Gohr    }
4882c064c1SAndreas Gohr
4982c064c1SAndreas Gohr    /**
5082c064c1SAndreas Gohr     * Appends the instruction to render our syntax output component to each page
5128b818ceSAndreas Gohr     * after the first found headline or the very begining if no headline was found
5282c064c1SAndreas Gohr     *
5382c064c1SAndreas Gohr     * @param Doku_Event $event
5482c064c1SAndreas Gohr     * @param $param
5582c064c1SAndreas Gohr     */
56*881e940cSAndreas Gohr    public function handle_output(Doku_Event $event, $param) {
575a1eab78SAndreas Gohr        global $ID;
58*881e940cSAndreas Gohr        if($this->lastread != $ID) return; // avoid nested calls
59*881e940cSAndreas Gohr        $this->lastread = '';
6002481400SAndreas Gohr        if(!page_exists($ID)) return;
6182c064c1SAndreas Gohr
6228b818ceSAndreas Gohr        $ins = -1;
6328b818ceSAndreas Gohr        $pos = 0;
6428b818ceSAndreas Gohr        foreach($event->data->calls as $num => $call) {
6528b818ceSAndreas Gohr            // try to find the first header
6628b818ceSAndreas Gohr            if($call[0] == 'header') {
6728b818ceSAndreas Gohr                $pos = $call[2];
6828b818ceSAndreas Gohr                $ins = $num;
6928b818ceSAndreas Gohr                break;
7028b818ceSAndreas Gohr            }
7182c064c1SAndreas Gohr
7228b818ceSAndreas Gohr            // abort when after we looked at the first 150 bytes
7328b818ceSAndreas Gohr            if($call[3] > 150) {
7428b818ceSAndreas Gohr                break;
7528b818ceSAndreas Gohr            }
7628b818ceSAndreas Gohr        }
7728b818ceSAndreas Gohr
7828b818ceSAndreas Gohr        // insert our own call after the found position
7928b818ceSAndreas Gohr        array_splice(
8028b818ceSAndreas Gohr            $event->data->calls,
8128b818ceSAndreas Gohr            $ins+1,
8228b818ceSAndreas Gohr            0,
8328b818ceSAndreas Gohr            array(
8428b818ceSAndreas Gohr                array(
8582c064c1SAndreas Gohr                    'plugin',
8682c064c1SAndreas Gohr                    array(
875a1eab78SAndreas Gohr                        'struct_output', array('pos' => $pos), DOKU_LEXER_SPECIAL, ''
8882c064c1SAndreas Gohr                    ),
8982c064c1SAndreas Gohr                    $pos
9028b818ceSAndreas Gohr                )
9128b818ceSAndreas Gohr            )
9282c064c1SAndreas Gohr        );
9382c064c1SAndreas Gohr    }
9482c064c1SAndreas Gohr
9582c064c1SAndreas Gohr}
9682c064c1SAndreas Gohr
9782c064c1SAndreas Gohr// vim:ts=4:sw=4:et:
98