VuFind API Documentation

Wikipedia
in package
implements TranslatorAwareInterface Uses TranslatorAwareTrait

Wikipedia connection class

Tags
category

VuFind

author

Chris Hallberg challber@villanova.edu

license

http://opensource.org/licenses/gpl-2.0.php GNU General Public License

link

Wiki

Interfaces, Classes, Traits and Enums

TranslatorAwareInterface
Lightweight translator aware marker interface (used as an alternative to \Laminas\I18n\Translator\TranslatorAwareInterface, which requires an excessive number of methods to be implemented).

Table of Contents

$client  : Client
HTTP client
$lang  : string
Selected language
$pagesRetrieved  : array<string|int, mixed>
Log of Wikipedia pages already retrieved
$translator  : TranslatorInterface
Translator
__construct()  : mixed
Constructor
get()  : array<string|int, mixed>|null
This method is responsible for connecting to Wikipedia via the REST API and pulling the content for the relevant author.
getTranslator()  : TranslatorInterface
Get translator object.
getTranslatorLocale()  : string
Get the locale from the translator.
setLanguage()  : void
Set language
setTranslator()  : TranslatorAwareInterface
Set a translator
translate()  : string
Translate a string (or string-castable object)
translateWithPrefix()  : string
Translate a string (or string-castable object) using a prefix, or without the prefix if a prefixed translation is not found.
alreadyRetrieved()  : bool
Check if a page has already been retrieved; if it hasn't, flag it as retrieved for future reference.
checkForRedirect()  : array<string|int, mixed>
Check for redirection in the Wikipedia response
extractBodyText()  : string
Extract body text
extractImageFromBody()  : array<string|int, mixed>
Support method for parseWikipedia - extract first image from body
extractImageFromInfoBox()  : array<string|int, mixed>
Extract image information from an infobox
extractInfoBox()  : string
Support method for parseWikipedia - extract infobox details
extractTextDomain()  : array<string|int, mixed>
Given a translation string with or without a text domain, return an array with the raw string and the text domain separated.
getDebugTranslation()  : string
Build a debug-mode translation
getWikipediaImageURL()  : mixed
This method is responsible for obtaining an image URL based on a name.
parseWikipedia()  : array<string|int, mixed>
_parseWikipedia
sanitizeTranslationKey()  : string
Make sure there are not any illegal characters in the translation key that might prevent successful lookup in language files.
sanitizeWikipediaBody()  : string
Support method for parseWikipedia - fix up details in the body
stripImageAndFileLinks()  : string
Support method for sanitizeWikipediaBody -- strip image/file links.
translateString()  : string
Get translation for a string

Properties

$client

HTTP client

protected Client $client

$lang

Selected language

protected string $lang = 'en'

$pagesRetrieved

Log of Wikipedia pages already retrieved

protected array<string|int, mixed> $pagesRetrieved = []

Methods

__construct()

Constructor

public __construct(Client $client) : mixed
Parameters
$client : Client

HTTP client

Return values
mixed

get()

This method is responsible for connecting to Wikipedia via the REST API and pulling the content for the relevant author.

public get(string $author) : array<string|int, mixed>|null
Parameters
$author : string

The author name to search for

Return values
array<string|int, mixed>|null

getTranslator()

Get translator object.

public getTranslator() : TranslatorInterface
Return values
TranslatorInterface

getTranslatorLocale()

Get the locale from the translator.

public getTranslatorLocale([string $default = 'en' ]) : string
Parameters
$default : string = 'en'

Default to use if translator absent.

Return values
string

setLanguage()

Set language

public setLanguage(string $lang) : void
Parameters
$lang : string

Language

Return values
void

translate()

Translate a string (or string-castable object)

public translate(string|object|array<string|int, mixed> $target[, array<string|int, mixed> $tokens = [] ][, string $default = null ][, bool $useIcuFormatter = false ][, array<string|int, string> $fallbackDomains = [] ]) : string
Parameters
$target : string|object|array<string|int, mixed>

String to translate or an array of text domain and string to translate

$tokens : array<string|int, mixed> = []

Tokens to inject into the translated string

$default : string = null

Default value to use if no translation is found (null for no default).

$useIcuFormatter : bool = false

Should we use an ICU message formatter instead of the default behavior?

$fallbackDomains : array<string|int, string> = []

Text domains to check if no match is found in the domain specified in $target

Return values
string

translateWithPrefix()

Translate a string (or string-castable object) using a prefix, or without the prefix if a prefixed translation is not found.

