File: InlineParser.php

package info (click to toggle)
phpwiki 1.3.14-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 15,716 kB
  • ctags: 23,548
  • sloc: php: 88,295; sql: 1,476; sh: 1,378; perl: 765; makefile: 602; awk: 28
file content (1170 lines) | stat: -rwxr-xr-x 38,334 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
<?php 
rcs_id('$Id: InlineParser.php,v 1.91 2007/06/07 18:56:57 rurban Exp $');
/* Copyright (C) 2002 Geoffrey T. Dairiki <dairiki@dairiki.org>
 * Copyright (C) 2004,2005,2006,2007 Reini Urban
 *
 * This file is part of PhpWiki.
 * 
 * PhpWiki is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * PhpWiki is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PhpWiki; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
/**
 * This is the code which deals with the inline part of the (new-style)
 * wiki-markup.
 *
 * @package Markup
 * @author Geoffrey T. Dairiki, Reini Urban
 */
/**
 */

/**
 * This is the character used in wiki markup to escape characters with
 * special meaning.
 */
define('ESCAPE_CHAR', '~');

require_once(dirname(__FILE__).'/HtmlElement.php');
require_once('lib/CachedMarkup.php');
require_once(dirname(__FILE__).'/stdlib.php');


function WikiEscape($text) {
    return str_replace('#', ESCAPE_CHAR . '#', $text);
}

function UnWikiEscape($text) {
    return preg_replace('/' . ESCAPE_CHAR . '(.)/', '\1', $text);
}

/**
 * Return type from RegexpSet::match and RegexpSet::nextMatch.
 *
 * @see RegexpSet
 */
class RegexpSet_match {
    /**
     * The text leading up the the next match.
     */
    var $prematch;
    /**
     * The matched text.
     */
    var $match;
    /**
     * The text following the matched text.
     */
    var $postmatch;
    /**
     * Index of the regular expression which matched.
     */
    var $regexp_ind;
}

/**
 * A set of regular expressions.
 *
 * This class is probably only useful for InlineTransformer.
 */
class RegexpSet
{
    /** Constructor
     *
     * @param array $regexps A list of regular expressions.  The
     * regular expressions should not include any sub-pattern groups
     * "(...)".  (Anonymous groups, like "(?:...)", as well as
     * look-ahead and look-behind assertions are okay.)
     */
    function RegexpSet ($regexps) {
        assert($regexps);
        $this->_regexps = array_unique($regexps);
        if (!defined('_INLINE_OPTIMIZATION')) define('_INLINE_OPTIMIZATION',0);
    }

    /**
     * Search text for the next matching regexp from the Regexp Set.
     *
     * @param string $text The text to search.
     *
     * @return RegexpSet_match  A RegexpSet_match object, or false if no match.
     */
    function match ($text) {
        return $this->_match($text, $this->_regexps, '*?');
    }

    /**
     * Search for next matching regexp.
     *
     * Here, 'next' has two meanings:
     *
     * Match the next regexp(s) in the set, at the same position as the last match.
     *
     * If that fails, match the whole RegexpSet, starting after the position of the
     * previous match.
     *
     * @param string $text Text to search.
     *
     * @param RegexpSet_match $prevMatch A RegexpSet_match object.
     * $prevMatch should be a match object obtained by a previous
     * match upon the same value of $text.
     *
     * @return RegexpSet_match A RegexpSet_match object, or false if no match.
     */
    function nextMatch ($text, $prevMatch) {
        // Try to find match at same position.
        $pos = strlen($prevMatch->prematch);
        $regexps = array_slice($this->_regexps, $prevMatch->regexp_ind + 1);
        if ($regexps) {
            $repeat = sprintf('{%d}', $pos);
            if ( ($match = $this->_match($text, $regexps, $repeat)) ) {
                $match->regexp_ind += $prevMatch->regexp_ind + 1;
                return $match;
            }
            
        }
        
        // Failed.  Look for match after current position.
        $repeat = sprintf('{%d,}?', $pos + 1);
        return $this->_match($text, $this->_regexps, $repeat);
    }

    // Syntax: http://www.pcre.org/pcre.txt
    //   x - EXTENDED, ignore whitespace
    //   s - DOTALL
    //   A - ANCHORED
    //   S - STUDY
    function _match ($text, $regexps, $repeat) {
        // If one of the regexps is an empty string, php will crash here: 
        // sf.net: Fatal error: Allowed memory size of 8388608 bytes exhausted 
        //         (tried to allocate 634 bytes)
        if (_INLINE_OPTIMIZATION) { // disabled, wrong
	    // So we try to minize memory usage, by looping explicitly,
	    // and storing only those regexp which actually match. 
	    // There may be more than one, so we have to find the longest, 
	    // and match inside until the shortest is empty.
	    $matched = array(); $matched_ind = array();
	    for ($i=0; $i<count($regexps); $i++) {
		if (!trim($regexps[$i])) {
		    trigger_error("empty regexp $i", E_USER_WARNING);
		    continue;
		}
		$pat= "/ ( . $repeat ) ( " . $regexps[$i] . " ) /x";
		if (preg_match($pat, $text, $_m)) {
		    $m = $_m; // FIXME: prematch, postmatch is wrong
		    $matched[] = $regexps[$i];
		    $matched_ind[] = $i;
		    $regexp_ind = $i;
		}
	    }
	    // To overcome ANCHORED:
	    // We could sort by longest match and iterate over these.
	    if (empty($matched)) return false;
        }
        $match = new RegexpSet_match;
        
        // Optimization: if the matches are only "$" and another, then omit "$"
        if (! _INLINE_OPTIMIZATION or count($matched) > 2) {
            assert(!empty($repeat));
            assert(!empty($regexps));
            // We could do much better, if we would know the matching markup for the 
            // longest regexp match:
            $hugepat= "/ ( . $repeat ) ( (" . join(')|(', $regexps) . ") ) /Asx";
            // Proposed premature optimization 1:
            //$hugepat= "/ ( . $repeat ) ( (" . join(')|(', array_values($matched)) . ") ) /Asx";
            if (! preg_match($hugepat, $text, $m)) {
                return false;
            }
            // Proposed premature optimization 1:
            //$match->regexp_ind = $matched_ind[count($m) - 4];
            $match->regexp_ind = count($m) - 4;
        } else {
            $match->regexp_ind = $regexp_ind;
        }
        
        $match->postmatch = substr($text, strlen($m[0]));
        $match->prematch = $m[1];
        $match->match = $m[2];

        /* DEBUGGING */
        if (DEBUG & _DEBUG_PARSER) {
          static $_already_dumped = 0;
          if (!$_already_dumped) {
            var_dump($regexps); 
            if (_INLINE_OPTIMIZATION)
            	var_dump($matched);
            var_dump($matched_inc); 
          }
          $_already_dumped = 1;
          PrintXML(HTML::dl(HTML::dt("input"),
                          HTML::dd(HTML::pre($text)),
                          HTML::dt("regexp"),
                          HTML::dd(HTML::pre($match->regexp_ind, ":", $regexps[$match->regexp_ind])),
                          HTML::dt("prematch"),
                          HTML::dd(HTML::pre($match->prematch)),
                          HTML::dt("match"),
                          HTML::dd(HTML::pre($match->match)),
                          HTML::dt("postmatch"),
                          HTML::dd(HTML::pre($match->postmatch))
                          ));
        }
        return $match;
    }
}



/**
 * A simple markup rule (i.e. terminal token).
 *
 * These are defined by a regexp.
 *
 * When a match is found for the regexp, the matching text is replaced.
 * The replacement content is obtained by calling the SimpleMarkup::markup method.
 */ 
class SimpleMarkup
{
    var $_match_regexp;

    /** Get regexp.
     *
     * @return string Regexp which matches this token.
     */
    function getMatchRegexp () {
        return $this->_match_regexp;
    }

    /** Markup matching text.
     *
     * @param string $match The text which matched the regexp
     * (obtained from getMatchRegexp).
     *
     * @return mixed The expansion of the matched text.
     */
    function markup ($match /*, $body */) {
        trigger_error("pure virtual", E_USER_ERROR);
    }
}

/**
 * A balanced markup rule.
 *
 * These are defined by a start regexp, and an end regexp.
 */ 
class BalancedMarkup
{
    var $_start_regexp;

    /** Get the starting regexp for this rule.
     *
     * @return string The starting regexp.
     */
    function getStartRegexp () {
        return $this->_start_regexp;
    }
    
    /** Get the ending regexp for this rule.
     *
     * @param string $match The text which matched the starting regexp.
     *
     * @return string The ending regexp.
     */
    function getEndRegexp ($match) {
        return $this->_end_regexp;
    }

    /** Get expansion for matching input.
     *
     * @param string $match The text which matched the starting regexp.
     *
     * @param mixed $body Transformed text found between the starting
     * and ending regexps.
     *
     * @return mixed The expansion of the matched text.
     */
    function markup ($match, $body) {
        trigger_error("pure virtual", E_USER_ERROR);
    }
}

class Markup_escape  extends SimpleMarkup
{
    function getMatchRegexp () {
        return ESCAPE_CHAR . '(?: [[:alnum:]]+ | .)';
    }
    
    function markup ($match) {
        assert(strlen($match) >= 2);
        return substr($match, 1);
    }
}

/**
 * [image.jpg size=50% border=5], [image.jpg size=50x30]
 * Support for the following attributes: see stdlib.php:LinkImage()
 *   size=<precent>%, size=<width>x<height>
 *   border=n, align=\w+, hspace=n, vspace=n
 */
function isImageLink($link) {
    if (!$link) return false;
    assert(defined('INLINE_IMAGES'));
    return preg_match("/\\.(" . INLINE_IMAGES . ")$/i", $link)
        or preg_match("/\\.(" . INLINE_IMAGES . ")\s+(size|border|align|hspace|vspace)=/i", $link);
}

