Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F7461527
ApiWebappManifest.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
5 KB
Referenced Files
None
Subscribers
None
ApiWebappManifest.php
View Options
<?php
declare
(
strict_types
=
1
);
namespace
MediaWiki\Skins\Citizen\Api
;
use
Exception
;
use
MediaWiki\Api\ApiBase
;
use
MediaWiki\Api\ApiMain
;
use
MediaWiki\Config\Config
;
use
MediaWiki\Http\HttpRequestFactory
;
use
MediaWiki\Language\Language
;
use
MediaWiki\MainConfigNames
;
use
MediaWiki\SpecialPage\SpecialPage
;
use
MediaWiki\Title\Title
;
use
MediaWiki\Utils\UrlUtils
;
/**
* Based on the MobileFrontend extension
* Return the webapp manifest for this wiki
*
* T282500
* TODO: This should be merged to core
*/
class
ApiWebappManifest
extends
ApiBase
{
/* 1 week */
private
const
CACHE_MAX_AGE
=
604800
;
private
Config
$config
;
private
array
$options
;
public
function
__construct
(
private
readonly
ApiMain
$main
,
private
readonly
string
$moduleName
,
private
readonly
Language
$contentLanguage
,
private
readonly
HttpRequestFactory
$httpRequestFactory
,
private
readonly
UrlUtils
$urlUtils
,
)
{
parent
::
__construct
(
$main
,
$moduleName
);
$this
->
config
=
$this
->
getConfig
();
$this
->
options
=
$this
->
config
->
get
(
'CitizenManifestOptions'
);
}
/**
* Execute the requested Api actions.
*/
public
function
execute
():
void
{
$config
=
$this
->
config
;
$resultObj
=
$this
->
getResult
();
$main
=
$this
->
main
;
$options
=
$this
->
options
;
$resultObj
->
addValue
(
null
,
'dir'
,
$this
->
contentLanguage
->
getDir
()
);
$resultObj
->
addValue
(
null
,
'lang'
,
$config
->
get
(
MainConfigNames
::
LanguageCode
)
);
$resultObj
->
addValue
(
null
,
'name'
,
$config
->
get
(
MainConfigNames
::
Sitename
)
);
// Need to set it manually because the default from start_url does not include script namespace
// E.g. index.php URLs will be thrown out of the PWA
$resultObj
->
addValue
(
null
,
'scope'
,
$config
->
get
(
MainConfigNames
::
Server
)
.
'/'
);
$resultObj
->
addValue
(
null
,
'icons'
,
$this
->
getIcons
()
);
$resultObj
->
addValue
(
null
,
'display'
,
'standalone'
);
$resultObj
->
addValue
(
null
,
'orientation'
,
'natural'
);
$resultObj
->
addValue
(
null
,
'start_url'
,
Title
::
newMainPage
()->
getLocalURL
()
);
$resultObj
->
addValue
(
null
,
'theme_color'
,
$options
[
'theme_color'
]
);
$resultObj
->
addValue
(
null
,
'background_color'
,
$options
[
'background_color'
]
);
$resultObj
->
addValue
(
null
,
'shortcuts'
,
$this
->
getShortcuts
()
);
if
(
$options
[
'short_name'
]
!==
''
)
{
$resultObj
->
addValue
(
null
,
'short_name'
,
$options
[
'short_name'
]
);
}
if
(
$options
[
'description'
]
!==
''
)
{
$resultObj
->
addValue
(
null
,
'description'
,
$options
[
'description'
]
);
}
$main
->
setCacheMaxAge
(
self
::
CACHE_MAX_AGE
);
$main
->
setCacheMode
(
'public'
);
}
/**
* Get icons for manifest
*/
private
function
getIcons
():
array
{
$iconsConfig
=
$this
->
options
[
'icons'
];
if
(
!
is_array
(
$iconsConfig
)
||
count
(
$iconsConfig
)
===
0
)
{
return
$this
->
getIconsFromLogos
();
}
$icons
=
[];
$allowedKeys
=
[
'src'
,
'sizes'
,
'type'
,
'purpose'
];
foreach
(
$iconsConfig
as
$iconConfig
)
{
if
(
!
is_array
(
$iconConfig
)
)
{
continue
;
}
$icon
=
array_intersect_key
(
$iconConfig
,
array_flip
(
$allowedKeys
)
);
if
(
count
(
$icon
)
===
0
)
{
continue
;
}
$icons
[]
=
$icon
;
}
return
$icons
;
}
/**
* Get icons from wgLogos
*/
private
function
getIconsFromLogos
():
array
{
$urlUtils
=
$this
->
urlUtils
;
$httpRequestFactory
=
$this
->
httpRequestFactory
;
$icons
=
[];
$logos
=
$this
->
config
->
get
(
MainConfigNames
::
Logos
);
if
(
!
$logos
)
{
return
$icons
;
}
$logoKeys
=
[
'1x'
,
'1.5x'
,
'2x'
,
'icon'
,
'svg'
];
foreach
(
$logoKeys
as
$logoKey
)
{
// Avoid undefined index
if
(
!
isset
(
$logos
[
$logoKey
]
)
)
{
continue
;
}
$logoPath
=
(
string
)
$logos
[
$logoKey
];
try
{
$logoUrl
=
$urlUtils
->
expand
(
$logoPath
,
PROTO_CURRENT
)
??
''
;
$request
=
$httpRequestFactory
->
create
(
$logoUrl
,
[],
__METHOD__
);
$request
->
execute
();
$logoContent
=
$request
->
getContent
();
}
catch
(
Exception
$e
)
{
// Log the exception or handle it accordingly
$logoContent
=
''
;
}
if
(
$logoContent
!==
''
)
{
$logoSize
=
getimagesizefromstring
(
$logoContent
);
}
$icon
=
[
'src'
=>
$logoPath
];
if
(
isset
(
$logoSize
)
&&
$logoSize
!==
false
)
{
$icon
[
'sizes'
]
=
$logoSize
[
0
]
.
'x'
.
$logoSize
[
1
];
$icon
[
'type'
]
=
$logoSize
[
'mime'
];
}
// Set sizes to any if it is a SVG
if
(
str_ends_with
(
$logoPath
,
'svg'
)
)
{
$icon
[
'sizes'
]
=
'any'
;
$icon
[
'type'
]
=
'image/svg+xml'
;
}
// Exit if not sizes are detected
if
(
!
isset
(
$icon
[
'sizes'
]
)
)
{
continue
;
}
$icons
[]
=
$icon
;
}
return
$icons
;
}
/**
* Get shortcuts for manifest
*/
private
function
getShortcuts
():
array
{
$specialPages
=
[
'Search'
,
'Randompage'
,
'RecentChanges'
];
return
array_map
(
static
function
(
$specialPage
)
{
$title
=
SpecialPage
::
getSafeTitleFor
(
$specialPage
);
return
[
'name'
=>
$title
->
getBaseText
(),
'url'
=>
$title
->
getLocalURL
()
];
},
$specialPages
);
}
/**
* Get the JSON printer
*/
public
function
getCustomPrinter
():
ApiWebappManifestFormatJson
{
return
new
ApiWebappManifestFormatJson
(
$this
->
main
,
'webmanifest'
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Mon, Nov 23, 03:07 (3 d, 1 h ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
72/93/bf30df61f15498df7439f20577bb
Default Alt Text
ApiWebappManifest.php (5 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment