Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| GadgetLoadConditions | |
100.00% |
15 / 15 |
|
100.00% |
2 / 2 |
10 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| check | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
9 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | * |
| 18 | * @file |
| 19 | */ |
| 20 | |
| 21 | namespace MediaWiki\Extension\Gadgets; |
| 22 | |
| 23 | use MediaWiki\Output\OutputPage; |
| 24 | use MediaWiki\Skin\Skin; |
| 25 | use MediaWiki\User\User; |
| 26 | |
| 27 | /** |
| 28 | * @author Siddharth VP |
| 29 | */ |
| 30 | class GadgetLoadConditions { |
| 31 | /** @var User */ |
| 32 | private $user; |
| 33 | /** @var Skin */ |
| 34 | private $skin; |
| 35 | /** @var string */ |
| 36 | private $action; |
| 37 | /** @var int */ |
| 38 | private $namespace; |
| 39 | /** @var array<string,int> */ |
| 40 | private $categories; |
| 41 | /** @var string */ |
| 42 | private $contentModel; |
| 43 | /** @var string|null */ |
| 44 | private $withGadgetParam; |
| 45 | |
| 46 | public function __construct( OutputPage $out ) { |
| 47 | $this->user = $out->getUser(); |
| 48 | $this->skin = $out->getSkin(); |
| 49 | $this->action = $out->getContext()->getActionName(); |
| 50 | $this->namespace = $out->getTitle()->getNamespace(); |
| 51 | $this->categories = array_fill_keys( $out->getCategories(), 1 ); |
| 52 | $this->contentModel = $out->getTitle()->getContentModel(); |
| 53 | $this->withGadgetParam = $out->getRequest()->getRawVal( 'withgadget' ); |
| 54 | } |
| 55 | |
| 56 | public function check( Gadget $gadget ): bool { |
| 57 | $urlLoad = $this->withGadgetParam === $gadget->getName() && $gadget->supportsUrlLoad(); |
| 58 | |
| 59 | return ( $gadget->isEnabled( $this->user ) || $urlLoad ) |
| 60 | && $gadget->isAllowed( $this->user ) |
| 61 | && $gadget->isActionSupported( $this->action ) |
| 62 | && $gadget->isSkinSupported( $this->skin ) |
| 63 | && $gadget->isNamespaceSupported( $this->namespace ) |
| 64 | && $gadget->isCategorySupported( $this->categories ) |
| 65 | && $gadget->isContentModelSupported( $this->contentModel ); |
| 66 | } |
| 67 | } |