Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
61.64% |
45 / 73 |
|
57.14% |
4 / 7 |
CRAP | |
0.00% |
0 / 1 |
Citation | |
61.64% |
45 / 73 |
|
57.14% |
4 / 7 |
36.28 | |
0.00% |
0 / 1 |
init | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
addMetadata | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getAllowedParameterNames | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
addJsonLD | |
100.00% |
34 / 34 |
|
100.00% |
1 / 1 |
2 | |||
addMetaTags | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
6 | |||
getIsPartOf | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
30 | |||
getPublisher | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 |
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\Generator\Plugins; |
23 | |
24 | use MediaWiki\Extension\WikiSEO\Generator\AbstractBaseGenerator; |
25 | use MediaWiki\Extension\WikiSEO\Generator\GeneratorInterface; |
26 | use OutputPage; |
27 | |
28 | /** |
29 | * Citation metatags |
30 | * Mainly taken from https://gist.github.com/hubgit/f3e359ab51da6d15118b |
31 | */ |
32 | class Citation extends AbstractBaseGenerator implements GeneratorInterface { |
33 | /** |
34 | * @inheritdoc |
35 | */ |
36 | protected $tags = [ |
37 | 'description', |
38 | 'citation_author', |
39 | 'citation_abstract_html_url', |
40 | 'citation_conference_title', |
41 | 'citation_creation_date', |
42 | 'citation_doi', |
43 | 'citation_firstpage', |
44 | 'citation_headline', |
45 | 'citation_isbn', |
46 | 'citation_issn', |
47 | 'citation_issue', |
48 | 'citation_journal_title', |
49 | 'citation_keywords', |
50 | 'citation_lastpage', |
51 | 'citation_license', |
52 | 'citation_name', |
53 | 'citation_pdf_url', |
54 | 'citation_publication_date', |
55 | 'citation_publisher', |
56 | 'citation_title', |
57 | 'citation_type', |
58 | 'citation_volume', |
59 | ]; |
60 | |
61 | /** |
62 | * @inheritDoc |
63 | */ |
64 | public function init( array $metadata, OutputPage $out ): void { |
65 | $this->metadata = $metadata; |
66 | $this->outputPage = $out; |
67 | } |
68 | |
69 | /** |
70 | * @inheritDoc |
71 | */ |
72 | public function addMetadata(): void { |
73 | $this->addJsonLD(); |
74 | $this->addMetaTags(); |
75 | } |
76 | |
77 | /** |
78 | * @inheritDoc |
79 | */ |
80 | public function getAllowedParameterNames(): array { |
81 | return $this->tags; |
82 | } |
83 | |
84 | /** |
85 | * Adds ld+json metadata |
86 | * |
87 | * @return void |
88 | */ |
89 | private function addJsonLD(): void { |
90 | $template = '<script type="application/ld+json">%s</script>'; |
91 | |
92 | $jsonLd = [ |
93 | '@context' => 'https://schema.org', |
94 | '@graph' => [ |
95 | [ |
96 | '@type' => $this->metadata['citation_type'] ?? 'ScholarlyArticle', |
97 | 'name' => $this->metadata['citation_name'] ?? null, |
98 | 'headline' => $this->metadata['citation_headline'] ?? null, |
99 | 'datePublished' => $this->metadata['citation_publication_date'] ?? null, |
100 | 'dateCreated' => $this->metadata['citation_creation_date'] ?? null, |
101 | 'isPartOf' => $this->getIsPartOf(), |
102 | 'pageStart' => $this->metadata['citation_firstpage'] ?? null, |
103 | 'sameAs' => $this->metadata['citation_doi'] ?? null, |
104 | 'description' => $this->metadata['description'] ?? null, |
105 | 'copyrightHolder' => $this->metadata['citation_author'] ?? null, |
106 | 'license' => $this->metadata['citation_license'] ?? null, |
107 | |
108 | 'publisher' => $this->getPublisher(), |
109 | 'keywords' => $this->metadata['citation_keywords'] ?? null, |
110 | |
111 | 'author' => [] |
112 | ] |
113 | ], |
114 | |
115 | ]; |
116 | |
117 | foreach ( explode( ';', $this->metadata['citation_author'] ?? '' ) as $author ) { |
118 | $parts = explode( ',', $author ); |
119 | $jsonLd['@graph'][0]['author'][] = [ |
120 | '@type' => 'Person', |
121 | 'givenName' => $parts[1] ?? null, |
122 | 'familyName' => $parts[0] ?? null, |
123 | 'name' => $author, |
124 | ]; |
125 | } |
126 | |
127 | $this->outputPage->addHeadItem( |
128 | 'jsonld-metadata-citation', |
129 | sprintf( $template, json_encode( $jsonLd ) ) |
130 | ); |
131 | } |
132 | |
133 | /** |
134 | * Adds <meta> citation data |
135 | * |
136 | * @return void |
137 | */ |
138 | private function addMetaTags(): void { |
139 | foreach ( $this->tags as $tag ) { |
140 | if ( !array_key_exists( $tag, $this->metadata ) || $tag === 'description' ) { |
141 | continue; |
142 | } |
143 | |
144 | if ( $tag === 'citation_author' ) { |
145 | foreach ( explode( ';', $this->metadata[$tag] ) as $part ) { |
146 | $this->outputPage->addMeta( $tag, trim( $part ) ); |
147 | } |
148 | } else { |
149 | $this->outputPage->addMeta( $tag, $this->metadata[$tag] ); |
150 | } |
151 | } |
152 | } |
153 | |
154 | /** |
155 | * Part of JsonLD |
156 | * |
157 | * @return array|null |
158 | */ |
159 | private function getIsPartOf(): ?array { |
160 | if ( !isset( $this->metadata['citation_issue'] ) && !isset( $this->metadata['citation_volume'] ) ) { |
161 | return null; |
162 | } |
163 | |
164 | $partOf = null; |
165 | if ( isset( $this->metadata['citation_volume'] ) ) { |
166 | $journal = null; |
167 | |
168 | if ( isset( $this->metadata['citation_publisher'] ) ) { |
169 | $journal = [ |
170 | '@type' => 'Periodical', |
171 | 'name' => $this->metadata['citation_publisher'], |
172 | ]; |
173 | } |
174 | |
175 | $partOf = [ |
176 | '@type' => 'PublicationVolume', |
177 | 'volumeNumber' => $this->metadata['citation_volume'], |
178 | 'isPartOf' => $journal, |
179 | ]; |
180 | } |
181 | |
182 | return [ |
183 | '@type' => 'PublicationIssue', |
184 | 'issueNumber' => $this->metadata['citation_issue'] ?? null, |
185 | 'isPartOf' => $partOf |
186 | ]; |
187 | } |
188 | |
189 | /** |
190 | * Publisher Json LD |
191 | * |
192 | * @return array|null |
193 | */ |
194 | private function getPublisher(): ?array { |
195 | if ( !isset( $this->metadata['citation_publisher'] ) ) { |
196 | return null; |
197 | } |
198 | |
199 | return [ |
200 | '@type' => 'Organization', |
201 | 'name' => $this->metadata['citation_publisher'] ?? null, |
202 | 'logo' => '', |
203 | ]; |
204 | } |
205 | } |