Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 38
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 / 38
0.00% covered (danger)
0.00%
0 / 2
90
0.00% covered (danger)
0.00%
0 / 1
 run
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
30
 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        $title = $parser->getTitle();
13        if ( $title->getNamespace() !== NS_TEMPLATE ) {
14            return '<div class="error">Error: #template_params can only be called within a template.</div>';
15        }
16
17        // This will set a separate cache for each user
18        // language - so that a user viewing the output of
19        // #template_params in one language won't affect the display
20        // for a user viewing it in another language.
21        $parser->getOptions()->getUserLangObj();
22
23        $params = func_get_args();
24        // We don't need the parser.
25        array_shift( $params );
26
27        $fieldData = [];
28        foreach ( $params as $param ) {
29            [ $fieldName, $fieldParams ] = self::parseWikitextString( $param );
30            if ( $fieldName !== '' ) {
31                $fieldData[$fieldName] = $fieldParams;
32            }
33        }
34
35        $parserOutput = $parser->getOutput();
36        $parserOutput->setPageProperty( 'PageFormsTemplateParams', serialize( $fieldData ) );
37
38        $text = wfMessage( "pf_template_docu", $title->getText() )->escaped();
39        $text .= "<pre>\n{{" . $title->getText() . "\n";
40        foreach ( $fieldData as $fieldName => $fieldParams ) {
41            $text .= "|$fieldName=\n";
42        }
43        $text .= "}}</pre>\n";
44        $text .= '<p>' . wfMessage( "pf_template_docufooter" )->escaped() . '</p>';
45
46        return $text;
47    }
48
49    public static function parseWikitextString( $fieldString ) {
50        $fieldParams = [];
51        $matches = [];
52        $foundMatch = preg_match( '/([^(]*)\s*\((.*)\)/s', $fieldString, $matches );
53        $allowedValuesParam = "";
54        if ( $foundMatch ) {
55            $fieldName = trim( $matches[1] );
56            $extraParamsString = $matches[2];
57            $extraParams = explode( ';', $extraParamsString );
58            foreach ( $extraParams as $extraParam ) {
59                $extraParamParts = explode( '=', $extraParam, 2 );
60                if ( count( $extraParamParts ) == 1 ) {
61                    $paramKey = strtolower( trim( $extraParamParts[0] ) );
62                    $fieldParams[$paramKey] = true;
63                } else {
64                    $paramKey = strtolower( trim( $extraParamParts[0] ) );
65                    $paramValue = trim( $extraParamParts[1] );
66                    $fieldParams[$paramKey] = $paramValue;
67                }
68            }
69        } else {
70            $fieldName = trim( $fieldString );
71        }
72
73        return [ $fieldName, $fieldParams ];
74    }
75
76}