Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
18.75% |
9 / 48 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ApiDescription | |
18.75% |
9 / 48 |
|
25.00% |
1 / 4 |
153.31 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getDescription | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| getDescriptionFromExtracts | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
72 | |||
| checkExtensions | |
35.71% |
5 / 14 |
|
0.00% |
0 / 1 |
8.25 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | * |
| 17 | * @file |
| 18 | */ |
| 19 | |
| 20 | declare( strict_types=1 ); |
| 21 | |
| 22 | namespace MediaWiki\Extension\WikiSEO; |
| 23 | |
| 24 | use ApiMain; |
| 25 | use ExtensionDependencyError; |
| 26 | use ExtensionRegistry; |
| 27 | use InvalidArgumentException; |
| 28 | use MediaWiki\Request\FauxRequest; |
| 29 | use MediaWiki\Title\Title; |
| 30 | use MWException; |
| 31 | |
| 32 | class ApiDescription { |
| 33 | |
| 34 | /** |
| 35 | * @var Title The page title to get the description from |
| 36 | */ |
| 37 | private $title; |
| 38 | |
| 39 | /** |
| 40 | * Flag to try to remove dangling sentences |
| 41 | * |
| 42 | * @var bool |
| 43 | */ |
| 44 | private $tryCleanSentence; |
| 45 | |
| 46 | /** |
| 47 | * Description source |
| 48 | * Currently only TextExtracts is supported |
| 49 | * |
| 50 | * @var string |
| 51 | */ |
| 52 | private $source; |
| 53 | |
| 54 | /** |
| 55 | * ApiDescription constructor. |
| 56 | * @param Title $title |
| 57 | * @param bool $tryCleanSentence |
| 58 | * @param string $source |
| 59 | * @throws ExtensionDependencyError |
| 60 | */ |
| 61 | public function __construct( Title $title, bool $tryCleanSentence = false, string $source = 'extracts' ) { |
| 62 | $this->title = $title; |
| 63 | $this->tryCleanSentence = $tryCleanSentence; |
| 64 | $this->source = strtolower( $source ); |
| 65 | |
| 66 | $this->checkExtensions(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Request the description |
| 71 | * |
| 72 | * @return string |
| 73 | * @throws MWException |
| 74 | */ |
| 75 | public function getDescription(): string { |
| 76 | switch ( $this->source ) { |
| 77 | case 'extracts': |
| 78 | return $this->getDescriptionFromExtracts(); |
| 79 | |
| 80 | default: |
| 81 | return ''; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Call text extracts |
| 87 | * Returns an empty string on error |
| 88 | * |
| 89 | * @return string |
| 90 | * @throws MWException |
| 91 | */ |
| 92 | public function getDescriptionFromExtracts(): string { |
| 93 | $query = [ |
| 94 | 'format' => 'json', |
| 95 | 'action' => 'query', |
| 96 | 'prop' => 'extracts', |
| 97 | 'titles' => $this->title->getPrefixedText(), |
| 98 | 'exchars' => 160, |
| 99 | 'exintro' => 1, |
| 100 | 'explaintext' => 1, |
| 101 | 'exsectionformat' => 'plain' |
| 102 | ]; |
| 103 | |
| 104 | $request = new ApiMain( new FauxRequest( $query ) ); |
| 105 | |
| 106 | $request->execute(); |
| 107 | |
| 108 | $data = $request->getResult()->getResultData(); |
| 109 | |
| 110 | if ( $data === null ) { |
| 111 | return ''; |
| 112 | } |
| 113 | |
| 114 | if ( !isset( $data['batchcomplete'] ) || |
| 115 | !isset( $data['query']['pages'] ) || |
| 116 | $data['batchcomplete'] === false || |
| 117 | empty( $data['query']['pages'] ) |
| 118 | ) { |
| 119 | return ''; |
| 120 | } |
| 121 | |
| 122 | $text = array_shift( $data['query']['pages'] )['extract']['*'] ?? ''; |
| 123 | |
| 124 | if ( $this->tryCleanSentence === true && substr( $text, -1 ) !== '.' ) { |
| 125 | $parts = explode( '.', $text ); |
| 126 | array_pop( $parts ); |
| 127 | $text = sprintf( '%s.', implode( '.', $parts ) ); |
| 128 | } |
| 129 | |
| 130 | return str_replace( "\n", ' ', strip_tags( $text ) ); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * @throws ExtensionDependencyError |
| 135 | * @throws InvalidArgumentException |
| 136 | */ |
| 137 | private function checkExtensions(): void { |
| 138 | switch ( $this->source ) { |
| 139 | case 'extracts': |
| 140 | if ( !ExtensionRegistry::getInstance()->isLoaded( 'TextExtracts' ) ) { |
| 141 | throw new ExtensionDependencyError( [ |
| 142 | [ |
| 143 | 'msg' => 'TextExtracts not loaded', |
| 144 | 'type' => 'missing-extensions', |
| 145 | 'missing' => 'TextExtracts', |
| 146 | ] |
| 147 | ] ); |
| 148 | } |
| 149 | return; |
| 150 | |
| 151 | default: |
| 152 | throw new InvalidArgumentException( |
| 153 | sprintf( 'Format "%s" is not implemented. Use "extracts".', $this->source ) |
| 154 | ); |
| 155 | } |
| 156 | } |
| 157 | } |