From 1c4fc4679e1074d083d3a95d71f2bcb704d4ea98 Mon Sep 17 00:00:00 2001 From: Sam Wilson Date: Tue, 7 Jul 2026 07:48:26 +0800 Subject: [PATCH 1/5] Add user-agent support --- README.md | 10 ++++++++++ src/WikidataQuery.php | 19 ++++++++++++++++++- src/WikisourceApi.php | 22 ++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dea4543..26708df 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,16 @@ The default cache times are as follows: You can enable logging by passing `WikisourceApi::setLogger()` any object that implements [PSR-3's](http://www.php-fig.org/psr/psr-3/) `LoggerInterface`. +## User agent + +Don't forget to set a User-Agent with some contact details, +conforming to the [WMF User-Agent Policy](https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Wikimedia_Foundation_User-Agent_Policy). + +```php +$wsApi = new \Wikisource\Api\WikisourceApi(); +$wgApi->setUserAgent( 'my-example-tool.toolforge.org' ); +``` + ## Issues Please report all issues via Phabricator (tag them with the diff --git a/src/WikidataQuery.php b/src/WikidataQuery.php index 1e80046..5b45f7f 100644 --- a/src/WikidataQuery.php +++ b/src/WikidataQuery.php @@ -17,6 +17,9 @@ class WikidataQuery { /** @var \Psr\Log\LoggerInterface The logger to use */ protected $logger; + /** @var string HTTP user agent. */ + protected $userAgent; + /** @var string The Sparql query to run. */ protected $query; @@ -28,6 +31,16 @@ public function __construct( $query ) { $this->query = $query; } + /** + * Set the user agent that will be used when sending API requests. + * + * @param string $userAgent + * @return void + */ + public function setUserAgent( string $userAgent ): void { + $this->userAgent = $userAgent; + } + /** * Get the results of this query. * @return string[] Array of results keyed by the names given in the Sparql query. @@ -48,7 +61,11 @@ public function fetch() { */ protected function getXml( $query ) { $url = "https://query.wikidata.org/bigdata/namespace/wdq/sparql?query=" . urlencode( $query ); - $client = new Client(); + $client = new Client( [ + 'headers' => [ + 'User-Agent' => $this->userAgent, + ], + ] ); $response = $client->request( 'GET', $url ); return new SimpleXMLElement( $response->getBody()->getContents() ); } diff --git a/src/WikisourceApi.php b/src/WikisourceApi.php index 72e5acb..0f3039d 100644 --- a/src/WikisourceApi.php +++ b/src/WikisourceApi.php @@ -24,6 +24,9 @@ class WikisourceApi { /** @var \Psr\Log\LoggerInterface The logger to use. */ protected $logger; + /** @var string The HTTP user agent to send with API requests. */ + protected $userAgent = 'Wikisource PHP API; packagist.org/packages/wikisource/api'; + /** * Construct a new WikisourceApi object. The logger will default to NullLogger until you set * something else via WikisourceApi::setLogger(). @@ -44,6 +47,24 @@ public function setLogger( LoggerInterface $logger ) { $this->logger = $logger; } + /** + * Set the user agent that will be used when sending API requests to Wikisources, Wikidata etc. + * + * @param string $userAgent + * @return void + */ + public function setUserAgent( string $userAgent ): void { + $this->userAgent = $userAgent; + } + + /** + * Get the user agent to use when sending API requests. + * @return string + */ + public function getUserAgent(): string { + return $this->userAgent; + } + /** * Enable caching * @param CacheItemPoolInterface $pool The cache pool. @@ -116,6 +137,7 @@ public function fetchWikisources( $cacheLifetime = null ) { . "?lang rdfs:label ?langName . FILTER(LANG(?langName) = ?langCode || " . "( ?langCode = 'mul' && LANG(?langName) = 'en' )) . " . "}"; $wdQuery = new WikidataQuery( $query ); + $wdQuery->setUserAgent( $this->getUserAgent() ); $data = $wdQuery->fetch(); if ( !is_numeric( $cacheLifetime ) ) { $cacheLifetime = 60 * 60 * 24 * 30; From 08dd7ad6bab99db3c253253260249698ebccc67a Mon Sep 17 00:00:00 2001 From: Sam Wilson Date: Tue, 7 Jul 2026 07:52:21 +0800 Subject: [PATCH 2/5] Default to empty string --- src/WikidataQuery.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WikidataQuery.php b/src/WikidataQuery.php index 5b45f7f..5630a00 100644 --- a/src/WikidataQuery.php +++ b/src/WikidataQuery.php @@ -18,7 +18,7 @@ class WikidataQuery { protected $logger; /** @var string HTTP user agent. */ - protected $userAgent; + protected $userAgent = ''; /** @var string The Sparql query to run. */ protected $query; From b0708579782bcc196f9de88ce083b318c41b5b41 Mon Sep 17 00:00:00 2001 From: Sam Wilson Date: Tue, 7 Jul 2026 07:54:18 +0800 Subject: [PATCH 3/5] Drop PHP 8.0, add 8.5 --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 10d2d61..8ce2a21 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,7 +15,7 @@ jobs: strategy: matrix: - php: [ '8.0', '8.1', '8.2', '8.3' ] + php: [ '8.1', '8.2', '8.3', '8.5' ] steps: - name: Checkout From 64202914ddabd087960d46854597c7a3db0760dc Mon Sep 17 00:00:00 2001 From: Sam Wilson Date: Tue, 21 Jul 2026 10:17:32 +0800 Subject: [PATCH 4/5] header, and typo --- README.md | 2 +- src/WikidataQuery.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 26708df..d77217c 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ conforming to the [WMF User-Agent Policy](https://foundation.wikimedia.org/wiki/ ```php $wsApi = new \Wikisource\Api\WikisourceApi(); -$wgApi->setUserAgent( 'my-example-tool.toolforge.org' ); +$wsApi->setUserAgent( 'my-example-tool.toolforge.org' ); ``` ## Issues diff --git a/src/WikidataQuery.php b/src/WikidataQuery.php index 5630a00..be4c380 100644 --- a/src/WikidataQuery.php +++ b/src/WikidataQuery.php @@ -64,6 +64,7 @@ protected function getXml( $query ) { $client = new Client( [ 'headers' => [ 'User-Agent' => $this->userAgent, + 'Accept' => 'application/sparql-results+xml', ], ] ); $response = $client->request( 'GET', $url ); From 28c07a00f77e8a8aad04ceb2bb84e92621bf57da Mon Sep 17 00:00:00 2001 From: Sam Wilson Date: Tue, 21 Jul 2026 10:20:44 +0800 Subject: [PATCH 5/5] Upgrade addwiki to v4 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index b1c207c..360c06b 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "psr/cache": "^1|^2|^3", "psr/log": "^1|^2|^3", "dflydev/dot-access-data": "^3.0", - "addwiki/mediawiki-api": "^3", + "addwiki/addwiki": "^4", "symfony/dom-crawler": "^4.2|^5.0|^6|^7" }, "require-dev": {