MaxSem has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/262841

Change subject: Use ParserOutput's extension data to store geotags
......................................................................

Use ParserOutput's extension data to store geotags

This is a breaking change, requires corresponding tweaks in Wikibase

Change-Id: I230fd71b82160d498f39982d910ef468bcc0e951
---
M includes/CoordinatesOutput.php
M includes/CoordinatesParserFunction.php
M includes/Hooks.php
M tests/MiscGeoDataTest.php
M tests/TagTest.php
5 files changed, 78 insertions(+), 22 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/GeoData 
refs/changes/41/262841/1

diff --git a/includes/CoordinatesOutput.php b/includes/CoordinatesOutput.php
index d1a0d10..129f883 100644
--- a/includes/CoordinatesOutput.php
+++ b/includes/CoordinatesOutput.php
@@ -3,6 +3,7 @@
 namespace GeoData;
 
 use MWException;
+use ParserOutput;
 
 /**
  * Class that holds output of a parse opertion
@@ -12,6 +13,52 @@
        private $primary = false,
                $secondary = array();
 
+       /**
+        * Returns CoordinatesOutput for a given ParserOutput
+        *
+        * @param ParserOutput $parserOutput
+        * @return CoordinatesOutput
+        */
+       public static function fromParserOutput( ParserOutput $parserOutput ) {
+               /** @var CoordinatesOutput $result */
+               $result = $parserOutput->getExtensionData( 'geoData' );
+               if ( !$result ) {
+                       // @fixme: remove this b/c support code in summer 2016
+                       $result = isset( $parserOutput->geoData ) ? 
$parserOutput->geoData : null;
+                       if ( $result ) {
+                               $result->saveTo( $parserOutput );
+                               unset( $parserOutput->geoData );
+                       }
+               }
+
+               return $result;
+       }
+
+       /**
+        *
+        *
+        * @param ParserOutput $parserOutput
+        * @return CoordinatesOutput
+        */
+       public static function initFor( ParserOutput $parserOutput ) {
+               $result = self::fromParserOutput( $parserOutput );
+               if ( !$result ) {
+                       $result = new self;
+                       $result->saveTo( $parserOutput );
+               }
+
+               return $result;
+       }
+
+       /**
+        * Saves this object in ParserOutput
+        *
+        * @param ParserOutput $po
+        */
+       public function saveTo( ParserOutput $po ) {
+               $po->setExtensionData( 'geoData', $this );
+       }
+
        public function getCount() {
                return count( $this->secondary ) + ( $this->primary ? 1 : 0 );
        }
diff --git a/includes/CoordinatesParserFunction.php 
b/includes/CoordinatesParserFunction.php
index 458d143..146f5d1 100644
--- a/includes/CoordinatesParserFunction.php
+++ b/includes/CoordinatesParserFunction.php
@@ -19,11 +19,6 @@
         */
        private $parser;
 
-       /**
-        * @var ParserOutput
-        */
-       private $output;
-
        private $named = array(),
                $unnamed = array(),
                $info;
