Skip to content

Commit

Permalink
Avoid null value for Wikidata ID
Browse files Browse the repository at this point in the history
Workaround for Fatal error: Uncaught TypeError: Return value of
Wikisource\Api\Wikisource::getWikidataId() must be of the type string,
null returned in src/Wikisource.php:127
  • Loading branch information
samwilson committed Apr 15, 2022
1 parent 8c66578 commit 83b7507
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
6 changes: 4 additions & 2 deletions examples/index_pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@
$existing = count( $prideAndPrejudiceIndex->getPageList( true ) );

// Output summary.
echo $prideAndPrejudiceIndex->getTitle() . " has $pagesCount pages ($existing of which exist) "
. "and is of quality '" . $prideAndPrejudiceIndex->getQuality() . "'.\n";
echo $prideAndPrejudiceIndex->getTitle()
. " (on " . $enWs->getDomainName() . ", " . $enWs->getWikidataId() . ") "
. "has $pagesCount pages ($existing of which exist) "
. "and is of quality '" . $prideAndPrejudiceIndex->getQuality() . "'.\n";
2 changes: 1 addition & 1 deletion examples/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
$wikisources = $wsApi->fetchWikisources();
echo count( $wikisources ) . " Wikisources found:\n";
foreach ( $wikisources as $ws ) {
echo "* " . $ws->getLanguageCode() . " - " . $ws->getLanguageName() . "\n";
echo "* " . $ws->getLanguageCode() . " - " . $ws->getLanguageName() . " - " . $ws->getWikidataId() . "\n";
}
5 changes: 4 additions & 1 deletion src/Wikisource.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ public function getDomainName(): string {
*/
public function getWikidataId(): string {
if ( !$this->wikidataId ) {
$this->getWikisoureApi()->fetchWikisource( $this->getLanguageCode() );
// Get another Wikisource object, but via fetchWikisource() to ensure it has a Wikidata ID.
$this->wikidataId = $this->getWikisoureApi()
->fetchWikisource( $this->getLanguageCode() )
->getWikidataId();
}
return $this->wikidataId;
}
Expand Down

0 comments on commit 83b7507

Please sign in to comment.