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