MDL-68738 wiki: Prevent URLs inside links from turning into another link
[moodle.git] / mod / wiki / parser / markups / html.php
CommitLineData
00710f4c
DC
1<?php
2
3/**
4 * HTML parser implementation. It only implements links.
5 *
6 * @author Josep Arús
7 *
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
593b8385 9 * @package mod_wiki
00710f4c
DC
10 */
11include_once("nwiki.php");
12
13class html_parser extends nwiki_parser {
14 protected $blockrules = array();
15
16 protected $section_editing = true;
17
3d89ccb3
MG
18 /** @var int Minimum level of the headers on the page (usually tinymce uses <h1> and atto <h3>) */
19 protected $minheaderlevel = null;
20
00710f4c
DC
21 public function __construct() {
22 parent::__construct();
c159e2aa
FM
23 // The order is important, headers should be parsed before links.
24 $this->tagrules = array(
25 // Headers are considered tags here.
26 'header' => array(
3d89ccb3 27 'expression' => "/<\s*h([1-6])\s*>(.+?)<\/h[1-6]>/is"
c159e2aa
FM
28 ),
29 'link' => $this->tagrules['link'],
30 'url' => $this->tagrules['url']
00710f4c
DC
31 );
32 }
33
3d89ccb3
MG
34 /**
35 * Find minimum header level used on the page (<h1>, <h3>, ...)
36 *
37 * @param string $text
38 * @return int
39 */
40 protected function find_min_header_level($text) {
41 preg_match_all($this->tagrules['header']['expression'], $text, $matches);
42 return !empty($matches[1]) ? min($matches[1]) : 1;
43 }
44
00710f4c
DC
45 protected function before_parsing() {
46 parent::before_parsing();
47
3d89ccb3 48 $this->minheaderlevel = $this->find_min_header_level($this->string);
59641dd5
DM
49
50 // Protect all explicit links from further wiki parsing. The link text may contain another URL which would get
51 // converted into another link via {@see nwiki_parser::$tagrules} 'url' element.
52 if (preg_match_all('/<a\s[^>]+?>(.*?)<\/a>/is', $this->string, $matches)) {
53 foreach (array_unique($matches[0]) as $match) {
54 $this->string = str_replace($match, $this->protect($match), $this->string);
55 }
56 }
57
00710f4c
DC
58 $this->rules($this->string);
59 }
60
61 /**
2264828f
CF
62 * Header tag rule
63 * @param array $match Header regex match
64 * @return string
00710f4c 65 */
2264828f 66 protected function header_tag_rule($match) {
3d89ccb3 67 return $this->generate_header($match[2], (int)$match[1] - $this->minheaderlevel + 1);
00710f4c
DC
68 }
69
70 /**
71 * Section editing: Special for HTML Parser (It parses <h1></h1>)
72 */
73
74 public function get_section($header, $text, $clean = false) {
75 if ($clean) {
76 $text = preg_replace('/\r\n/', "\n", $text);
77 $text = preg_replace('/\r/', "\n", $text);
78 $text .= "\n\n";
79 }
80
3d89ccb3
MG
81 $minheaderlevel = $this->find_min_header_level($text);
82
83 $h1 = array("<\s*h{$minheaderlevel}\s*>", "<\/h{$minheaderlevel}>");
00710f4c 84
7c5c8035 85 $regex = "/(.*?)({$h1[0]}\s*".preg_quote($header, '/')."\s*{$h1[1]}.*?)((?:{$h1[0]}.*)|$)/is";
c159e2aa 86 preg_match($regex, $text, $match);
00710f4c
DC
87
88 if (!empty($match)) {
89 return array($match[1], $match[2], $match[3]);
90 } else {
91 return false;
92 }
93 }
94
95 protected function get_repeated_sections(&$text, $repeated = array()) {
96 $this->repeated_sections = $repeated;
2264828f 97 return preg_replace_callback($this->tagrules['header'], array($this, 'get_repeated_sections_callback'), $text);
00710f4c
DC
98 }
99
100 protected function get_repeated_sections_callback($match) {
2264828f 101 $text = trim($match[2]);
00710f4c
DC
102
103 if (in_array($text, $this->repeated_sections)) {
104 $this->returnvalues['repeated_sections'][] = $text;
105 return parser_utils::h('p', $text);
106 } else {
107 $this->repeated_sections[] = $text;
108 }
109
110 return $match[0];
111 }
112}