2019-07-19 20:46:20 +00:00
|
|
|
<?php
|
|
|
|
|
2021-04-07 22:22:05 +00:00
|
|
|
namespace MediaWiki\Extension\Math;
|
|
|
|
|
2019-07-19 20:46:20 +00:00
|
|
|
use DataValues\StringValue;
|
2024-01-06 15:30:26 +00:00
|
|
|
use MediaWiki\Html\Html;
|
2019-07-19 20:46:20 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
use Wikibase\DataModel\Entity\EntityId;
|
|
|
|
use Wikibase\Lib\Formatters\SnakFormatter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class stores information about mathematical Wikibase items.
|
|
|
|
*/
|
|
|
|
class MathWikibaseInfo {
|
|
|
|
/**
|
|
|
|
* @var EntityId
|
|
|
|
*/
|
|
|
|
private $id;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string the label of the item
|
|
|
|
*/
|
|
|
|
private $label;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string description of the item
|
|
|
|
*/
|
|
|
|
private $description;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var StringValue a symbol representing the item
|
|
|
|
*/
|
|
|
|
private $symbol;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var MathWikibaseInfo[]
|
|
|
|
*/
|
|
|
|
private $hasParts = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var MathFormatter
|
|
|
|
*/
|
|
|
|
private $mathFormatter;
|
|
|
|
|
2019-11-26 17:24:58 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $url;
|
|
|
|
|
2022-07-08 05:13:08 +00:00
|
|
|
/**
|
|
|
|
* @param EntityId $entityId
|
|
|
|
* @param MathFormatter|null $mathFormatter to format math equations. Default format is HTML.
|
|
|
|
*/
|
|
|
|
public function __construct( EntityId $entityId, MathFormatter $mathFormatter = null ) {
|
2019-07-19 20:46:20 +00:00
|
|
|
$this->id = $entityId;
|
2022-07-08 05:13:08 +00:00
|
|
|
$this->mathFormatter = $mathFormatter ?: new MathFormatter( SnakFormatter::FORMAT_HTML );
|
2019-07-19 20:46:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $label
|
|
|
|
*/
|
|
|
|
public function setLabel( $label ) {
|
|
|
|
$this->label = $label;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $description
|
|
|
|
*/
|
|
|
|
public function setDescription( $description ) {
|
|
|
|
$this->description = $description;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param StringValue $symbol
|
|
|
|
*/
|
|
|
|
public function setSymbol( $symbol ) {
|
|
|
|
$this->symbol = $symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param MathWikibaseInfo $info
|
|
|
|
*/
|
|
|
|
public function addHasPartElement( MathWikibaseInfo $info ) {
|
2022-07-08 05:13:08 +00:00
|
|
|
$this->hasParts[] = $info;
|
2019-07-19 20:46:20 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 17:24:58 +00:00
|
|
|
/**
|
|
|
|
* @param string $link
|
|
|
|
*/
|
|
|
|
public function setUrl( $link ) {
|
|
|
|
$this->url = $link;
|
|
|
|
}
|
|
|
|
|
2019-07-19 20:46:20 +00:00
|
|
|
/**
|
|
|
|
* @param MathWikibaseInfo[] $infos
|
|
|
|
*/
|
|
|
|
public function addHasPartElements( $infos ) {
|
|
|
|
array_push( $this->hasParts, ...$infos );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return EntityId id
|
|
|
|
*/
|
|
|
|
public function getId() {
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string label
|
|
|
|
*/
|
|
|
|
public function getLabel() {
|
|
|
|
return $this->label;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string description
|
|
|
|
*/
|
|
|
|
public function getDescription() {
|
|
|
|
return $this->description;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return StringValue symbol
|
|
|
|
*/
|
|
|
|
public function getSymbol() {
|
|
|
|
return $this->symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string|null html formatted version of the symbol
|
|
|
|
*/
|
|
|
|
public function getFormattedSymbol() {
|
|
|
|
if ( $this->symbol ) {
|
|
|
|
return $this->mathFormatter->format( $this->getSymbol() );
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return MathWikibaseInfo[] hasparts
|
|
|
|
*/
|
|
|
|
public function getParts() {
|
|
|
|
return $this->hasParts;
|
|
|
|
}
|
|
|
|
|
2022-07-15 12:30:15 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getUrl() {
|
|
|
|
return $this->url;
|
|
|
|
}
|
|
|
|
|
2022-07-08 05:13:08 +00:00
|
|
|
/**
|
|
|
|
* @return MathFormatter
|
|
|
|
*/
|
|
|
|
public function getFormatter() {
|
|
|
|
return $this->mathFormatter;
|
|
|
|
}
|
|
|
|
|
2019-07-19 20:46:20 +00:00
|
|
|
/**
|
|
|
|
* Does this info object has elements?
|
|
|
|
* @return bool true if there are elements otherwise false
|
|
|
|
*/
|
|
|
|
public function hasParts() {
|
|
|
|
return $this->hasParts !== [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates an HTML table representation of the has-parts elements
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function generateTableOfParts() {
|
|
|
|
$lang = MediaWikiServices::getInstance()->getContentLanguage();
|
|
|
|
$labelAlign = $lang->isRTL() ? 'left' : 'right';
|
|
|
|
$labelAlignOpposite = !$lang->isRTL() ? 'left' : 'right';
|
|
|
|
|
2019-11-26 17:24:58 +00:00
|
|
|
$output = Html::openElement( "table", [ "style" => "padding: 5px" ] );
|
|
|
|
$output .= Html::openElement( "tbody" );
|
2019-07-19 20:46:20 +00:00
|
|
|
|
|
|
|
foreach ( $this->hasParts as $part ) {
|
2019-11-26 17:24:58 +00:00
|
|
|
$output .= Html::openElement( "tr" );
|
|
|
|
|
|
|
|
$output .= Html::openElement(
|
2019-07-19 20:46:20 +00:00
|
|
|
"td",
|
2019-11-26 17:24:58 +00:00
|
|
|
[ "style" => "font-weight: bold; text-align:$labelAlign;" ]
|
2019-07-19 20:46:20 +00:00
|
|
|
);
|
2019-11-26 17:24:58 +00:00
|
|
|
|
|
|
|
if ( $part->url ) {
|
|
|
|
$output .= Html::element(
|
|
|
|
"a",
|
|
|
|
[ "href" => $part->url ],
|
|
|
|
$part->getLabel()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$output .= $part->getLabel();
|
|
|
|
}
|
|
|
|
|
|
|
|
$output .= Html::closeElement( "td" );
|
|
|
|
$output .= Html::openElement(
|
2019-07-19 20:46:20 +00:00
|
|
|
"td",
|
2019-11-26 17:24:58 +00:00
|
|
|
[ "style" => "text-align:center; padding: 2px; padding-left: 10px; padding-right: 10px;" ]
|
2019-07-19 20:46:20 +00:00
|
|
|
);
|
2019-11-26 17:24:58 +00:00
|
|
|
|
|
|
|
if ( $part->url ) {
|
|
|
|
$output .= Html::rawElement(
|
|
|
|
"a",
|
|
|
|
[ "href" => $part->url ],
|
|
|
|
$part->getFormattedSymbol()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$output .= $part->getFormattedSymbol();
|
|
|
|
}
|
|
|
|
|
|
|
|
$output .= Html::closeElement( "td" );
|
|
|
|
$output .= Html::element(
|
2019-07-19 20:46:20 +00:00
|
|
|
"td",
|
|
|
|
[ "style" => "font-style: italic; text-align:$labelAlignOpposite;" ],
|
|
|
|
$part->getDescription()
|
|
|
|
);
|
2019-11-26 17:24:58 +00:00
|
|
|
$output .= Html::closeElement( "tr" );
|
2019-07-19 20:46:20 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 17:24:58 +00:00
|
|
|
$output .= Html::closeElement( "tbody" );
|
|
|
|
$output .= Html::closeElement( "table" );
|
|
|
|
|
2019-07-19 20:46:20 +00:00
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-26 17:24:58 +00:00
|
|
|
* Generates a minimalized Html representation of the has-parts elements.
|
2019-07-19 20:46:20 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function generateSmallTableOfParts() {
|
2019-11-26 17:24:58 +00:00
|
|
|
$output = Html::openElement( "table" );
|
|
|
|
$output .= Html::openElement( "tbody" );
|
2019-07-19 20:46:20 +00:00
|
|
|
|
|
|
|
foreach ( $this->hasParts as $part ) {
|
2019-11-26 17:24:58 +00:00
|
|
|
$output .= Html::openElement( "tr" );
|
|
|
|
$output .= Html::rawElement(
|
2019-07-19 20:46:20 +00:00
|
|
|
"td",
|
2022-07-08 05:13:08 +00:00
|
|
|
[ "style" => "text-align: center; padding-right: 5px;" ],
|
2019-07-19 20:46:20 +00:00
|
|
|
$part->getFormattedSymbol()
|
|
|
|
);
|
2019-11-26 17:24:58 +00:00
|
|
|
$output .= Html::element( "td", [ "style" => "text-align:left;" ], $part->getLabel() );
|
|
|
|
$output .= Html::closeElement( "tr" );
|
2019-07-19 20:46:20 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 17:24:58 +00:00
|
|
|
$output .= Html::closeElement( "tbody" );
|
|
|
|
$output .= Html::closeElement( "table" );
|
2019-07-19 20:46:20 +00:00
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
}
|