https://www.mediawiki.org/wiki/Special:Code/MediaWiki/114711

Revision: 114711
Author:   maxsem
Date:     2012-04-04 18:26:09 +0000 (Wed, 04 Apr 2012)
Log Message:
-----------
Added handling of coordinates from EXIF for images

Modified Paths:
--------------
    trunk/extensions/GeoData/GeoData.body.php
    trunk/extensions/GeoData/GeoData.php
    trunk/extensions/GeoData/GeoDataHooks.php

Modified: trunk/extensions/GeoData/GeoData.body.php
===================================================================
--- trunk/extensions/GeoData/GeoData.body.php   2012-04-04 18:09:18 UTC (rev 
114710)
+++ trunk/extensions/GeoData/GeoData.body.php   2012-04-04 18:26:09 UTC (rev 
114711)
@@ -137,7 +137,7 @@
         * @param Array $coordInfo
         * @return int: Sign modifier or 0 if not a suffix
         */
-       private static function parseSuffix( $str, $coordInfo ) {
+       public static function parseSuffix( $str, $coordInfo ) {
                global $wgContLang;
                $str = $wgContLang->uc( trim( $str ) );
                return isset( $coordInfo['abbr'][$str] ) ? 
$coordInfo['abbr'][$str] : 0;

Modified: trunk/extensions/GeoData/GeoData.php
===================================================================
--- trunk/extensions/GeoData/GeoData.php        2012-04-04 18:09:18 UTC (rev 
114710)
+++ trunk/extensions/GeoData/GeoData.php        2012-04-04 18:26:09 UTC (rev 
114711)
@@ -38,6 +38,7 @@
 $wgHooks['UnitTestsList'][] = 'GeoDataHooks::onUnitTestsList';
 $wgHooks['ArticleDeleteComplete'][] = 'GeoDataHooks::onArticleDeleteComplete';
 $wgHooks['LinksUpdate'][] = 'GeoDataHooks::onLinksUpdate';
+$wgHooks['FileUpload'][] = 'GeoDataHooks::onFileUpload';
 
 // =================== start configuration settings ===================
 

Modified: trunk/extensions/GeoData/GeoDataHooks.php
===================================================================
--- trunk/extensions/GeoData/GeoDataHooks.php   2012-04-04 18:09:18 UTC (rev 
114710)
+++ trunk/extensions/GeoData/GeoDataHooks.php   2012-04-04 18:26:09 UTC (rev 
114711)
@@ -69,9 +69,16 @@
                global $wgUseDumbLinkUpdate;
                $out = $linksUpdate->getParserOutput();
                $data = array();
+               $coordFromMetadata = self::getCoordinatesIfFile( 
$linksUpdate->getTitle() );
                if ( isset( $out->geoData ) ) {
                        $geoData = $out->geoData;
+                       // Use coordinates from file metadata unless overridden 
on description page
+                       if ( $coordFromMetadata && !$geoData->getPrimary() ) {
+                               $geoData->addPrimary( $coordFromMetadata );
+                       }
                        $data = $geoData->getAll();
+               } elseif ( $coordFromMetadata ) {
+                       $data[] = $coordFromMetadata;
                }
                if ( $wgUseDumbLinkUpdate || !count( $data ) ) {
                        self::doDumbUpdate( $data, $linksUpdate->mId );
@@ -81,6 +88,46 @@
                return true;
        }
 
+       private static function getCoordinatesIfFile( Title $title ) {
+               if ( $title->getNamespace() != NS_FILE ) {
+                       return null;
+               }
+               $file = wfFindFile( $title );
+               if ( !$file ) {
+                       return null;
+               }
+               $metadata = $file->getMetadata();
+               wfSuppressWarnings();
+               $metadata = unserialize( $metadata );
+               wfRestoreWarnings();
+               if ( isset( $metadata ) && isset( $metadata['GPSLatitude'] ) && 
isset( $metadata['GPSLongitude'] ) ) {
+                       $lat = $metadata['GPSLatitude'];
+                       $lon = $metadata['GPSLongitude'];
+                       $refs = self::decodeRefs( $metadata );
+                       $lat *= $refs[0];
+                       $lon *= $refs[1];
+                       if ( GeoData::validateCoord( $lat, $lon, 'earth' ) ) {
+                               $coord = new Coord( $lat, $lon );
+                               $coord->primary = true;
+                               return $coord;
+                       }
+               }
+               return null;
+       }
+
+       private static function decodeRefs( $metadata ) {
+               global $wgGlobes;
+               if ( isset( $metadata['GPSLatitudeRef'] ) && isset( 
$metadata['GPSLongitudeRef'] ) ) {
+                       $coordInfo = GeoData::getCoordInfo();
+                       $latRef = GeoData::parseSuffix( 
$metadata['GPSLatitudeRef'], $coordInfo['lat'] );
+                       $lonRef = GeoData::parseSuffix( 
$metadata['GPSLongitudeRef'], $wgGlobes['earth'] );
+                       if ( $latRef != 0 && $lonRef != 0 ) {
+                               return array( $latRef, $lonRef );
+                       }
+               }
+               return array( 1, 1 );
+       }
+
        private static function doDumbUpdate( $coords, $pageId ) {
                $dbw = wfGetDB( DB_MASTER );
                $dbw->delete( 'geo_tags', array( 'gt_page_id' => $pageId ), 
__METHOD__ );
@@ -119,4 +166,20 @@
                        $dbw->insert( 'geo_tags', $add, __METHOD__ );
                }
        }
+
+       /**
+        * FileUpload hook handler
+        * @see https://www.mediawiki.org/wiki/Manual:Hooks/FileUpload
+        *
+        * @param LocalFile $file
+        * @return bool
+        */
+       public static function onFileUpload( LocalFile $file ) {
+               $wp = WikiPage::factory( $file->getTitle() );
+               $po = new ParserOptions();
+               $pout = $wp->getParserOutput( $po );
+               $lu = new LinksUpdate( $file->getTitle(), $pout );
+               self::onLinksUpdate( $lu );
+               return true;
+       }
 }


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

Reply via email to