Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
MediaWikiTwigLoader
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 4
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getSource
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
20
 getCacheKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isFresh
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * MediaWiki and Twig templating. It's... beautiful?
4 *
5 * -- License --
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 */
23
24use Twig\Loader\LoaderInterface;
25
26/**
27 * Provides methods for Twig templates to integrate with MediaWiki.
28 *
29 * Twig Templates can in fact be localized! If the original file name is a.b; then this class will
30 * look for files named a.lang.b where lang is every language code in the current languages fallback
31 * list. It will also of course use the original a.b file if no localized file exists.
32 */
33class MediaWikiTwigLoader implements LoaderInterface {
34
35    /** @var string */
36    private $mTwigTemplatePath;
37
38    /** @var IContextSource */
39    private $mContext;
40
41    /**
42     * @param string $twigTemplatePath
43     * @param IContextSource $context
44     */
45    public function __construct( $twigTemplatePath, IContextSource $context ) {
46        $this->mTwigTemplatePath = $twigTemplatePath;
47        $this->mContext = $context;
48    }
49
50    /**
51     * Simple function; it returns a filesystem Twig template. The trick here is that the twig
52     * template can be localized. A localization
53     *
54     * @param string $name
55     *
56     * @return bool|mixed|string
57     * @throws MWException if the template $name can not be found
58     */
59    public function getSource( $name ) {
60        $lang = $this->mContext->getLanguage();
61
62        // This is supposedly a file in the filesystem. We want to look for the language
63        // variant first, followed by all the fallbacks, only then falling back to the original
64        // title.
65        $parts = explode( '.', $name );
66        $langVariants = [ $lang->getCode() ];
67        $langVariants = array_merge( $langVariants, $lang->getFallbackLanguages() );
68
69        foreach ( $langVariants as $langCode ) {
70            $localParts = $parts;
71            array_splice( $localParts, -1, 0, $langCode );
72            $paths[] = $this->mTwigTemplatePath . '/' . implode( '.', $localParts );
73        }
74        $paths[] = $this->mTwigTemplatePath . '/' . $name;
75
76        // Obtain any of the files in the $paths array
77        foreach ( $paths as $path ) {
78            if ( file_exists( $path ) ) {
79                return file_get_contents( $path );
80            }
81        }
82
83        // If we're here, it means that we couldn't find an appropriate template. Throw the
84        // generic error response.
85        throw new MWException( "Twig template '$name' could not be found to load." );
86    }
87
88    /**
89     * Obtains the cache key name. Really just the name of the page with the language code appended
90     * @param string $name
91     *
92     * @return string
93     */
94    public function getCacheKey( $name ) {
95        return $name . '-' . $this->mContext->getLanguage()->getCode();
96    }
97
98    /**
99     * Will force a refresh after $wgTwigCacheExpiry seconds
100     * @param string $name
101     * @param int $time
102     *
103     * @return bool
104     */
105    public function isFresh( $name, $time ) {
106        global $wgTwigCacheExpiry;
107        return ( time() < ( $time + $wgTwigCacheExpiry ) );
108    }
109}