VectorGOLEM/includes/SkinVector.php

135 lines
4.0 KiB
PHP
Raw Normal View History

<?php
/**
* Vector - Modern version of MonoBook with fresh look and many usability
* improvements.
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Skins
*/
use MediaWiki\MediaWikiServices;
use Vector\Constants;
use Wikimedia\WrappedString;
/**
* Skin subclass for Vector
* @ingroup Skins
* @final skins extending SkinVector are not supported
* @unstable
*/
class SkinVector extends SkinTemplate {
public $skinname = Constants::SKIN_NAME;
public $stylename = 'Vector';
public $template = 'VectorTemplate';
/**
* @inheritDoc
* @return array
*/
public function getDefaultModules() {
$modules = parent::getDefaultModules();
if ( $this->isLegacy() ) {
$modules['styles']['skin'][] = 'skins.vector.styles.legacy';
$modules[Constants::SKIN_NAME] = 'skins.vector.legacy.js';
} else {
$modules['styles'] = array_merge(
$modules['styles'],
[ 'skins.vector.styles', 'mediawiki.ui.icon', 'skins.vector.icons' ]
);
$modules[Constants::SKIN_NAME][] = 'skins.vector.js';
}
return $modules;
}
/**
* Set up the VectorTemplate. Overrides the default behaviour of SkinTemplate allowing
* the safe calling of constructor with additional arguments. If dropping this method
* please ensure that VectorTemplate constructor arguments match those in SkinTemplate.
*
* @internal
* @param string $classname
* @return VectorTemplate
*/
protected function setupTemplate( $classname ) {
Add opt-out link to Sidebar for Vector/Logged-in Users Without Abstractions This commit is singularly focused on adding a link to the sidebar for Vector, logged-in users. It does the bare minimum to fulfill the requirements of T243281. Additionally, it will help to answer the question "Do we need to use abstractions (other than maybe different templates) to separate Legacy Vector from Vector" by intentionally leaving out any abstractions in order to make it easier to compare with a follow-up patch (Ib2ef15180df73360cc1de25b893e49d415d23e1a) which does use abstractions. It is a good thing to question whether or not we need addtional abstractions in VectorTemplate and if they will help us as unnecessary abstractions can have the opposite effect and just lead to further frustrations down the road. Therefore, I urge you, the reviewer, to let me know your thoughts! If abstractions are viewed as not making our lives any easier, the follow-up patches may be completely discarded and that's totally okay with me. :) I think it's a good think to talk about now though. Important changes: * The VectorTemplate constructor was changed to allow injecting the config, templateParser, and isLegacy boolean (only the config was allowed before this commit). According to MediaWiki's Stable Interface Policy, "Constructor signatures are generally considered unstable unless explicitly declared stable for calling" [3]. Given that VecorTemplate's constructor is not marked as stable, it is justified to do this without warning according to the policy. * Due to the above, the 'setTemplate' method is no longer needed and was marked as deprecated. * VectorTemplateTest was made to adapt to the new VectorTemplate constructor. Additionally, it now extends from MediaWikiIntegrationTestCase which my intelliphense server can pick up. I *think* MediaWikiTestCase is just an alias to MediaWikiIntegrationTestCase [1] and MediaWikiTestCase file was renamed to MediaWikiIntegrationTestCase in [2], but I'm willing to change it back if there is pushback to this. Open questions: * What are VectorTemplate's responsibilities? To me, it acts right now as a controller (because it echos the full HTML string from the template), a model (because SkinTemplate::prepareQuickTemplate sets data on it which it later retrieves through `$this->get()`), a presenter (because it adds data tailored for a web-centric view), and a view (because it renders HTML strings instead of letting the view/template be solely responsible for that). Arguably, some business logic might be mixed in there as well (because it checks to see if a User is logged in/has necessary permissions to show x which my changes here add to). This might not be a problem if we keep VectorTemplate relatively small, but will it remain this way as we progress further in Desktop Improvements? * How do we write tests for VectorTemplate without exposing unnecessary public methods? For example, if I want to test the `getSkinData()` method to see what state will be sent to the template, how should I do this? One option might be to use `TestingAccessWrapper` to expose these private methods which is what `VectorTemplateTest::testbuildViewsProps()` does. Another option is to accept this method as public. Is there a better way? Keep in mind that even with access to this method, there might be many things to mock. [1] https://github.com/wikimedia/mediawiki/blob/0030cb525be6cabc1d63de80586b2017d4bbe354/tests/common/TestsAutoLoader.php#L64 [2] Ie717b0ecf4fcfd089d46248f14853c80b7ef4a76 [3] https://www.mediawiki.org/wiki/Stable_interface_policy Bug: T243281 Change-Id: I0571b041bcd7f19bec9f103fa7bccdd093f6394d
2020-03-17 20:21:33 +00:00
$tp = new TemplateParser( __DIR__ . '/templates' );
return new VectorTemplate( $this->getConfig(), $tp, $this->isLegacy() );
}
/**
* Whether or not the legacy version of the skin is being used.
*
* @return bool
*/
private function isLegacy() : bool {
$isLatestSkinFeatureEnabled = MediaWikiServices::getInstance()
->getService( Constants::SERVICE_FEATURE_MANAGER )
->isFeatureEnabled( Constants::FEATURE_LATEST_SKIN );
return !$isLatestSkinFeatureEnabled;
}
/**
* @internal only for use inside VectorTemplate
* @return array of data for a Mustache template
*/
public function getTemplateData() {
$out = $this->getOutput();
$title = $out->getTitle();
$indicators = [];
foreach ( $out->getIndicators() as $id => $content ) {
$indicators[] = [
'id' => Sanitizer::escapeIdForAttribute( "mw-indicator-$id" ),
'class' => 'mw-indicator',
'html' => $content,
];
}
return [
// Data objects:
'data-indicators' => $indicators,
// HTML strings:
'html-printtail' => WrappedString::join( "\n", [
MWDebug::getDebugHTML( $this->getContext() ),
$this->bottomScripts(),
wfReportTime( $out->getCSP()->getNonce() )
] ) . '</body></html>',
'html-sitenotice' => $this->getSiteNotice(),
'html-userlangattributes' => $this->prepareUserLanguageAttributes(),
'html-subtitle' => $this->prepareSubtitle(),
// Always returns string, cast to null if empty.
'html-undelete' => $this->prepareUndeleteLink() ?: null,
// Result of OutputPage::addHTML calls
'html-bodycontent' => $this->wrapHTML( $title, $out->mBodytext ),
'html-dataAfterContent' => $this->afterContentHook(),
// From MWDebug::getHTMLDebugLog (when $wgShowDebug is enabled)
'html-debuglog' => $this->generateDebugHTML(),
];
}
/**
* @internal only for use inside VectorTemplate
* @return array
*/
public function getMenuProps() {
return $this->buildContentNavigationUrls();
}
}