function LinkBracketLink($bracketlink) {

    // $bracketlink will start and end with brackets; in between will
    // be either a page name, a URL or both separated by a pipe.
    
    // Strip brackets and leading space
    // FIXME: \n inside [] will lead to errors
    preg_match('/(\#?) \[\s* (?: (.*?) \s* (?<!' . ESCAPE_CHAR . ')(\|) )? \s* (.+?) \s*\]/x',
	       $bracketlink, $matches);
    if (count($matches) < 4) {
    	trigger_error(_("Invalid [] syntax ignored").": ".$bracketlink, E_USER_WARNING);
    	return new Cached_Link;
    }
    list (, $hash, $label, $bar, $rawlink) = $matches;

    $label = UnWikiEscape($label);
    /*
     * Check if the user has typed a explicit URL. This solves the
     * problem where the URLs have a ~ character, which would be stripped away.
     *   "[http:/server/~name/]" will work as expected
     *   "http:/server/~name/"   will NOT work as expected, will remove the ~
     */
    if (   string_starts_with ($rawlink, "http://")
        or string_starts_with ($rawlink, "https://") ) 
    {
        $link = $rawlink;
        // Mozilla Browser URI Obfuscation Weakness 2004-06-14
        //   http://www.securityfocus.com/bid/10532/
        //   goodurl+"%2F%20%20%20."+badurl
        if (preg_match("/%2F(%20)+\./i", $rawlink)) {
            $rawlink = preg_replace("/%2F(%20)+\./i","%2F.",$rawlink);
        }
    } else
        $link  = UnWikiEscape($rawlink);

    /* Relatives links by Joel Schaubert.
     * Recognize [../bla] or [/bla] as relative links, without needing http://
     * but [ /link ] only if SUBPAGE_SEPERATOR is not "/". 
     * Normally /Page links to the subpage /Page.
     */
    if (SUBPAGE_SEPARATOR == '/') {
    	if (preg_match('/^\.\.\//', $link)) {
            return new Cached_ExternalLink($link, $label);
    	}
    } else if (preg_match('/^(\.\.\/|\/)/', $link)) {
        return new Cached_ExternalLink($link, $label);
    }
    // [label|link]
    // if label looks like a url to an image, we want an image link.
    if (isImageLink($label)) {
        $imgurl = $label;
        $intermap = getInterwikiMap();
        if (preg_match("/^" . $intermap->getRegexp() . ":/", $label)) {
            $imgurl = $intermap->link($label);
            $imgurl = $imgurl->getAttr('href');
        } elseif (! preg_match("#^(" . ALLOWED_PROTOCOLS . "):#", $imgurl)) {
            // local theme linkname like 'images/next.gif'.
            global $WikiTheme;
            $imgurl = $WikiTheme->getImageURL($imgurl);
        }
        $label = LinkImage($imgurl, $link);
    }

    if ($hash) {
        // It's an anchor, not a link...
        $id = MangleXmlIdentifier($link);
        return HTML::a(array('name' => $id, 'id' => $id),
                       $bar ? $label : $link);
    }

    if (preg_match("#^(" . ALLOWED_PROTOCOLS . "):#", $link)) {
        // if it's an image, embed it; otherwise, it's a regular link
        if (isImageLink($link) and empty($label)) // patch #1348996 by Robert Litwiniec
            return LinkImage($link, $label);
        else
            return new Cached_ExternalLink($link, $label);
    }
    elseif (substr($link,0,8) == 'phpwiki:')
        return new Cached_PhpwikiURL($link, $label);

    /* Semantic relations and attributes. 
     * Relation and attribute names must be word chars only, no space.
     * Links and Attributes may contain everything. word, nums, units, space, groupsep, numsep, ...
     */
    elseif (preg_match("/^ (\w+) (:[:=]) (.*) $/x", $link) and !isImageLink($link))
        return new Cached_SemanticLink($link, $label);

    /* Do not store the link */    
    elseif (substr($link,0,1) == ':')
        return new Cached_WikiLink($link, $label);

    /*
     * Inline images in Interwiki urls's:
     * [File:my_image.gif] inlines the image,
     * File:my_image.gif shows a plain inter-wiki link,
     * [what a pic|File:my_image.gif] shows a named inter-wiki link to the gif
     * [File:my_image.gif|what a pic] shows a inlimed image linked to the page "what a pic"
     *
     * Note that for simplicity we will accept embedded object tags (non-images) 
     * here also, and seperate them later in LinkImage()
     */
    elseif (strstr($link,':')
            and ($intermap = getInterwikiMap()) 
            and preg_match("/^" . $intermap->getRegexp() . ":/", $link)) 
    {
        // trigger_error("label: $label link: $link", E_USER_WARNING);
        if (empty($label) and isImageLink($link)) {
            // if without label => inlined image [File:xx.gif]
            $imgurl = $intermap->link($link);
            return LinkImage($imgurl->getAttr('href'), $label);
        }
        return new Cached_InterwikiLink($link, $label);
    } else {
        // Split anchor off end of pagename.
        if (preg_match('/\A(.*)(?<!'.ESCAPE_CHAR.')#(.*?)\Z/', $rawlink, $m)) {
            list(,$rawlink,$anchor) = $m;
            $pagename = UnWikiEscape($rawlink);
            $anchor = UnWikiEscape($anchor);
            if (!$label)
                $label = $link;
        }
        else {
            $pagename = $link;
            $anchor = false;
        }
        return new Cached_WikiLink($pagename, $label, $anchor);
    }
}

