1<?php
2/**
3 * linksenhanced Plugin - Render Link Title using Wiki Markup
4 * Heavily based on the standard linking code.
5 *
6 * Usage:
7 *
8 * [[check^http://www.dokuwiki.org|www.dokuwiki.org]]
9 * [[render noparagraph^http://www.dokuwiki.org|<faicon fa fe-euro> FontAwesome Code]]
10 * [[render^http://www.dokuwiki.org|//Formatted Output, probably within a paragraph//]]
11 *
12 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
13 * @author     Andreas Böhler <dev@aboehler.at>
14 */
15
16if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
17if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
18require_once(DOKU_PLUGIN.'syntax.php');
19
20/**
21 * All DokuWiki plugins to extend the parser/rendering mechanism
22 * need to inherit from this class
23 */
24class syntax_plugin_linksenhanced_table extends DokuWiki_Syntax_Plugin {
25
26    function getType() {
27        return 'substition';
28    }
29
30    function getPType() {
31        return 'normal';
32    }
33
34    function getAllowedTypes() {
35        return array('container','substition','protected','disabled','paragraphs','formatting');
36    }
37
38    function getSort() {
39        return 202;
40    }
41
42    function connectTo($mode) {
43        $this->Lexer->addSpecialPattern('\{\{linksenhanced>[^}]*\}\}',$mode,'plugin_linksenhanced_table');
44    }
45
46   /**
47    * Handle the match. Use either the standard linking mechanism or, when enabled,
48    * pass the title through the parser
49    */
50    function handle($match, $state, $pos, Doku_Handler $handler) {
51        $options = trim(substr($match,16,-2));
52        $options = explode(',', $options);
53
54        $data = array('links' => array(),
55                      'namespace' => false
56                     );
57
58        foreach($options as $option)
59        {
60            list($key, $val) = explode('=', $option);
61            $key = strtolower(trim($key));
62            $val = trim($val);
63            $data[$key] = $val;
64        }
65
66        $data['links'] = $this->getPagesAndLinks($data['namespace']);
67        return $data;
68    }
69    /**
70     * Create output
71     */
72
73    function getPagesAndLinks($namespace)
74    {
75        global $conf;
76        $opts = array(
77        'depth' => 0,
78        'listfiles' => true,
79        'listdirs'  => false,
80        'pagesonly' => true,
81        'firsthead' => true,
82        'sneakyacl' => $conf['sneaky_index'],
83        );
84        if($namespace !== false)
85            $namespace = explode(';', $namespace);
86
87        $data = array();
88        $retData = array();
89        search($data, $conf['datadir'],'search_universal',$opts);
90        foreach($data as $k => $pdata)
91        {
92            $ns = getNS($pdata['id']);
93            if($ns === false)
94                $ns = '';
95
96            if($namespace !== false && !in_array($ns, $namespace))
97                continue;
98
99            $meta = p_get_metadata($pdata['id']);
100            if(is_array($meta['plugin_linksenhanced']['links']))
101            {
102                $retData[$pdata['id']] = $meta['plugin_linksenhanced']['links'];
103            }
104        }
105        return $retData;
106    }
107
108    function render($format, Doku_Renderer $R, $data) {
109        if($format == 'metadata')
110        {
111            $R->meta['plugin_linksenhanced']['nocache'] = true;
112            return true;
113        }
114        if($format != 'xhtml') return false;
115
116        echo '<h1>Link Overview</h1>';
117        echo '<table>';
118        echo '<tr><th width="30\%">Page</th><th width="90\%">Link Status</th></tr>';
119
120        foreach($data['links'] as $page => $links)
121        {
122            echo '<td rowspan="'.count($links).'">'.$page.'</td>';
123            $first = true;
124            foreach($links as $link)
125            {
126                if(!$first)
127                    echo '<tr>';
128                else
129                    $first = false;
130                echo '<td><a href="'.$link.'" class="plugin_linksenhanced_pending">'.$link.'</td>';
131                echo '</tr>';
132            }
133        }
134        echo '</tr>';
135        echo '</table>';
136    }
137}
138