MediaWiki  master
ForeignAPIRepo Class Reference

A foreign repository for a remote MediaWiki accessible through api.php requests. More...

Inheritance diagram for ForeignAPIRepo:
Collaboration diagram for ForeignAPIRepo:

Public Member Functions

 __construct ( $info)
 
 canCacheThumbs ()
 Are we locally caching the thumbnails? More...
 
 enumFiles ( $callback)
 
 fetchImageQuery ( $query)
 Make an API query in the foreign repo, caching results. More...
 
 fileExistsBatch (array $files)
 
 findBySha1 ( $hash)
 
 getFileProps ( $virtualUrl)
 
 getImageInfo ( $data)
 
 getInfo ()
 Get information about the repo - overrides/extends the parent class's information. More...
 
 getThumbError ( $name, $width=-1, $height=-1, $otherParams='', $lang=null)
 
 getThumbUrlFromCache ( $name, $width, $height, $params="")
 Return the imageurl from cache if possible. More...
 
 getZonePath ( $zone)
 Get the local directory corresponding to one of the basic zones. More...
 
 getZoneUrl ( $zone, $ext=null)
 
 httpGetCached ( $attribute, $query, $cacheTTL=3600)
 HTTP GET request to a mediawiki API (with caching) More...
 
 newFile ( $title, $time=false)
 Per docs in FileRepo, this needs to return false if we don't support versioned files. More...
 
