wiki.techinc.nl/includes/resourceloader/ResourceLoaderUserTokensModule.php
Bartosz Dziewoński a129d68344 resourceloader: Move FILTER_NOMIN annotation to the beginning of output
This allows ResourceLoader::filter() to handle this case slightly
faster, since it searches for this annotation from the beginning.
In practice this is a negligible performance optimization, but let's
set a good example for the future.

Also tweak the comments and whitespace:
* Move comment about the FILTER_NOMIN from doc comment to code comment,
  it's an implementation detail and not meant for public documentation
* Remove duplicated documentation from the parent class
* Change whitespace to be identical in both cases

Change-Id: I624914ff28d903027ba58710708ccc3c66af9e24
2018-01-30 19:12:34 +00:00

76 lines
2 KiB
PHP

<?php
/**
* ResourceLoader module for user tokens.
*
* 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
* @author Krinkle
*/
/**
* Module for user tokens
*/
class ResourceLoaderUserTokensModule extends ResourceLoaderModule {
protected $origin = self::ORIGIN_CORE_INDIVIDUAL;
protected $targets = [ 'desktop', 'mobile' ];
/**
* Fetch the tokens for the current user.
*
* @param ResourceLoaderContext $context
* @return array List of tokens keyed by token type
*/
protected function contextUserTokens( ResourceLoaderContext $context ) {
$user = $context->getUserObj();
return [
'editToken' => $user->getEditToken(),
'patrolToken' => $user->getEditToken( 'patrol' ),
'watchToken' => $user->getEditToken( 'watch' ),
'csrfToken' => $user->getEditToken(),
];
}
/**
* @param ResourceLoaderContext $context
* @return string JavaScript code
*/
public function getScript( ResourceLoaderContext $context ) {
// Use FILTER_NOMIN annotation to prevent needless minification and caching (T84960).
return ResourceLoader::FILTER_NOMIN . Xml::encodeJsCall(
'mw.user.tokens.set',
[ $this->contextUserTokens( $context ) ],
ResourceLoader::inDebugMode()
);
}
/**
* @return bool
*/
public function supportsURLLoading() {
return false;
}
/**
* @return string
*/
public function getGroup() {
return 'private';
}
}