mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/DiscussionTools
synced 2024-11-24 00:13:36 +00:00
Merge "Add an API version of Special:FindComment"
This commit is contained in:
commit
d74fff198d
|
@ -362,6 +362,13 @@
|
|||
"RevisionLookup"
|
||||
]
|
||||
},
|
||||
"discussiontoolsfindcomment": {
|
||||
"class": "MediaWiki\\Extension\\DiscussionTools\\ApiDiscussionToolsFindComment",
|
||||
"services": [
|
||||
"DiscussionTools.ThreadItemStore",
|
||||
"TitleFormatter"
|
||||
]
|
||||
},
|
||||
"discussiontoolsgetsubscriptions": {
|
||||
"class": "MediaWiki\\Extension\\DiscussionTools\\ApiDiscussionToolsGetSubscriptions",
|
||||
"services": [
|
||||
|
|
|
@ -18,6 +18,8 @@
|
|||
"apihelp-discussiontoolsedit-paramvalue-paction-addcomment": "Add a new comment as a reply to an existing comment.",
|
||||
"apihelp-discussiontoolsedit-paramvalue-paction-addtopic": "Add a new discussion section and the first comment in it.",
|
||||
"apihelp-discussiontoolsedit-summary": "Post a message on a discussion page.",
|
||||
"apihelp-discussiontoolsfindcomment-summary": "Find a comment by its ID or name.",
|
||||
"apihelp-discussiontoolsfindcomment-param-idorname": "Comment ID or name",
|
||||
"apihelp-discussiontoolsgetsubscriptions-param-commentname": "Names of the topics to check",
|
||||
"apihelp-discussiontoolsgetsubscriptions-summary": "Get the subscription statuses of given topics.",
|
||||
"apihelp-discussiontoolspageinfo-param-oldid": "The revision number to use (defaults to latest revision).",
|
||||
|
|
|
@ -20,6 +20,8 @@
|
|||
"apihelp-discussiontoolsedit-paramvalue-paction-addcomment": "{{doc-apihelp-paramvalue|discussiontoolsedit|paction|addcomment}}",
|
||||
"apihelp-discussiontoolsedit-paramvalue-paction-addtopic": "{{doc-apihelp-paramvalue|discussiontoolsedit|paction|addtopic}}",
|
||||
"apihelp-discussiontoolsedit-summary": "{{doc-apihelp-summary|discussiontoolsedit}}",
|
||||
"apihelp-discussiontoolsfindcomment-summary": "{{doc-apihelp-summary|discussiontoolsfindcomment}}",
|
||||
"apihelp-discussiontoolsfindcomment-param-idorname": "{{doc-apihelp-param|discussiontoolsfindcomment|idorname}}",
|
||||
"apihelp-discussiontoolsgetsubscriptions-param-commentname": "{{doc-apihelp-param|discussiontoolsgetsubscriptions|commentname}}",
|
||||
"apihelp-discussiontoolsgetsubscriptions-summary": "{{doc-apihelp-summary|discussiontoolsgetsubscriptions}}",
|
||||
"apihelp-discussiontoolspageinfo-param-oldid": "{{doc-apihelp-param|discussiontoolspageinfo|oldid}}",
|
||||
|
|
115
includes/ApiDiscussionToolsFindComment.php
Normal file
115
includes/ApiDiscussionToolsFindComment.php
Normal file
|
@ -0,0 +1,115 @@
|
|||
<?php
|
||||
|
||||
namespace MediaWiki\Extension\DiscussionTools;
|
||||
|
||||
use ApiBase;
|
||||
use ApiMain;
|
||||
use ApiUsageException;
|
||||
use MediaWiki\Extension\DiscussionTools\ThreadItem\DatabaseThreadItem;
|
||||
use Title;
|
||||
use TitleFormatter;
|
||||
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();
|
||||
|
||||
$idOrName = $params['idorname'];
|
||||
|
||||
$values = [];
|
||||
|
||||
$byId = $this->threadItemStore->findNewestRevisionsById( $idOrName );
|
||||
foreach ( $byId as $item ) {
|
||||
$values[] = $this->getValue( $item, 'id' );
|
||||
}
|
||||
|
||||
$byName = $this->threadItemStore->findNewestRevisionsByName( $idOrName );
|
||||
foreach ( $byName as $item ) {
|
||||
$values[] = $this->getValue( $item, 'name' );
|
||||
}
|
||||
|
||||
$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
|
||||
* @param string $matchedBy How the thread item was matched (id or name)
|
||||
* @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' => [
|
||||
ParamValidator::PARAM_REQUIRED => true,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function needsToken() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function isWriteMode() {
|
||||
return false;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue