2017-07-12 15:12:40 +00:00
|
|
|
<?php
|
2018-04-15 23:21:12 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2019-04-08 17:08:57 +00:00
|
|
|
namespace MediaWiki\Minerva\Menu;
|
2017-07-12 15:12:40 +00:00
|
|
|
|
|
|
|
use DomainException;
|
2019-07-01 18:06:01 +00:00
|
|
|
use MediaWiki\Minerva\Menu\Entries\IMenuEntry;
|
2022-06-21 16:20:53 +00:00
|
|
|
use MediaWiki\Minerva\Menu\Entries\SingleMenuEntry;
|
2017-07-12 15:12:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Model for a menu that can be presented in a skin.
|
|
|
|
*/
|
2019-07-12 03:06:13 +00:00
|
|
|
final class Group {
|
2017-07-12 15:12:40 +00:00
|
|
|
/**
|
2019-05-14 10:31:54 +00:00
|
|
|
* @var IMenuEntry[]
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
|
|
|
private $entries = [];
|
|
|
|
|
2019-10-23 18:30:55 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2021-12-17 10:31:44 +00:00
|
|
|
private $id;
|
2019-10-23 18:30:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $id of the menu defaults to null (optional)
|
|
|
|
*/
|
|
|
|
public function __construct( $id ) {
|
|
|
|
$this->id = $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the identifier for the group
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getId() {
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
2019-04-08 17:08:57 +00:00
|
|
|
/**
|
|
|
|
* Return entries count
|
|
|
|
*
|
2019-05-21 17:01:00 +00:00
|
|
|
* @return bool
|
2019-04-08 17:08:57 +00:00
|
|
|
*/
|
|
|
|
public function hasEntries() {
|
|
|
|
return count( $this->entries ) > 0;
|
|
|
|
}
|
|
|
|
|
2017-07-12 15:12:40 +00:00
|
|
|
/**
|
|
|
|
* Get all entries represented as plain old PHP arrays.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getEntries() {
|
2021-05-03 14:12:42 +00:00
|
|
|
$entryPresenter = static function ( IMenuEntry $entry ) {
|
2017-07-12 15:12:40 +00:00
|
|
|
$result = [
|
|
|
|
'name' => $entry->getName(),
|
|
|
|
'components' => $entry->getComponents(),
|
|
|
|
];
|
2019-05-14 10:31:54 +00:00
|
|
|
$classes = $entry->getCSSClasses();
|
|
|
|
if ( $classes ) {
|
|
|
|
$result[ 'class' ] = implode( ' ', $classes );
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
};
|
|
|
|
|
|
|
|
return array_map( $entryPresenter, $this->entries );
|
|
|
|
}
|
|
|
|
|
2019-05-14 09:38:39 +00:00
|
|
|
/**
|
|
|
|
* Helper method to verify that the $name of entry is unique (do not exists
|
|
|
|
* in current Group )
|
|
|
|
* @param string $name
|
|
|
|
* @throws DomainException When the entry already exists
|
|
|
|
*/
|
|
|
|
private function throwIfNotUnique( $name ) {
|
2019-05-29 17:00:22 +00:00
|
|
|
try {
|
|
|
|
$this->search( $name );
|
|
|
|
} catch ( DomainException $exception ) {
|
|
|
|
return;
|
2019-05-14 09:38:39 +00:00
|
|
|
}
|
2019-05-29 17:00:22 +00:00
|
|
|
throw new DomainException( "The \"${name}\" entry already exists." );
|
2019-05-14 09:38:39 +00:00
|
|
|
}
|
|
|
|
|
2019-05-14 10:31:54 +00:00
|
|
|
/**
|
|
|
|
* Insert new menu entry
|
|
|
|
* @param IMenuEntry $entry
|
|
|
|
* @throws DomainException When the entry already exists
|
|
|
|
*/
|
|
|
|
public function insertEntry( IMenuEntry $entry ) {
|
|
|
|
$this->throwIfNotUnique( $entry->getName() );
|
|
|
|
$this->entries[] = $entry;
|
|
|
|
}
|
|
|
|
|
2017-07-12 15:12:40 +00:00
|
|
|
/**
|
|
|
|
* Searches for a menu entry by name.
|
|
|
|
*
|
|
|
|
* @param string $name
|
2020-01-26 19:26:31 +00:00
|
|
|
* @return int If the menu entry exists, then the 0-based index of the entry; otherwise, -1
|
2019-05-29 17:00:22 +00:00
|
|
|
* @throws DomainException
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
|
|
|
private function search( $name ) {
|
|
|
|
$count = count( $this->entries );
|
|
|
|
|
|
|
|
for ( $i = 0; $i < $count; ++$i ) {
|
|
|
|
if ( $this->entries[$i]->getName() === $name ) {
|
|
|
|
return $i;
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 17:00:22 +00:00
|
|
|
throw new DomainException( "The \"{$name}\" entry doesn't exist." );
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert an entry after an existing one.
|
2022-07-07 01:57:50 +00:00
|
|
|
* @deprecated since 1.39
|
2017-07-12 15:12:40 +00:00
|
|
|
* @param string $targetName The name of the existing entry to insert
|
|
|
|
* the new entry after
|
|
|
|
* @param string $name The name of the new entry
|
2022-06-21 16:20:53 +00:00
|
|
|
* @param string $text Entry label
|
|
|
|
* @param string $url The URL entry points to
|
|
|
|
* @param string $className Optional HTML classes
|
|
|
|
* @param string|null $icon defaults to $name if not specified
|
|
|
|
* @param bool $trackable Whether an entry will track clicks or not. Default is false.
|
2017-10-05 17:17:38 +00:00
|
|
|
* @param bool $isJSOnly Whether the entry works without JS
|
2017-07-12 15:12:40 +00:00
|
|
|
* @throws DomainException When the existing entry doesn't exist
|
|
|
|
*/
|
2022-06-21 16:20:53 +00:00
|
|
|
public function insertAfter( $targetName, $name, $text, $url,
|
2022-07-07 01:57:50 +00:00
|
|
|
$className = '', $icon = null, $trackable = false, $isJSOnly = false
|
|
|
|
) {
|
|
|
|
wfDeprecated( __METHOD__, '1.39' );
|
2019-05-14 09:38:39 +00:00
|
|
|
$this->throwIfNotUnique( $name );
|
2017-07-12 15:12:40 +00:00
|
|
|
$index = $this->search( $targetName );
|
|
|
|
|
2022-06-21 16:20:53 +00:00
|
|
|
$entry = SingleMenuEntry::create( $name, $text, $url, $className, $icon, $trackable );
|
|
|
|
if ( $isJSOnly ) {
|
|
|
|
$entry->setJSOnly();
|
|
|
|
}
|
2017-07-12 15:12:40 +00:00
|
|
|
array_splice( $this->entries, $index + 1, 0, [ $entry ] );
|
|
|
|
}
|
2019-05-29 17:00:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $targetName
|
|
|
|
* @return IMenuEntry
|
|
|
|
* @throws DomainException
|
|
|
|
*/
|
|
|
|
public function getEntryByName( $targetName ): IMenuEntry {
|
|
|
|
$index = $this->search( $targetName );
|
|
|
|
return $this->entries[$index];
|
|
|
|
}
|
2019-10-23 18:30:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize the group for use in a template
|
|
|
|
* @return array{entries:array,id:string}
|
|
|
|
*/
|
|
|
|
public function serialize() {
|
|
|
|
return [
|
|
|
|
'entries' => $this->getEntries(),
|
|
|
|
'id' => $this->getId(),
|
|
|
|
];
|
|
|
|
}
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|