mediawiki-extensions-Echo/includes/EchoOnWikiList.php
Alexander Vorwerk bba113d92e Avoid using ContentHandler::getContentText()
ContentHandler::getContentText() is deprecated and should be
replaced with Content::getText() for TextContent instances.

Change-Id: Ia92c5a41954b1d21deb39dcafabbdff60a414f55
2021-05-17 23:44:00 +02:00

57 lines
1.3 KiB
PHP

<?php
/**
* Implements EchoContainmentList interface for sourcing a list of items from a wiki
* page. Uses the pages latest revision ID as cache key.
*/
class EchoOnWikiList implements EchoContainmentList {
/**
* @var Title|null A title object representing the page to source the list from,
* or null if the page does not exist.
*/
protected $title;
/**
* @param int $titleNs An NS_* constant representing the mediawiki namespace of the page
* @param string $titleString String portion of the wiki page title
*/
public function __construct( $titleNs, $titleString ) {
$title = Title::newFromText( $titleString, $titleNs );
if ( $title !== null && $title->getArticleID() ) {
$this->title = $title;
}
}
/**
* @inheritDoc
*/
public function getValues() {
if ( !$this->title ) {
return [];
}
$article = WikiPage::newFromID( $this->title->getArticleID() );
if ( $article === null || !$article->exists() ) {
return [];
}
$content = $article->getContent();
$text = ( $content instanceof TextContent ) ? $content->getText() : null;
if ( $text === null ) {
return [];
}
return array_filter( array_map( 'trim', explode( "\n", $text ) ) );
}
/**
* @inheritDoc
*/
public function getCacheKey() {
if ( !$this->title ) {
return '';
}
return (string)$this->title->getLatestRevID();
}
}