Index: trunk/extensions/GeoData/tests/TagTest.php |
— | — | @@ -17,7 +17,8 @@ |
18 | 18 | $this->assertEmpty( $out->geoData['secondary'] ); |
19 | 19 | return; |
20 | 20 | } |
21 | | - $coord = $out->geoData['primary'] ? $out->geoData['primary'] : $out->geoData['secondary'][0]; |
| 21 | + $all = $out->geoData->getAll(); |
| 22 | + $coord = $all[0]; |
22 | 23 | foreach ( $expected as $field => $value ) { |
23 | 24 | $this->assertEquals( $value, $coord->$field, "Checking field $field" ); |
24 | 25 | } |
Index: trunk/extensions/GeoData/CoordinatesParserFunction.php |
— | — | @@ -1,6 +1,9 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | |
| 5 | +/** |
| 6 | + * Handler for the #coordinates parser function |
| 7 | + */ |
5 | 8 | class CoordinatesParserFunction { |
6 | 9 | /** |
7 | 10 | * @var Parser |
— | — | @@ -16,13 +19,17 @@ |
17 | 20 | $unnamed = array(), |
18 | 21 | $info; |
19 | 22 | |
| 23 | + /** |
| 24 | + * Constructor |
| 25 | + * @param Parser $parser: Parser object to associate with |
| 26 | + */ |
20 | 27 | public function __construct( Parser $parser ) { |
21 | 28 | $this->parser = $parser; |
22 | 29 | $this->info = GeoData::getCoordInfo(); |
23 | 30 | } |
24 | 31 | |
25 | 32 | /** |
26 | | - * Handler for the #coordinates parser function |
| 33 | + * #coordinates parser function callback |
27 | 34 | * |
28 | 35 | * @param Parser $parser |
29 | 36 | * @param PPFrame $frame |
— | — | @@ -30,8 +37,13 @@ |
31 | 38 | * @return Mixed |
32 | 39 | */ |
33 | 40 | public function coordinates( $parser, $frame, $args ) { |
| 41 | + if ( $parser != $this->parser ) { |
| 42 | + throw new MWException( __METHOD__ . '() called by wrong parser' ); |
| 43 | + } |
34 | 44 | $this->output = $parser->getOutput(); |
35 | | - $this->prepareOutput(); |
| 45 | + if ( !isset( $this->output->geoData ) ) { |
| 46 | + $this->output->geoData = new CoordinatesOutput(); |
| 47 | + } |
36 | 48 | |
37 | 49 | $this->unnamed = array(); |
38 | 50 | $this->named = array(); |
— | — | @@ -68,6 +80,10 @@ |
69 | 81 | return array( "<span class=\"error\">{$errorText}</span>", 'noparse' => false ); |
70 | 82 | } |
71 | 83 | |
| 84 | + /** |
| 85 | + * Add an unnamed parameter to the list, turining it into a named one if needed |
| 86 | + * @param String $value: Parameter |
| 87 | + */ |
72 | 88 | private function addArg( $value ) { |
73 | 89 | if ( isset( $this->info['primary'][$value] ) ) { |
74 | 90 | $this->named['primary'] = true; |
— | — | @@ -77,18 +93,6 @@ |
78 | 94 | $this->unnamed[] = $value; |
79 | 95 | } |
80 | 96 | } |
81 | | - /** |
82 | | - * Make sure that parser output has our storage array |
83 | | - */ |
84 | | - private function prepareOutput() { |
85 | | - if ( !isset( $this->output->geoData ) ) { |
86 | | - $this->output->geoData = array( |
87 | | - 'primary' => false, |
88 | | - 'secondary' => array(), |
89 | | - 'limitExceeded' => false, |
90 | | - ); |
91 | | - } |
92 | | - } |
93 | 97 | |
94 | 98 | /** |
95 | 99 | * Applies a coordinate to parser output |
— | — | @@ -98,24 +102,22 @@ |
99 | 103 | */ |
100 | 104 | private function applyCoord( Coord $coord ) { |
101 | 105 | global $wgMaxCoordinatesPerPage; |
102 | | - $output = $this->output; |
103 | | - $count = count( $output->geoData['secondary'] ) + ( $output->geoData['primary'] ? 1 : 0 ); |
104 | | - if ( $count >= $wgMaxCoordinatesPerPage ) { |
105 | | - if ( $output->geoData['limitExceeded'] ) { |
| 106 | + $geoData = $this->output->geoData; |
| 107 | + if ( $geoData->getCount() >= $wgMaxCoordinatesPerPage ) { |
| 108 | + if ( $geoData->limitExceeded ) { |
106 | 109 | return Status::newFatal( '' ); |
107 | 110 | } |
108 | | - $output->geoData['limitExceeded'] = true; |
| 111 | + $geoData->limitExceeded = true; |
109 | 112 | return Status::newFatal( 'geodata-limit-exceeded' ); |
110 | 113 | } |
111 | 114 | if ( $coord->primary ) { |
112 | | - if ( $output->geoData['primary'] ) { |
113 | | - $output->geoData['secondary'][] = $coord; |
| 115 | + if ( $geoData->getPrimary() ) { |
114 | 116 | return Status::newFatal( 'geodata-multiple-primary' ); |
115 | 117 | } else { |
116 | | - $output->geoData['primary'] = $coord; |
| 118 | + $geoData->addPrimary( $coord ); |
117 | 119 | } |
118 | 120 | } else { |
119 | | - $output->geoData['secondary'][] = $coord; |
| 121 | + $geoData->addSecondary( $coord ); |
120 | 122 | } |
121 | 123 | return Status::newGood(); |
122 | 124 | } |
— | — | @@ -177,3 +179,40 @@ |
178 | 180 | $this->output->addCategory( $name, $this->parser->getTitle()->getText() ); |
179 | 181 | } |
180 | 182 | } |
| 183 | + |
| 184 | +class CoordinatesOutput { |
| 185 | + public $limitExceeded = false; |
| 186 | + private $primary = false, |
| 187 | + $secondary = array(); |
| 188 | + |
| 189 | + public function getCount() { |
| 190 | + return count( $this->secondary ) + ( $this->primary ? 1 : 0 ); |
| 191 | + } |
| 192 | + |
| 193 | + public function addPrimary( Coord $c ) { |
| 194 | + if ( $this->primary ) { |
| 195 | + throw new MWException( 'Attempted to insert second primary function into ' . __CLASS__ ); |
| 196 | + } |
| 197 | + $this->primary = $c; |
| 198 | + } |
| 199 | + |
| 200 | + public function addSecondary( Coord $c ) { |
| 201 | + $this->secondary[] = $c; |
| 202 | + } |
| 203 | + |
| 204 | + public function getPrimary() { |
| 205 | + return $this->primary; |
| 206 | + } |
| 207 | + |
| 208 | + public function getSecondary() { |
| 209 | + return $this->secondary; |
| 210 | + } |
| 211 | + |
| 212 | + public function getAll() { |
| 213 | + $res = $this->secondary; |
| 214 | + if ( $this->primary ) { |
| 215 | + array_unshift( $res, $this->primary ); |
| 216 | + } |
| 217 | + return $res; |
| 218 | + } |
| 219 | +} |
\ No newline at end of file |
Index: trunk/extensions/GeoData/GeoData.php |
— | — | @@ -20,6 +20,7 @@ |
21 | 21 | $wgAutoloadClasses['GeoData'] = "$dir/GeoData.body.php"; |
22 | 22 | $wgAutoloadClasses['GeoDataHooks'] = "$dir/GeoDataHooks.php"; |
23 | 23 | $wgAutoloadClasses['GeoMath'] = "$dir/GeoMath.php"; |
| 24 | +$wgAutoloadClasses['CoordinatesOutput'] = "$dir/CoordinatesParserFunction.php"; |
24 | 25 | |
25 | 26 | $wgAPIListModules['geosearch'] = 'ApiQueryGeoSearch'; |
26 | 27 | $wgAPIPropModules['coordinates'] = 'ApiQueryCoordinates'; |
Index: trunk/extensions/GeoData/GeoDataHooks.php |
— | — | @@ -77,10 +77,7 @@ |
78 | 78 | $data = array(); |
79 | 79 | if ( isset( $out->geoData ) ) { |
80 | 80 | $geoData = $out->geoData; |
81 | | - if ( $geoData['primary'] ) { |
82 | | - $data[] = $geoData['primary']; |
83 | | - } |
84 | | - $data = array_merge( $data, $geoData['secondary'] ); |
| 81 | + $data = $geoData->getAll(); |
85 | 82 | } |
86 | 83 | if ( $wgUseDumbLinkUpdate || !count( $data ) ) { |
87 | 84 | self::doDumbUpdate( $data, $linksUpdate->mId ); |