mediawiki-skins-Citizen/includes/Api/ApiWebappManifest.php

155 lines
4.2 KiB
PHP
Raw Normal View History

2019-08-15 17:40:13 +00:00
<?php
2020-06-17 02:56:19 +00:00
/**
* Citizen - A responsive skin developed for the Star Citizen Wiki
*
* This file is part of Citizen.
*
* Citizen is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Citizen is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Citizen. If not, see <https://www.gnu.org/licenses/>.
*
* @file
*/
2019-08-15 17:40:13 +00:00
namespace MediaWiki\Skins\Citizen\Api;
use ApiBase;
use MediaWiki\MediaWikiServices;
use MediaWiki\Title\Title;
use SpecialPage;
2019-08-15 17:40:13 +00:00
/**
* Based on the MobileFrontend extension
2019-08-15 17:40:13 +00:00
* Return the webapp manifest for this wiki
*
* T282500
* TODO: This should be merged to core
2019-08-15 17:40:13 +00:00
*/
class ApiWebappManifest extends ApiBase {
/**
* Execute the requested Api actions.
*/
public function execute() {
2020-08-09 18:41:01 +00:00
$services = MediaWikiServices::getInstance();
2020-08-09 18:41:01 +00:00
$config = $this->getConfig();
$resultObj = $this->getResult();
$resultObj->addValue( null, 'dir', $services->getContentLanguage()->getDir() );
$resultObj->addValue( null, 'lang', $config->get( 'LanguageCode' ) );
$resultObj->addValue( null, 'name', $config->get( '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( 'Server' ) . '/' );
$resultObj->addValue( null, 'icons', $this->getIcons( $config, $services ) );
$resultObj->addValue( null, 'display', 'standalone' );
$resultObj->addValue( null, 'orientation', 'portrait' );
$resultObj->addValue( null, 'start_url', Title::newMainPage()->getLocalURL() );
$resultObj->addValue( null, 'theme_color', $config->get( 'CitizenManifestThemeColor' ) );
$resultObj->addValue( null, 'background_color', $config->get( 'CitizenManifestBackgroundColor' ) );
$resultObj->addValue( null, 'shortcuts', $this->getShortcuts() );
$main = $this->getMain();
2023-01-17 21:34:02 +00:00
$main->setCacheMaxAge( 604800 );
$main->setCacheMode( 'public' );
}
/**
* Get icons for manifest
*
* @param Config $config
* @param MediaWikiServices $services
* @return array
*/
private function getIcons( $config, $services ) {
2019-08-15 17:40:13 +00:00
$icons = [];
2022-10-23 17:38:05 +00:00
$logos = $config->get( 'Logos' );
2022-10-23 17:38:05 +00:00
// That really shouldn't happen
if ( $logos !== false ) {
$logoKeys = [
'1x',
'1.5x',
'2x',
'icon',
'svg'
2019-08-15 17:40:13 +00:00
];
2022-10-23 17:38:05 +00:00
foreach ( $logoKeys as $logoKey ) {
// Avoid undefined index
if ( !isset( $logos[$logoKey] ) ) {
continue;
}
2022-10-23 17:38:05 +00:00
$logo = (string)$logos[$logoKey];
if ( !empty( $logo ) ) {
$logoUrl = $services->getUrlUtils()->expand( $logo, PROTO_CURRENT );
2022-10-23 17:38:05 +00:00
$request = $services->getHttpRequestFactory()->create( $logoUrl, [], __METHOD__ );
$request->execute();
$logoContent = $request->getContent();
if ( !empty( $logoContent ) ) {
$logoSize = getimagesizefromstring( $logoContent );
}
$icon = [
'src' => $logo
];
2022-10-23 17:38:05 +00:00
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 ( substr( $logo, -3 ) === 'svg' ) {
$icon['sizes'] = 'any';
$icon['type'] = 'image/svg+xml';
}
$icons[] = $icon;
}
2019-08-15 17:40:13 +00:00
}
}
return $icons;
2019-08-15 17:40:13 +00:00
}
/**
* Get shortcuts for manifest
*
* @return array
*/
private function getShortcuts() {
$shortcuts = [];
$specialPages = [ 'Search', 'Randompage', 'RecentChanges' ];
foreach ( $specialPages as $specialPage ) {
$shortcut = [];
$title = SpecialPage::getSafeTitleFor( $specialPage );
$shortcut['name'] = $title->getBaseText();
$shortcut['url'] = $title->getLocalURL();
$shortcuts[] = $shortcut;
}
return $shortcuts;
}
2019-08-15 17:40:13 +00:00
/**
* Get the JSON printer
*
* @return ApiWebappManifestFormatJson
2019-08-15 17:40:13 +00:00
*/
public function getCustomPrinter() {
return new ApiWebappManifestFormatJson( $this->getMain(), 'webmanifest' );
2019-08-15 17:40:13 +00:00
}
}