xref: /dokuwiki/inc/Parsing/ParserMode/Wordblock.php (revision 504c13e8df88563c11b3720b317991bc38835a35)
1<?php
2
3namespace dokuwiki\Parsing\ParserMode;
4
5use dokuwiki\Parsing\Handler;
6use dokuwiki\Parsing\Lexer\Lexer;
7
8/**
9 * @fixme is this actually used?
10 */
11class Wordblock extends AbstractMode
12{
13    protected $badwords = [];
14    protected $pattern = '';
15
16    /**
17     * Wordblock constructor.
18     * @param $badwords
19     */
20    public function __construct($badwords)
21    {
22        $this->badwords = $badwords;
23    }
24
25    /** @inheritdoc */
26    public function getSort()
27    {
28        return 250;
29    }
30
31    /** @inheritdoc */
32    public function preConnect()
33    {
34
35        if (count($this->badwords) == 0 || $this->pattern != '') {
36            return;
37        }
38
39        $sep = '';
40        foreach ($this->badwords as $badword) {
41            $this->pattern .= $sep . '(?<=\b)(?i)' . Lexer::escape($badword) . '(?-i)(?=\b)';
42            $sep = '|';
43        }
44    }
45
46    /** @inheritdoc */
47    public function connectTo($mode)
48    {
49        if ((string) $this->pattern !== '') {
50            $this->Lexer->addSpecialPattern($this->pattern, $mode, 'wordblock');
51        }
52    }
53
54    /** @inheritdoc */
55    public function handle($match, $state, $pos, Handler $handler)
56    {
57        $handler->addCall('wordblock', [$match], $pos);
58        return true;
59    }
60}
61