2019-08-15 17:40:13 +00:00
|
|
|
<?php
|
|
|
|
|
2019-12-25 23:12:14 +00:00
|
|
|
namespace Citizen;
|
|
|
|
|
|
|
|
use ApiBase;
|
|
|
|
use ApiFormatJson;
|
|
|
|
use ApiResult;
|
|
|
|
use ConfigException;
|
|
|
|
use Exception;
|
|
|
|
use MWHttpRequest;
|
|
|
|
use Title;
|
|
|
|
|
2019-08-15 17:40:13 +00:00
|
|
|
/**
|
|
|
|
* Extract and modified from MobileFrontend extension
|
|
|
|
* Return the webapp manifest for this wiki
|
|
|
|
*/
|
|
|
|
class ApiWebappManifest extends ApiBase {
|
|
|
|
/**
|
|
|
|
* Execute the requested Api actions.
|
|
|
|
*/
|
|
|
|
public function execute() {
|
|
|
|
$resultObj = $this->getResult();
|
2019-12-25 23:12:14 +00:00
|
|
|
$resultObj->addValue( null, 'name', $this->getConfigSafe( 'Sitename' ) );
|
|
|
|
// Might as well add shortname
|
|
|
|
$resultObj->addValue( null, 'short_name', $this->getConfigSafe( 'Sitename' ) );
|
|
|
|
|
2019-08-15 17:40:13 +00:00
|
|
|
$resultObj->addValue( null, 'orientation', 'portrait' );
|
2019-12-25 23:12:14 +00:00
|
|
|
|
|
|
|
if ( $this->getConfigSafe( 'ContLang', false ) !== false ) {
|
|
|
|
$resultObj->addValue( null, 'dir', $this->getConfigSafe( 'ContLang' )->getDir() );
|
|
|
|
}
|
|
|
|
$resultObj->addValue( null, 'lang', $this->getConfigSafe( 'LanguageCode' ) );
|
|
|
|
|
|
|
|
// Changed to standalone to provide better experience
|
|
|
|
$resultObj->addValue( null, 'display', 'standalone' );
|
|
|
|
|
|
|
|
$resultObj->addValue( null, 'theme_color',
|
|
|
|
$this->getConfigSafe( 'CitizenManifestThemeColor' ) );
|
|
|
|
$resultObj->addValue( null, 'background_color',
|
|
|
|
$this->getConfigSafe( 'CitizenManifestBackgroundColor' ) );
|
|
|
|
|
2019-08-15 17:40:13 +00:00
|
|
|
$resultObj->addValue( null, 'start_url', Title::newMainPage()->getLocalUrl() );
|
|
|
|
|
2019-12-25 23:12:14 +00:00
|
|
|
$this->addIcons( $resultObj );
|
|
|
|
|
|
|
|
$main = $this->getMain();
|
|
|
|
$main->setCacheControl( [ 's-maxage' => 86400, 'max-age' => 86400 ] );
|
|
|
|
$main->setCacheMode( 'public' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ApiResult $result
|
|
|
|
*/
|
|
|
|
private function addIcons( $result ) {
|
2019-08-15 17:40:13 +00:00
|
|
|
$icons = [];
|
|
|
|
|
2019-12-25 23:12:14 +00:00
|
|
|
$appleTouchIcon = $this->getConfigSafe( 'AppleTouchIcon', false );
|
|
|
|
|
2019-08-15 17:40:13 +00:00
|
|
|
if ( $appleTouchIcon !== false ) {
|
2019-12-25 23:12:14 +00:00
|
|
|
try {
|
|
|
|
$appleTouchIconUrl = wfExpandUrl( $appleTouchIcon, PROTO_CURRENT );
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-15 17:40:13 +00:00
|
|
|
$request = MWHttpRequest::factory( $appleTouchIconUrl );
|
|
|
|
$request->execute();
|
|
|
|
$appleTouchIconContent = $request->getContent();
|
2019-12-25 23:12:14 +00:00
|
|
|
|
2019-08-15 17:40:13 +00:00
|
|
|
if ( !empty( $appleTouchIconContent ) ) {
|
|
|
|
$appleTouchIconSize = getimagesizefromstring( $appleTouchIconContent );
|
|
|
|
}
|
2019-12-25 23:12:14 +00:00
|
|
|
|
2019-08-15 17:40:13 +00:00
|
|
|
$icon = [
|
2019-12-25 23:12:14 +00:00
|
|
|
'src' => $appleTouchIcon,
|
2019-08-15 17:40:13 +00:00
|
|
|
];
|
2019-12-25 23:12:14 +00:00
|
|
|
|
2019-08-15 17:40:13 +00:00
|
|
|
if ( isset( $appleTouchIconSize ) && $appleTouchIconSize !== false ) {
|
2019-12-25 23:12:14 +00:00
|
|
|
$icon['sizes'] = $appleTouchIconSize[0] . 'x' . $appleTouchIconSize[1];
|
2019-08-15 17:40:13 +00:00
|
|
|
$icon['type'] = $appleTouchIconSize['mime'];
|
|
|
|
}
|
2019-12-25 23:12:14 +00:00
|
|
|
|
2019-08-15 17:40:13 +00:00
|
|
|
$icons[] = $icon;
|
|
|
|
}
|
|
|
|
|
2019-12-25 23:12:14 +00:00
|
|
|
$result->addValue( null, 'icons', $icons );
|
2019-08-15 17:40:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the JSON printer
|
|
|
|
*
|
|
|
|
* @return ApiFormatJson
|
|
|
|
*/
|
|
|
|
public function getCustomPrinter() {
|
|
|
|
return new ApiFormatJson( $this->getMain(), 'json' );
|
|
|
|
}
|
|
|
|
|
2019-12-25 23:12:14 +00:00
|
|
|
/**
|
|
|
|
* Calls getConfig. Returns empty string on exception or $default;
|
|
|
|
*
|
|
|
|
* @param string $key
|
2020-06-17 02:49:41 +00:00
|
|
|
* @param string|int|null $default
|
2019-12-25 23:12:14 +00:00
|
|
|
* @return mixed|string
|
|
|
|
* @see Config::get()
|
|
|
|
*/
|
|
|
|
private function getConfigSafe( $key, $default = null ) {
|
|
|
|
try {
|
|
|
|
return $this->getConfig()->get( $key );
|
|
|
|
} catch ( ConfigException $e ) {
|
|
|
|
return $default ?? '';
|
|
|
|
}
|
|
|
|
}
|
2019-08-15 17:40:13 +00:00
|
|
|
}
|