class Markup_bracketlink  extends SimpleMarkup
{
    var $_match_regexp = "\\#? \\[ .*? [^]\\s] .*? \\]";
    
    function markup ($match) {
        $link = LinkBracketLink($match);
        assert($link->isInlineElement());
        return $link;
    }
}

class Markup_spellcheck extends SimpleMarkup
{
    function Markup_spellcheck () {
	$this->suggestions = $GLOBALS['request']->getArg('suggestions');
    }
    function getMatchRegexp () {
    	if (empty($this->suggestions))
    	    return "(?# false )";
	$words = array_keys($this->suggestions);
        return "(?<= \W ) (?:" . join('|', $words) . ") (?= \W )";
    }
    
    function markup ($match) {
    	if (empty($this->suggestions) or empty($this->suggestions[$match]))
    	    return $match;
        return new Cached_SpellCheck(UnWikiEscape($match), $this->suggestions[$match]);
    }
}

class Markup_searchhighlight extends SimpleMarkup
{
    function Markup_searchhighlight () {
        $result = $GLOBALS['request']->_searchhighlight;
        require_once("lib/TextSearchQuery.php");
        $query = new TextSearchQuery($result['query']);
        $this->hilight_re = $query->getHighlightRegexp();
        $this->engine = $result['engine'];
    }
    function getMatchRegexp () {
        return $this->hilight_re;
    }
    function markup ($match) {
        return new Cached_SearchHighlight(UnWikiEscape($match), $this->engine);
    }
}

class Markup_url extends SimpleMarkup
{
    function getMatchRegexp () {
        return "(?<![[:alnum:]]) (?:" . ALLOWED_PROTOCOLS . ") : [^\s<>\"']+ (?<![ ,.?; \] \) ])";
    }
    
    function markup ($match) {
        return new Cached_ExternalLink(UnWikiEscape($match));
    }
}

class Markup_interwiki extends SimpleMarkup
{
    function getMatchRegexp () {
        $map = getInterwikiMap();
        return "(?<! [[:alnum:]])" . $map->getRegexp(). ": [^:=]\S+ (?<![ ,.?;! \] \) \" \' ])";
    }

    function markup ($match) {
        return new Cached_InterwikiLink(UnWikiEscape($match));
    }
}

class Markup_semanticlink extends SimpleMarkup
{
    var $_match_regexp = "(?:\w+:[:=]\S+)"; // no units seperated by space allowed here

    function markup ($match) {
        return new Cached_SemanticLink(UnWikiEscape($match));
    }
}

class Markup_wikiword extends SimpleMarkup
{
    function getMatchRegexp () {
        global $WikiNameRegexp;
        if (!trim($WikiNameRegexp)) return " " . WIKI_NAME_REGEXP;
        return " $WikiNameRegexp";
    }

    function markup ($match) {
        if (!$match) return false;
        if ($this->_isWikiUserPage($match))
            return new Cached_UserLink($match); //$this->_UserLink($match);
        else
            return new Cached_WikiLink($match);
    }

    // FIXME: there's probably a more useful place to put these two functions    
    function _isWikiUserPage ($page) {
        global $request;
        $dbi = $request->getDbh();
        $page_handle = $dbi->getPage($page);
        if ($page_handle and $page_handle->get('pref'))
            return true;
        else
            return false;
    }

    function _UserLink($PageName) {
        $link = HTML::a(array('href' => $PageName));
        $link->pushContent(PossiblyGlueIconToText('wikiuser', $PageName));
        $link->setAttr('class', 'wikiuser');
        return $link;
    }
}

class Markup_linebreak extends SimpleMarkup
{
    //var $_match_regexp = "(?: (?<! %) %%% (?! %) | <(?:br|BR)> | <(?:br|BR) \/> )";
    var $_match_regexp = "(?: (?<! %) %%% (?! %) | <(?:br|BR)> )";

    function markup ($match) {
        return HTML::br();
    }
}

class Markup_old_emphasis  extends BalancedMarkup
{
    var $_start_regexp = "''|__";

    function getEndRegexp ($match) {
        return $match;
    }
    
    function markup ($match, $body) {
        $tag = $match == "''" ? 'em' : 'strong';
        return new HtmlElement($tag, $body);
    }
}

