r108398 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108397‎ | r108398 | r108399 >
Date:10:46, 9 January 2012
Author:maxsem
Status:deferred
Tags:geodata 
Comment:
Moar refactoring: created a separate class for output
Modified paths:
  • /trunk/extensions/GeoData/CoordinatesParserFunction.php (modified) (history)
  • /trunk/extensions/GeoData/GeoData.php (modified) (history)
  • /trunk/extensions/GeoData/GeoDataHooks.php (modified) (history)
  • /trunk/extensions/GeoData/tests/TagTest.php (modified) (history)

Diff [purge]

Index: trunk/extensions/GeoData/tests/TagTest.php
@@ -17,7 +17,8 @@
1818 $this->assertEmpty( $out->geoData['secondary'] );
1919 return;
2020 }
21 - $coord = $out->geoData['primary'] ? $out->geoData['primary'] : $out->geoData['secondary'][0];
 21+ $all = $out->geoData->getAll();
 22+ $coord = $all[0];
2223 foreach ( $expected as $field => $value ) {
2324 $this->assertEquals( $value, $coord->$field, "Checking field $field" );
2425 }
Index: trunk/extensions/GeoData/CoordinatesParserFunction.php
@@ -1,6 +1,9 @@
22 <?php
33
44
 5+/**
 6+ * Handler for the #coordinates parser function
 7+ */
58 class CoordinatesParserFunction {
69 /**
710 * @var Parser
@@ -16,13 +19,17 @@
1720 $unnamed = array(),
1821 $info;
1922
 23+ /**
 24+ * Constructor
 25+ * @param Parser $parser: Parser object to associate with
 26+ */
2027 public function __construct( Parser $parser ) {
2128 $this->parser = $parser;
2229 $this->info = GeoData::getCoordInfo();
2330 }
2431
2532 /**
26 - * Handler for the #coordinates parser function
 33+ * #coordinates parser function callback
2734 *
2835 * @param Parser $parser
2936 * @param PPFrame $frame
@@ -30,8 +37,13 @@
3138 * @return Mixed
3239 */
3340 public function coordinates( $parser, $frame, $args ) {
 41+ if ( $parser != $this->parser ) {
 42+ throw new MWException( __METHOD__ . '() called by wrong parser' );
 43+ }
3444 $this->output = $parser->getOutput();
35 - $this->prepareOutput();
 45+ if ( !isset( $this->output->geoData ) ) {
 46+ $this->output->geoData = new CoordinatesOutput();
 47+ }
3648
3749 $this->unnamed = array();
3850 $this->named = array();
@@ -68,6 +80,10 @@
6981 return array( "<span class=\"error\">{$errorText}</span>", 'noparse' => false );
7082 }
7183
 84+ /**
 85+ * Add an unnamed parameter to the list, turining it into a named one if needed
 86+ * @param String $value: Parameter
 87+ */
7288 private function addArg( $value ) {
7389 if ( isset( $this->info['primary'][$value] ) ) {
7490 $this->named['primary'] = true;
@@ -77,18 +93,6 @@
7894 $this->unnamed[] = $value;
7995 }
8096 }
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 - }
9397
9498 /**
9599 * Applies a coordinate to parser output
@@ -98,24 +102,22 @@
99103 */
100104 private function applyCoord( Coord $coord ) {
101105 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 ) {
106109 return Status::newFatal( '' );
107110 }
108 - $output->geoData['limitExceeded'] = true;
 111+ $geoData->limitExceeded = true;
109112 return Status::newFatal( 'geodata-limit-exceeded' );
110113 }
111114 if ( $coord->primary ) {
112 - if ( $output->geoData['primary'] ) {
113 - $output->geoData['secondary'][] = $coord;
 115+ if ( $geoData->getPrimary() ) {
114116 return Status::newFatal( 'geodata-multiple-primary' );
115117 } else {
116 - $output->geoData['primary'] = $coord;
 118+ $geoData->addPrimary( $coord );
117119 }
118120 } else {
119 - $output->geoData['secondary'][] = $coord;
 121+ $geoData->addSecondary( $coord );
120122 }
121123 return Status::newGood();
122124 }
@@ -177,3 +179,40 @@
178180 $this->output->addCategory( $name, $this->parser->getTitle()->getText() );
179181 }
180182 }
 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 @@
2121 $wgAutoloadClasses['GeoData'] = "$dir/GeoData.body.php";
2222 $wgAutoloadClasses['GeoDataHooks'] = "$dir/GeoDataHooks.php";
2323 $wgAutoloadClasses['GeoMath'] = "$dir/GeoMath.php";
 24+$wgAutoloadClasses['CoordinatesOutput'] = "$dir/CoordinatesParserFunction.php";
2425
2526 $wgAPIListModules['geosearch'] = 'ApiQueryGeoSearch';
2627 $wgAPIPropModules['coordinates'] = 'ApiQueryCoordinates';
Index: trunk/extensions/GeoData/GeoDataHooks.php
@@ -77,10 +77,7 @@
7878 $data = array();
7979 if ( isset( $out->geoData ) ) {
8080 $geoData = $out->geoData;
81 - if ( $geoData['primary'] ) {
82 - $data[] = $geoData['primary'];
83 - }
84 - $data = array_merge( $data, $geoData['secondary'] );
 81+ $data = $geoData->getAll();
8582 }
8683 if ( $wgUseDumbLinkUpdate || !count( $data ) ) {
8784 self::doDumbUpdate( $data, $linksUpdate->mId );

Sign-offs

UserFlagDate
😂inspected14:02, 9 January 2012

Status & tagging log