Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.71% |
72 / 84 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ApiWikispeechSegment | |
85.71% |
72 / 84 |
|
50.00% |
2 / 4 |
12.42 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
90.24% |
37 / 41 |
|
0.00% |
0 / 1 |
9.08 | |||
| getAllowedParams | |
100.00% |
30 / 30 |
|
100.00% |
1 / 1 |
1 | |||
| getExamplesMessages | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Wikispeech\Api; |
| 4 | |
| 5 | /** |
| 6 | * @file |
| 7 | * @ingroup API |
| 8 | * @ingroup Extensions |
| 9 | * @license GPL-2.0-or-later |
| 10 | */ |
| 11 | |
| 12 | use ApiBase; |
| 13 | use ApiMain; |
| 14 | use FormatJson; |
| 15 | use MediaWiki\Http\HttpRequestFactory; |
| 16 | use MediaWiki\Revision\RevisionStore; |
| 17 | use Mediawiki\Title\Title; |
| 18 | use MediaWiki\Wikispeech\ConfigurationValidator; |
| 19 | use MediaWiki\Wikispeech\Segment\SegmentPageFactory; |
| 20 | use Wikimedia\ObjectCache\WANObjectCache; |
| 21 | use Wikimedia\ParamValidator\ParamValidator; |
| 22 | |
| 23 | /** |
| 24 | * API module to segment a page. |
| 25 | * |
| 26 | * @since 0.0.1 |
| 27 | */ |
| 28 | class ApiWikispeechSegment extends ApiBase { |
| 29 | |
| 30 | /** @var WANObjectCache */ |
| 31 | private $cache; |
| 32 | |
| 33 | /** @var HttpRequestFactory */ |
| 34 | private $requestFactory; |
| 35 | |
| 36 | /** @var RevisionStore */ |
| 37 | private $revisionStore; |
| 38 | |
| 39 | /** @var SegmentPageFactory */ |
| 40 | private $segmentPageFactory; |
| 41 | |
| 42 | /** |
| 43 | * @since 0.1.13 add parameter $segmentPageFactory, remove parameter $configFactory. |
| 44 | * @since 0.1.7 |
| 45 | * @param ApiMain $mainModule |
| 46 | * @param string $moduleName |
| 47 | * @param WANObjectCache $cache |
| 48 | * @param HttpRequestFactory $requestFactory |
| 49 | * @param RevisionStore $revisionStore |
| 50 | * @param SegmentPageFactory $segmentPageFactory |
| 51 | * @param string $modulePrefix |
| 52 | */ |
| 53 | public function __construct( |
| 54 | ApiMain $mainModule, |
| 55 | string $moduleName, |
| 56 | WANObjectCache $cache, |
| 57 | HttpRequestFactory $requestFactory, |
| 58 | RevisionStore $revisionStore, |
| 59 | SegmentPageFactory $segmentPageFactory, |
| 60 | string $modulePrefix = '' |
| 61 | ) { |
| 62 | parent::__construct( $mainModule, $moduleName, $modulePrefix ); |
| 63 | $this->cache = $cache; |
| 64 | $this->requestFactory = $requestFactory; |
| 65 | $this->revisionStore = $revisionStore; |
| 66 | $this->segmentPageFactory = $segmentPageFactory; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Execute an API request. |
| 71 | * |
| 72 | * @since 0.0.1 |
| 73 | */ |
| 74 | public function execute() { |
| 75 | $parameters = $this->extractRequestParams(); |
| 76 | if ( |
| 77 | isset( $parameters['consumer-url'] ) && |
| 78 | !$this->getConfig()->get( 'WikispeechProducerMode' ) ) { |
| 79 | $this->dieWithError( 'apierror-wikispeech-consumer-not-allowed' ); |
| 80 | } |
| 81 | $title = Title::newFromText( $parameters['page'] ); |
| 82 | if ( !$title || $title->isExternal() ) { |
| 83 | $this->dieWithError( [ |
| 84 | 'apierror-invalidtitle', |
| 85 | wfEscapeWikiText( $parameters['page'] ) |
| 86 | ] ); |
| 87 | } |
| 88 | if ( !isset( $parameters['consumer-url'] ) && !$title->exists() ) { |
| 89 | $this->dieWithError( 'apierror-missingtitle' ); |
| 90 | } |
| 91 | $result = FormatJson::parse( |
| 92 | $parameters['removetags'], |
| 93 | FormatJson::FORCE_ASSOC |
| 94 | ); |
| 95 | if ( !$result->isGood() ) { |
| 96 | $this->dieWithError( 'apierror-wikispeech-segment-removetagsinvalidjson' ); |
| 97 | } |
| 98 | $rawRemoveTags = $result->getValue(); |
| 99 | if ( !ConfigurationValidator::isValidRemoveTags( $rawRemoveTags ) ) { |
| 100 | $this->dieWithError( 'apierror-wikispeech-segment-removetagsinvalid' ); |
| 101 | } |
| 102 | $segmentPageResponse = $this->segmentPageFactory |
| 103 | ->setSegmentBreakingTags( $parameters['segmentbreakingtags'] ) |
| 104 | ->setRemoveTags( $rawRemoveTags ) |
| 105 | ->setPartOfContent( $parameters['part-of-content'] ) |
| 106 | ->setUseRevisionPropertiesCache( true ) |
| 107 | ->setRequirePageRevisionProperties( false ) |
| 108 | ->setUseSegmentsCache( true ) |
| 109 | ->setContextSource( $this->getContext() ) |
| 110 | ->setRevisionStore( $this->revisionStore ) |
| 111 | ->setHttpRequestFactory( $this->requestFactory ) |
| 112 | ->setConsumerUrl( $parameters['consumer-url'] ) |
| 113 | ->segmentPage( |
| 114 | $title, |
| 115 | null |
| 116 | ); |
| 117 | |
| 118 | $this->getResult()->addValue( |
| 119 | null, |
| 120 | $this->getModuleName(), |
| 121 | [ 'segments' => $segmentPageResponse->getSegments()->toArray() ] |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Specify what parameters the API accepts. |
| 127 | * |
| 128 | * @since 0.0.1 |
| 129 | * @return array |
| 130 | */ |
| 131 | public function getAllowedParams() { |
| 132 | return array_merge( |
| 133 | parent::getAllowedParams(), |
| 134 | [ |
| 135 | 'page' => [ |
| 136 | ParamValidator::PARAM_TYPE => 'string', |
| 137 | ParamValidator::PARAM_REQUIRED => true |
| 138 | ], |
| 139 | 'removetags' => [ |
| 140 | ParamValidator::PARAM_TYPE => 'string', |
| 141 | ParamValidator::PARAM_DEFAULT => json_encode( |
| 142 | $this->getConfig()->get( 'WikispeechRemoveTags' ) |
| 143 | ) |
| 144 | ], |
| 145 | 'segmentbreakingtags' => [ |
| 146 | ParamValidator::PARAM_TYPE => 'string', |
| 147 | ParamValidator::PARAM_ISMULTI => true, |
| 148 | ParamValidator::PARAM_DEFAULT => implode( |
| 149 | '|', |
| 150 | $this->getConfig()->get( 'WikispeechSegmentBreakingTags' ) |
| 151 | ) |
| 152 | ], |
| 153 | 'part-of-content' => [ |
| 154 | ParamValidator::PARAM_TYPE => 'boolean', |
| 155 | ParamValidator::PARAM_DEFAULT => false |
| 156 | ], |
| 157 | 'consumer-url' => [ |
| 158 | ParamValidator::PARAM_TYPE => 'string' |
| 159 | ] |
| 160 | ] |
| 161 | ); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Give examples of usage. |
| 166 | * |
| 167 | * @since 0.0.1 |
| 168 | * @return array |
| 169 | */ |
| 170 | public function getExamplesMessages() { |
| 171 | return [ |
| 172 | 'action=wikispeech-segment&format=json&page=Main_Page' |
| 173 | => 'apihelp-wikispeech-segment-example-1', |
| 174 | // phpcs:ignore Generic.Files.LineLength |
| 175 | 'action=wikispeech-segment&format=json&page=Main_Page&removetags={"sup": true, "div": "toc"}&segmentbreakingtags=h1|h2' |
| 176 | => 'apihelp-wikispeech-segment-example-2', |
| 177 | 'action=wikispeech-segment&format=json&page=Main_Page&consumer-url=https://consumer.url/w' |
| 178 | => 'apihelp-wikispeech-segment-example-3', |
| 179 | ]; |
| 180 | } |
| 181 | } |