skinOptions ) { $this->skinOptions = MediaWikiServices::getInstance()->getService( 'Minerva.SkinOptions' ); } return $this->skinOptions; } /** * @return bool */ private function hasPageActions() { $title = $this->getTitle(); return !$title->isSpecialPage() && !$title->isMainPage() && Action::getActionName( $this->getContext() ) === 'view'; } /** * @return bool */ private function hasSecondaryActions() { return !$this->getUserPageHelper()->isUserPage(); } /** * @return bool */ private function isFallbackEditor() { $action = $this->getRequest()->getVal( 'action' ); return $action === 'edit'; } /** * Returns available page actions if the page has any. * * @param array $nav result of SkinTemplate::buildContentNavigationUrls * @return array|null * @throws MWException */ private function getPageActions( array $nav ) { if ( $this->isFallbackEditor() || !$this->hasPageActions() ) { return null; } $services = MediaWikiServices::getInstance(); /** @var \MediaWiki\Minerva\Menu\PageActions\PageActionsDirector $pageActionsDirector */ $pageActionsDirector = $services->getService( 'Minerva.Menu.PageActionsDirector' ); $sidebar = parent::buildSidebar(); $actions = $nav['actions'] ?? []; return $pageActionsDirector->buildMenu( $sidebar['TOOLBOX'], $actions ); } /** * @inheritDoc */ public function getTemplateData(): array { $data = parent::getTemplateData(); // FIXME: Can we use $data instead of calling buildContentNavigationUrls ? $nav = $this->buildContentNavigationUrls(); if ( !$this->hasCategoryLinks() ) { unset( $data['html-categories'] ); } return $data + [ 'array-minerva-banners' => $this->prepareBanners( $data['html-site-notice'] ), 'html-minerva-user-notifications' => $this->prepareUserNotificationsButton( $this->getNewtalks() ), 'data-minerva-main-menu' => $this->getMainMenu()->getMenuData()['items'], 'html-minerva-tagline' => $this->getTaglineHtml(), 'html-minerva-heading' => $this->prepareHeader(), 'html-minerva-post-heading' => $this->isTalkPageWithViewAction() ? $this->getTalkPagePostHeadingHtml() : '', 'html-minerva-user-menu' => $this->getPersonalToolsMenu( $nav ), 'is-minerva-beta' => $this->getSkinOptions()->get( SkinOptions::BETA_MODE ), 'data-minerva-tabs' => $this->getTabsData( $nav ), 'data-minerva-page-actions' => $this->getPageActions( $nav ), 'data-minerva-secondary-actions' => $this->getSecondaryActions( $nav ), 'html-minerva-subject-link' => $this->getSubjectPage(), 'data-minerva-history-link' => $this->getHistoryLink( $this->getTitle() ), ]; } /** * Tabs are available if a page has page actions but is not the talk page of * the main page. * * Special pages have tabs if SkinOptions::TABS_ON_SPECIALS is enabled. * This is used by Extension:GrowthExperiments * * @return bool */ private function hasPageTabs() { $title = $this->getTitle(); $skinOptions = $this->getSkinOptions(); $isSpecialPage = $title->isSpecialPage(); $subjectPage = MediaWikiServices::getInstance()->getNamespaceInfo() ->getSubjectPage( $title ); $isMainPageTalk = Title::newFromLinkTarget( $subjectPage )->isMainPage(); return ( $this->hasPageActions() && !$isMainPageTalk ) || ( $isSpecialPage && $skinOptions->get( SkinOptions::TABS_ON_SPECIALS ) ); } /** * @param array $contentNavigationUrls * @return array */ private function getTabsData( array $contentNavigationUrls ) { $skinOptions = $this->getSkinOptions(); $hasTalkTabs = $skinOptions->get( SkinOptions::TALK_AT_TOP ) && $this->hasPageTabs(); if ( !$hasTalkTabs ) { return []; } return $contentNavigationUrls ? [ 'items' => array_values( $contentNavigationUrls['namespaces'] ), ] : []; } /** * Lazy load the permissions object. We don't want to initialize it as it requires many * dependencies, sometimes some of those dependencies cannot be fulfilled (like missing Title * object) * @return IMinervaPagePermissions */ private function getPermissions(): IMinervaPagePermissions { if ( $this->permissions === null ) { $this->permissions = MediaWikiServices::getInstance() ->getService( 'Minerva.Permissions' ) ->setContext( $this->getContext() ); } return $this->permissions; } /** * Initalized main menu. Please use getter. * @var MainMenuDirector */ private $mainMenu; /** * Build the Main Menu Director by passing the skin options * * @return MainMenuDirector */ protected function getMainMenu(): MainMenuDirector { if ( !$this->mainMenu ) { $this->mainMenu = MediaWikiServices::getInstance()->getService( 'Minerva.Menu.MainDirector' ); } return $this->mainMenu; } /** * Prepare all Minerva menus * * @param array $nav result of SkinTemplate::buildContentNavigationUrls * @return string|null */ private function getPersonalToolsMenu( array $nav ) { $services = MediaWikiServices::getInstance(); /** @var \MediaWiki\Minerva\Menu\User\UserMenuDirector $userMenuDirector */ $userMenuDirector = $services->getService( 'Minerva.Menu.UserMenuDirector' ); $personalUrls = isset( $nav['user-menu'] ) ? $this->injectLegacyMenusIntoPersonalTools( $nav ) : []; $personalTools = $this->getSkin()->getPersonalToolsForMakeListItem( $personalUrls ); return $userMenuDirector->renderMenuData( $personalTools ); } /** * @return string */ protected function getSubjectPage() { $services = MediaWikiServices::getInstance(); $title = $this->getTitle(); $skinOptions = $this->getSkinOptions(); // If it's a talk page, add a link to the main namespace page // In AMC we do not need to do this as there is an easy way back to the article page // via the talk/article tabs. if ( $title->isTalkPage() && !$skinOptions->get( SkinOptions::TALK_AT_TOP ) ) { // if it's a talk page for which we have a special message, use it switch ( $title->getNamespace() ) { case NS_USER_TALK: $msg = 'mobile-frontend-talk-back-to-userpage'; break; case NS_PROJECT_TALK: $msg = 'mobile-frontend-talk-back-to-projectpage'; break; case NS_FILE_TALK: $msg = 'mobile-frontend-talk-back-to-filepage'; break; default: // generic (all other NS) $msg = 'mobile-frontend-talk-back-to-page'; } $subjectPage = $services->getNamespaceInfo()->getSubjectPage( $title ); return MediaWikiServices::getInstance()->getLinkRenderer()->makeLink( $subjectPage, $this->msg( $msg, $title->getText() )->text(), [ 'data-event-name' => 'talk.returnto', 'class' => 'return-link' ] ); } else { return ''; } } /** * Overrides Skin::doEditSectionLink * @param Title $nt The title being linked to (may not be the same as * the current page, if the section is included from a template) * @param string $section * @param string|null $tooltip * @param Language $lang * @return string */ public function doEditSectionLink( Title $nt, $section, $tooltip, Language $lang ) { if ( $this->getPermissions()->isAllowed( IMinervaPagePermissions::EDIT_OR_CREATE ) && !$nt->isMainPage() ) { $message = $this->msg( 'mobile-frontend-editor-edit' )->inLanguage( $lang )->text(); $html = Html::openElement( 'span', [ 'class' => 'mw-editsection' ] ); $html .= Html::element( 'a', [ 'href' => $nt->getLocalURL( [ 'action' => 'edit', 'section' => $section ] ), 'title' => $this->msg( 'editsectionhint', $tooltip )->inLanguage( $lang )->text(), 'data-section' => $section, // Note visibility of the edit section link button is controlled by .edit-page in ui.less so // we default to enabled even though this may not be true. 'class' => MinervaUI::iconClass( 'edit-base20', 'element', 'edit-page mw-ui-icon-flush-right', 'wikimedia' ), ], $message ); $html .= Html::closeElement( 'span' ); return $html; } return ''; } /** * Takes a title and returns classes to apply to the body tag * @param Title $title * @return string */ public function getPageClasses( $title ) { $skinOptions = $this->getSkinOptions(); $className = parent::getPageClasses( $title ); $className .= ' ' . ( $skinOptions->get( SkinOptions::BETA_MODE ) ? 'beta' : 'stable' ); if ( $title->isMainPage() ) { $className .= ' page-Main_Page '; } if ( $this->getUser()->isRegistered() ) { $className .= ' is-authenticated'; } // The new treatment should only apply to the main namespace if ( $title->getNamespace() === NS_MAIN && $skinOptions->get( SkinOptions::PAGE_ISSUES ) ) { $className .= ' issues-group-B'; } return $className; } /** * Whether the output page contains category links and the category feature is enabled. * @return bool */ private function hasCategoryLinks() { $skinOptions = $this->getSkinOptions(); if ( !$skinOptions->get( SkinOptions::CATEGORIES ) ) { return false; } $categoryLinks = $this->getOutput()->getCategoryLinks(); if ( !count( $categoryLinks ) ) { return false; } return !empty( $categoryLinks['normal'] ) || !empty( $categoryLinks['hidden'] ); } /** * @return SkinUserPageHelper */ public function getUserPageHelper() { return MediaWikiServices::getInstance()->getService( 'Minerva.SkinUserPageHelper' ); } /** * Prepares the user button. * @param string $newTalks New talk page messages for the current user * @return string */ protected function prepareUserNotificationsButton( $newTalks ) { $user = $this->getUser(); if ( $user->isRegistered() ) { $currentTitle = $this->getTitle(); $notificationsMsg = $this->msg( 'mobile-frontend-user-button-tooltip' )->text(); $notificationIconClass = MinervaUI::iconClass( 'bellOutline-base20', 'element', '', 'wikimedia' ); $badge = Html::element( 'a', [ 'class' => $notificationIconClass, 'href' => SpecialPage::getTitleFor( 'Mytalk' )->getLocalURL( [ 'returnto' => $currentTitle->getPrefixedText() ] ), ], $notificationsMsg ); Hooks::run( 'SkinMinervaReplaceNotificationsBadge', [ $user, $currentTitle, &$badge ] ); return $badge; } return ''; } /** * Get a history link which describes author and relative time of last edit * @param Title $title The Title object of the page being viewed * @param string $timestamp * @return array */ protected function getRelativeHistoryLink( Title $title, $timestamp ) { $user = $this->getUser(); $userDate = $this->getLanguage()->userDate( $timestamp, $user ); $text = $this->msg( 'minerva-last-modified-date', $userDate, $this->getLanguage()->userTime( $timestamp, $user ) )->parse(); return [ // Use $edit['timestamp'] (Unix format) instead of $timestamp (MW format) 'data-timestamp' => wfTimestamp( TS_UNIX, $timestamp ), 'href' => $this->getHistoryUrl( $title ), 'text' => $text, ] + $this->getRevisionEditorData( $title ); } /** * Get a history link which makes no reference to user or last edited time * @param Title $title The Title object of the page being viewed * @return array */ protected function getGenericHistoryLink( Title $title ) { $text = $this->msg( 'mobile-frontend-history' )->plain(); return [ 'href' => $this->getHistoryUrl( $title ), 'text' => $text, ]; } /** * Get the URL for the history page for the given title using Special:History * when available. * @param Title $title The Title object of the page being viewed * @return string */ protected function getHistoryUrl( Title $title ) { return ExtensionRegistry::getInstance()->isLoaded( 'MobileFrontend' ) && SpecialMobileHistory::shouldUseSpecialHistory( $title, $this->getUser() ) ? SpecialPage::getTitleFor( 'History', $title )->getLocalURL() : $title->getLocalURL( [ 'action' => 'history' ] ); } /** * Prepare the content for the 'last edited' message, e.g. 'Last edited on 30 August * 2013, at 23:31'. This message is different for the main page since main page * content is typically transcluded rather than edited directly. * * The relative time is only rendered on the latest revision. * For older revisions the last modified information will not render with a relative time * nor will it show the name of the editor. * @param Title $title The Title object of the page being viewed * @return array|null */ protected function getHistoryLink( Title $title ) { $out = $this->getOutput(); $isLatestRevision = $out->getRevisionId() === $title->getLatestRevID(); // Get rev_timestamp of current revision (preloaded by MediaWiki core) $timestamp = $out->getRevisionTimestamp(); # No cached timestamp, load it from the database if ( $timestamp === null ) { $timestamp = MediaWikiServices::getInstance() ->getRevisionLookup() ->getTimestampFromId( $out->getRevisionId() ); } $historyIconClasses = [ 'historyIconClass' => MinervaUI::iconClass( 'history-base20', 'mw-ui-icon-small', '', 'wikimedia' ), 'arrowIconClass' => MinervaUI::iconClass( 'expand-gray', 'small', 'mf-mw-ui-icon-rotate-anti-clockwise indicator', // Uses icon in MobileFrontend so must be prefixed mf. // Without MobileFrontend it will not render. // Rather than maintain 2 versions (and variants) of the arrow icon which can conflict // with each othe and bloat CSS, we'll // use the MobileFrontend one. Long term when T177432 and T160690 are resolved // we should be able to use one icon definition and break this dependency. 'mf' ), ]; $historyLink = !$isLatestRevision || $title->isMainPage() ? $this->getGenericHistoryLink( $title ) : $this->getRelativeHistoryLink( $title, $timestamp ); $title = $this->getTitle(); return $title->canExist() && $title->exists() ? $historyLink + $historyIconClasses : null; } /** * Returns data attributes representing the editor for the current revision. * @param LinkTarget $title The Title object of the page being viewed * @return array representing user with name and gender fields. Empty if the editor no longer * exists in the database or is hidden from public view. */ private function getRevisionEditorData( LinkTarget $title ) { $rev = MediaWikiServices::getInstance()->getRevisionLookup() ->getRevisionByTitle( $title ); $result = []; if ( $rev ) { $revUser = $rev->getUser(); // Note the user will only be returned if that information is public if ( $revUser ) { $revUser = User::newFromIdentity( $revUser ); $editorName = $revUser->getName(); $editorGender = $revUser->getOption( 'gender' ); $result += [ 'data-user-name' => $editorName, 'data-user-gender' => $editorGender, ]; } } return $result; } /** * Returns the HTML representing the tagline * @return string HTML for tagline */ protected function getTaglineHtml() { $tagline = ''; if ( $this->getUserPageHelper()->isUserPage() ) { $pageUser = $this->getUserPageHelper()->getPageUser(); $fromDate = $pageUser->getRegistration(); if ( $this->getUserPageHelper()->isUserPageAccessibleToCurrentUser() && is_string( $fromDate ) ) { $fromDateTs = wfTimestamp( TS_UNIX, $fromDate ); // This is shown when js is disabled. js enhancement made due to caching $tagline = $this->msg( 'mobile-frontend-user-page-member-since', $this->getLanguage()->userDate( new MWTimestamp( $fromDateTs ), $this->getUser() ), $pageUser )->text(); // Define html attributes for usage with js enhancement (unix timestamp, gender) $attrs = [ 'id' => 'tagline-userpage', 'data-userpage-registration-date' => $fromDateTs, 'data-userpage-gender' => $pageUser->getOption( 'gender' ) ]; } } else { $title = $this->getTitle(); if ( $title ) { $out = $this->getOutput(); $tagline = $out->getProperty( 'wgMFDescription' ); } } $attrs[ 'class' ] = 'tagline'; return Html::element( 'div', $attrs, $tagline ); } /** * Returns the HTML representing the heading. * @return string HTML for header */ protected function getHeadingHtml() { $isUserPage = $this->getUserPageHelper()->isUserPage(); $isUserPageAccessible = $this->getUserPageHelper()->isUserPageAccessibleToCurrentUser(); if ( $isUserPage && $isUserPageAccessible ) { // The heading is just the username without namespace $heading = $this->getUserPageHelper()->getPageUser()->getName(); } else { $heading = $this->getOutput()->getPageTitle(); } return Html::rawElement( 'h1', [ 'id' => 'section_0' ], $heading ); } /** * @return bool Whether or not current title is a Talk page with the default * action ('view') */ private function isTalkPageWithViewAction() { $title = $this->getTitle(); // Hook is @unstable and only for use by DiscussionTools. Do not use for any other purpose. $hookContainer = MediaWikiServices::getInstance()->getHookContainer(); if ( !$hookContainer->run( 'MinervaNeueTalkPageOverlay', [ $title, $this->getOutput() ] ) ) { return false; } return $title->isTalkPage() && Action::getActionName( $this->getContext() ) === "view"; } /** * @internal Should not be used outside Minerva. * @todo Find better place for this. * * @return bool Whether or not the simplified talk page is enabled and action is 'view' */ public function isSimplifiedTalkPageEnabled(): bool { $title = $this->getTitle(); $skinOptions = $this->getSkinOptions(); return $this->isTalkPageWithViewAction() && $skinOptions->get( SkinOptions::SIMPLIFIED_TALK ) && // Only if viewing the latest revision, as we can't get the section numbers otherwise // (and even if we could, they would be useless, because edits often add and remove sections). $this->getOutput()->getRevisionId() === $title->getLatestRevID() && $title->getContentModel() === CONTENT_MODEL_WIKITEXT; } /** * Returns the postheadinghtml for the talk page with view action * * @return string HTML for postheadinghtml */ private function getTalkPagePostHeadingHtml() { $title = $this->getTitle(); $html = ''; // T237589: We don't want to show the add discussion button on Flow pages, // only wikitext pages if ( $this->getPermissions()->isTalkAllowed() && $title->getContentModel() === CONTENT_MODEL_WIKITEXT ) { $addTopicButton = $this->getTalkButton( $title, wfMessage( 'minerva-talk-add-topic' )->text(), true ); $html = Html::element( 'a', $addTopicButton['attributes'] + [ 'data-event-name' => 'talkpage.add-topic' ], $addTopicButton['label'] ); } $title = $this->getTitle(); if ( $this->isSimplifiedTalkPageEnabled() && $title->canExist() ) { $parserOutputAccess = MediaWikiServices::getInstance()->getParserOutputAccess(); $parserOptions = ParserOptions::newFromContext( $this->getContext() ); $pageRecord = $title->toPageRecord(); $status = $parserOutputAccess->getParserOutput( $pageRecord, $parserOptions ); $statusValue = $status->getValue(); $sectionCount = ( $status->isGood() && $statusValue instanceof ParserOutput ) ? count( $statusValue->getSections() ) : 0; $message = $sectionCount > 0 ? wfMessage( 'minerva-talk-explained' ) : wfMessage( 'minerva-talk-explained-empty' ); $html .= Html::element( 'div', [ 'class' => 'minerva-talk-content-explained' ], $message->text() ); } return $html; } /** * Create and prepare header content * @return string */ protected function prepareHeader() { $title = $this->getTitle(); if ( $title->isMainPage() ) { $user = $this->getUser(); $msg = $this->msg( 'mobile-frontend-logged-in-homepage-notification', $user->getName() ); if ( $user->isRegistered() && !$msg->isDisabled() ) { $out = $this->getOutput(); $out->setPageTitle( $msg->text() ); } else { // Don’t add any