| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\DiscussionTools; |
| 4 |
|
| 5 |
use MediaWiki\Api\ApiBase; |
| 6 |
use MediaWiki\Api\ApiMain; |
| 7 |
use MediaWiki\Api\ApiUsageException; |
| 8 |
use MediaWiki\Extension\DiscussionTools\ThreadItem\DatabaseThreadItem; |
| 9 |
use MediaWiki\Title\Title; |
| 10 |
use MediaWiki\Title\TitleFormatter; |
| 11 |
use Wikimedia\ParamValidator\ParamValidator; |
| 12 |
|
| 13 |
class ApiDiscussionToolsFindComment extends ApiBase { |
| 14 |
|
| 15 |
private ThreadItemStore $threadItemStore; |
| 16 |
private TitleFormatter $titleFormatter; |
| 17 |
|
| 18 |
public function __construct( |
| 19 |
ApiMain $main, |
| 20 |
string $name, |
| 21 |
ThreadItemStore $threadItemStore, |
| 22 |
TitleFormatter $titleFormatter |
| 23 |
) { |
| 24 |
parent::__construct( $main, $name ); |
| 25 |
$this->threadItemStore = $threadItemStore; |
| 26 |
$this->titleFormatter = $titleFormatter; |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public function execute() { |
| 34 |
$params = $this->extractRequestParams(); |
| 35 |
|
| 36 |
$values = []; |
| 37 |
|
| 38 |
$this->requireAtLeastOneParameter( $params, 'idorname', 'heading', 'page' ); |
| 39 |
|
| 40 |
if ( $params['idorname'] ) { |
| 41 |
$idOrName = $params['idorname']; |
| 42 |
|
| 43 |
$byId = $this->threadItemStore->findNewestRevisionsById( $idOrName ); |
| 44 |
foreach ( $byId as $item ) { |
| 45 |
$values[] = $this->getValue( $item, 'id' ); |
| 46 |
} |
| 47 |
|
| 48 |
$byName = $this->threadItemStore->findNewestRevisionsByName( $idOrName ); |
| 49 |
foreach ( $byName as $item ) { |
| 50 |
$values[] = $this->getValue( $item, 'name' ); |
| 51 |
} |
| 52 |
} else { |
| 53 |
$this->requireAtLeastOneParameter( $params, 'heading' ); |
| 54 |
$this->requireAtLeastOneParameter( $params, 'page' ); |
| 55 |
|
| 56 |
$heading = $params['heading']; |
| 57 |
$page = $params['page']; |
| 58 |
|
| 59 |
$title = Title::newFromText( $page ); |
| 60 |
if ( $title ) { |
| 61 |
$articleId = $title->getArticleId(); |
| 62 |
|
| 63 |
if ( $articleId ) { |
| 64 |
try { |
| 65 |
$byHeading = $this->threadItemStore->findNewestRevisionsByHeading( |
| 66 |
$heading, $articleId, $title->getTitleValue() |
| 67 |
); |
| 68 |
} catch ( PageNeverHadThreadsException $e ) { |
| 69 |
$this->dieWithError( [ 'apierror-discussiontools-findcomment-pagenevertalk' ] ); |
| 70 |
} |
| 71 |
foreach ( $byHeading as $item ) { |
| 72 |
$values[] = $this->getValue( $item, 'heading' ); |
| 73 |
} |
| 74 |
} else { |
| 75 |
|
| 76 |
$this->dieWithError( [ 'apierror-discussiontools-findcomment-pagenevertalk' ] ); |
| 77 |
} |
| 78 |
} else { |
| 79 |
|
| 80 |
$this->dieWithError( [ 'apierror-discussiontools-findcomment-pagenevertalk' ] ); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
$redirects = 0; |
| 85 |
foreach ( $values as $value ) { |
| 86 |
if ( $value['couldredirect'] ) { |
| 87 |
$redirects++; |
| 88 |
if ( $redirects > 1 ) { |
| 89 |
break; |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
foreach ( $values as $value ) { |
| 94 |
if ( $redirects === 1 && $value['couldredirect'] ) { |
| 95 |
$value['shouldredirect'] = true; |
| 96 |
} |
| 97 |
$this->getResult()->addValue( $this->getModuleName(), null, $value ); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
private function getValue( DatabaseThreadItem $item, string $matchedBy ): array { |
| 109 |
$title = Title::castFromPageReference( $item->getPage() ); |
| 110 |
|
| 111 |
return [ |
| 112 |
'id' => $item->getId(), |
| 113 |
'name' => $item->getName(), |
| 114 |
'title' => $this->titleFormatter->getPrefixedText( $item->getPage() ), |
| 115 |
'oldid' => !$item->getRevision()->isCurrent() ? $item->getRevision()->getId() : null, |
| 116 |
'matchedby' => $matchedBy, |
| 117 |
|
| 118 |
|
| 119 |
'couldredirect' => $item->isCanonicalPermalink(), |
| 120 |
]; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public function getAllowedParams() { |
| 127 |
return [ |
| 128 |
'idorname' => [ |
| 129 |
ParamValidator::PARAM_TYPE => 'string', |
| 130 |
], |
| 131 |
'heading' => [ |
| 132 |
ParamValidator::PARAM_TYPE => 'string', |
| 133 |
], |
| 134 |
'page' => [ |
| 135 |
ParamValidator::PARAM_TYPE => 'string', |
| 136 |
], |
| 137 |
]; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public function needsToken() { |
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public function isWriteMode() { |
| 151 |
return false; |
| 152 |
} |
| 153 |
} |