Index: trunk/extensions/GeoData/GeoDataHooks.php |
— | — | @@ -69,9 +69,16 @@ |
70 | 70 | global $wgUseDumbLinkUpdate; |
71 | 71 | $out = $linksUpdate->getParserOutput(); |
72 | 72 | $data = array(); |
| 73 | + $coordFromMetadata = self::getCoordinatesIfFile( $linksUpdate->getTitle() ); |
73 | 74 | if ( isset( $out->geoData ) ) { |
74 | 75 | $geoData = $out->geoData; |
| 76 | + // Use coordinates from file metadata unless overridden on description page |
| 77 | + if ( $coordFromMetadata && !$geoData->getPrimary() ) { |
| 78 | + $geoData->addPrimary( $coordFromMetadata ); |
| 79 | + } |
75 | 80 | $data = $geoData->getAll(); |
| 81 | + } elseif ( $coordFromMetadata ) { |
| 82 | + $data[] = $coordFromMetadata; |
76 | 83 | } |
77 | 84 | if ( $wgUseDumbLinkUpdate || !count( $data ) ) { |
78 | 85 | self::doDumbUpdate( $data, $linksUpdate->mId ); |
— | — | @@ -81,6 +88,46 @@ |
82 | 89 | return true; |
83 | 90 | } |
84 | 91 | |
| 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 | + |
85 | 132 | private static function doDumbUpdate( $coords, $pageId ) { |
86 | 133 | $dbw = wfGetDB( DB_MASTER ); |
87 | 134 | $dbw->delete( 'geo_tags', array( 'gt_page_id' => $pageId ), __METHOD__ ); |
— | — | @@ -119,4 +166,20 @@ |
120 | 167 | $dbw->insert( 'geo_tags', $add, __METHOD__ ); |
121 | 168 | } |
122 | 169 | } |
| 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 | + } |
123 | 186 | } |
Index: trunk/extensions/GeoData/GeoData.body.php |
— | — | @@ -137,7 +137,7 @@ |
138 | 138 | * @param Array $coordInfo |
139 | 139 | * @return int: Sign modifier or 0 if not a suffix |
140 | 140 | */ |
141 | | - private static function parseSuffix( $str, $coordInfo ) { |
| 141 | + public static function parseSuffix( $str, $coordInfo ) { |
142 | 142 | global $wgContLang; |
143 | 143 | $str = $wgContLang->uc( trim( $str ) ); |
144 | 144 | return isset( $coordInfo['abbr'][$str] ) ? $coordInfo['abbr'][$str] : 0; |
Index: trunk/extensions/GeoData/GeoData.php |
— | — | @@ -38,6 +38,7 @@ |
39 | 39 | $wgHooks['UnitTestsList'][] = 'GeoDataHooks::onUnitTestsList'; |
40 | 40 | $wgHooks['ArticleDeleteComplete'][] = 'GeoDataHooks::onArticleDeleteComplete'; |
41 | 41 | $wgHooks['LinksUpdate'][] = 'GeoDataHooks::onLinksUpdate'; |
| 42 | +$wgHooks['FileUpload'][] = 'GeoDataHooks::onFileUpload'; |
42 | 43 | |
43 | 44 | // =================== start configuration settings =================== |
44 | 45 | |