r114711 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r114710‎ | r114711 | r114712 >
Date:18:26, 4 April 2012
Author:maxsem
Status:deferred
Tags:
Comment:
Added handling of coordinates from EXIF for images
Modified paths:
  • /trunk/extensions/GeoData/GeoData.body.php (modified) (history)
  • /trunk/extensions/GeoData/GeoData.php (modified) (history)
  • /trunk/extensions/GeoData/GeoDataHooks.php (modified) (history)

Diff [purge]

Index: trunk/extensions/GeoData/GeoDataHooks.php
@@ -69,9 +69,16 @@
7070 global $wgUseDumbLinkUpdate;
7171 $out = $linksUpdate->getParserOutput();
7272 $data = array();
 73+ $coordFromMetadata = self::getCoordinatesIfFile( $linksUpdate->getTitle() );
7374 if ( isset( $out->geoData ) ) {
7475 $geoData = $out->geoData;
 76+ // Use coordinates from file metadata unless overridden on description page
 77+ if ( $coordFromMetadata && !$geoData->getPrimary() ) {
 78+ $geoData->addPrimary( $coordFromMetadata );
 79+ }
7580 $data = $geoData->getAll();
 81+ } elseif ( $coordFromMetadata ) {
 82+ $data[] = $coordFromMetadata;
7683 }
7784 if ( $wgUseDumbLinkUpdate || !count( $data ) ) {
7885 self::doDumbUpdate( $data, $linksUpdate->mId );
@@ -81,6 +88,46 @@
8289 return true;
8390 }
8491
 92+ private static function getCoordinatesIfFile( Title $title ) {
 93+ if ( $title->getNamespace() != NS_FILE ) {
 94+ return null;
 95+ }
 96+ $file = wfFindFile( $title );
 97+ if ( !$file ) {
 98+ return null;
 99+ }
 100+ $metadata = $file->getMetadata();
 101+ wfSuppressWarnings();
 102+ $metadata = unserialize( $metadata );
 103+ wfRestoreWarnings();
 104+ if ( isset( $metadata ) && isset( $metadata['GPSLatitude'] ) && isset( $metadata['GPSLongitude'] ) ) {
 105+ $lat = $metadata['GPSLatitude'];
 106+ $lon = $metadata['GPSLongitude'];
 107+ $refs = self::decodeRefs( $metadata );
 108+ $lat *= $refs[0];
 109+ $lon *= $refs[1];
 110+ if ( GeoData::validateCoord( $lat, $lon, 'earth' ) ) {
 111+ $coord = new Coord( $lat, $lon );
 112+ $coord->primary = true;
 113+ return $coord;
 114+ }
 115+ }
 116+ return null;
 117+ }
 118+
 119+ private static function decodeRefs( $metadata ) {
 120+ global $wgGlobes;
 121+ if ( isset( $metadata['GPSLatitudeRef'] ) && isset( $metadata['GPSLongitudeRef'] ) ) {
 122+ $coordInfo = GeoData::getCoordInfo();
 123+ $latRef = GeoData::parseSuffix( $metadata['GPSLatitudeRef'], $coordInfo['lat'] );
 124+ $lonRef = GeoData::parseSuffix( $metadata['GPSLongitudeRef'], $wgGlobes['earth'] );
 125+ if ( $latRef != 0 && $lonRef != 0 ) {
 126+ return array( $latRef, $lonRef );
 127+ }
 128+ }
 129+ return array( 1, 1 );
 130+ }
 131+
85132 private static function doDumbUpdate( $coords, $pageId ) {
86133 $dbw = wfGetDB( DB_MASTER );
87134 $dbw->delete( 'geo_tags', array( 'gt_page_id' => $pageId ), __METHOD__ );
@@ -119,4 +166,20 @@
120167 $dbw->insert( 'geo_tags', $add, __METHOD__ );
121168 }
122169 }
 170+
 171+ /**
 172+ * FileUpload hook handler
 173+ * @see https://www.mediawiki.org/wiki/Manual:Hooks/FileUpload
 174+ *
 175+ * @param LocalFile $file
 176+ * @return bool
 177+ */
 178+ public static function onFileUpload( LocalFile $file ) {
 179+ $wp = WikiPage::factory( $file->getTitle() );
 180+ $po = new ParserOptions();
 181+ $pout = $wp->getParserOutput( $po );
 182+ $lu = new LinksUpdate( $file->getTitle(), $pout );
 183+ self::onLinksUpdate( $lu );
 184+ return true;
 185+ }
123186 }
Index: trunk/extensions/GeoData/GeoData.body.php
@@ -137,7 +137,7 @@
138138 * @param Array $coordInfo
139139 * @return int: Sign modifier or 0 if not a suffix
140140 */
141 - private static function parseSuffix( $str, $coordInfo ) {
 141+ public static function parseSuffix( $str, $coordInfo ) {
142142 global $wgContLang;
143143 $str = $wgContLang->uc( trim( $str ) );
144144 return isset( $coordInfo['abbr'][$str] ) ? $coordInfo['abbr'][$str] : 0;
Index: trunk/extensions/GeoData/GeoData.php
@@ -38,6 +38,7 @@
3939 $wgHooks['UnitTestsList'][] = 'GeoDataHooks::onUnitTestsList';
4040 $wgHooks['ArticleDeleteComplete'][] = 'GeoDataHooks::onArticleDeleteComplete';
4141 $wgHooks['LinksUpdate'][] = 'GeoDataHooks::onLinksUpdate';
 42+$wgHooks['FileUpload'][] = 'GeoDataHooks::onFileUpload';
4243
4344 // =================== start configuration settings ===================
4445

Status & tagging log