Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 84 |
|
0.00% |
0 / 12 |
CRAP | |
0.00% |
0 / 1 |
PFWikiPage | |
0.00% |
0 / 84 |
|
0.00% |
0 / 12 |
2652 | |
0.00% |
0 / 1 |
addTemplate | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
addTemplateParam | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
30 | |||
getEmbeddedTemplateForParam | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
20 | |||
addSection | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
addFreeTextSection | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setFreeText | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
makeFreeTextOnlyInclude | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
freeTextOnlyInclude | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
findEmbeddedTemplates | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
42 | |||
createTemplateCall | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
110 | |||
createTemplateCallsForTemplateName | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
createPageText | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
132 |
1 | <?php |
2 | /** |
3 | * @author Yaron Koren |
4 | * @file |
5 | * @ingroup PF |
6 | */ |
7 | |
8 | /** |
9 | * Represents the structured contents of a wiki page. |
10 | */ |
11 | class PFWikiPage { |
12 | private $mComponents = []; |
13 | private $mEmbeddedTemplateDefs = []; |
14 | private $mEmbeddedTemplateCalls = []; |
15 | private $mFreeTextOnlyInclude = false; |
16 | |
17 | function addTemplate( $templateInForm ) { |
18 | $templateName = $templateInForm->getTemplateName(); |
19 | $this->mComponents[] = new PFWikiPageTemplate( $templateName, !$templateInForm->allowsMultiple() ); |
20 | if ( $templateInForm->getInstanceNum() == 0 ) { |
21 | $embedInTemplate = $templateInForm->getEmbedInTemplate(); |
22 | $embedInParam = $templateInForm->getEmbedInField(); |
23 | if ( $embedInTemplate != null && $embedInParam != null ) { |
24 | $this->mEmbeddedTemplateDefs[] = [ $embedInTemplate, $embedInParam, $templateName ]; |
25 | } |
26 | } |
27 | } |
28 | |
29 | function addTemplateParam( $templateName, $instanceNum, $paramName, $value ) { |
30 | $curInstance = 0; |
31 | foreach ( $this->mComponents as $i => $component ) { |
32 | if ( $component instanceof PFWikiPageTemplate && $component->getName() == $templateName ) { |
33 | if ( $curInstance++ == $instanceNum ) { |
34 | $this->mComponents[$i]->addParam( $paramName, $value ); |
35 | return; |
36 | } |
37 | } |
38 | } |
39 | } |
40 | |
41 | function getEmbeddedTemplateForParam( $templateName, $paramName ) { |
42 | foreach ( $this->mEmbeddedTemplateDefs as $etd ) { |
43 | if ( $etd[0] == $templateName && $etd[1] == $paramName ) { |
44 | return $etd[2]; |
45 | } |
46 | } |
47 | return null; |
48 | } |
49 | |
50 | function addSection( $sectionName, $headerLevel, $sectionText, $sectionOptions ) { |
51 | $this->mComponents[] = new PFWikiPageSection( $sectionName, $headerLevel, $sectionText, $sectionOptions ); |
52 | } |
53 | |
54 | function addFreeTextSection() { |
55 | $this->mComponents[] = new PFWikiPageFreeText(); |
56 | } |
57 | |
58 | function setFreeText( $text ) { |
59 | foreach ( $this->mComponents as $i => $component ) { |
60 | if ( $component instanceof PFWikiPageFreeText ) { |
61 | $this->mComponents[$i]->setText( $text ); |
62 | return; |
63 | } |
64 | } |
65 | // Throw an exception here if no free text section found? |
66 | } |
67 | |
68 | function makeFreeTextOnlyInclude() { |
69 | $this->mFreeTextOnlyInclude = true; |
70 | } |
71 | |
72 | function freeTextOnlyInclude() { |
73 | return $this->mFreeTextOnlyInclude; |
74 | } |
75 | |
76 | /** |
77 | * Create an array of the template calls in the page that are embedded |
78 | * in other templates. |
79 | */ |
80 | private function findEmbeddedTemplates() { |
81 | foreach ( $this->mEmbeddedTemplateDefs as $etd ) { |
82 | $embeddedTemplateName = $etd[2]; |
83 | foreach ( $this->mComponents as $component ) { |
84 | if ( $component instanceof PFWikiPageTemplate ) { |
85 | if ( $embeddedTemplateName == $component->getName() ) { |
86 | if ( !array_key_exists( $embeddedTemplateName, $this->mEmbeddedTemplateCalls ) ) { |
87 | $this->mEmbeddedTemplateCalls[$embeddedTemplateName] = []; |
88 | } |
89 | $this->mEmbeddedTemplateCalls[$embeddedTemplateName][] = $component; |
90 | } |
91 | } |
92 | } |
93 | } |
94 | } |
95 | |
96 | function createTemplateCall( $template ) { |
97 | $lastNumericParam = 0; |
98 | $template->addUnhandledParams(); |
99 | |
100 | $templateCall = '{{' . $template->getName(); |
101 | foreach ( $template->getParams() as $templateParam ) { |
102 | $paramName = $templateParam->getName(); |
103 | $embeddedTemplateName = $this->getEmbeddedTemplateForParam( $template->getName(), $paramName ); |
104 | $paramValue = $templateParam->getValue(); |
105 | |
106 | // If there's no value, skip this param. |
107 | if ( $embeddedTemplateName == '' && |
108 | // Filter out blank values, but not '0'. |
109 | ( $paramValue === '' || $paramValue === null ) |
110 | ) { |
111 | continue; |
112 | } |
113 | |
114 | // Include the field name only for non-numeric field names. |
115 | if ( is_numeric( $paramName ) ) { |
116 | // Add at least one pipe, but possibly more - |
117 | // one each for any numeric param in between |
118 | // that wasn't submitted. |
119 | while ( $lastNumericParam < $paramName ) { |
120 | $templateCall .= '|'; |
121 | $lastNumericParam++; |
122 | } |
123 | } else { |
124 | $templateCall .= "\n|$paramName="; |
125 | } |
126 | if ( $embeddedTemplateName != '' ) { |
127 | foreach ( $this->mEmbeddedTemplateCalls[$embeddedTemplateName] as $embeddedTemplate ) { |
128 | $templateCall .= $this->createTemplateCall( $embeddedTemplate ); |
129 | } |
130 | } else { |
131 | $templateCall .= $paramValue; |
132 | } |
133 | } |
134 | // For mostly aesthetic purposes, if the template call ends with |
135 | // a bunch of pipes (i.e., it's an indexed template with unused |
136 | // parameters at the end), remove the pipes. |
137 | $templateCall = preg_replace( '/\|*$/', '', $templateCall ); |
138 | |
139 | // Add another newline before the final bracket, if this |
140 | // template call is already more than one line |
141 | if ( strpos( $templateCall, "\n" ) ) { |
142 | $templateCall .= "\n"; |
143 | } |
144 | $templateCall .= "}}"; |
145 | |
146 | return $templateCall; |
147 | } |
148 | |
149 | function createTemplateCallsForTemplateName( $templateName ) { |
150 | $text = ''; |
151 | foreach ( $this->mComponents as $component ) { |
152 | if ( $component instanceof PFWikiPageTemplate ) { |
153 | if ( $component->getName() == $templateName ) { |
154 | $text .= $this->createTemplateCall( $component ) . "\n"; |
155 | } |
156 | } |
157 | } |
158 | return $text; |
159 | } |
160 | |
161 | function createPageText() { |
162 | // First, go through and store the templates that are embedded, |
163 | // so that they can have special printing. |
164 | $this->findEmbeddedTemplates(); |
165 | |
166 | // Now create the text. |
167 | $pageText = ''; |
168 | foreach ( $this->mComponents as $component ) { |
169 | if ( $component instanceof PFWikiPageTemplate ) { |
170 | if ( !array_key_exists( $component->getName(), $this->mEmbeddedTemplateCalls ) ) { |
171 | $pageText .= $this->createTemplateCall( $component ) . "\n"; |
172 | } |
173 | } elseif ( $component instanceof PFWikiPageSection ) { |
174 | if ( $component->getText() !== "" || $component->isHideIfEmpty() === false ) { |
175 | $sectionName = $component->getHeader(); |
176 | for ( $i = 0; $i < $component->getHeaderLevel(); $i++ ) { |
177 | $sectionName = "=$sectionName="; |
178 | } |
179 | $pageText .= "$sectionName\n"; |
180 | if ( $component->getText() != '' ) { |
181 | $pageText .= $component->getText() . "\n"; |
182 | } |
183 | $pageText .= "\n"; |
184 | } |
185 | } elseif ( $component instanceof PFWikiPageFreeText ) { |
186 | $freeText = $component->getText(); |
187 | if ( $this->mFreeTextOnlyInclude ) { |
188 | $freeText = "<onlyinclude>$freeText</onlyinclude>"; |
189 | } |
190 | $pageText .= "$freeText\n"; |
191 | } |
192 | } |
193 | return $pageText; |
194 | } |
195 | } |