2021-01-28 11:46:27 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @ingroup Skins
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare( strict_types=1 );
|
|
|
|
|
2022-05-26 20:54:52 +00:00
|
|
|
namespace MediaWiki\Skins\Citizen\Hooks;
|
2021-01-28 11:46:27 +00:00
|
|
|
|
2021-04-05 13:56:44 +00:00
|
|
|
use MediaWiki\Hook\BeforePageDisplayHook;
|
2022-11-07 22:28:39 +00:00
|
|
|
use MediaWiki\Hook\SidebarBeforeOutputHook;
|
|
|
|
use MediaWiki\Hook\SkinBuildSidebarHook;
|
2022-10-05 18:04:16 +00:00
|
|
|
use MediaWiki\Hook\SkinTemplateNavigation__UniversalHook;
|
2022-10-05 18:06:47 +00:00
|
|
|
use MediaWiki\Skins\Hook\SkinPageReadyConfigHook;
|
2021-01-28 11:46:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hooks to run relating the skin
|
|
|
|
*/
|
2022-10-05 18:04:16 +00:00
|
|
|
class SkinHooks implements
|
|
|
|
BeforePageDisplayHook,
|
2022-11-07 22:28:39 +00:00
|
|
|
SidebarBeforeOutputHook,
|
|
|
|
SkinBuildSidebarHook,
|
2022-10-05 18:04:16 +00:00
|
|
|
SkinPageReadyConfigHook,
|
|
|
|
SkinTemplateNavigation__UniversalHook
|
|
|
|
{
|
2021-04-05 13:56:44 +00:00
|
|
|
/**
|
|
|
|
* Adds the inline theme switcher script to the page
|
|
|
|
*
|
|
|
|
* @param OutputPage $out
|
|
|
|
* @param Skin $skin
|
|
|
|
*/
|
|
|
|
public function onBeforePageDisplay( $out, $skin ): void {
|
2021-05-08 16:41:59 +00:00
|
|
|
// It's better to exit before any additional check
|
|
|
|
if ( $skin->getSkinName() !== 'citizen' ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-05 13:56:44 +00:00
|
|
|
$nonce = $out->getCSP()->getNonce();
|
|
|
|
|
|
|
|
// Script content at 'skins.citizen.scripts.theme/inline.js
|
2022-05-22 19:12:05 +00:00
|
|
|
// phpcs:disable Generic.Files.LineLength.TooLong
|
2021-04-05 13:56:44 +00:00
|
|
|
$script = sprintf(
|
|
|
|
'<script%s>%s</script>',
|
|
|
|
$nonce !== false ? sprintf( ' nonce="%s"', $nonce ) : '',
|
2022-06-29 19:29:44 +00:00
|
|
|
'window.applyPref=()=>{const a="skin-citizen-",b="skin-citizen-theme",c=a=>window.localStorage.getItem(a),d=c("skin-citizen-theme"),e=()=>{const d={fontsize:"font-size",pagewidth:"--width-layout",lineheight:"--line-height"},e=()=>["auto","dark","light"].map(b=>a+b),f=a=>{let b=document.getElementById("citizen-style");null===b&&(b=document.createElement("style"),b.setAttribute("id","citizen-style"),document.head.appendChild(b)),b.textContent=`:root{${a}}`};try{const g=c(b);let h="";if(null!==g){const b=document.documentElement;b.classList.remove(...e(a)),b.classList.add(a+g)}for(const[b,e]of Object.entries(d)){const d=c(a+b);null!==d&&(h+=`${e}:${d};`)}h&&f(h)}catch(a){}};if("auto"===d){const a=window.matchMedia("(prefers-color-scheme: dark)"),c=a.matches?"dark":"light",d=(a,b)=>window.localStorage.setItem(a,b);d(b,c),e(),a.addEventListener("change",()=>{e()}),d(b,"auto")}else e()},(()=>{window.applyPref()})();'
|
2021-04-05 13:56:44 +00:00
|
|
|
);
|
2022-05-22 19:12:05 +00:00
|
|
|
// phpcs:enable Generic.Files.LineLength.TooLong
|
2021-04-05 13:56:44 +00:00
|
|
|
|
|
|
|
$out->addHeadItem( 'skin.citizen.inline', $script );
|
|
|
|
}
|
2022-10-05 18:04:16 +00:00
|
|
|
|
2022-11-07 22:28:39 +00:00
|
|
|
/**
|
|
|
|
* Modify toolbox links
|
|
|
|
* For some reason onSkinBuildSidebar was not able to get toolbox
|
2022-11-07 22:29:15 +00:00
|
|
|
* So we need to use this hook instead
|
2022-11-07 22:28:39 +00:00
|
|
|
*
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/SidebarBeforeOutput
|
|
|
|
* @param Skin $skin
|
|
|
|
* @param array &$sidebar
|
|
|
|
*/
|
|
|
|
public function onSidebarBeforeOutput( $skin, &$sidebar ): void {
|
|
|
|
// Be extra safe because it might be active on other skins with caching
|
|
|
|
if ( $skin->getSkinName() === 'citizen' && $sidebar ) {
|
|
|
|
if ( isset( $sidebar['TOOLBOX'] ) ) {
|
|
|
|
self::updateToolboxMenu( $sidebar );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modify sidebar links
|
|
|
|
* This is cached compared to onSidebarBeforeOutput
|
|
|
|
*
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/SkinBuildSidebar
|
|
|
|
* @param Skin $skin
|
|
|
|
* @param array &$bar
|
|
|
|
*/
|
|
|
|
public function onSkinBuildSidebar( $skin, &$bar ): void {
|
|
|
|
// Be extra safe because it might be active on other skins with caching
|
|
|
|
if ( $skin->getSkinName() === 'citizen' && $bar ) {
|
|
|
|
foreach ( $bar as $key => $item ) {
|
|
|
|
self::addIconsToMenuItems( $bar, $key );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 18:04:16 +00:00
|
|
|
/**
|
|
|
|
* SkinPageReadyConfig hook handler
|
|
|
|
*
|
|
|
|
* Replace searchModule provided by skin.
|
|
|
|
*
|
|
|
|
* @since 1.35
|
|
|
|
* @param ResourceLoaderContext $context
|
|
|
|
* @param mixed[] &$config Associative array of configurable options
|
|
|
|
* @return void This hook must not abort, it must return no value
|
|
|
|
*/
|
|
|
|
public function onSkinPageReadyConfig( $context, array &$config ): void {
|
|
|
|
// It's better to exit before any additional check
|
|
|
|
if ( $context->getSkin() !== 'citizen' ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tell the `mediawiki.page.ready` module not to wire up search.
|
|
|
|
$config['search'] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modify navigation links
|
2022-10-05 18:06:47 +00:00
|
|
|
*
|
2022-10-05 18:04:16 +00:00
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/SkinTemplateNavigation::Universal
|
|
|
|
* @param SkinTemplate $sktemplate
|
|
|
|
* @param array &$links
|
|
|
|
*/
|
|
|
|
public function onSkinTemplateNavigation__Universal( $sktemplate, &$links ): void {
|
2022-11-01 00:40:43 +00:00
|
|
|
// Be extra safe because it might be active on other skins with caching
|
|
|
|
if ( $sktemplate->getSkinName() === 'citizen' ) {
|
2022-11-01 01:18:53 +00:00
|
|
|
if ( isset( $links['actions'] ) ) {
|
2022-11-07 22:39:02 +00:00
|
|
|
self::updateActionsMenu( $links );
|
2022-11-01 01:18:53 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 15:47:51 +00:00
|
|
|
if ( isset( $links['associated-pages'] ) ) {
|
2022-11-07 22:39:02 +00:00
|
|
|
self::updateAssociatedPagesMenu( $links );
|
2022-11-01 15:47:51 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 00:40:43 +00:00
|
|
|
if ( isset( $links['user-menu'] ) ) {
|
2022-11-01 01:18:53 +00:00
|
|
|
self::updateUserMenu( $sktemplate, $links );
|
2022-11-01 00:40:43 +00:00
|
|
|
}
|
2022-11-02 03:01:52 +00:00
|
|
|
|
|
|
|
if ( isset( $links['views'] ) ) {
|
2022-11-07 22:39:02 +00:00
|
|
|
self::updateViewsMenu( $links );
|
2022-11-02 03:01:52 +00:00
|
|
|
}
|
2022-11-01 00:40:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-01 01:18:53 +00:00
|
|
|
* Update actions menu items
|
2022-11-01 00:40:43 +00:00
|
|
|
*
|
|
|
|
* @param array &$links
|
|
|
|
*/
|
2022-11-07 22:39:02 +00:00
|
|
|
private static function updateActionsMenu( &$links ) {
|
2022-11-01 01:18:53 +00:00
|
|
|
// Most icons are not mapped yet in the actions menu
|
|
|
|
$iconMap = [
|
|
|
|
'delete' => 'trash',
|
|
|
|
'move' => 'move',
|
|
|
|
'protect' => 'lock',
|
|
|
|
'unprotect' => 'unLock'
|
|
|
|
];
|
|
|
|
|
2022-11-01 15:47:51 +00:00
|
|
|
self::mapIconsToMenuItems( $links, 'actions', $iconMap );
|
2022-11-01 01:18:53 +00:00
|
|
|
self::addIconsToMenuItems( $links, 'actions' );
|
|
|
|
}
|
|
|
|
|
2022-11-01 15:47:51 +00:00
|
|
|
/**
|
|
|
|
* Update associated pages menu items
|
|
|
|
*
|
|
|
|
* @param array &$links
|
|
|
|
*/
|
2022-11-07 22:39:02 +00:00
|
|
|
private static function updateAssociatedPagesMenu( &$links ) {
|
2022-11-01 15:47:51 +00:00
|
|
|
// Most icons are not mapped yet in the associated pages menu
|
|
|
|
$iconMap = [
|
|
|
|
'talk' => 'speechBubbles',
|
|
|
|
'main' => 'article',
|
2022-11-07 22:33:37 +00:00
|
|
|
'user' => 'userAvatar',
|
|
|
|
'user_talk' => 'userTalk'
|
2022-11-01 15:47:51 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
self::mapIconsToMenuItems( $links, 'associated-pages', $iconMap );
|
|
|
|
self::addIconsToMenuItems( $links, 'associated-pages' );
|
|
|
|
}
|
|
|
|
|
2022-11-07 22:28:39 +00:00
|
|
|
/**
|
|
|
|
* Update toolbox menu items
|
|
|
|
*
|
|
|
|
* @param array &$links
|
|
|
|
*/
|
|
|
|
private static function updateToolboxMenu( &$links ) {
|
|
|
|
// Most icons are not mapped yet in the toolbox menu
|
|
|
|
$iconMap = [
|
|
|
|
'whatlinkshere' => 'articleRedirect',
|
|
|
|
'recentchangeslinked' => 'recentChanges',
|
|
|
|
'print' => 'printer',
|
|
|
|
'permalink' => 'link',
|
|
|
|
'info' => 'infoFilled',
|
|
|
|
'contributions' => 'userContributions',
|
|
|
|
'log' => 'history',
|
2022-11-07 22:29:15 +00:00
|
|
|
'blockip' => 'block',
|
2022-11-07 22:28:39 +00:00
|
|
|
'userrights' => 'userGroup'
|
|
|
|
];
|
|
|
|
|
|
|
|
self::mapIconsToMenuItems( $links, 'TOOLBOX', $iconMap );
|
|
|
|
self::addIconsToMenuItems( $links, 'TOOLBOX' );
|
|
|
|
}
|
|
|
|
|
2022-11-01 01:18:53 +00:00
|
|
|
/**
|
|
|
|
* Update user menu
|
|
|
|
*
|
|
|
|
* @param SkinTemplate $sktemplate
|
|
|
|
* @param array &$links
|
|
|
|
*/
|
|
|
|
private static function updateUserMenu( $sktemplate, &$links ) {
|
2022-10-06 00:06:48 +00:00
|
|
|
$user = $sktemplate->getUser();
|
|
|
|
$isRegistered = $user->isRegistered();
|
|
|
|
$isTemp = $user->isTemp();
|
2022-10-05 21:23:51 +00:00
|
|
|
|
2022-10-06 00:06:48 +00:00
|
|
|
if ( $isTemp ) {
|
|
|
|
// Remove temporary user page text from user menu and recreate it in user info
|
|
|
|
unset( $links['user-menu']['tmpuserpage'] );
|
2022-10-05 23:51:20 +00:00
|
|
|
// Remove links as they are added to the bottom of user menu later
|
|
|
|
// unset( $links['user-menu']['logout'] );
|
2022-10-06 00:07:21 +00:00
|
|
|
} elseif ( $isRegistered ) {
|
2022-10-06 00:06:48 +00:00
|
|
|
// Remove user page link from user menu and recreate it in user info
|
|
|
|
unset( $links['user-menu']['userpage'] );
|
2022-10-05 23:51:20 +00:00
|
|
|
} else {
|
2022-10-06 00:06:48 +00:00
|
|
|
// Remove anon user page text from user menu and recreate it in user info
|
2022-10-05 21:23:51 +00:00
|
|
|
unset( $links['user-menu']['anonuserpage'] );
|
2022-10-05 23:51:20 +00:00
|
|
|
// Remove links as they are added to the bottom of user menu later
|
|
|
|
// unset( $links['user-menu']['createaccount'] );
|
|
|
|
// unset( $links['user-menu']['login'] );
|
|
|
|
// unset( $links['user-menu']['login-private'] );
|
2022-10-05 21:23:51 +00:00
|
|
|
}
|
2022-11-01 00:40:43 +00:00
|
|
|
|
2022-11-01 01:18:53 +00:00
|
|
|
self::addIconsToMenuItems( $links, 'user-menu' );
|
|
|
|
}
|
|
|
|
|
2022-11-02 03:01:52 +00:00
|
|
|
/**
|
|
|
|
* Update views menu items
|
|
|
|
*
|
|
|
|
* @param array &$links
|
|
|
|
*/
|
2022-11-07 22:39:02 +00:00
|
|
|
private static function updateViewsMenu( &$links ) {
|
2022-11-02 03:01:52 +00:00
|
|
|
// Most icons are not mapped yet in the views menu
|
|
|
|
$iconMap = [
|
|
|
|
'view' => 'article',
|
|
|
|
// View source button only appears when the user do not have permission
|
|
|
|
'viewsource' => 'editLock',
|
|
|
|
'history' => 'history',
|
|
|
|
'edit' => 'edit',
|
|
|
|
// Extension:VisualEditor
|
|
|
|
// For some reason the icon span element keeps getting removed
|
|
|
|
// So we are adding this the legacy way
|
|
|
|
// 've-edit' => 'edit',
|
|
|
|
// Extension:DiscussionTools
|
|
|
|
'addsection' => 'speechBubbleAdd'
|
|
|
|
];
|
|
|
|
|
|
|
|
// If a VE edit button is present, use wikiText icon for source edit
|
|
|
|
if ( isset( $links['views']['ve-edit'] ) ) {
|
|
|
|
$iconMap['edit'] = 'wikiText';
|
|
|
|
}
|
|
|
|
|
|
|
|
self::mapIconsToMenuItems( $links, 'views', $iconMap );
|
|
|
|
self::addIconsToMenuItems( $links, 'views' );
|
|
|
|
}
|
|
|
|
|
2022-11-01 15:47:51 +00:00
|
|
|
/**
|
|
|
|
* Set the icon parameter of the menu item based on the mapping
|
|
|
|
*
|
|
|
|
* @param array &$links
|
|
|
|
* @param string $menu identifier
|
|
|
|
* @param array $map icon mapping
|
|
|
|
*/
|
|
|
|
private static function mapIconsToMenuItems( &$links, $menu, $map ) {
|
|
|
|
foreach ( $map as $key => $icon ) {
|
|
|
|
if ( isset( $links[$menu][$key] ) ) {
|
|
|
|
$links[$menu][$key]['icon'] ??= $icon;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 01:18:53 +00:00
|
|
|
/**
|
|
|
|
* Add the HTML needed for icons to menu items
|
|
|
|
*
|
|
|
|
* @param array &$links
|
|
|
|
* @param string $menu identifier
|
|
|
|
*/
|
|
|
|
private static function addIconsToMenuItems( &$links, $menu ) {
|
2022-11-01 00:40:43 +00:00
|
|
|
// Loop through each menu to check/append its link classes.
|
2022-11-01 01:18:53 +00:00
|
|
|
foreach ( $links[$menu] as $key => $item ) {
|
2022-11-01 00:40:43 +00:00
|
|
|
$icon = $item['icon'] ?? '';
|
2022-11-01 01:18:53 +00:00
|
|
|
|
2022-11-01 01:19:56 +00:00
|
|
|
if ( $icon ) {
|
2022-11-01 01:18:53 +00:00
|
|
|
// Html::makeLink will pass this through rawElement
|
|
|
|
// Avoid using mw-ui-icon in case its styles get loaded
|
|
|
|
$links[$menu][$key]['link-html'] = '<span class="citizen-ui-icon mw-ui-icon-' . $icon . ' mw-ui-icon-wikimedia-' . $icon . '"></span>';
|
|
|
|
}
|
2022-11-01 00:40:43 +00:00
|
|
|
}
|
2022-10-05 18:04:16 +00:00
|
|
|
}
|
2021-01-28 11:46:27 +00:00
|
|
|
}
|