Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 22 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 2 |
MediaWikiTwig | |
0.00% |
0 / 10 |
|
0.00% |
0 / 2 |
6 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
render | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
MediaWikiTwigCallbacks | |
0.00% |
0 / 12 |
|
0.00% |
0 / 6 |
42 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFunctions | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
twig_wfMessage | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
twig_wfText | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
twig_wfWikiText | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Nice little wrapper around Twig for mediawiki. Exposes some MW functions |
4 | * to a twig template as well. |
5 | * |
6 | * -- License -- |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by |
9 | * the Free Software Foundation; either version 2 of the License, or |
10 | * (at your option) any later version. |
11 | * |
12 | * This program is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * GNU General Public License for more details. |
16 | * |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; if not, write to the Free Software Foundation, Inc., |
19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
20 | * http://www.gnu.org/copyleft/gpl.html |
21 | * |
22 | * @file |
23 | */ |
24 | |
25 | /** |
26 | * Wrapper around the Twig templating engine that allows MW code to easily integrate HTML templates. |
27 | * Those templates can also easily call MW templates and code in turn. This is not the safest thing |
28 | * in the world. Escaping is required and needs to be looked at very carefully! |
29 | * |
30 | * NOTE: Twig autoescaping is DISABLED! It plays havock with MW template autoexpansion. |
31 | */ |
32 | class MediaWikiTwig { |
33 | /** @var Twig_Environment */ |
34 | protected $mTwig; |
35 | /** @var array */ |
36 | protected $mCallbacks; |
37 | |
38 | /** |
39 | * @param string $templatePath Absolute path to root of Twig templates directory. All |
40 | * templates must include the relative path from this root. |
41 | * @param IContextSource $context The page context. IE from a MW page $this->getContext() |
42 | */ |
43 | public function __construct( $templatePath, IContextSource $context ) { |
44 | global $wgTwigCachePath; |
45 | |
46 | $loader = new MediaWikiTwigLoader( $templatePath, $context ); |
47 | $this->mTwig = new Twig_Environment( $loader, [ |
48 | 'cache' => $wgTwigCachePath . '/' . md5( $templatePath ), |
49 | 'auto_reload' => true, |
50 | 'autoescape' => false, |
51 | ] ); |
52 | $this->mTwig->addExtension( new MediaWikiTwigCallbacks( $context ) ); |
53 | } |
54 | |
55 | /** |
56 | * Renders a Twig template and returns the processed HTML data. |
57 | * |
58 | * @param string $template Relative path to the Twig template |
59 | * @param array $params Any key->value parameters that the template requires |
60 | * |
61 | * @return string Rendered HTML data |
62 | */ |
63 | public function render( $template, $params = [] ) { |
64 | return $this->mTwig->render( $template, $params ); |
65 | } |
66 | } |
67 | |
68 | /** |
69 | * All exposed MW functions to Twig Templates |
70 | */ |
71 | class MediaWikiTwigCallbacks extends Twig_Extension { |
72 | /** @var IContextSource */ |
73 | protected $mContext; |
74 | |
75 | public function __construct( IContextSource $context ) { |
76 | $this->mContext = $context; |
77 | } |
78 | |
79 | /** @return string */ |
80 | public function getName() { |
81 | return 'MediaWiki Twig Handler'; |
82 | } |
83 | |
84 | /** |
85 | * @return array<string,Twig_Function_Method> |
86 | */ |
87 | public function getFunctions() { |
88 | return [ |
89 | 'wfMessage' => new Twig_Function_Method( $this, 'twig_wfMessage' ), |
90 | 'wfText' => new Twig_Function_Method( $this, 'twig_wfText' ), |
91 | 'wfWikiText' => new Twig_Function_Method( $this, 'twig_wfWikiText' ), |
92 | ]; |
93 | } |
94 | |
95 | /** |
96 | * Fully parses a MW i18n message into HTML |
97 | * |
98 | * @param string $message Message name |
99 | * @param array $params Any parameters for the message; straight array, no K/V |
100 | * |
101 | * @return string Parsed HTML string |
102 | */ |
103 | public function twig_wfMessage( $message, $params = [] ) { |
104 | return wfMessage( $message, $params )->parse(); |
105 | } |
106 | |
107 | /** |
108 | * Retrieves non-wiki-parsed MW i18n message |
109 | * |
110 | * @param string $message Message name |
111 | * @param array $params Any parameters for the message; straight array, no K/V |
112 | * |
113 | * @return string HTML string |
114 | */ |
115 | public function twig_wfText( $message, $params = [] ) { |
116 | return wfMessage( $message, $params )->text(); |
117 | } |
118 | |
119 | /** |
120 | * Fully parses arbitrary wiki text; use for things like template inclusion. Message inclusion |
121 | * should use wfMessage() |
122 | * |
123 | * @param string $wikiText Arbitrary wiki text |
124 | * |
125 | * @return string Fully parsed HTML string |
126 | */ |
127 | public function twig_wfWikiText( $wikiText ) { |
128 | return $this->mContext->getOutput()->parseInlineAsInterface( $wikiText ); |
129 | } |
130 | } |