Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
76.47% |
26 / 34 |
|
72.73% |
8 / 11 |
CRAP | |
0.00% |
0 / 1 |
FRInclusionManager | |
76.47% |
26 / 34 |
|
72.73% |
8 / 11 |
19.33 | |
0.00% |
0 / 1 |
singleton | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
__clone | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
clear | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setReviewedVersions | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setStableVersionCache | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
formatTemplateArray | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
stabilizeParserOutput | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
parserOutputIsStabilized | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getReviewedTemplateVersion | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
getStableTemplateVersion | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | use MediaWiki\Title\Title; |
4 | |
5 | /** |
6 | * Class containing template version usage requirements for |
7 | * Parser based on the source text (being parsed) revision ID. |
8 | * |
9 | * Parser hooks check this to determine what template version to use. |
10 | * If no requirements are set, the page is parsed as normal. |
11 | */ |
12 | class FRInclusionManager { |
13 | /** @var array[]|null templates at review time */ |
14 | private $reviewedVersions = null; |
15 | /** @var array[] Stable versions of templates */ |
16 | private $stableVersions = []; |
17 | |
18 | /** @var self|null */ |
19 | private static $instance = null; |
20 | |
21 | /** |
22 | * @return self |
23 | */ |
24 | public static function singleton() { |
25 | if ( self::$instance == null ) { |
26 | self::$instance = new self(); |
27 | } |
28 | return self::$instance; |
29 | } |
30 | |
31 | private function __clone() { |
32 | } |
33 | |
34 | private function __construct() { |
35 | $this->stableVersions['templates'] = []; |
36 | } |
37 | |
38 | /** |
39 | * Reset all template version data |
40 | * @return void |
41 | */ |
42 | public function clear() { |
43 | $this->reviewedVersions = null; |
44 | $this->stableVersions['templates'] = []; |
45 | } |
46 | |
47 | /** |
48 | * (a) Stabilize inclusions in Parser output |
49 | * (b) Set the template versions used in the flagged version of a revision |
50 | * @param int[][] $tmpParams (ns => dbKey => revId ) |
51 | */ |
52 | private function setReviewedVersions( array $tmpParams ) { |
53 | $this->reviewedVersions = []; |
54 | $this->reviewedVersions['templates'] = self::formatTemplateArray( $tmpParams ); |
55 | } |
56 | |
57 | /** |
58 | * Set the stable versions of some template |
59 | * @param int[][] $tmpParams (ns => dbKey => revId ) |
60 | */ |
61 | private function setStableVersionCache( array $tmpParams ) { |
62 | $this->stableVersions['templates'] = self::formatTemplateArray( $tmpParams ); |
63 | } |
64 | |
65 | /** |
66 | * Clean up a template version array |
67 | * @param int[][] $params (ns => dbKey => revId ) |
68 | * @return int[][] |
69 | */ |
70 | private function formatTemplateArray( array $params ) { |
71 | $res = []; |
72 | foreach ( $params as $ns => $templates ) { |
73 | $res[$ns] = []; |
74 | foreach ( $templates as $dbKey => $revId ) { |
75 | $res[$ns][$dbKey] = (int)$revId; |
76 | } |
77 | } |
78 | return $res; |
79 | } |
80 | |
81 | /** |
82 | * (a) Stabilize inclusions in Parser output |
83 | * (b) Load all of the "review time" versions of template from $frev |
84 | * (c) Load their stable version counterparts (avoids DB hits) |
85 | * Note: Used when calling FlaggedRevs::parseStableRevision(). |
86 | * @param FlaggedRevision $frev |
87 | * @return void |
88 | */ |
89 | public function stabilizeParserOutput( FlaggedRevision $frev ) { |
90 | // Stable versions |
91 | $tStbVersions = []; |
92 | # We can preload *most* of the stable version IDs the parser will need... |
93 | if ( FlaggedRevs::inclusionSetting() == FR_INCLUDES_STABLE ) { |
94 | $tStbVersions = $frev->getStableTemplateVersions(); |
95 | } |
96 | $this->reviewedVersions = []; |
97 | $this->reviewedVersions['templates'] = []; |
98 | $this->setStableVersionCache( $tStbVersions ); |
99 | } |
100 | |
101 | /** |
102 | * Should Parser stabilize includes? |
103 | * @return bool |
104 | */ |
105 | public function parserOutputIsStabilized() { |
106 | return is_array( $this->reviewedVersions ); |
107 | } |
108 | |
109 | /** |
110 | * Get the "review time" template version for parser |
111 | * @param Title $title |
112 | * @return int|null |
113 | */ |
114 | public function getReviewedTemplateVersion( Title $title ) { |
115 | if ( !is_array( $this->reviewedVersions ) ) { |
116 | throw new LogicException( "prepareForParse() nor setReviewedVersions() called yet" ); |
117 | } |
118 | $dbKey = $title->getDBkey(); |
119 | $namespace = $title->getNamespace(); |
120 | return $this->reviewedVersions['templates'][$namespace][$dbKey] ?? null; |
121 | } |
122 | |
123 | /** |
124 | * Get the stable version of a template |
125 | * @param Title $title |
126 | * @return int |
127 | */ |
128 | public function getStableTemplateVersion( Title $title ) { |
129 | $dbKey = $title->getDBkey(); |
130 | $namespace = $title->getNamespace(); |
131 | $id = $this->stableVersions['templates'][$namespace][$dbKey] ?? |
132 | FlaggedRevision::getStableRevId( $title ); |
133 | $this->stableVersions['templates'][$namespace][$dbKey] = $id; // cache |
134 | return $id; |
135 | } |
136 | } |