Skip to content

Commit

Permalink
Merge pull request #14 from Parthiv-M/handle-mulwikisource
Browse files Browse the repository at this point in the history
Handle mul wikisource pages
  • Loading branch information
samwilson authored Sep 18, 2023
2 parents e3a60b8 + a8536b1 commit c52f260
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/Wikisource.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,13 @@ public function getLanguageName() {
*/
public function getDomainName(): string {
$entity = $this->getWikisoureApi()->getWikdataEntity( $this->getWikidataId() );
if ( isset( $entity['claims'][self::PROP_WEBSITE] ) ) {
// if it is mul wikisource, return base url without subdomain
if ( $this->getLanguageCode() !== 'mul' && isset( $entity['claims'][self::PROP_WEBSITE] ) ) {
$website = $entity['claims'][self::PROP_WEBSITE][0]['mainsnak']['datavalue']['value'];
$urlParts = parse_url( $website );
return $urlParts['host'];
}
if ( $this->getLanguageCode() ) {
if ( $this->getLanguageCode() && $this->getLanguageCode() !== 'mul' ) {
return $this->getLanguageCode() . '.wikisource.org';
}
return 'wikisource.org';
Expand Down
17 changes: 13 additions & 4 deletions src/WikisourceApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ public function fetchWikisources( $cacheLifetime = null ) {
// Language of work or name.
. "?item wdt:P407 ?lang . "
// RDF label of the language, in the language.
. "?lang rdfs:label ?langName . FILTER(LANG(?langName) = ?langCode) . " . "}";
// filter for mul wikisource
. "?lang rdfs:label ?langName . FILTER(LANG(?langName) = ?langCode || "
. "( ?langCode = 'mul' && LANG(?langName) = 'en' )) . " . "}";
$wdQuery = new WikidataQuery( $query );
$data = $wdQuery->fetch();
if ( !is_numeric( $cacheLifetime ) ) {
Expand Down Expand Up @@ -151,15 +153,22 @@ public function fetchWikisource( $langCode ) {
* Get a Wikisource from a given URL.
* @param string $url The Wikisource URL, with any path (or none).
* @return Wikisource|bool The Wikisource requested, or false if the URL isn't a Wikisource
* URL (i.e. xxx.wikisource.org).
* URL (i.e. xxx.wikisource.org or wikisource.org).
*/
public function newWikisourceFromUrl( $url ) {
preg_match( '|//([a-z]{2,3}).wikisource.org|i', $url, $matches );
// match wikisources with subdomain like xy.wikisource.org or xyz.wikisource.org
preg_match( '|//([a-z]{0,3})\.?wikisource.org|i', $url, $matches );
if ( !isset( $matches[1] ) ) {
$this->logger->debug( "Unable to find Wikisource URL in: $url" );
return false;
}
$langCode = $matches[1];
// if wikisource.org, then set $langCode as mul
// indicating mul.wikisource.org
if ( $matches[1] == "" ) {
$langCode = "mul";
} else {
$langCode = $matches[1];
}
$ws = new Wikisource( $this, $this->logger );
$ws->setLanguageCode( $langCode );
return $ws;
Expand Down

0 comments on commit c52f260

Please sign in to comment.