- Public Member Functions inherited from FileRepo
 __construct (array $info=null)
 
 backendSupportsUnicodePaths ()
 
 canTransformLocally ()
 Returns true if the repository can transform files locally. More...
 
 canTransformVia404 ()
 Returns true if the repository can transform files via a 404 handler. More...
 
 checkRedirect ( $title)
 Checks if there is a redirect named as $title. More...
 
 cleanDir ( $dir)
 Deletes a directory if empty. More...
 
 cleanupBatch (array $files, $flags=0)
 Deletes a batch of files. More...
 
 cleanupDeletedBatch (array $storageKeys)
 Delete files in the deleted directory if they are not referenced in the filearchive table. More...
 
 concatenate (array $srcPaths, $dstPath, $flags=0)
 Concatenate a list of temporary files into a target file location. More...
 
 delete ( $srcRel, $archiveRel)
 Move a file to the deletion archive. More...
 
 deleteBatch (array $sourceDestPairs)
 Move a group of files to the deletion archive. More...
 
 fileExists ( $file)
 Checks existence of a file. More...
 
 findBySha1s (array $hashes)
 Get an array of arrays or iterators of file objects for files that have the given SHA-1 content hashes. More...
 
 findFile ( $title, $options=[])
 Find an instance of the named file created at the specified time Returns false if the file does not exist. More...
 
 findFileFromKey ( $sha1, $options=[])
 Find an instance of the file with this key, created at the specified time Returns false if the file does not exist. More...
 
 findFiles (array $items, $flags=0)
 Find many files at once. More...
 
 findFilesByPrefix ( $prefix, $limit)
 Return an array of files where the name starts with $prefix. More...
 
 freeTemp ( $virtualUrl)
 Remove a temporary file or mark it for garbage collection. More...
 
 getBackend ()
 Get the file backend instance. More...
 
 getDeletedHashPath ( $key)
 Get a relative path for a deletion archive key, e.g. More...
 
 getDescriptionRenderUrl ( $name, $lang=null)
 Get the URL of the content-only fragment of the description page. More...
 
 getDescriptionStylesheetUrl ()
 Get the URL of the stylesheet to apply to description pages. More...
 
 getDescriptionUrl ( $name)
 Get the URL of an image description page. More...
 
 getDisplayName ()
 Get the human-readable name of the repo. More...
 
 getFileSha1 ( $virtualUrl)
 Get the sha1 (base 36) of a file with a given virtual URL/storage path. More...
 
 getFileSize ( $virtualUrl)
 Get the size of a file with a given virtual URL/storage path. More...
 
 getFileTimestamp ( $virtualUrl)
 Get the timestamp of a file with a given virtual URL/storage path. More...
 
 getHashLevels ()
 Get the number of hash directory levels. More...
 
 getHashPath ( $name)
 Get a relative path including trailing slash, e.g. More...
 
 getLocalCacheKey ( $kClassSuffix,... $components)
 Get a site-local, repository-qualified, WAN cache key. More...
 
 getLocalCopy ( $virtualUrl)
 Get a local FS copy of a file with a given virtual URL/storage path. More...
 
 getLocalReference ( $virtualUrl)
 Get a local FS file with a given virtual URL/storage path. More...
 
 getName ()
 Get the name of this repository, as specified by $info['name]' to the constructor. More...
 
 getNameFromTitle ( $title)
 Get the name of a file from its title. More...
 
 getReadOnlyReason ()
 Get an explanatory message if this repo is read-only. More...
 
 getRootDirectory ()
 Get the public zone root storage directory of the repository. More...
 
 getSharedCacheKey ( $kClassSuffix,... $components)
 Get a global, repository-qualified, WAN cache key. More...
 
 getTempHashPath ( $suffix)
 Get a relative path including trailing slash, e.g. More...
 
 getTempRepo ()
 Get a temporary private FileRepo associated with this repo. More...
 
 getThumbProxySecret ()
 Get the secret key for the proxied thumb service. More...
 
 getThumbProxyUrl ()
 Get the URL thumb.php requests are being proxied to. More...
 
 getThumbScriptUrl ()
 Get the URL of thumb.php. More...
 
 getUploadStash (UserIdentity $user=null)
 Get an UploadStash associated with this repo. More...
 
 getVirtualUrl ( $suffix=false)
 Get a URL referring to this repository, with the private mwrepo protocol. More...
 
 hasSha1Storage ()
 Returns whether or not storage is SHA-1 based. More...
 
 invalidateImageRedirect ( $title)
 Invalidates image redirect cache related to that image Doesn't do anything for repositories that don't support image redirects. More...
 
 isLocal ()
 Returns true if this the local file repository. More...
 
 makeUrl ( $query='', $entry='index')
 Make an url to this repo. More...
 
 nameForThumb ( $name)
 Get the portion of the file that contains the origin file name. More...
 
 newFatal ( $message,... $parameters)
 Create a new fatal error. More...
 
 newGood ( $value=null)
 Create a new good result. More...
 
 paranoidClean ( $param)
 Path disclosure protection function. More...
 
 passThrough ( $param)
 Path disclosure protection function. More...
 
 publish ( $src, $dstRel, $archiveRel, $flags=0, array $options=[])
 Copy or move a file either from a storage path, virtual URL, or file system path, into this repository at the specified destination location. More...
 
 publishBatch (array $ntuples, $flags=0)
 Publish a batch of files. More...
 
 quickCleanDir ( $dir)
 Deletes a directory if empty. More...
 
 quickImport ( $src, $dst, $options=null)
 Import a file from the local file system into the repo. More...
 
 quickImportBatch (array $triples)
 Import a batch of files from the local file system into the repo. More...
 
 quickPurge ( $path)
 Purge a file from the repo. More...
 
 quickPurgeBatch (array $paths)
 Purge a batch of files from the repo. More...
 
 resolveVirtualUrl ( $url)
 Get the backend storage path corresponding to a virtual URL. More...
 
 store ( $srcPath, $dstZone, $dstRel, $flags=0)
 Store a file to a given destination. More...
 
 storeBatch (array $triplets, $flags=0)
 Store a batch of files. More...
 
 storeTemp ( $originalName, $srcPath)
 Pick a random name in the temp zone and store a file to it. More...
 
 streamFileWithStatus ( $virtualUrl, $headers=[], $optHeaders=[])
 Attempt to stream a file with the given virtual URL/storage path. More...
 
 supportsSha1URLs ()
 Returns whether or not repo supports having originals SHA-1s in the thumb URLs. More...
 
 validateFilename ( $filename)
 Determine if a relative path is valid, i.e. More...
 

Static Public Member Functions

static getUserAgent ()
 The user agent the ForeignAPIRepo will use. More...
 
static httpGet ( $url, $timeout='default', $options=[], &$mtime=false)
 
- Static Public Member Functions inherited from FileRepo
static isVirtualUrl ( $url)
 Determine if a string is an mwrepo:// URL. More...
 

Protected Member Functions

 assertWritableRepo ()
 
- Protected Member Functions inherited from FileRepo
 enumFilesInStorage ( $callback)
 Call a callback function for every public file in the repository. More...
 
 getZoneLocation ( $zone)
 The storage container and base path of a zone. More...
 
 initDirectory ( $dir)
 Creates a directory with the appropriate zone permissions. More...
 
 initZones ( $doZones=[])
 Ensure that a single zone or list of zones is defined for usage. More...
 
 resolveToStoragePathIfVirtual ( $path)
 If a path is a virtual URL, resolve it to a storage path. More...
 

Static Protected Member Functions

static getIIProps ()
 
- Static Protected Member Functions inherited from FileRepo
static getHashPathForLevel ( $name, $levels)
 

Protected Attributes

int $apiMetadataExpiry = 14400
 API metadata cache time. More...
 
int $apiThumbCacheExpiry = 86400
 Check back with Commons after this expiry. More...
 
int $fileCacheExpiry = 2592000
 Redownload thumbnail files after this expiry. More...
 
 $fileFactory = [ ForeignAPIFile::class, 'newFromTitle' ]
 
array $mFileExists = []
 
- Protected Attributes inherited from FileRepo
int $abbrvThreshold
 File names over this size will use the short form of thumbnail names. More...
 
string $articleUrl
 Equivalent to $wgArticlePath, e.g. More...
 
FileBackend $backend
 
int $deletedHashLevels
 The number of directory levels for hash-based division of deleted files. More...
 
string $descBaseUrl
 URL of image description pages, e.g. More...
 
bool $disableLocalTransform = false
 Disable local image scaling. More...
 
null string $favicon = null
 The URL to a favicon (optional, may be a server-local path URL). More...
 
callable $fileFactory = [ UnregisteredLocalFile::class, 'newFromTitle' ]
 Override these in the base class. More...
 
callable false $fileFactoryKey = false
 Override these in the base class. More...
 
int $hashLevels
 The number of directory levels for hash-based division of files. More...
 
bool $hasSha1Storage = false
 
bool $initialCapital
 Equivalent to $wgCapitalLinks (or $wgCapitalLinkOverrides[NS_FILE], determines whether filenames implicitly start with a capital letter. More...
 
bool $isPrivate
 Whether all zones should be private (e.g. More...
 
callable false $oldFileFactory = false
 Override these in the base class. More...
 
callable false $oldFileFactoryKey = false
 Override these in the base class. More...
 
string $pathDisclosureProtection = 'simple'
 May be 'paranoid' to remove all parameters from error messages, 'none' to leave the paths in unchanged, or 'simple' to replace paths with placeholders. More...
 
string $scriptDirUrl
 URL of the MediaWiki installation, equivalent to $wgScriptPath, e.g. More...
 
bool $supportsSha1URLs = false
 
string $thumbProxySecret
 Secret key to pass as an X-Swift-Secret header to the proxied thumb service. More...
 
string $thumbProxyUrl
 URL of where to proxy thumb.php requests to. More...
 
string $thumbScriptUrl
 URL of thumb.php. More...
 
string false $thumbUrl
 The base thumbnail URL. More...
 
bool $transformVia404
 Whether to skip media file transformation on parse and rely on a 404 handler instead. More...
 
string false $url
 Public zone URL. More...
 
WANObjectCache $wanCache
 
array $zones = []
 Map of zones to config. More...
 

Additional Inherited Members

- Public Attributes inherited from FileRepo
int $descriptionCacheExpiry
 
bool $fetchDescription
 Whether to fetch commons image description pages and display them on the local wiki. More...
 
string $name
 
const DELETE_SOURCE = 1
 
const NAME_AND_TIME_ONLY = 1
 
const OVERWRITE = 2
 
const OVERWRITE_SAME = 4
 
const SKIP_LOCKING = 8
 

Detailed Description

A foreign repository for a remote MediaWiki accessible through api.php requests.

Example config:
'class' => ForeignAPIRepo::class,
'name' => 'shared',
'apibase' => 'https://en.wikipedia.org/w/api.php',
'fetchDescription' => true, // Optional
'descriptionCacheExpiry' => 3600,
];
$wgForeignFileRepos
Config variable stub for the ForeignFileRepos setting, for use by phpdoc and IDEs.

Definition at line 43 of file ForeignAPIRepo.php.

Constructor & Destructor Documentation

◆ __construct()

ForeignAPIRepo::__construct (   $info)
Parameters
array | null$info

Definition at line 86 of file ForeignAPIRepo.php.

References canCacheThumbs().

Member Function Documentation

◆ assertWritableRepo()

ForeignAPIRepo::assertWritableRepo ( )
protected
Exceptions
MWException

Reimplemented from FileRepo.

Definition at line 629 of file ForeignAPIRepo.php.

◆ canCacheThumbs()

ForeignAPIRepo::canCacheThumbs ( )

Are we locally caching the thumbnails?

Returns
bool

Definition at line 484 of file ForeignAPIRepo.php.

Referenced by __construct(), and getThumbUrlFromCache().

◆ enumFiles()

ForeignAPIRepo::enumFiles (   $callback)
Parameters
callable$callback
Exceptions
MWException

Reimplemented from FileRepo.

Definition at line 621 of file ForeignAPIRepo.php.

◆ fetchImageQuery()

ForeignAPIRepo::fetchImageQuery (   $query)

Make an API query in the foreign repo, caching results.

Parameters
array$query
Returns
array|null

Implements IForeignRepoWithMWApi.

Definition at line 208 of file ForeignAPIRepo.php.

References FormatJson\decode(), and httpGetCached().

Referenced by fileExistsBatch(), findBySha1(), and getThumbError().

◆ fileExistsBatch()

ForeignAPIRepo::fileExistsBatch ( array  $files)
Parameters
string[]$files
Returns
array

Reimplemented from FileRepo.

Definition at line 143 of file ForeignAPIRepo.php.

References $file, fetchImageQuery(), FileBackend\isStoragePath(), and wfWarn().

◆ findBySha1()

ForeignAPIRepo::findBySha1 (   $hash)
Parameters
string$hash
Returns
ForeignAPIFile[]

Reimplemented from FileRepo.

Definition at line 256 of file ForeignAPIRepo.php.

References fetchImageQuery(), ForeignAPIFile\getProps(), Title\makeTitle(), and NS_FILE.

◆ getFileProps()

ForeignAPIRepo::getFileProps (   $virtualUrl)
Parameters
string$virtualUrl
Returns
array

Reimplemented from FileRepo.

Definition at line 198 of file ForeignAPIRepo.php.

◆ getIIProps()

static ForeignAPIRepo::getIIProps ( )
staticprotected
Returns
string
Since
1.23

Definition at line 576 of file ForeignAPIRepo.php.

◆ getImageInfo()

ForeignAPIRepo::getImageInfo (   $data)
Parameters
array$data
Returns
bool|array

Definition at line 236 of file ForeignAPIRepo.php.

Referenced by getThumbError().

◆ getInfo()

ForeignAPIRepo::getInfo ( )

Get information about the repo - overrides/extends the parent class's information.

Returns
array
Since
1.22

Reimplemented from FileRepo.

Definition at line 503 of file ForeignAPIRepo.php.

References FormatJson\decode(), and httpGetCached().

◆ getThumbError()

ForeignAPIRepo::getThumbError (   $name,
  $width = -1,
  $height = -1,
  $otherParams = '',
  $lang = null 
)
Parameters
string$name
int$width
int$height
string$otherParams
string | null$langLanguage code for language of error
Returns
bool|MediaTransformError
Since
1.22

Definition at line 316 of file ForeignAPIRepo.php.

References $lang, FileRepo\$name, fetchImageQuery(), FileRepo\getDisplayName(), getImageInfo(), and wfDebug().

◆ getThumbUrlFromCache()

ForeignAPIRepo::getThumbUrlFromCache (   $name,
  $width,
  $height,
  $params = "" 
)

Return the imageurl from cache if possible.

If the url has been requested today, get it from cache Otherwise retrieve remote thumb url, check for local file.

Parameters
string$nameIs a dbkey form of a title
int$width
int$height
string$paramsOther rendering parameters (page number, etc) from handler's makeParamString.
Returns
bool|string

Definition at line 358 of file ForeignAPIRepo.php.

References $apiThumbCacheExpiry, FileRepo\$backend, FileRepo\$name, canCacheThumbs(), FileBackend\fileExists(), FileRepo\getBackend(), FileBackend\getFileTimestamp(), FileRepo\getHashPath(), FileRepo\getLocalCacheKey(), getZonePath(), getZoneUrl(), httpGet(), FileBackend\prepare(), FileBackend\quickCreate(), FileRepo\validateFilename(), wfDebug(), and wfTimestamp().

◆ getUserAgent()

static ForeignAPIRepo::getUserAgent ( )
static

The user agent the ForeignAPIRepo will use.

Returns
string

Definition at line 492 of file ForeignAPIRepo.php.

Referenced by httpGet().

◆ getZonePath()

ForeignAPIRepo::getZonePath (   $zone)

Get the local directory corresponding to one of the basic zones.

Parameters
string$zone
Returns
bool|null|string

Reimplemented from FileRepo.

Definition at line 471 of file ForeignAPIRepo.php.

Referenced by getThumbUrlFromCache().

◆ getZoneUrl()

ForeignAPIRepo::getZoneUrl (   $zone,
  $ext = null 
)
See also
FileRepo::getZoneUrl()
Parameters
string$zone
string | null$extOptional file extension
Returns
string

Reimplemented from FileRepo.

Definition at line 455 of file ForeignAPIRepo.php.

References $ext, FileRepo\$thumbUrl, and FileRepo\$url.

Referenced by getThumbUrlFromCache().

◆ httpGet()

static ForeignAPIRepo::httpGet (   $url,
  $timeout = 'default',
  $options = [],
$mtime = false 
)
static
Parameters
string$url
string$timeout
array$options
int | false&$mtimeResulting Last-Modified UNIX timestamp if received
Returns
string|false

Definition at line 537 of file ForeignAPIRepo.php.

References FileRepo\$url, getUserAgent(), PROTO_HTTP, wfDebug(), wfExpandUrl(), and wfTimestamp().

Referenced by getThumbUrlFromCache(), and httpGetCached().

◆ httpGetCached()

ForeignAPIRepo::httpGetCached (   $attribute,
  $query,
  $cacheTTL = 3600 
)

HTTP GET request to a mediawiki API (with caching)

Parameters
string$attributeUsed in cache key creation, mostly
array$queryThe query parameters for the API request
int$cacheTTLTime to live for the memcached caching
Returns
string|null

Definition at line 587 of file ForeignAPIRepo.php.

References FileRepo\$url, httpGet(), FileRepo\makeUrl(), and wfAppendQuery().

Referenced by fetchImageQuery(), and getInfo().

◆ newFile()

ForeignAPIRepo::newFile (   $title,
  $time = false 
)

Per docs in FileRepo, this needs to return false if we don't support versioned files.

Well, we don't.

Parameters
PageIdentity | LinkTarget | string$title
string | bool$time
Returns
File|false

Reimplemented from FileRepo.

Definition at line 131 of file ForeignAPIRepo.php.

References $title.

Member Data Documentation

◆ $apiMetadataExpiry

int ForeignAPIRepo::$apiMetadataExpiry = 14400
protected

API metadata cache time.

Since
1.38

This is often the performance bottleneck for ForeignAPIRepo. For each file used, we must fetch file metadata for it and every high-dpi variant, in serial, during the parse. This is slow if a page has many files, with RTT of the handshake often being significant. The metadata rarely changes, but if a new version of the file was uploaded, it might be displayed incorrectly until its metadata entry falls out of cache.

Definition at line 75 of file ForeignAPIRepo.php.

◆ $apiThumbCacheExpiry

int ForeignAPIRepo::$apiThumbCacheExpiry = 86400
protected

Check back with Commons after this expiry.

Definition at line 59 of file ForeignAPIRepo.php.

Referenced by getThumbUrlFromCache().

◆ $fileCacheExpiry

int ForeignAPIRepo::$fileCacheExpiry = 2592000
protected

Redownload thumbnail files after this expiry.

Definition at line 62 of file ForeignAPIRepo.php.

◆ $fileFactory

ForeignAPIRepo::$fileFactory = [ ForeignAPIFile::class, 'newFromTitle' ]
protected

Definition at line 57 of file ForeignAPIRepo.php.

◆ $mFileExists

array ForeignAPIRepo::$mFileExists = []
protected

Definition at line 78 of file ForeignAPIRepo.php.


The documentation for this class was generated from the following file: