Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.00% |
76 / 80 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| InfoAction | |
95.00% |
76 / 80 |
|
60.00% |
3 / 5 |
21 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| onInfoAction | |
100.00% |
53 / 53 |
|
100.00% |
1 / 1 |
9 | |||
| formatKeywords | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| formatImage | |
85.71% |
12 / 14 |
|
0.00% |
0 / 1 |
6.10 | |||
| formatAuthor | |
77.78% |
7 / 9 |
|
0.00% |
0 / 1 |
4.18 | |||
| 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\Hooks; |
| 23 | |
| 24 | use IContextSource; |
| 25 | use MediaWiki\Extension\WikiSEO\Validator; |
| 26 | use MediaWiki\Hook\InfoActionHook; |
| 27 | use MediaWiki\Html\Html; |
| 28 | use MediaWiki\Page\PageProps; |
| 29 | use MediaWiki\Title\TitleFactory; |
| 30 | use Message; |
| 31 | use RepoGroup; |
| 32 | |
| 33 | class InfoAction implements InfoActionHook { |
| 34 | /** |
| 35 | * InfoAction constructor. |
| 36 | */ |
| 37 | public function __construct( |
| 38 | private readonly RepoGroup $repoGroup, |
| 39 | private readonly PageProps $pageProps, |
| 40 | private readonly TitleFactory $titleFactory, |
| 41 | ) { |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Adds SEO page props as a table to the page when calling with ?action=info |
| 46 | * |
| 47 | * @param IContextSource $context |
| 48 | * @param array &$pageInfo |
| 49 | * @return bool|void |
| 50 | */ |
| 51 | public function onInfoAction( $context, &$pageInfo ) { |
| 52 | $properties = $this->pageProps->getProperties( |
| 53 | $context->getTitle(), |
| 54 | Validator::getValidParams() |
| 55 | ); |
| 56 | |
| 57 | $properties = array_shift( $properties ); |
| 58 | |
| 59 | if ( $properties === null || count( $properties ) === 0 ) { |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | $pageInfo['header-seo'] = [ |
| 64 | [ |
| 65 | sprintf( |
| 66 | '<h3>%s</h3>', |
| 67 | ( new Message( 'wiki-seo-pageinfo-header-description' ) )->escaped() |
| 68 | ), |
| 69 | sprintf( |
| 70 | '<h3>%s</h3>', |
| 71 | ( new Message( 'wiki-seo-pageinfo-header-content' ) )->escaped() |
| 72 | ), |
| 73 | ] |
| 74 | ]; |
| 75 | |
| 76 | foreach ( $properties as $param => $value ) { |
| 77 | switch ( $param ) { |
| 78 | case 'keywords': |
| 79 | $content = $this->formatKeywords( $value ); |
| 80 | break; |
| 81 | |
| 82 | case 'image': |
| 83 | $content = $this->formatImage( $value ); |
| 84 | break; |
| 85 | |
| 86 | case 'author': |
| 87 | $content = $this->formatAuthor( $value ); |
| 88 | break; |
| 89 | |
| 90 | default: |
| 91 | $content = sprintf( '%s', strip_tags( $value ) ); |
| 92 | break; |
| 93 | } |
| 94 | |
| 95 | $description = new Message( sprintf( 'wiki-seo-param-%s-description', $param ) ); |
| 96 | if ( $description->exists() ) { |
| 97 | $description = sprintf( |
| 98 | '<b>%s</b> (<code>%s</code>)<br>%s', |
| 99 | ( new Message( sprintf( 'wiki-seo-param-%s', $param ) ) )->escaped(), |
| 100 | $param, |
| 101 | $description->parse() |
| 102 | ); |
| 103 | } else { |
| 104 | $description = sprintf( |
| 105 | '<b>%s</b> (<code>%s</code>)', |
| 106 | ( new Message( sprintf( 'wiki-seo-param-%s', $param ) ) )->escaped(), |
| 107 | $param |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | $pageInfo['header-seo'][] = [ |
| 112 | $description, |
| 113 | $content |
| 114 | ]; |
| 115 | } |
| 116 | |
| 117 | $belowMessage = new Message( 'wiki-seo-pageinfo-below' ); |
| 118 | |
| 119 | $pageInfo['header-seo'][] = [ |
| 120 | 'below', |
| 121 | $belowMessage->parse() |
| 122 | ]; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Explodes a comma separated list and maps it into an ul list |
| 127 | * |
| 128 | * @param string|null $value |
| 129 | * @return string |
| 130 | */ |
| 131 | private function formatKeywords( ?string $value ): string { |
| 132 | return sprintf( '<ul>%s</ul>', implode( '', array_map( static function ( $keyword ) { |
| 133 | return sprintf( '<li>%s</li>', trim( strip_tags( $keyword ) ) ); |
| 134 | }, explode( ',', $value ?? '' ) ) ) ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Formats an image to a 200px thumbnail for display |
| 139 | * |
| 140 | * @param string|null $value |
| 141 | * @return string |
| 142 | */ |
| 143 | private function formatImage( ?string $value ): string { |
| 144 | $title = $this->titleFactory->newFromText( $value, NS_FILE ); |
| 145 | |
| 146 | if ( $title === null || !$title->exists() || !$title->inNamespace( NS_FILE ) ) { |
| 147 | return htmlspecialchars( (string)$value ); |
| 148 | } |
| 149 | |
| 150 | $file = $this->repoGroup->findFile( $title->getDBkey() ); |
| 151 | |
| 152 | if ( $file ) { |
| 153 | $transform = $file->transform( [ 'width' => 200 ] ); |
| 154 | if ( $transform ) { |
| 155 | return Html::rawElement( 'img', [ |
| 156 | 'src' => $transform->getUrl(), |
| 157 | 'alt' => $title->getBaseText(), |
| 158 | 'width' => 200, |
| 159 | 'style' => 'height: auto', |
| 160 | ] ); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | return htmlspecialchars( (string)$value ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Formats the author link into an internal link |
| 169 | * |
| 170 | * @param string|null $value |
| 171 | * @return string |
| 172 | */ |
| 173 | private function formatAuthor( ?string $value ): string { |
| 174 | $parsed = parse_url( $value ?? '' ); |
| 175 | if ( $parsed === false || empty( $parsed['path'] ) ) { |
| 176 | return htmlspecialchars( (string)$value ); |
| 177 | } |
| 178 | |
| 179 | $title = $this->titleFactory->newFromText( ltrim( $parsed['path'], '/' ), NS_USER ); |
| 180 | |
| 181 | if ( $title === null ) { |
| 182 | return htmlspecialchars( (string)$value ); |
| 183 | } |
| 184 | |
| 185 | return Html::rawElement( 'a', [ |
| 186 | 'href' => $title->getFullURL(), |
| 187 | ], $title->getPrefixedText() ?? $title->getText() ); |
| 188 | } |
| 189 | } |