class Markup_nestled_emphasis extends BalancedMarkup
{
    function getStartRegexp() {
	static $start_regexp = false;

	if (!$start_regexp) {
	    // The three possible delimiters
            // (none of which can be followed by itself.)
	    $i = "_ (?! _)";
	    $b = "\\* (?! \\*)";
	    $tt = "= (?! =)";

	    $any = "(?: ${i}|${b}|${tt})"; // any of the three.

	    // Any of [_*=] is okay if preceded by space or one of [-"'/:]
	    $start[] = "(?<= \\s|^|[-\"'\\/:]) ${any}";

	    // _ or * is okay after = as long as not immediately followed by =
	    $start[] = "(?<= =) (?: ${i}|${b}) (?! =)";
	    // etc...
	    $start[] = "(?<= _) (?: ${b}|${tt}) (?! _)";
	    $start[] = "(?<= \\*) (?: ${i}|${tt}) (?! \\*)";


	    // any delimiter okay after an opening brace ( [{<(] )
	    // as long as it's not immediately followed by the matching closing
	    // brace.
	    $start[] = "(?<= { ) ${any} (?! } )";
	    $start[] = "(?<= < ) ${any} (?! > )";
	    $start[] = "(?<= \\( ) ${any} (?! \\) )";
	    
	    $start = "(?:" . join('|', $start) . ")";
	    
	    // Any of the above must be immediately followed by non-whitespace.
	    $start_regexp = $start . "(?= \S)";
	}

	return $start_regexp;
    }

    function getEndRegexp ($match) {
        $chr = preg_quote($match);
        return "(?<= \S | ^ ) (?<! $chr) $chr (?! $chr) (?= \s | [-)}>\"'\\/:.,;!? _*=] | $)";
    }
    
    function markup ($match, $body) {
        switch ($match) {
        case '*': return new HtmlElement('b', $body);
        case '=': return new HtmlElement('tt', $body);
        case '_': return new HtmlElement('i', $body);
        }
    }
}

class Markup_html_emphasis extends BalancedMarkup
{
    var $_start_regexp = 
        "<(?: b|big|i|small|tt|em|strong|cite|code|dfn|kbd|samp|strike|del|var|sup|sub )>";

    function getEndRegexp ($match) {
        return "<\\/" . substr($match, 1);
    }
    
    function markup ($match, $body) {
        $tag = substr($match, 1, -1);
        return new HtmlElement($tag, $body);
    }
}

class Markup_html_divspan extends BalancedMarkup
{
    var $_start_regexp = 
        "<(?: div|span )(?: \s[^>]*)?>";

    function getEndRegexp ($match) {
    	if (substr($match,1,4) == 'span')
    	    $tag = 'span';
    	else
    	    $tag = 'div';
        return "<\\/" . $tag . '>';
    }
    
    function markup ($match, $body) {
    	if (substr($match,1,4) == 'span')
    	    $tag = 'span';
    	else
    	    $tag = 'div';
    	$rest = substr($match,1+strlen($tag),-1);
    	if (!empty($rest)) {
    	    list($key,$val) = explode("=",$rest);
    	    $args = array($key => $val);
    	} else $args = array();
        return new HtmlElement($tag, $args, $body);
    }
}


class Markup_html_abbr extends BalancedMarkup
{
    //rurban: abbr|acronym need an optional title tag.
    //sf.net bug #728595
    var $_start_regexp = "<(?: abbr|acronym )(?: \stitle=[^>]*)?>";

    function getEndRegexp ($match) {
    	if (substr($match,1,4) == 'abbr')
    	    $tag = 'abbr';
    	else
    	    $tag = 'acronym';
        return "<\\/" . $tag . '>';
    }
    
    function markup ($match, $body) {
    	if (substr($match,1,4) == 'abbr')
    	    $tag = 'abbr';
    	else
    	    $tag = 'acronym';
    	$rest = substr($match,1+strlen($tag),-1);
    	if (!empty($rest)) {
    	    list($key,$val) = explode("=",$rest);
    	    $args = array($key => $val);
    	} else $args = array();
        return new HtmlElement($tag, $args, $body);
    }
}

/** ENABLE_MARKUP_COLOR
 *  See http://www.pmwiki.org/wiki/PmWiki/WikiStyles and
 *      http://www.flexwiki.com/default.aspx/FlexWiki/FormattingRules.html
 */
class Markup_color extends BalancedMarkup {
    // %color=blue% blue text %% and back to normal
    var $_start_regexp = "%color=(?: [^%]*)%";
    var $_end_regexp = "%%";
    
    function markup ($match, $body) {
    	$color = strtoupper(substr($match, 7, -1));
        if (strlen($color) != 7 
            and in_array($color, array('RED', 'BLUE', 'GRAY', 'YELLOW', 'GREEN', 'CYAN', 'BLACK'))) 
	{   // must be a valid color name
            return new HtmlElement('font', array('color' => $color), $body);
        } elseif ((substr($color,0,1) == '#') 
                  and (strspn(substr($color,1),'0123456789ABCDEF') == strlen($color)-1)) {
            return new HtmlElement('font', array('color' => $color), $body);
        } else {
            trigger_error(sprintf(_("unknown color %s ignored"), substr($match, 7, -1)), E_USER_WARNING);
        }
        	
    }
}

// Special version for single-line plugins formatting, 
//  like: '<small>< ?plugin PopularNearby ? ></small>'
class Markup_plugin extends SimpleMarkup
{
    var $_match_regexp = '<\?plugin(?:-form)?\s[^\n]+?\?>';

    function markup ($match) {
	//$xml = new Cached_PluginInvocation($match);
	//$xml->setTightness(true,true);
	return new Cached_PluginInvocation($match);
    }
}

