Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
OnlyInclude
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 2
182
0.00% covered (danger)
0.00%
0 / 1
 onAny
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 onAnyInclude
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
132
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\Parsoid\Wt2Html\TT;
5
6use Wikimedia\Parsoid\NodeData\DataParsoid;
7use Wikimedia\Parsoid\Tokens\EndTagTk;
8use Wikimedia\Parsoid\Tokens\EOFTk;
9use Wikimedia\Parsoid\Tokens\KV;
10use Wikimedia\Parsoid\Tokens\SelfclosingTagTk;
11use Wikimedia\Parsoid\Tokens\TagTk;
12use Wikimedia\Parsoid\Tokens\Token;
13
14/**
15 * OnlyInclude sadly forces synchronous template processing, as it needs to
16 * hold onto all tokens in case an onlyinclude block is encountered later.
17 * This can fortunately be worked around by caching the tokens after
18 * onlyinclude processing (which is a good idea anyway).
19 */
20class OnlyInclude extends TokenHandler {
21    /** @var array */
22    private $accum = [];
23
24    /** @var bool */
25    private $inOnlyInclude = false;
26
27    /** @var bool */
28    private $foundOnlyInclude = false;
29
30    /**
31     * @inheritDoc
32     */
33    public function onAny( $token ): ?TokenHandlerResult {
34        return !empty( $this->options['inTemplate'] ) ?
35            $this->onAnyInclude( $token ) : null;
36    }
37
38    /**
39     * @param Token|array $token
40     * @return TokenHandlerResult|null
41     */
42    private function onAnyInclude( $token ): ?TokenHandlerResult {
43        if ( $token instanceof EOFTk ) {
44            $this->inOnlyInclude = false;
45            if ( count( $this->accum ) && !$this->foundOnlyInclude ) {
46                $res = $this->accum;
47                $res[] = $token;
48                $this->accum = [];
49                return new TokenHandlerResult( $res );
50            } else {
51                $this->foundOnlyInclude = false;
52                $this->accum = [];
53                return null;
54            }
55        }
56
57        $isTag = $token instanceof TagTk ||
58            $token instanceof EndTagTk ||
59            $token instanceof SelfclosingTagTk;
60
61        if ( $isTag && $token->getName() === 'onlyinclude' ) {
62            // FIXME: This doesn't seem to consider self-closing tags
63            // or close really
64            if ( !$this->inOnlyInclude ) {
65                $this->foundOnlyInclude = true;
66                $this->inOnlyInclude = true;
67            } else {
68                $this->inOnlyInclude = false;
69            }
70
71            $tagName = 'mw:Includes/OnlyInclude';
72            if ( $token instanceof EndTagTk ) {
73                $tagName .= '/End';
74            }
75
76            $dp = new DataParsoid;
77            $dp->tsr = $token->dataParsoid->tsr;
78            $dp->src = $dp->tsr->substr(
79                $this->manager->getFrame()->getSrcText()
80            );
81
82            // FIXME: Just drop these, we're inTemplate
83            $meta = new SelfclosingTagTk(
84                'meta', [ new KV( 'typeof', $tagName ) ], $dp
85            );
86
87            return new TokenHandlerResult( [ $meta ] );
88        } else {
89            if ( $this->inOnlyInclude ) {
90                return null;
91            } else {
92                $this->accum[] = $token;
93                return new TokenHandlerResult( [] );
94            }
95        }
96    }
97}