Skip to content

Commit 1c5fc41

Browse files
authored
GH#2658: preserve Wikimedia metadata and dimensions (#2661)
* fix: preserve Wikimedia metadata and dimensions * fix: preserve Wikimedia license attribution
1 parent df3212d commit 1c5fc41

1 file changed

Lines changed: 27 additions & 14 deletions

File tree

includes/Abilities/ImageSources/WikimediaImageSource.php

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ public function search( string $keyword, int $per_page = 10, array $filters = []
4848
$url = (string) ( $info['url'] ?? '' );
4949
if ( '' === $url || empty( $info['width'] ) || empty( $info['height'] ) ) {
5050
continue; }
51-
$meta = $info['extmetadata'] ?? array();
52-
$license = (string) ( $meta['LicenseShortName']['value'] ?? 'CC BY-SA' );
53-
$author = wp_strip_all_tags( (string) ( $meta['Artist']['value'] ?? '' ) );
54-
$hits[] = array(
51+
$meta = $info['extmetadata'] ?? array();
52+
$license = (string) ( $meta['LicenseShortName']['value'] ?? '' );
53+
$author = wp_strip_all_tags( (string) ( $meta['Artist']['value'] ?? '' ) );
54+
$license_url = (string) ( $meta['LicenseUrl']['value'] ?? '' );
55+
$attribution = wp_strip_all_tags( (string) ( $meta['Attribution']['value'] ?? '' ) );
56+
$hits[] = array(
5557
'id' => (string) ( $page['title'] ?? '' ),
5658
'preview' => (string) ( $info['thumburl'] ?? $url ),
5759
'medium' => (string) ( $info['thumburl'] ?? $url ),
@@ -62,9 +64,9 @@ public function search( string $keyword, int $per_page = 10, array $filters = []
6264
'author' => $author,
6365
'author_url' => '',
6466
'license' => $license,
65-
'license_url' => 'https://creativecommons.org/licenses/',
67+
'license_url' => $license_url,
6668
'source' => 'wikimedia',
67-
'attribution' => trim( $author . ' / ' . $license ),
69+
'attribution' => '' !== $attribution ? $attribution : trim( $author . ' / ' . $license ),
6870
);
6971
}
7072
return array(
@@ -74,38 +76,49 @@ public function search( string $keyword, int $per_page = 10, array $filters = []
7476
);
7577
}
7678

77-
public function get_image( string $image_id ): array|\WP_Error {
78-
$args = array(
79+
public function get_image( string $image_id, int $width = 0, int $height = 0 ): array|\WP_Error {
80+
$args = array(
7981
'action' => 'query',
8082
'format' => 'json',
8183
'formatversion' => 2,
8284
'titles' => $image_id,
8385
'prop' => 'imageinfo',
8486
'iiprop' => 'url|size|extmetadata',
8587
);
88+
if ( $width > 0 ) {
89+
$args['iiurlwidth'] = $width;
90+
}
91+
if ( $height > 0 ) {
92+
$args['iiurlheight'] = $height;
93+
}
8694
$response = SafeHttpClient::instance()->safe_remote_get( add_query_arg( $args, self::API ), array( 'timeout' => 30 ) );
8795
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
8896
return new WP_Error( 'wikimedia_error', 'Failed to retrieve Wikimedia image.' ); }
8997
$page = ( json_decode( wp_remote_retrieve_body( $response ), true )['query']['pages'][0] ?? array() );
9098
$info = $page['imageinfo'][0] ?? array();
9199
if ( empty( $info['url'] ) ) {
92100
return new WP_Error( 'wikimedia_error', 'No Wikimedia image URL available.' ); }
101+
$meta = $info['extmetadata'] ?? array();
102+
$license = (string) ( $meta['LicenseShortName']['value'] ?? '' );
103+
$author = wp_strip_all_tags( (string) ( $meta['Artist']['value'] ?? '' ) );
104+
$license_url = (string) ( $meta['LicenseUrl']['value'] ?? '' );
105+
$attribution = wp_strip_all_tags( (string) ( $meta['Attribution']['value'] ?? '' ) );
93106
return array(
94-
'url' => $info['url'],
107+
'url' => (string) ( $info['thumburl'] ?? $info['url'] ),
95108
'width' => (int) ( $info['width'] ?? 0 ),
96109
'height' => (int) ( $info['height'] ?? 0 ),
97110
'title' => $image_id,
98-
'author' => '',
111+
'author' => $author,
99112
'author_url' => '',
100-
'license' => 'CC BY-SA',
101-
'license_url' => 'https://creativecommons.org/licenses/',
102-
'attribution' => 'Wikimedia Commons',
113+
'license' => $license,
114+
'license_url' => $license_url,
115+
'attribution' => '' !== $attribution ? $attribution : trim( $author . ' / ' . $license ),
103116
'source' => 'wikimedia',
104117
);
105118
}
106119

107120
public function download( string $image_id, int $width = 0, int $height = 0 ): string|\WP_Error {
108-
$image = $this->get_image( $image_id );
121+
$image = $this->get_image( $image_id, $width, $height );
109122
if ( is_wp_error( $image ) ) {
110123
return $image; }
111124
return SafeHttpClient::instance()->safe_download_url( (string) $image['url'], 60 );

0 commit comments

Comments
 (0)