public translateWithPrefix(string $prefix, string|object|array<string|int, mixed> $target[, array<string|int, mixed> $tokens = [] ][, string $default = null ][, bool $useIcuFormatter = false ][, array<string|int, string> $fallbackDomains = [] ]) : string
Parameters
$prefix : string

Translation key prefix

$target : string|object|array<string|int, mixed>

String to translate or an array of text domain and string to translate

$tokens : array<string|int, mixed> = []

Tokens to inject into the translated string

$default : string = null

Default value to use if no translation is found (null for no default).

$useIcuFormatter : bool = false

Should we use an ICU message formatter instead of the default behavior?

$fallbackDomains : array<string|int, string> = []

Text domains to check if no match is found in the domain specified in $target

Return values
string

alreadyRetrieved()

Check if a page has already been retrieved; if it hasn't, flag it as retrieved for future reference.

protected alreadyRetrieved(string $author) : bool
Parameters
$author : string

Author being retrieved

Return values
bool

checkForRedirect()

Check for redirection in the Wikipedia response

protected checkForRedirect(array<string|int, mixed> $body) : array<string|int, mixed>
Parameters
$body : array<string|int, mixed>

Response body

Return values
array<string|int, mixed>

extractBodyText()

Extract body text

protected extractBodyText(array<string|int, mixed> $body, string $infoboxStr) : string
Parameters
$body : array<string|int, mixed>

Body details

$infoboxStr : string

Infobox found within body (if any)

Return values
string

extractImageFromBody()

Support method for parseWikipedia - extract first image from body

protected extractImageFromBody(array<string|int, mixed> $body) : array<string|int, mixed>
Parameters
$body : array<string|int, mixed>

The Wikipedia response to parse

Return values
array<string|int, mixed>

extractImageFromInfoBox()

Extract image information from an infobox

protected extractImageFromInfoBox(string $infoboxStr) : array<string|int, mixed>
Parameters
$infoboxStr : string

Infobox text

Return values
array<string|int, mixed>

Array with two values values: image name and image caption

extractInfoBox()

Support method for parseWikipedia - extract infobox details

protected extractInfoBox(array<string|int, mixed> $body) : string
Parameters
$body : array<string|int, mixed>

The Wikipedia response to parse

Return values
string

extractTextDomain()

Given a translation string with or without a text domain, return an array with the raw string and the text domain separated.

protected extractTextDomain(string|object|array<string|int, mixed> $target) : array<string|int, mixed>
Parameters
$target : string|object|array<string|int, mixed>

String to translate or an array of text domain and string to translate

Return values
array<string|int, mixed>

getDebugTranslation()

Build a debug-mode translation

protected getDebugTranslation(string $domain, string $str, array<string|int, mixed> $tokens) : string
Parameters
$domain : string

Text domain

$str : string

String to translate

$tokens : array<string|int, mixed>

Tokens to inject into the translated string

Return values
string

getWikipediaImageURL()

This method is responsible for obtaining an image URL based on a name.

protected getWikipediaImageURL(string $imageName) : mixed
Parameters
$imageName : string

The image name to look up

Return values
mixed

URL on success, false on failure

parseWikipedia()

_parseWikipedia

protected parseWikipedia(array<string|int, mixed> $rawBody) : array<string|int, mixed>

This method is responsible for parsing the output from the Wikipedia REST API.

Parameters
$rawBody : array<string|int, mixed>

The Wikipedia response to parse

Tags
author

Rushikesh Katikar rushikesh.katikar@gmail.com

Return values
array<string|int, mixed>

sanitizeTranslationKey()

Make sure there are not any illegal characters in the translation key that might prevent successful lookup in language files.

protected sanitizeTranslationKey(string $key) : string
Parameters
$key : string

Key to sanitize

Return values
string

Sanitized key

sanitizeWikipediaBody()

Support method for parseWikipedia - fix up details in the body

protected sanitizeWikipediaBody(string $body) : string
Parameters
$body : string

The Wikipedia response to sanitize

Return values
string

Support method for sanitizeWikipediaBody -- strip image/file links.

protected stripImageAndFileLinks(string $body) : string
Parameters
$body : string

The Wikipedia response to sanitize

Return values
string

translateString()

Get translation for a string

protected translateString(string $rawStr[, array<string|int, mixed> $tokens = [] ][, string $default = null ][, string $domain = 'default' ][, bool $useIcuFormatter = false ]) : string
Parameters
$rawStr : string

String to translate

$tokens : array<string|int, mixed> = []

Tokens to inject into the translated string

$default : string = null

Default value to use if no translation is found (null for no default).

$domain : string = 'default'

Text domain (omit for default)

$useIcuFormatter : bool = false

Should we use an ICU message formatter instead of the default behavior?

Return values
string

Search results