diff --git a/src/Components/NavbarHorizontal/PersonalTools.php.orig b/src/Components/NavbarHorizontal/PersonalTools.php.orig deleted file mode 100644 index 64d44f5..0000000 --- a/src/Components/NavbarHorizontal/PersonalTools.php.orig +++ /dev/null @@ -1,324 +0,0 @@ -. - * - * @file - * @ingroup Skins - */ - -namespace Skins\Chameleon\Components\NavbarHorizontal; - -use MediaWiki\MediaWikiServices; -use Skins\Chameleon\Components\Component; -use Skins\Chameleon\IdRegistry; - -/** - * The NavbarHorizontal\PersonalTools class. - * - * Provides a PersonalTools component to be included in a NavbarHorizontal component. - * - * @author Stephan Gambke - * @since 1.6 - * @ingroup Skins - */ -class PersonalTools extends Component { - - private const ECHO_LINK_KEYS = [ 'notifications-alert', 'notifications-notice' ]; - private const ATTR_SHOW_ECHO = 'showEcho'; - private const SHOW_ECHO_ICONS = 'icons'; - private const SHOW_ECHO_LINKS = 'links'; - private const ATTR_SHOW_USER_NAME = 'showUserName'; - private const SHOW_USER_NAME_NONE = 'none'; - private const SHOW_USER_NAME_TRY_REALNAME = 'try-realname'; - private const SHOW_USER_NAME_USERNAME_ONLY = 'username-only'; - // Boolean values for showUserName deprecated since Chameleon 4.2.0: - private const SHOW_USER_NAME_NO = 'no'; - private const SHOW_USER_NAME_YES = 'yes'; - private const ATTR_PROMOTE_LONE_ITEMS = 'promoteLoneItems'; - private const ATTR_SHOW_USER_AVATAR = 'showUserAvatar'; - - protected ?string $avatarUrl = null; - - /** - * @return String - * @throws \FatalError - * @throws \MWException - */ - public function getHtml($tpl = null) { - if ( !is_null( $tpl ) ) { - $this->setSkinTemplate( $tpl ); - } - $tools = $this->getSkinTemplate()->getPersonalTools(); - - $this->setUserAvatar(); - - // Flatten classes to avoid MW bug: https://phabricator.wikimedia.org/T262160 - // NB: This bug is finally fixed in MW >=1.36. - foreach ( $tools as $key => $item ) { - if ( !empty( $item['links'][0]['class'] ) && is_array( $item['links'][0]['class'] ) ) { - $tools[$key]['links'][0]['class'] = implode( ' ', $item['links'][0]['class'] ); - } - } - - // start personal tools element - $echoHtml = ''; - if ( $this->getShowEcho() === self::SHOW_ECHO_ICONS ) { - $echoHtml = $this->indent() . $this->getEchoIcons( $tools ); - } - - if ( count( $tools ) == 1 ) { - $promotableItems = - array_map( 'trim', - explode( ';', $this->getAttribute( self::ATTR_PROMOTE_LONE_ITEMS ) ) ); - $loneKey = array_key_first( $tools ); - if ( in_array( $loneKey, $promotableItems, true ) ) { - $item = $tools[$loneKey]; - return $echoHtml . - $this->indent() . '' . - $this->indent() . '
'; - } - } - - return $echoHtml . - $this->indent() . '' . - $this->indent() . ' '; - } - - /** - * @return string - * @throws \FatalError - * @throws \MWException - */ - protected function getNewtalkNotifier() { - $user = $this->getSkinTemplate()->getSkin()->getUser(); - - $newMessagesAlert = $this->getSkinTemplate()->getMsg( 'chameleon-newmessages' )->text(); - - if ( empty( $this->getSkinTemplate()->data[ 'newtalk' ] ) ) { - return ''; - } - - $talkClass = $user->isRegistered() ? 'pt-mytalk' : 'pt-anontalk'; - - $newtalkNotifier = $this->indent( 1 ) . ''; - $this->indent( -1 ); - - return $newtalkNotifier; - } - - /** - * @return string - * @throws \FatalError - * @throws \MWException - */ - protected function getUserName() { - $user = $this->getSkinTemplate()->getSkin()->getUser(); - if ( $user->isRegistered() ) { - $showUserName = $this->getAttribute( self::ATTR_SHOW_USER_NAME ); - if ( ( $showUserName == self::SHOW_USER_NAME_TRY_REALNAME ) || - ( $showUserName == self::SHOW_USER_NAME_YES ) ) { - $username = !empty( $user->getRealName() ) ? $user->getRealName() : $user->getName(); - return '' . htmlspecialchars( $username ) . ''; - } elseif ( $showUserName == self::SHOW_USER_NAME_USERNAME_ONLY ) { - return '' . htmlspecialchars( $user->getName() ) . ''; - } - } - return ''; - } - - /** - * @param array $tools - * - * @return string - * @throws \MWException - */ - protected function getToolsHtml( $tools ) { - $this->indent( 1 ); - $ret = ''; - - // add personal tools (links to user page, user talk, prefs, ...) - foreach ( $tools as $key => $item ) { - if ( in_array( $key, self::ECHO_LINK_KEYS ) ) { - $showEcho = $this->getShowEcho(); - if ( $showEcho === self::SHOW_ECHO_LINKS ) { - // Remove Echo classes to render as a link - unset( $item['links'][0]['class'] ); - } elseif ( $showEcho === self::SHOW_ECHO_ICONS ) { - // Icons will be rendered elsewhere - continue; - } - } - - if ( isset( $item['id'] ) ) { - $ret .= $this->indent() . $this->getSkinTemplate()->makeListItem( $key, $item, - [ 'tag' => 'div', 'link-class' => $item['id'] ] ); - } else { - $ret .= $this->indent() . $this->getSkinTemplate()->makeListItem( $key, $item, - [ 'tag' => 'div' ] ); - } - } - - $this->indent( -1 ); - return $ret; - } - - /** - * @param array $tools - * - * @return string - * @throws \MWException - */ - protected function getEchoIcons( $tools ) { - $items = ''; - - foreach ( $tools as $key => $item ) { - if ( in_array( $key, self::ECHO_LINK_KEYS ) ) { - $items .= $this->indent() . - $this->getSkinTemplate()->makeListItem( $key, $item ); - } - } - - if ( empty( $items ) ) { - return ''; - } - - return '' . - ' '; - } - - - /** - * @return string - * @throws \FatalError - * @throws \MWException - */ - protected function getDropdownToggle(): string { - $user = $this->getSkinTemplate()->getSkin()->getUser(); - - if ( $user->isRegistered() ) { - $toolsClass = 'navbar-userloggedin' . ( !$this->avatarUrl ? '' : '-avatar' ); - $toolsLinkText = $this->getSkinTemplate()->getMsg( 'chameleon-loggedin' )-> - params( $user->getName() )->text(); - - } else { - $toolsClass = 'navbar-usernotloggedin'; - $toolsLinkText = $this->getSkinTemplate()->getMsg( 'chameleon-notloggedin' )->text(); - } - - // TODO Rename '...LinkText' to '...LinkTitle' in both the hook and variable. - MediaWikiServices::getInstance()->getHookContainer()->run( 'ChameleonNavbarHorizontalPersonalToolsLinkText', [ &$toolsLinkText, - $this->getSkin() ] ); - - $newtalkNotifierHtml = $this->getNewtalkNotifier(); - $userNameHtml = $this->getUserName(); - - MediaWikiServices::getInstance()->getHookContainer()->run( 'ChameleonNavbarHorizontalPersonalToolsLinkInnerHtml', - [ &$newtalkNotifierHtml, &$userNameHtml, $this ] ); - - $this->indent( 1 ); - - $attr = [ - 'class' => $toolsClass, - 'href' => '#', 'data-toggle' => 'dropdown', 'data-boundary' => 'viewport', - 'title' => $toolsLinkText - ]; - - if ( $this->avatarUrl ) { - $attr['style'] = "background-image:url('$this->avatarUrl')"; - } - - $dropdownToggle = IdRegistry::getRegistry()->element( 'a', $attr, $newtalkNotifierHtml . $userNameHtml, - $this->indent() ); - - $this->indent( -1 ); - - return $dropdownToggle; - } - - private function setUserAvatar(): void { - if ( !$this->shouldShowUserAvatar() ) { - return; - } - - $user = $this->getSkinTemplate()->getSkin()->getUser(); - if ( !$user->isRegistered() ) { - return; - } - - // let users of the skin set an avatar url by some - // other criteria. e.g. SMW could use - // \SMW\DIProperty::newFromUserLabel( 'User image' ) - if ( !MediaWikiServices::getInstance()->getHookContainer()->run( 'ChameleonNavbarHorizontalPersonalToolsAvatarUrl', - [ &$this->avatarUrl, $this->getSkin() ] ) ) { - return; - } - - // retrieve an image with the same name - // of the user with some common extension - $imageExt = [ 'png', 'jpg', 'jpeg' ]; - $imagePage = null; - $username = $user->getName(); - foreach ( $imageExt as $ext ) { - $title = \Title::makeTitleSafe( NS_FILE, "$username.$ext" ); - if ( $title && $title->isKnown() ) { - $imagePage = new \WikiFilePage( $title ); - break; - } - } - if ( !$imagePage ) { - return; - } - - $this->avatarUrl = $imagePage->getFile()->createThumb( 41, 41 ); - } - - private function shouldShowUserAvatar(): bool { - return filter_var( $this->getAttribute( self::ATTR_SHOW_USER_AVATAR ), FILTER_VALIDATE_BOOLEAN ); - } - - /** - * @return string - */ - protected function getShowEcho() { - return $this->getAttribute( self::ATTR_SHOW_ECHO, self::SHOW_ECHO_ICONS ); - } - -}