// Special version for plugins in xml syntax 
// <name arg=value>body</name> or <name /> => < ? plugin pluginname arg=value body ? >
// PLUGIN_MARKUP_MAP = "html:RawHtml dot:GraphViz toc:CreateToc amath:AsciiMath richtable:RichTable include:IncludePage tex:TexToPng"
class Markup_xml_plugin extends BalancedMarkup
{
    //var $_start_regexp = "<(?: ".join('|',PLUGIN_MARKUP_MAP)." )(?: \s[^>]*)>";

    function getStartRegexp () {
	global $PLUGIN_MARKUP_MAP;
        static $_start_regexp;
        if ($_start_regexp) return $_start_regexp;
        if (empty($PLUGIN_MARKUP_MAP))
            return '';
        //"<(?: html|dot|toc|amath|richtable|include|tex )(?: \s[^>]*)>"
	$_start_regexp = "<(?: ".join('|',array_keys($PLUGIN_MARKUP_MAP))." )(?: \s[^>]* | / )>";
        return $_start_regexp;
    }
    function getEndRegexp ($match) {
        return "<\\/" . $match . '>';
    }
    function markup ($match, $body) {
	global $PLUGIN_MARKUP_MAP;
        $name = substr($match,2,-2); 
	$vars = '';
        if (preg_match('/^(\S+)\|(.*)$/', $name, $_m)) {
            $name = $_m[1];
            $vars = $_m[2]; //str_replace(' ', '&', $_m[2]);
        }
        if (!isset($PLUGIN_MARKUP_MAP[$name])) {
            trigger_error("No plugin for $name $vars defined.", E_USER_WARNING);
            return "";
        }
        $plugin = $PLUGIN_MARKUP_MAP[$name];
	return new Cached_PluginInvocation("<"."?plugin $plugin $vars $body ?".">");
    }
}

/** ENABLE_MARKUP_TEMPLATE
 *  Template syntax similar to mediawiki
 *  {{template}}
 * => < ? plugin Template page=template ? >
 *  {{template|var=value|...}}
 * => < ? plugin Template page=template var=value ... ? >
 */
class Markup_template_plugin  extends SimpleMarkup
{
    // patch #1732793: allow \n, mult. {{ }} in one line, and single letters
    var $_match_regexp = '\{\{.*?\}\}';
    
    function markup ($match) {
        $page = substr(str_replace("\n", "", $match),2,-2); $vars = '';
        if (preg_match('/^(\S+)\|(.*)$/', $page, $_m)) {
            $page = $_m[1];
            $vars = '"' . preg_replace('/\|/', '" "', $_m[2]) . '"'; 
            $vars = preg_replace('/"(\S+)=([^"]*)"/', '\\1="\\2"', $vars);
        }
        if ($vars)
    	    $s = '<'.'?plugin Template page="'.$page.'" '.$vars.' ?'.'>';
    	else
    	    $s = '<'.'?plugin Template page="' . $page . '" ?'.'>';
	return new Cached_PluginInvocation($s);
    }
}

// "..." => "&#133;"  browser specific display (not cached?)
// Support some HTML::Entities: (C) for copy, --- for mdash, -- for ndash
// TODO: "--" => "&emdash;" browser specific display (not cached?)

class Markup_html_entities  extends SimpleMarkup {
    //var $_match_regexp = '(: \.\.\.|\-\-|\-\-\-|\(C\) )';

    function Markup_html_entities() {
        $this->_entities = array('...'  => '&#133;',
                                 '--'   => '&ndash;',
                                 '---'  => '&mdash;',
                                 '(C)'  => '&copy;',
                                 '&copy;' => '&copy;',
                                 '&trade;'  => '&trade;',
                                 );
        $this->_match_regexp = 
            '(: ' . 
            join('|', array_map('preg_quote', array_keys($this->_entities))) . 
            ' )';
    }
   
    function markup ($match) {
        return HTML::Raw($this->_entities[$match]);
    }
}

class Markup_isonumchars  extends SimpleMarkup {
    var $_match_regexp = '\&\#\d{2,5};';
    
    function markup ($match) {
        return HTML::Raw($match);
    }
}

class Markup_isohexchars extends SimpleMarkup {
    // hexnums, like &#x00A4; <=> &curren;
    var $_match_regexp = '\&\#x[0-9a-fA-F]{2,4};';
    
    function markup ($match) {
        return HTML::Raw($match);
    }
}

// FIXME: Do away with magic phpwiki forms.  (Maybe phpwiki: links too?)
// FIXME: Do away with plugin-links.  They seem not to be used.
//Plugin link

class InlineTransformer
{
    var $_regexps = array();
    var $_markup = array();
    
