DynamicPageList3/includes/Lister/SubPageList.php

128 lines
2.3 KiB
PHP
Raw Normal View History

2020-11-22 20:03:02 +00:00
<?php
namespace MediaWiki\Extension\DynamicPageList3\Lister;
2020-11-22 20:03:02 +00:00
class SubPageList extends UnorderedList {
/**
* Listing style for this class.
*
2021-10-01 22:52:30 +00:00
* @var int
2020-11-22 20:03:02 +00:00
*/
public $style = parent::LIST_UNORDERED;
/**
* List(Section) Start
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:03:02 +00:00
*/
public $listStart = '<ul%s>';
/**
* List(Section) End
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:03:02 +00:00
*/
public $listEnd = '</ul>';
/**
* Item Start
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:03:02 +00:00
*/
public $itemStart = '<li%s>';
/**
* Item End
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:03:02 +00:00
*/
public $itemEnd = '</li>';
/**
* Format a list of articles into a singular list.
*
2021-10-01 22:52:30 +00:00
* @param array $articles
* @param int $start
* @param int $count
* @return string
2020-11-22 20:03:02 +00:00
*/
2021-02-22 23:48:01 +00:00
public function formatList( $articles, $start, $count ) {
2020-11-22 20:03:02 +00:00
$filteredCount = 0;
$items = [];
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
for ( $i = $start; $i < $start + $count; $i++ ) {
2020-11-22 20:03:02 +00:00
$article = $articles[$i];
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( empty( $article ) || empty( $article->mTitle ) ) {
2020-11-22 20:03:02 +00:00
continue;
}
$pageText = null;
2021-02-22 23:48:01 +00:00
if ( $this->includePageText ) {
$pageText = $this->transcludePage( $article, $filteredCount );
2020-11-22 20:03:02 +00:00
} else {
$filteredCount++;
}
$this->rowCount = $filteredCount++;
2021-02-22 23:48:01 +00:00
$parts = explode( '/', $article->mTitle );
$item = $this->formatItem( $article, $pageText );
$items = $this->nestItem( $parts, $items, $item );
2020-11-22 20:03:02 +00:00
}
2021-02-22 23:48:01 +00:00
return $this->getListStart() . $this->implodeItems( $items ) . $this->listEnd;
2020-11-22 20:03:02 +00:00
}
/**
* Nest items down to the proper level.
*
2021-10-01 22:52:30 +00:00
* @param array &$parts
* @param array $items
* @param string $item
* @return array
2020-11-22 20:03:02 +00:00
*/
2021-02-22 23:48:01 +00:00
private function nestItem( &$parts, $items, $item ) {
$firstPart = reset( $parts );
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( count( $parts ) > 1 ) {
array_shift( $parts );
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( !isset( $items[$firstPart] ) ) {
2020-11-22 20:03:02 +00:00
$items[$firstPart] = [];
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$items[$firstPart] = $this->nestItem( $parts, $items[$firstPart], $item );
2021-10-01 22:52:30 +00:00
2020-11-22 20:03:02 +00:00
return $items;
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:03:02 +00:00
$items[$firstPart][] = $item;
return $items;
}
/**
* Join together items after being processed by formatItem().
*
2021-10-01 22:52:30 +00:00
* @param array $items
* @return string
2020-11-22 20:03:02 +00:00
*/
2021-02-22 23:48:01 +00:00
protected function implodeItems( $items ) {
2020-11-22 20:03:02 +00:00
$list = '';
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $items as $key => $item ) {
if ( is_string( $item ) ) {
2020-11-22 20:03:02 +00:00
$list .= $item;
continue;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( is_array( $item ) ) {
$list .= $this->getItemStart() . $key . $this->getListStart() . $this->implodeItems( $item ) . $this->listEnd . $this->getItemEnd();
2020-11-22 20:03:02 +00:00
}
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:03:02 +00:00
return $list;
}
}