Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 54 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
ApiQueryImageForPage | |
0.00% |
0 / 54 |
|
0.00% |
0 / 4 |
342 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
getImageData | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
156 | |||
getAllowedParams | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace ProofreadPage\Api; |
4 | |
5 | use ApiBase; |
6 | use ApiQueryBase; |
7 | use ApiResult; |
8 | use MediaWiki\Title\Title; |
9 | use ProofreadPage\Context; |
10 | use ProofreadPage\FileNotFoundException; |
11 | use ProofreadPage\Page\PageDisplayHandler; |
12 | use Wikimedia\ParamValidator\ParamValidator; |
13 | |
14 | class ApiQueryImageForPage extends ApiQueryBase { |
15 | /** |
16 | * @var Context |
17 | */ |
18 | private $context; |
19 | |
20 | /** |
21 | * @var PageDisplayhandler |
22 | */ |
23 | private $pageDisplayHandler; |
24 | |
25 | /** @var string API module prefix */ |
26 | private static $prefix = 'prppifp'; |
27 | |
28 | /** |
29 | * @param \ApiQuery $query |
30 | * @param string $moduleName |
31 | */ |
32 | public function __construct( $query, $moduleName ) { |
33 | parent::__construct( $query, $moduleName, static::$prefix ); |
34 | $this->context = $this->context = Context::getDefaultContext(); |
35 | $this->pageDisplayHandler = new PageDisplayHandler( $this->context ); |
36 | } |
37 | |
38 | /** |
39 | * @inheritDoc |
40 | */ |
41 | public function execute() { |
42 | $params = $this->extractRequestParams(); |
43 | |
44 | $pageSet = $this->getPageSet()->getGoodAndMissingPages(); |
45 | $result = $this->getResult(); |
46 | $pagePageImages = []; |
47 | |
48 | $props = array_fill_keys( $params['prop'], true ); |
49 | |
50 | foreach ( $pageSet as $pageID => $page ) { |
51 | if ( $page->getNamespace() !== $this->context->getPageNamespaceId() ) { |
52 | continue; |
53 | } |
54 | |
55 | $title = Title::castFromPageIdentity( $page ); |
56 | |
57 | if ( !$title ) { |
58 | continue; |
59 | } |
60 | |
61 | $result->addValue( [ 'query', 'pages', $pageID ], 'imagesforpage', $this->getImageData( $title, $props ) ); |
62 | } |
63 | } |
64 | |
65 | /** |
66 | * Get data about images for a title based on parameters supplied by the user |
67 | * |
68 | * The function does not check for any kind of null values returned for the various getImage...() calls |
69 | * instead relying on a file provider check at the start to confirm the existence of a file for the page title. |
70 | * |
71 | * If the page title is outside bounds, the details of the last page is returned. |
72 | * |
73 | * @param Title $title |
74 | * @param array $props parameters sent by user |
75 | * @return array Array of image urls |
76 | */ |
77 | public function getImageData( Title $title, array $props ): array { |
78 | $file = null; |
79 | try { |
80 | $fileProvider = $this->context->getFileProvider(); |
81 | $file = $fileProvider->getFileForPageTitle( $title ); |
82 | } catch ( FileNotFoundException $e ) { |
83 | return []; |
84 | } |
85 | |
86 | if ( !$file || !$file->exists() ) { |
87 | return []; |
88 | } |
89 | |
90 | $thumbnail = $this->pageDisplayHandler->getImageThumbnail( $title ); |
91 | |
92 | $data = []; |
93 | |
94 | // Check if the thumbnail has been created, if not do not send back any URL for the thumbnail |
95 | if ( $thumbnail ) { |
96 | $data['thumbnail'] = $thumbnail->getUrl(); |
97 | } |
98 | |
99 | if ( isset( $props['size'] ) ) { |
100 | $data['size'] = $this->pageDisplayHandler->getImageWidth( $title ); |
101 | } |
102 | |
103 | if ( isset( $props['filename'] ) ) { |
104 | $data['filename'] = $file->getName(); |
105 | } |
106 | |
107 | if ( isset( $props['responsiveimages'] ) && $thumbnail ) { |
108 | $responsiveUrls = $thumbnail->responsiveUrls; |
109 | $data['responsiveimages'] = []; |
110 | foreach ( $responsiveUrls as $density => $url ) { |
111 | $data['responsiveimages'][$density] = $url; |
112 | } |
113 | ApiResult::setArrayType( $data['responsiveimages'], 'kvp' ); |
114 | ApiResult::setIndexedTagName( $data['responsiveimages'], 'responsiveimage' ); |
115 | } |
116 | |
117 | $fullSizeImage = $this->pageDisplayHandler->getImageFullSize( $title ); |
118 | |
119 | if ( isset( $props['fullsize'] ) && $fullSizeImage ) { |
120 | $data['fullsize'] = $fullSizeImage->getUrl(); |
121 | } |
122 | |
123 | return $data; |
124 | } |
125 | |
126 | /** |
127 | * @inheritDoc |
128 | */ |
129 | protected function getAllowedParams() { |
130 | return [ |
131 | 'prop' => [ |
132 | ParamValidator::PARAM_ISMULTI => true, |
133 | ParamValidator::PARAM_DEFAULT => 'filename|size|fullsize|responsiveimages', |
134 | ParamValidator::PARAM_TYPE => [ |
135 | 'filename', |
136 | 'size', |
137 | 'fullsize', |
138 | 'responsiveimages' |
139 | ], |
140 | ApiBase::PARAM_HELP_MSG_PER_VALUE => [], |
141 | ], |
142 | ]; |
143 | } |
144 | } |