2020-09-21 21:32:12 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
|
|
|
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentCommentItem;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentHeadingItem;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentThreadItem;
|
2023-12-11 15:38:02 +00:00
|
|
|
use MediaWiki\Html\Html;
|
2023-11-10 01:40:12 +00:00
|
|
|
use MediaWiki\Languages\LanguageFactory;
|
2020-09-21 21:32:12 +00:00
|
|
|
use MediaWiki\Linker\Linker;
|
|
|
|
use MediaWiki\Page\ParserOutputAccess;
|
2024-10-19 21:39:18 +00:00
|
|
|
use MediaWiki\Parser\ParserOptions;
|
2023-12-11 15:38:02 +00:00
|
|
|
use MediaWiki\SpecialPage\FormSpecialPage;
|
|
|
|
use MediaWiki\SpecialPage\SpecialPage;
|
2023-08-19 18:16:15 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-12-11 15:38:02 +00:00
|
|
|
use MediaWiki\Utils\MWTimestamp;
|
2020-09-21 21:32:12 +00:00
|
|
|
use Wikimedia\Assert\Assert;
|
|
|
|
use Wikimedia\Parsoid\Utils\DOMCompat;
|
|
|
|
use Wikimedia\Parsoid\Utils\DOMUtils;
|
|
|
|
|
|
|
|
class SpecialDiscussionToolsDebug extends FormSpecialPage {
|
|
|
|
|
2023-11-10 01:40:12 +00:00
|
|
|
private LanguageFactory $languageFactory;
|
2020-09-21 21:32:12 +00:00
|
|
|
private ParserOutputAccess $parserOutputAccess;
|
|
|
|
private CommentParser $commentParser;
|
|
|
|
|
|
|
|
public function __construct(
|
2023-11-10 01:40:12 +00:00
|
|
|
LanguageFactory $languageFactory,
|
2020-09-21 21:32:12 +00:00
|
|
|
ParserOutputAccess $parserOutputAccess,
|
|
|
|
CommentParser $commentParser
|
|
|
|
) {
|
|
|
|
parent::__construct( 'DiscussionToolsDebug' );
|
2023-11-10 01:40:12 +00:00
|
|
|
$this->languageFactory = $languageFactory;
|
2020-09-21 21:32:12 +00:00
|
|
|
$this->parserOutputAccess = $parserOutputAccess;
|
|
|
|
$this->commentParser = $commentParser;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getDescription() {
|
2023-09-21 20:10:57 +00:00
|
|
|
return $this->msg( 'discussiontoolsdebug-title' );
|
2020-09-21 21:32:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
protected function getFormFields() {
|
|
|
|
return [
|
|
|
|
'pagetitle' => [
|
|
|
|
'label-message' => 'discussiontoolsdebug-pagetitle',
|
|
|
|
'name' => 'pagetitle',
|
|
|
|
'type' => 'title',
|
|
|
|
'required' => true,
|
2023-08-25 17:20:53 +00:00
|
|
|
'exists' => true,
|
2020-09-21 21:32:12 +00:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
protected function getSubpageField() {
|
|
|
|
return 'pagetitle';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
protected function getDisplayFormat() {
|
|
|
|
return 'ooui';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function requiresPost() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function onSubmit( array $data ) {
|
|
|
|
$title = Title::newFromText( $data['pagetitle'] );
|
|
|
|
|
|
|
|
$status = $this->parserOutputAccess->getParserOutput(
|
|
|
|
$title->toPageRecord(),
|
|
|
|
ParserOptions::newFromAnon()
|
|
|
|
);
|
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parserOutput = $status->getValue();
|
|
|
|
$html = $parserOutput->getText();
|
|
|
|
|
|
|
|
$doc = DOMUtils::parseHTML( $html );
|
|
|
|
$container = DOMCompat::getBody( $doc );
|
|
|
|
$threadItemSet = $this->commentParser->parse( $container, $title->getTitleValue() );
|
|
|
|
|
|
|
|
$out = $this->getOutput();
|
|
|
|
|
2023-05-20 14:18:13 +00:00
|
|
|
$out->addHTML( $this->msg(
|
|
|
|
'discussiontoolsdebug-intro',
|
|
|
|
$title->getPrefixedText(),
|
|
|
|
SpecialPage::getTitleFor(
|
|
|
|
'ApiSandbox',
|
|
|
|
false,
|
|
|
|
'action=discussiontoolspageinfo&prop=threaditemshtml&excludesignatures=1&page='
|
|
|
|
. urlencode( $title->getPrefixedText() )
|
|
|
|
)->getFullText()
|
|
|
|
)->parseAsBlock() );
|
2020-09-21 21:32:12 +00:00
|
|
|
|
2023-11-10 01:40:12 +00:00
|
|
|
$pageLang = $this->languageFactory->getLanguage( $parserOutput->getLanguage() );
|
2020-09-21 21:32:12 +00:00
|
|
|
$pageLangAttribs = [
|
|
|
|
'lang' => $pageLang->getHtmlCode(),
|
|
|
|
'dir' => $pageLang->getDir(),
|
|
|
|
'class' => 'mw-content-' . $pageLang->getDir(),
|
|
|
|
];
|
|
|
|
|
2023-04-07 19:54:26 +00:00
|
|
|
foreach ( $threadItemSet->getThreadsStructured() as $thread ) {
|
2020-09-21 21:32:12 +00:00
|
|
|
$out->addHTML( $this->formatComments( $thread, $pageLangAttribs ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
$out->addModuleStyles( 'ext.discussionTools.debug.styles' );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format a thread item with replies.
|
|
|
|
*
|
|
|
|
* @param ContentThreadItem $comment
|
|
|
|
* @param array $pageLangAttribs
|
|
|
|
* @return string HTML
|
|
|
|
*/
|
|
|
|
private function formatComments( ContentThreadItem $comment, array $pageLangAttribs ) {
|
|
|
|
if ( $comment instanceof ContentHeadingItem ) {
|
|
|
|
$contents = '<span class="mw-dt-heading">' . $comment->getHTML() . '</span>';
|
|
|
|
} else {
|
|
|
|
Assert::precondition( $comment instanceof ContentCommentItem, 'Must be ContentCommentItem' );
|
|
|
|
$contents =
|
|
|
|
'<span class="mw-dt-comment-signature">' .
|
|
|
|
'<span class="mw-dt-comment-author">' .
|
|
|
|
Linker::userLink( 0, $comment->getAuthor() ) .
|
|
|
|
'</span>' . ' ' .
|
|
|
|
'(' . Linker::userTalkLink( 0, $comment->getAuthor() ) . ') ' .
|
|
|
|
'<span class="mw-dt-comment-timestamp">' .
|
|
|
|
htmlspecialchars( $this->getLanguage()->getHumanTimestamp(
|
|
|
|
new MWTimestamp( $comment->getTimestamp()->getTimestamp() )
|
|
|
|
) ) .
|
|
|
|
'</span>' .
|
|
|
|
'</span>' .
|
|
|
|
Html::rawElement( 'div', $pageLangAttribs,
|
2023-05-20 14:18:13 +00:00
|
|
|
'<div class="mw-dt-comment-body mw-parser-output">' . $comment->getBodyHTML( true ) . '</div>'
|
2020-09-21 21:32:12 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
$level = $comment->getLevel();
|
|
|
|
|
|
|
|
$replies = '';
|
|
|
|
foreach ( $comment->getReplies() as $reply ) {
|
|
|
|
$replies .= $this->formatComments( $reply, $pageLangAttribs );
|
|
|
|
}
|
|
|
|
|
|
|
|
return Html::rawElement( $replies ? 'details' : 'div', [
|
|
|
|
'open' => (bool)$replies,
|
|
|
|
'class' => 'mw-dt-comment',
|
|
|
|
'data-level' => $level,
|
|
|
|
], ( $replies ? Html::rawElement( 'summary', [], $contents ) : $contents ) . $replies );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|