Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
63.16% |
48 / 76 |
|
57.14% |
4 / 7 |
CRAP | |
0.00% |
0 / 1 |
| Citation | |
63.16% |
48 / 76 |
|
57.14% |
4 / 7 |
34.20 | |
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% |
37 / 37 |
|
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( |
| 130 | $template, |
| 131 | json_encode( $jsonLd ) |
| 132 | ) |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Adds <meta> citation data |
| 138 | * |
| 139 | * @return void |
| 140 | */ |
| 141 | private function addMetaTags(): void { |
| 142 | foreach ( $this->tags as $tag ) { |
| 143 | if ( !array_key_exists( $tag, $this->metadata ) || $tag === 'description' ) { |
| 144 | continue; |
| 145 | } |
| 146 | |
| 147 | if ( $tag === 'citation_author' ) { |
| 148 | foreach ( explode( ';', $this->metadata[$tag] ) as $part ) { |
| 149 | $this->outputPage->addMeta( $tag, trim( $part ) ); |
| 150 | } |
| 151 | } else { |
| 152 | $this->outputPage->addMeta( $tag, $this->metadata[$tag] ); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Part of JsonLD |
| 159 | * |
| 160 | * @return array|null |
| 161 | */ |
| 162 | private function getIsPartOf(): ?array { |
| 163 | if ( !isset( $this->metadata['citation_issue'] ) && !isset( $this->metadata['citation_volume'] ) ) { |
| 164 | return null; |
| 165 | } |
| 166 | |
| 167 | $partOf = null; |
| 168 | if ( isset( $this->metadata['citation_volume'] ) ) { |
| 169 | $journal = null; |
| 170 | |
| 171 | if ( isset( $this->metadata['citation_publisher'] ) ) { |
| 172 | $journal = [ |
| 173 | '@type' => 'Periodical', |
| 174 | 'name' => $this->metadata['citation_publisher'], |
| 175 | ]; |
| 176 | } |
| 177 | |
| 178 | $partOf = [ |
| 179 | '@type' => 'PublicationVolume', |
| 180 | 'volumeNumber' => $this->metadata['citation_volume'], |
| 181 | 'isPartOf' => $journal, |
| 182 | ]; |
| 183 | } |
| 184 | |
| 185 | return [ |
| 186 | '@type' => 'PublicationIssue', |
| 187 | 'issueNumber' => $this->metadata['citation_issue'] ?? null, |
| 188 | 'isPartOf' => $partOf |
| 189 | ]; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Publisher Json LD |
| 194 | * |
| 195 | * @return array|null |
| 196 | */ |
| 197 | private function getPublisher(): ?array { |
| 198 | if ( !isset( $this->metadata['citation_publisher'] ) ) { |
| 199 | return null; |
| 200 | } |
| 201 | |
| 202 | return [ |
| 203 | '@type' => 'Organization', |
| 204 | 'name' => $this->metadata['citation_publisher'] ?? null, |
| 205 | 'logo' => '', |
| 206 | ]; |
| 207 | } |
| 208 | } |