1<?php
2/**
3 @file       linkbonus/syntax/base.php
4 @brief      Base syntax component for Linkbonus plugin.
5 @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 @author     Luis Machuca Bezzaza <luis.machuca [at] gulix.cl>
7 @version    0.04
8**/
9
10if(!defined('DW_LF')) define('DW_LF',"\n");
11
12if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
13if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
14require_once(DOKU_PLUGIN.'syntax.php');
15require_once(DOKU_PLUGIN. 'linkbonus/common.php'); // for common functions
16
17/**
18 * All DokuWiki plugins to extend the parser/rendering mechanism
19 * need to inherit from this class
20 */
21class syntax_plugin_linkbonus_base extends DokuWiki_Syntax_Plugin {
22
23    /**
24     * return some info
25     */
26    function getInfo() {
27      return DW_common_linkbonus::getInfo($this);
28      }
29
30    function getType() {
31      return DW_common_linkbonus::getType();
32      }
33
34    // priority can be increased via the 'priority' option
35    function getSort() {
36      return 240;
37      }
38
39    // this should allow the plugin to accept formatting syntax, eg.: italics.
40//    function getAllowedTypes() {
41//      return array();
42//      }
43
44    function connectTo($mode) {
45
46      /**
47       * we catch the following syntax:
48       * [[http(s)://somewhere.com/some/path|Link name|Link title|(fetchname?)|]]
49       *
50       */
51      $linkpart  = '@/[^\|]+'; // everything 'till the pipe
52      $namepart  = '[^\|\](?:\{\{|\}\})]+'; // we must NOT catch image links
53      $otherpart = '[^\]]+';
54      $REGEX     =
55      '\[\['. $linkpart. '\|'. $namepart. '\|'. $otherpart. '\]\]';
56      $this->Lexer->addSpecialPattern(
57        $REGEX,$mode,'plugin_linkbonus_base');
58      }
59
60
61    /**
62     * Handle the match
63     */
64    function handle($match, $state, $pos, &$handler) {
65        $match = substr($match, 2, -2);
66        /* split with non-escaped pipes ;)
67           result should be something like:
68           @/link  |  Name  | [Title] [ | parameters... ] */
69        $data  = preg_split('/(?<!\\\\)\|/', $match);
70        $link= array();
71        $link['url']     = array_shift($data);
72        $link['name']    = array_shift($data);
73        $link['title']   = array_shift($data);
74
75        /* If link-title is empty, use the same contents as link-name */
76        if (empty($link['title']) ) $link['title']= $link['name'];
77
78        /* Unescape pipes */
79        $link['name']  = str_replace ('\|', '|', $link['name']);
80        $link['title'] = str_replace ('\|', '|', $link['title']);
81
82        /* Recover base of server */
83        $link['url'] = substr($link['url'], 1); // remove leading '@'
84        $link['url'] = DW_common_linkbonus::_getServerBase().  $link['url'];
85
86
87        /* normal-link sanity check:
88           if at this point $data is empty, we are dealing with a
89           "normal" dokuwiki link and we can let DokuWiki take care of it
90        */
91        if (empty($data) && empty($link['title']) ) {
92          $handler->_addCall('externallink',array( $link['url'],$link['name'] ), $pos);
93          return array();
94          }
95        /* there is still the chance that we are dealing with a media link
96        */
97
98        /* check the rest of the parameters */
99        $set      = array(); // {false} if paramenter has not been detected
100        $dofetch  = false;
101        $dodoa    = false;
102
103        /* check if the page is actually a web page (eg.: not an image) */
104        //if (mimetype($link['url']) !== 'text/html') {
105        //  $handler->_addCall('externallink',array( $link['url'],$link['name'] ), $pos);
106        //  return array();
107        //  }
108
109        $keys     = array(
110        'fetchname', 'doa', 'class', 'rel', 'target'
111        );
112        if ($this->getConf('link_favicons') == true) $keys[]= 'favicon';
113
114        //$options  = array();
115        DW_common_linkbonus::parse_options ($data, $link, $keys);
116
117        // parse fetchname
118        if (array_key_exists('fetchname', $link)) {
119            $dofetch= true;
120            if ($link['fetchname'] === '' || empty($link['fetchname'])) $link['fetchname'] = 'title';
121            }
122
123        /* -----------------------------------------------------------
124           PostProcess some requirements
125         */
126
127        /* if there is any need to fetch external page, do so */
128        $xcontents= '';
129        $xstatus= array();
130
131        $pagestatus= false;
132        if ($link['doa'] || $link['fetchname']) {
133          $pagestatus = DW_common_linkbonus::_doTestPage($link['url']);
134          }
135
136        if ($link['fetchname']) {
137          if ($pagestatus == 'text/html') $ext_title= DW_common_linkbonus::_doGetPageTitle($link['url']);
138          else $ext_title = $link['name']. " file $pagestatus";
139
140          if ($link['fetchname'] == 'tooltip') $link['title'] = $ext_title;
141          else if ($link['fetchname'] == 'title') $link['name']= $ext_title;
142          } // end try fetch title
143
144        if (array_key_exists('doa', $link)) {
145          $link['class'].= ($pagestatus === false) ? 'doa_error' : 'doa_ok';
146          $link['title'].= ' '. $this->getLang(($pagestatus===false) ? 'doa_no' : 'doa_ok');
147          unset($link['doa']);
148          } // end dead-or-alive check
149
150        $link['class']= 'urlextern '. $link['class']; // default DW external-link class
151        $link['format'] = $this->getConf('in_formatting');
152
153        /* -----------------------------------------------------
154          at this point we have all the information required to
155          ask the Renderer system to create the link */
156        $data= array(
157         'link'   => $link,
158         'match'  => $match,
159         'state'  => $state
160         );
161        return $data;
162      }
163
164    /**
165     * Create output
166     */
167    function render($mode, &$renderer, $data) {
168      return DW_common_linkbonus::syntax_render($mode, $renderer, $data, 'external');
169      }
170
171}
172
173