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