Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@

defined('PHPWG_ROOT_PATH') or exit(1);

/**
* Get a HTTP user-agent that conforms to the WMF User-Agent Policy.
* @link https://foundation.wikimedia.org/wiki/Policy:Wikimedia_Foundation_User-Agent_Policy
* @return string
*/
function wikimediacommons_useragent(): string
{
$user_prefs = userprefs_get_param(WIKIMEDIACOMMONS_ID);
$username = isset($user_prefs['username']) ? 'User:'.$user_prefs['username'] : '';
return trim('piwigo/wikimedia-commons-plugin '.get_absolute_root_url().' '.$username);
}

// Main navigation.
$tab = $_GET['tab'] ?? 'settings';
if (in_array($tab, array('settings', 'oauth'))) {
Expand Down
2 changes: 1 addition & 1 deletion admin/oauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
$conf[WIKIMEDIACOMMONS_ID]['key'],
$conf[WIKIMEDIACOMMONS_ID]['secret']
));
$oauth_client_conf->setUserAgent('piwigo/wikimedia-commons-plugin '.get_absolute_root_url());
$oauth_client_conf->setUserAgent(wikimediacommons_useragent());
$client = new Client($oauth_client_conf);
if (isset($_GET['returnto'])) {
$client->setCallback(WIKIMEDIACOMMONS_ADMIN.'-oauth&returnto='.urlencode($_GET['returnto']));
Expand Down
5 changes: 3 additions & 2 deletions admin/photo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
defined('PHPWG_ROOT_PATH') or exit(1);

use Addwiki\Mediawiki\Api\Client\Action\Request\ActionRequest;
use Addwiki\Mediawiki\Api\Client\Auth\OAuthOwnerConsumer;
use Addwiki\Mediawiki\Api\Client\MediaWiki;
use Piwigo\Plugin\WikimediaCommons\CommonsFileUploader;
use Piwigo\Plugin\WikimediaCommons\OAuthOwnerConsumer;

check_status(ACCESS_ADMINISTRATOR);

Expand Down Expand Up @@ -87,7 +87,7 @@ function getWikitext($row): string
// phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound
$information = "{{Information\n"
."| description = \n"
."| date = {{taken on|".$row['date_creation']."|locationn=}}\n"
."| date = {{taken on|".$row['date_creation']."|location=}}\n"
."| source = $source\n"
."| author = ".$row['author']."\n"
."| permission = \n"
Expand Down Expand Up @@ -134,6 +134,7 @@ function uploadToCommons(array $row, string $title, string $text, string $captio
$user_prefs['access_key'],
$user_prefs['access_secret']
);
$auth_method->setIdentifierForUserAgent(wikimediacommons_useragent());
$api = MediaWiki::newFromPage(
$conf[WIKIMEDIACOMMONS_ID]['endpoint'],
$auth_method
Expand Down
1 change: 1 addition & 0 deletions language/en_UK/plugin.lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
$lang['Key:'] = 'Key:';
$lang['Secret:'] = 'Secret:';
$lang['With this form you can upload a photo from here to <a href="%s">Wikimedia Commons</a>.'] = 'With this form you can upload a photo from here to <a href="%s">Wikimedia Commons</a>.';
$lang['Connect to Wikimedia Commons'] = 'Connect to Wikimedia Commons';
$lang['You are logged in as %s.'] = 'You are logged in as %s.';
$lang['Disconnect from Wikimedia Commons.'] = 'Disconnect from Wikimedia Commons.';
$lang['Filename on Commons:'] = 'Filename on Commons:';
Expand Down
2 changes: 1 addition & 1 deletion main.inc.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/*
Plugin Name: Wikimedia Commons
Version: 0.1.0
Version: 0.2.0
Description: A Piwigo plugin for exporting photos to Wikimedia Commons.
Plugin URI: auto
Author: Sam Wilson
Expand Down
24 changes: 24 additions & 0 deletions src/OAuthOwnerConsumer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
// phpcs:disable

namespace Piwigo\Plugin\WikimediaCommons;

use Addwiki\Mediawiki\Api\Client\Auth\OAuthOwnerConsumer as AddWikiOAuthOwnerConsumer;

/**
* This class is a temporary workaround in order to set the user agent,
* until https://github.com/addwiki/addwiki/issues/229 is resolved.
*/
class OAuthOwnerConsumer extends AddWikiOAuthOwnerConsumer
{

private ?string $identifierForUserAgent = null;

public function setIdentifierForUserAgent($identifier): void {
$this->identifierForUserAgent = $identifier;
}

public function identifierForUserAgent(): ?string {
return $this->identifierForUserAgent ?? parent::identifierForUserAgent();
}
}
Loading