    function InlineTransformer ($markup_types = false) {
        global $request;
	// We need to extend the inline parsers by certain actions, like SearchHighlight, 
	// SpellCheck and maybe CreateToc.
        if (!$markup_types) {
            $non_default = false;
            $markup_types = array
                ('escape', 'bracketlink', 'url',
                 'interwiki',  'semanticlink', 'wikiword', 'linebreak',
                 'old_emphasis', 'nestled_emphasis',
                 'html_emphasis', 'html_abbr', 'plugin',
                 'isonumchars', 'isohexchars', /*'html_entities'*/
                 );
	    if (DISABLE_MARKUP_WIKIWORD)
                $markup_types = array_remove($markup_types, 'wikiword');

	    $action = $request->getArg('action');
	    if ($action == 'SpellCheck' and $request->getArg('suggestions'))
	    {   // insert it after url
		array_splice($markup_types, 2, 1, array('url','spellcheck'));
	    }
	    if (isset($request->_searchhighlight))
	    {   // insert it after url
		array_splice($markup_types, 2, 1, array('url','searchhighlight'));
                //$request->setArg('searchhighlight', false);
	    }
        } else {
            $non_default = true;
	}
        foreach ($markup_types as $mtype) {
            $class = "Markup_$mtype";
            $this->_addMarkup(new $class);
        }
        if (ENABLE_MARKUP_DIVSPAN and !$non_default)
            $this->_addMarkup(new Markup_html_divspan);
        if (ENABLE_MARKUP_COLOR and !$non_default)
            $this->_addMarkup(new Markup_color);
        if (ENABLE_MARKUP_TEMPLATE and !$non_default)
            $this->_addMarkup(new Markup_template_plugin);
        // This does not work yet
        if (0 and PLUGIN_MARKUP_MAP and !$non_default)
            $this->_addMarkup(new Markup_xml_plugin);
    }

    function _addMarkup ($markup) {
        if (isa($markup, 'SimpleMarkup'))
            $regexp = $markup->getMatchRegexp();
        else
            $regexp = $markup->getStartRegexp();

        assert( !isset($this->_markup[$regexp]) );
        assert( strlen(trim($regexp)) > 0 );
        $this->_regexps[] = $regexp;
        $this->_markup[] = $markup;
    }
        
    function parse (&$text, $end_regexps = array('$')) {
        $regexps = $this->_regexps;

        // $end_re takes precedence: "favor reduce over shift"
        array_unshift($regexps, $end_regexps[0]);
        //array_push($regexps, $end_regexps[0]);
        $regexps = new RegexpSet($regexps);
        
        $input = $text;
        $output = new XmlContent;

        $match = $regexps->match($input);
        
        while ($match) {
            if ($match->regexp_ind == 0) {
                // No start pattern found before end pattern.
                // We're all done!
                if (isset($markup) and is_object($markup) 
                    and isa($markup,'Markup_plugin')) 
                {
                    $current =& $output->_content[count($output->_content)-1];
                    $current->setTightness(true,true);
                }
                $output->pushContent($match->prematch);
                $text = $match->postmatch;
                return $output;
            }

            $markup = $this->_markup[$match->regexp_ind - 1];
            $body = $this->_parse_markup_body($markup, $match->match, 
                                              $match->postmatch, $end_regexps);
            if (!$body) {
                // Couldn't match balanced expression.
                // Ignore and look for next matching start regexp.
                $match = $regexps->nextMatch($input, $match);
                continue;
            }

            // Matched markup.  Eat input, push output.
            // FIXME: combine adjacent strings.
            if (isa($markup, 'SimpleMarkup'))
                $current = $markup->markup($match->match);
            else
                $current = $markup->markup($match->match, $body);
            $input = $match->postmatch;
            if (isset($markup) and is_object($markup) 
                and isa($markup,'Markup_plugin')) 
            {
                $current->setTightness(true,true);
            }
            $output->pushContent($match->prematch, $current);

            $match = $regexps->match($input);
        }

        // No pattern matched, not even the end pattern.
        // Parse fails.
        return false;
    }

    function _parse_markup_body ($markup, $match, &$text, $end_regexps) {
        if (isa($markup, 'SimpleMarkup'))
            return true;        // Done. SimpleMarkup is simple.

        if (!is_object($markup)) return false; // Some error: Should assert
        array_unshift($end_regexps, $markup->getEndRegexp($match));

        // Optimization: if no end pattern in text, we know the
        // parse will fail.  This is an important optimization,
        // e.g. when text is "*lots *of *start *delims *with
        // *no *matching *end *delims".
        $ends_pat = "/(?:" . join(").*(?:", $end_regexps) . ")/xs";
        if (!preg_match($ends_pat, $text))
            return false;
        return $this->parse($text, $end_regexps);
    }
}

class LinkTransformer extends InlineTransformer
{
    function LinkTransformer () {
        $this->InlineTransformer(array('escape', 'bracketlink', 'url',
                                       'semanticlink', 'interwiki', 'wikiword', 
                                       ));
    }
}

class NowikiTransformer extends InlineTransformer
{
    function NowikiTransformer () {
        $this->InlineTransformer
            (array('linebreak',
                   'html_emphasis', 'html_abbr', 'plugin',
                   'isonumchars', 'isohexchars', /*'html_entities',*/
                   ));
    }
}

