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