@@ -46,10 +41,6 @@
         */
        public function coordinates( $parser, $frame, $args ) {
                $this->parser = $parser;
-               $this->output = $parser->getOutput();
-               if ( !isset( $this->output->geoData ) ) {
-                       $this->output->geoData = new CoordinatesOutput();
-               }
 
                $this->unnamed = array();
                $this->named = array();
@@ -120,8 +111,10 @@
        private function applyCoord( Coord $coord ) {
                global $wgMaxCoordinatesPerPage, $wgContLang;
 
+               $parserOutput = $this->parser->getOutput();
                /** @var CoordinatesOutput $geoData */
-               $geoData = $this->output->geoData;
+               $geoData = CoordinatesOutput::initFor( $parserOutput );
+
                if ( $wgMaxCoordinatesPerPage >= 0 && $geoData->getCount() >= 
$wgMaxCoordinatesPerPage ) {
                        if ( $geoData->limitExceeded ) {
                                return Status::newFatal( '' );
diff --git a/includes/Hooks.php b/includes/Hooks.php
index a876712..8be1c9c 100644
--- a/includes/Hooks.php
+++ b/includes/Hooks.php
@@ -122,9 +122,9 @@
                $out = $linksUpdate->getParserOutput();
                $data = array();
                $coordFromMetadata = self::getCoordinatesIfFile( 
$linksUpdate->getTitle() );
-               if ( isset( $out->geoData ) ) {
-                       /** @var CoordinatesOutput $geoData */
-                       $geoData = $out->geoData;
+
+               $geoData = CoordinatesOutput::fromParserOutput( $out );
+               if ( $geoData ) {
                        // Use coordinates from file metadata unless overridden 
on description page
                        if ( $coordFromMetadata && !$geoData->getPrimary() ) {
                                $geoData->addPrimary( $coordFromMetadata );
@@ -257,8 +257,9 @@
        public static function onOutputPageParserOutput( OutputPage &$out, 
ParserOutput $po ) {
                global $wgGeoDataInJS;
 
-               if ( $wgGeoDataInJS && isset( $po->geoData ) ) {
-                       $coord = $po->geoData->getPrimary();
+               $geoData = CoordinatesOutput::fromParserOutput( $po );
+               if ( $wgGeoDataInJS && $geoData ) {
+                       $coord = $geoData->getPrimary();
                        if ( !$coord ) {
                                return true;
                        }
@@ -332,15 +333,17 @@
                ParserOutput $parserOutput )
        {
                global $wgGeoDataUseCirrusSearch, $wgGeoDataBackend;
+
+               $geoData = CoordinatesOutput::fromParserOutput( $parserOutput );
                if ( !( $wgGeoDataUseCirrusSearch || $wgGeoDataBackend == 
'elastic' )
-                       || !isset( $parserOutput->geoData ) )
+                       || !$geoData )
                {
                        return true;
                }
 
                $coords = array();
                /** @var Coord $coord */
-               foreach ( $parserOutput->geoData->getAll() as $coord ) {
+               foreach ( $geoData->getAll() as $coord ) {
                        $arr = $coord->getAsArray();
                        $arr['coord'] = array( 'lat' => $coord->lat, 'lon' => 
$coord->lon );
                        unset( $arr['id'] );
diff --git a/tests/MiscGeoDataTest.php b/tests/MiscGeoDataTest.php
index 676bcec..2daa8af 100644
--- a/tests/MiscGeoDataTest.php
+++ b/tests/MiscGeoDataTest.php
@@ -20,4 +20,13 @@
                        array( 179.9, -179.9, array( -1800, -1799, 1799, 1800 ) 
)
                );
        }
+
+       public function testCoordinatesOutputUpgrades() {
+               $parserOutput = new ParserOutput();
+               $geoData = new CoordinatesOutput();
+               $parserOutput->geoData = $geoData;
+
+               $this->assertSame( $geoData, 
CoordinatesOutput::fromParserOutput( $parserOutput ) );
+               $this->assertFalse( isset( $parserOutput->geoData ) );
+       }
 }
diff --git a/tests/TagTest.php b/tests/TagTest.php
index b187fce..d14dbbc 100644
--- a/tests/TagTest.php
+++ b/tests/TagTest.php
@@ -1,4 +1,5 @@
 <?php
+use GeoData\CoordinatesOutput;
 
 /**
  * @group GeoData
@@ -28,14 +29,17 @@
                $p = new Parser();
                $opt = new ParserOptions();
                $out = $p->parse( $input, Title::newMainPage(), $opt );
-               $this->assertTrue( isset( $out->geoData ) );
-               if ( !$expected ) {
-                       $this->assertEmpty( $out->geoData->getAll(),
-                               'Expected a failure but a result was found: ' . 
print_r( $out->geoData->getAll(), true )
+               $geoData = CoordinatesOutput::fromParserOutput( $out );
+
+               if ( $expected ) {
+                       $this->assertNotNull( $geoData );
+               } else {
+                       $this->assertNull( $geoData,
+                               'Expected a failure but a result was found: ' . 
print_r( $geoData, true )
                        );
                        return;
                }
-               $all = $out->geoData->getAll();
+               $all = $geoData->getAll();
                $this->assertEquals( 1, count( $all ), 'A result was expected, 
but there was error: ' . strip_tags( $out->getText() ) );
                $coord = $all[0];
                foreach ( $expected as $field => $value ) {

-- 
To view, visit https://gerrit.wikimedia.org/r/262841
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I230fd71b82160d498f39982d910ef468bcc0e951
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/GeoData
Gerrit-Branch: master
Gerrit-Owner: MaxSem <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to