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