Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F5964506
RemoteSchema.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
3 KB
Referenced Files
None
Subscribers
None
RemoteSchema.php
View Options
<?php
namespace
MediaWiki\Extension\EventLogging
;
use
JsonSerializable
;
use
MediaWiki\Http\HttpRequestFactory
;
use
MediaWiki\Json\FormatJson
;
use
MediaWiki\MediaWikiServices
;
use
stdClass
;
use
Wikimedia\ObjectCache\BagOStuff
;
/**
* Represents a schema revision on a remote wiki.
* Handles retrieval (via HTTP) and local caching.
*/
class
RemoteSchema
implements
JsonSerializable
{
public
const
LOCK_TIMEOUT
=
20
;
/** @var string */
public
$title
;
/** @var int */
public
$revision
;
/** @var BagOStuff */
public
$cache
;
/** @var HttpRequestFactory */
public
$httpRequestFactory
;
/** @var string */
public
$key
;
/** @var array|false */
public
$content
=
false
;
/**
* Constructor.
* @param string $title
* @param int $revision
* @param BagOStuff|null $cache (optional) cache client.
* @param HttpRequestFactory|null $httpRequestFactory (optional) HttpRequestFactory client.
*/
public
function
__construct
(
$title
,
$revision
,
$cache
=
null
,
$httpRequestFactory
=
null
)
{
global
$wgEventLoggingSchemaApiUri
;
$this
->
title
=
$title
;
$this
->
revision
=
$revision
;
$this
->
cache
=
$cache
?:
MediaWikiServices
::
getInstance
()
->
getObjectCacheFactory
()->
getInstance
(
CACHE_ANYTHING
);
$this
->
httpRequestFactory
=
$httpRequestFactory
?:
MediaWikiServices
::
getInstance
()->
getHttpRequestFactory
();
$this
->
key
=
$this
->
cache
->
makeGlobalKey
(
'eventlogging-schema'
,
$wgEventLoggingSchemaApiUri
,
$revision
);
}
/**
* Retrieves schema content.
* @return array|bool Schema or false if irretrievable.
*/
public
function
get
()
{
if
(
$this
->
content
)
{
return
$this
->
content
;
}
$this
->
content
=
$this
->
memcGet
();
if
(
$this
->
content
)
{
return
$this
->
content
;
}
$this
->
content
=
$this
->
httpGet
();
if
(
$this
->
content
)
{
$this
->
memcSet
();
}
return
$this
->
content
;
}
/**
* Returns an object containing serializable properties.
* @return array
*/
public
function
jsonSerialize
():
array
{
return
[
'schema'
=>
$this
->
get
()
?:
new
stdClass
(),
'revision'
=>
$this
->
revision
];
}
/**
* Retrieves content from memcached.
* @return array|bool Schema or false if not in cache.
*/
protected
function
memcGet
()
{
return
$this
->
cache
->
get
(
$this
->
key
);
}
/**
* Store content in memcached.
* @return bool
*/
protected
function
memcSet
()
{
return
$this
->
cache
->
set
(
$this
->
key
,
$this
->
content
);
}
/**
* Retrieves the schema using HTTP.
* Uses a memcached lock to avoid cache stampedes.
* @return array|bool Schema or false if unable to fetch.
*/
protected
function
httpGet
()
{
$uri
=
$this
->
getUri
();
if
(
!
$this
->
lock
()
)
{
EventLogging
::
getLogger
()->
warning
(
'Failed to get lock for requesting {schema_uri}.'
,
[
'schema_uri'
=>
$uri
]
);
return
false
;
}
$raw
=
$this
->
httpRequestFactory
->
get
(
$uri
,
[
'timeout'
=>
self
::
LOCK_TIMEOUT
*
0.8
],
__METHOD__
);
$content
=
FormatJson
::
decode
(
$raw
,
true
);
if
(
!
$content
)
{
EventLogging
::
getLogger
()->
error
(
'Request to {schema_uri} failed.'
,
[
'schema_uri'
=>
$uri
]
);
}
return
$content
?:
false
;
}
/**
* Acquire a mutex lock for HTTP retrieval.
* @return bool Whether lock was successfully acquired.
*/
protected
function
lock
()
{
return
$this
->
cache
->
add
(
$this
->
key
.
':lock'
,
1
,
self
::
LOCK_TIMEOUT
);
}
/**
* Constructs URI for retrieving schema from remote wiki.
* @return string URI.
*/
protected
function
getUri
()
{
global
$wgEventLoggingSchemaApiUri
;
return
wfAppendQuery
(
$wgEventLoggingSchemaApiUri
,
[
'action'
=>
'jsonschema'
,
'revid'
=>
$this
->
revision
,
'formatversion'
=>
2
,
'format'
=>
'json'
,
]
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, Oct 3, 10:21 (1 w, 1 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
c8/af/a995360c7d525df98e5cf73547cd
Default Alt Text
RemoteSchema.php (3 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment