2023-03-28 17:25:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
|
|
|
use ApiBase;
|
|
|
|
use ApiMain;
|
|
|
|
use ApiUsageException;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\ThreadItem\DatabaseThreadItem;
|
2023-08-19 04:14:36 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-12-11 15:38:02 +00:00
|
|
|
use MediaWiki\Title\TitleFormatter;
|
2023-03-28 17:25:16 +00:00
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
|
|
|
|
|
|
|
class ApiDiscussionToolsFindComment extends ApiBase {
|
|
|
|
|
|
|
|
private ThreadItemStore $threadItemStore;
|
|
|
|
private TitleFormatter $titleFormatter;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
ApiMain $main,
|
|
|
|
string $name,
|
|
|
|
ThreadItemStore $threadItemStore,
|
|
|
|
TitleFormatter $titleFormatter
|
|
|
|
) {
|
|
|
|
parent::__construct( $main, $name );
|
|
|
|
$this->threadItemStore = $threadItemStore;
|
|
|
|
$this->titleFormatter = $titleFormatter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @throws ApiUsageException
|
|
|
|
*/
|
|
|
|
public function execute() {
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
|
|
|
$values = [];
|
|
|
|
|
2023-10-23 13:15:04 +00:00
|
|
|
$this->requireAtLeastOneParameter( $params, 'idorname', 'heading', 'page' );
|
|
|
|
|
|
|
|
if ( $params['idorname'] ) {
|
|
|
|
$idOrName = $params['idorname'];
|
|
|
|
|
|
|
|
$byId = $this->threadItemStore->findNewestRevisionsById( $idOrName );
|
|
|
|
foreach ( $byId as $item ) {
|
|
|
|
$values[] = $this->getValue( $item, 'id' );
|
|
|
|
}
|
2023-03-28 17:25:16 +00:00
|
|
|
|
2023-10-23 13:15:04 +00:00
|
|
|
$byName = $this->threadItemStore->findNewestRevisionsByName( $idOrName );
|
|
|
|
foreach ( $byName as $item ) {
|
|
|
|
$values[] = $this->getValue( $item, 'name' );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->requireAtLeastOneParameter( $params, 'heading' );
|
|
|
|
$this->requireAtLeastOneParameter( $params, 'page' );
|
|
|
|
|
|
|
|
$heading = $params['heading'];
|
|
|
|
$page = $params['page'];
|
|
|
|
|
|
|
|
$title = Title::newFromText( $page );
|
|
|
|
if ( $title ) {
|
|
|
|
$articleId = $title->getArticleId();
|
|
|
|
|
|
|
|
if ( $articleId ) {
|
|
|
|
$byHeading = $this->threadItemStore->findNewestRevisionsByHeading( $heading, $articleId );
|
|
|
|
foreach ( $byHeading as $item ) {
|
|
|
|
$values[] = $this->getValue( $item, 'heading' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-28 17:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$redirects = 0;
|
|
|
|
foreach ( $values as $value ) {
|
|
|
|
if ( $value['couldredirect'] ) {
|
|
|
|
$redirects++;
|
|
|
|
if ( $redirects > 1 ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ( $values as $value ) {
|
|
|
|
if ( $redirects === 1 && $value['couldredirect'] ) {
|
|
|
|
$value['shouldredirect'] = true;
|
|
|
|
}
|
|
|
|
unset( $value['couldredirect'] );
|
|
|
|
$this->getResult()->addValue( $this->getModuleName(), null, $value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a value to add to the results
|
|
|
|
*
|
|
|
|
* @param DatabaseThreadItem $item Thread item
|
2023-10-23 13:15:04 +00:00
|
|
|
* @param string $matchedBy How the thread item was matched (id, name or heading)
|
2023-03-28 17:25:16 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getValue( DatabaseThreadItem $item, string $matchedBy ): array {
|
|
|
|
$title = Title::castFromPageReference( $item->getPage() );
|
|
|
|
|
|
|
|
return [
|
|
|
|
'id' => $item->getId(),
|
|
|
|
'name' => $item->getName(),
|
|
|
|
'title' => $this->titleFormatter->getPrefixedText( $item->getPage() ),
|
|
|
|
'oldid' => !$item->getRevision()->isCurrent() ? $item->getRevision()->getId() : null,
|
|
|
|
'matchedby' => $matchedBy,
|
|
|
|
// Could this be an automatic redirect? Will be converted to 'shouldredirect'
|
|
|
|
// if there is only one of these in the result set.
|
|
|
|
// Matches logic in Special:GoToComment
|
|
|
|
'couldredirect' => $item->getRevision()->isCurrent() && !is_string( $item->getTranscludedFrom() )
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getAllowedParams() {
|
|
|
|
return [
|
|
|
|
'idorname' => [
|
2023-10-23 13:15:04 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
|
|
|
],
|
|
|
|
'heading' => [
|
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
|
|
|
],
|
|
|
|
'page' => [
|
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
2023-03-28 17:25:16 +00:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function needsToken() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function isWriteMode() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|