Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
PFTemplateParams
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 2
110
0.00% covered (danger)
0.00%
0 / 1
 run
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
42
 parseWikitextString
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3/**
4 * Defines the #template_display parser function.
5 *
6 * @author Yaron Koren
7 */
8
9class PFTemplateParams {
10
11    public static function run( Parser $parser ) {
12        global $wgRenderHashAppend, $wgLang;
13
14        $title = $parser->getTitle();
15        if ( $title->getNamespace() !== NS_TEMPLATE ) {
16            return '<div class="error">Error: #template_params can only be called within a template.</div>';
17        }
18
19        // In theory, this will set a separate cache for each user
20        // language - so that a user viewing the output of
21        // #template_params in one language won't affect the display
22        // for a user viewing it in another language.
23        // In practice, setting this variable to *any* value seems to
24        // just disable the cache entirely. That's probably alright,
25        // though - template pages don't get viewed that frequently,
26        // so disabling the cache for them probably will not have a
27        // big effect on performance.
28        $wgRenderHashAppend = ';lang=' . $wgLang->getCode();
29
30        $params = func_get_args();
31        // We don't need the parser.
32        array_shift( $params );
33
34        $fieldData = [];
35        foreach ( $params as $param ) {
36            list( $fieldName, $fieldParams ) = self::parseWikitextString( $param );
37            if ( $fieldName !== '' ) {
38                $fieldData[$fieldName] = $fieldParams;
39            }
40        }
41
42        $parserOutput = $parser->getOutput();
43        if ( method_exists( $parserOutput, 'setPageProperty' ) ) {
44            // MW 1.38+
45            $parserOutput->setPageProperty( 'PageFormsTemplateParams', serialize( $fieldData ) );
46        } else {
47            $parserOutput->setProperty( 'PageFormsTemplateParams', serialize( $fieldData ) );
48        }
49
50        $text = wfMessage( "pf_template_docu", $title->getText() )->escaped();
51        $text .= "<pre>\n{{" . $title->getText() . "\n";
52        foreach ( $fieldData as $fieldName => $fieldParams ) {
53            $text .= "|$fieldName=\n";
54        }
55        $text .= "}}</pre>\n";
56        $text .= '<p>' . wfMessage( "pf_template_docufooter" )->escaped() . '</p>';
57
58        return $text;
59    }
60
61    public static function parseWikitextString( $fieldString ) {
62        $fieldParams = [];
63        $matches = [];
64        $foundMatch = preg_match( '/([^(]*)\s*\((.*)\)/s', $fieldString, $matches );
65        $allowedValuesParam = "";
66        if ( $foundMatch ) {
67            $fieldName = trim( $matches[1] );
68            $extraParamsString = $matches[2];
69            $extraParams = explode( ';', $extraParamsString );
70            foreach ( $extraParams as $extraParam ) {
71                $extraParamParts = explode( '=', $extraParam, 2 );
72                if ( count( $extraParamParts ) == 1 ) {
73                    $paramKey = strtolower( trim( $extraParamParts[0] ) );
74                    $fieldParams[$paramKey] = true;
75                } else {
76                    $paramKey = strtolower( trim( $extraParamParts[0] ) );
77                    $paramValue = trim( $extraParamParts[1] );
78                    $fieldParams[$paramKey] = $paramValue;
79                }
80            }
81        } else {
82            $fieldName = trim( $fieldString );
83        }
84
85        return [ $fieldName, $fieldParams ];
86    }
87
88}