function TransformInline($text, $markup = 2.0, $basepage=false) {
    static $trfm;
    $action = $GLOBALS['request']->getArg('action');
    if (empty($trfm) or $action == 'SpellCheck') {
        $trfm = new InlineTransformer;
    }
    
    if ($markup < 2.0) {
        $text = ConvertOldMarkup($text, 'inline');
    }

    if ($basepage) {
        return new CacheableMarkup($trfm->parse($text), $basepage);
    }
    return $trfm->parse($text);
}

function TransformLinks($text, $markup = 2.0, $basepage = false) {
    static $trfm;
    
    if (empty($trfm)) {
        $trfm = new LinkTransformer;
    }

    if ($markup < 2.0) {
        $text = ConvertOldMarkup($text, 'links');
    }
    
    if ($basepage) {
        return new CacheableMarkup($trfm->parse($text), $basepage);
    }
    return $trfm->parse($text);
}

/**
 * Transform only html markup and entities.
 */
function TransformInlineNowiki($text, $markup = 2.0, $basepage=false) {
    static $trfm;
    
    if (empty($trfm)) {
        $trfm = new NowikiTransformer;
    }
    if ($basepage) {
        return new CacheableMarkup($trfm->parse($text), $basepage);
    }
    return $trfm->parse($text);
}


// $Log: InlineParser.php,v $
// Revision 1.91  2007/06/07 18:56:57  rurban
// patch #1732793: allow \n, mult. {{ }} in one line, and single
// letters (slightly improved) by AlJeux and ReiniUrban
//
// Revision 1.90  2007/03/18 17:35:14  rurban
// Fix :DontStoreLink
//
// Revision 1.89  2007/02/17 14:16:28  rurban
// fix color GREY to GRAY
//
// Revision 1.88  2007/01/21 13:15:50  rurban
// Support spaces in attributes and relation links
//
// Revision 1.87  2007/01/20 15:53:51  rurban
// Rewrite of SearchHighlight: through ActionPage and InlineParser
//
// Revision 1.86  2007/01/20 11:25:07  rurban
// add SpellCheck support
//
// Revision 1.85  2007/01/07 18:42:49  rurban
// Add support for non-bracket semantic relation parsing. Assert empty regex (interwikimap?) earlier. Change {{Template||}} vars handling to new style. Stricter interwikimap matching not to find semantic links
//
// Revision 1.84  2007/01/02 13:18:07  rurban
// fix semantic attributes syntax :=, not :-, disable DIVSPAN and PLUGIN_MARKUP_MAP
//
// Revision 1.83  2006/12/22 00:23:24  rurban
// Fix Bug #1540007 "hardened-php issue, crawlers related"
// Broken str_replace with strings > 200 chars
//
// Revision 1.82  2006/12/02 19:53:05  rurban
// Simplify DISABLE_MARKUP_WIKIWORD handling by adding the new function
// stdlib: array_remove(). Hopefully PHP will not add this natively sooner
// or later.
//
// Revision 1.81  2006/11/19 13:52:52  rurban
// improve debug output: regex only once
//
// Revision 1.80  2006/10/12 06:32:30  rurban
// Optionally support new tags <div>, <span> with ENABLE_MARKUP_DIVSPAN (in work)
//
// Revision 1.79  2006/10/08 12:38:11  rurban
// New special interwiki link markup [:LinkTo] without storing the backlink
//
// Revision 1.78  2006/09/03 09:53:52  rurban
// more colors, case-insensitive color names
//
// Revision 1.77  2006/08/25 19:02:02  rurban
// patch #1348996 by Robert Litwiniec: fix show image semantics if label is given
//
// Revision 1.76  2006/08/19 11:02:35  rurban
// add strike and del to html emphasis: Patch #1542894 by Kai Krakow
//
// Revision 1.75  2006/08/15 13:43:10  rurban
// add Markup_xml_plugin (untested) and fix Markup_template_plugin
//
// Revision 1.74  2006/07/23 14:03:18  rurban
// add new feature: DISABLE_MARKUP_WIKIWORD
//
// Revision 1.73  2006/04/15 12:20:36  rurban
// fix relatives links patch by Joel Schaubert for [/
//
// Revision 1.72  2006/03/07 20:43:29  rurban
// relative external link, if no internal subpage. by joel Schaubert
//
// Revision 1.71  2005/11/14 22:31:12  rurban
// add SemanticWeb support
//
// Revision 1.70  2005/10/31 16:45:23  rurban
// added cfg-able markups only for default TextTransformation, not for links and others
//
// Revision 1.69  2005/09/14 05:57:19  rurban
// make ENABLE_MARKUP_TEMPLATE optional
//
// Revision 1.68  2005/09/10 21:24:32  rurban
// optionally support {{Template|vars}} syntax
//
// Revision 1.67  2005/06/06 17:41:20  rurban
// support new ENABLE_MARKUP_COLOR
//
// Revision 1.66  2005/04/23 11:15:49  rurban
// handle allowed inlined objects within INLINE_IMAGES
//
// Revision 1.65  2005/03/27 18:24:17  rurban
// add Log
//

// (c-file-style: "gnu")
// Local Variables:
// mode: php
// tab-width: 8
// c-basic-offset: 4
// c-hanging-comment-ender-p: nil
// indent-tabs-mode: nil
// End:   
?>