mirror of
https://github.com/StarCitizenTools/mediawiki-skins-Citizen.git
synced 2024-09-24 02:38:44 +00:00
Add 'Citizen' namespaces to Api class
Refactor code to pass MediaWiki guidelines
This commit is contained in:
parent
cf58f8b9a3
commit
41b1f157cd
|
@ -1,55 +1,91 @@
|
|||
<?php
|
||||
|
||||
namespace Citizen;
|
||||
|
||||
use ApiBase;
|
||||
use ApiFormatJson;
|
||||
use ApiResult;
|
||||
use ConfigException;
|
||||
use Exception;
|
||||
use MWHttpRequest;
|
||||
use Title;
|
||||
|
||||
/**
|
||||
* 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() {
|
||||
$config = $this->getConfig();
|
||||
$resultObj = $this->getResult();
|
||||
$resultObj->addValue( null, 'name', $config->get( 'Sitename' ) );
|
||||
$resultObj->addValue( null, 'short_name', $config->get( 'Sitename' ) ); // Might as well add shortname
|
||||
$resultObj->addValue( null, 'name', $this->getConfigSafe( 'Sitename' ) );
|
||||
// Might as well add shortname
|
||||
$resultObj->addValue( null, 'short_name', $this->getConfigSafe( 'Sitename' ) );
|
||||
|
||||
$resultObj->addValue( null, 'orientation', 'portrait' );
|
||||
$resultObj->addValue( null, 'dir', $config->get( 'ContLang' )->getDir() );
|
||||
$resultObj->addValue( null, 'lang', $config->get( 'LanguageCode' ) );
|
||||
$resultObj->addValue( null, 'display', 'standalone' ); // Changed to standalone to provide better experience
|
||||
$resultObj->addValue( null, 'theme_color', $config->get( 'CitizenManifestThemeColor' ) );
|
||||
$resultObj->addValue( null, 'background_color', $config->get( 'CitizenManifestBackgroundColor' ) );
|
||||
|
||||
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' ) );
|
||||
|
||||
$resultObj->addValue( null, 'start_url', Title::newMainPage()->getLocalUrl() );
|
||||
|
||||
$icons = [];
|
||||
|
||||
$appleTouchIcon = $config->get( 'AppleTouchIcon' );
|
||||
if ( $appleTouchIcon !== false ) {
|
||||
$appleTouchIconUrl = wfExpandUrl( $appleTouchIcon, PROTO_CURRENT );
|
||||
$request = MWHttpRequest::factory( $appleTouchIconUrl );
|
||||
$request->execute();
|
||||
$appleTouchIconContent = $request->getContent();
|
||||
if ( !empty( $appleTouchIconContent ) ) {
|
||||
$appleTouchIconSize = getimagesizefromstring( $appleTouchIconContent );
|
||||
}
|
||||
$icon = [
|
||||
'src' => $appleTouchIcon
|
||||
];
|
||||
if ( isset( $appleTouchIconSize ) && $appleTouchIconSize !== false ) {
|
||||
$icon['sizes'] = $appleTouchIconSize[0].'x'.$appleTouchIconSize[1];
|
||||
$icon['type'] = $appleTouchIconSize['mime'];
|
||||
}
|
||||
$icons[] = $icon;
|
||||
}
|
||||
|
||||
$resultObj->addValue( null, 'icons', $icons );
|
||||
$this->addIcons( $resultObj );
|
||||
|
||||
$main = $this->getMain();
|
||||
$main->setCacheControl( [ 's-maxage' => 86400, 'max-age' => 86400 ] );
|
||||
$main->setCacheMode( 'public' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ApiResult $result
|
||||
*/
|
||||
private function addIcons( $result ) {
|
||||
$icons = [];
|
||||
|
||||
$appleTouchIcon = $this->getConfigSafe( 'AppleTouchIcon', false );
|
||||
|
||||
if ( $appleTouchIcon !== false ) {
|
||||
try {
|
||||
$appleTouchIconUrl = wfExpandUrl( $appleTouchIcon, PROTO_CURRENT );
|
||||
} catch ( Exception $e ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$request = MWHttpRequest::factory( $appleTouchIconUrl );
|
||||
$request->execute();
|
||||
$appleTouchIconContent = $request->getContent();
|
||||
|
||||
if ( !empty( $appleTouchIconContent ) ) {
|
||||
$appleTouchIconSize = getimagesizefromstring( $appleTouchIconContent );
|
||||
}
|
||||
|
||||
$icon = [
|
||||
'src' => $appleTouchIcon,
|
||||
];
|
||||
|
||||
if ( isset( $appleTouchIconSize ) && $appleTouchIconSize !== false ) {
|
||||
$icon['sizes'] = $appleTouchIconSize[0] . 'x' . $appleTouchIconSize[1];
|
||||
$icon['type'] = $appleTouchIconSize['mime'];
|
||||
}
|
||||
|
||||
$icons[] = $icon;
|
||||
}
|
||||
|
||||
$result->addValue( null, 'icons', $icons );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the JSON printer
|
||||
*
|
||||
|
@ -59,4 +95,19 @@ class ApiWebappManifest extends ApiBase {
|
|||
return new ApiFormatJson( $this->getMain(), 'json' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls getConfig. Returns empty string on exception or $default;
|
||||
*
|
||||
* @param string $key
|
||||
* @param string|integer $default
|
||||
* @return mixed|string
|
||||
* @see Config::get()
|
||||
*/
|
||||
private function getConfigSafe( $key, $default = null ) {
|
||||
try {
|
||||
return $this->getConfig()->get( $key );
|
||||
} catch ( ConfigException $e ) {
|
||||
return $default ?? '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue