Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
32.43% |
24 / 74 |
|
14.29% |
1 / 7 |
CRAP | |
0.00% |
0 / 1 |
| Hooks | |
32.43% |
24 / 74 |
|
14.29% |
1 / 7 |
327.44 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onBeforePageDisplay | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
20 | |||
| onEditFilterMergedContent | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
110 | |||
| onGetPreferences | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
5 | |||
| getLinksForUser | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
| getLinksFromConfig | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| onOutputPageParserOutput | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
| 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 | namespace MediaWiki\Extension\RealMe; |
| 21 | |
| 22 | use MediaWiki\Config\Config; |
| 23 | use MediaWiki\Config\ServiceOptions; |
| 24 | use MediaWiki\Content\Content; |
| 25 | use MediaWiki\Content\JsonContent; |
| 26 | use MediaWiki\Context\IContextSource; |
| 27 | use MediaWiki\Hook\EditFilterMergedContentHook; |
| 28 | use MediaWiki\HTMLForm\HTMLForm; |
| 29 | use MediaWiki\Json\FormatJson; |
| 30 | use MediaWiki\Output\Hook\BeforePageDisplayHook; |
| 31 | use MediaWiki\Output\Hook\OutputPageParserOutputHook; |
| 32 | use MediaWiki\Parser\ParserOutput; |
| 33 | use MediaWiki\Preferences\Hook\GetPreferencesHook; |
| 34 | use MediaWiki\Status\Status; |
| 35 | use MediaWiki\Title\Title; |
| 36 | use MediaWiki\User\Options\UserOptionsLookup; |
| 37 | use MediaWiki\User\User; |
| 38 | use MediaWiki\User\UserFactory; |
| 39 | use MediaWiki\Utils\UrlUtils; |
| 40 | |
| 41 | class Hooks implements |
| 42 | BeforePageDisplayHook, |
| 43 | EditFilterMergedContentHook, |
| 44 | GetPreferencesHook, |
| 45 | OutputPageParserOutputHook |
| 46 | { |
| 47 | private const CONSTRUCTOR_OPTIONS = [ |
| 48 | 'RealMeUserPageUrlLimit', |
| 49 | ]; |
| 50 | |
| 51 | private readonly ServiceOptions $options; |
| 52 | |
| 53 | public function __construct( |
| 54 | Config $mainConfig, |
| 55 | private readonly UrlUtils $urlUtils, |
| 56 | private readonly UserFactory $userFactory, |
| 57 | private readonly UserOptionsLookup $userOptionsLookup, |
| 58 | ) { |
| 59 | $this->options = new ServiceOptions( self::CONSTRUCTOR_OPTIONS, $mainConfig ); |
| 60 | } |
| 61 | |
| 62 | /** @inheritDoc */ |
| 63 | public function onBeforePageDisplay( $out, $skin ): void { |
| 64 | // Add a help link on the JSON config page |
| 65 | $title = $out->getTitle(); |
| 66 | if ( $title && $title->inNamespace( NS_MEDIAWIKI ) |
| 67 | && $title->getText() === Constants::CONFIG_PAGE ) { |
| 68 | $out->addHelpLink( 'Help:Extension:RealMe' ); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** @inheritDoc */ |
| 73 | public function onEditFilterMergedContent( IContextSource $context, Content $content, Status $status, |
| 74 | $summary, User $user, $minoredit |
| 75 | ) { |
| 76 | $title = $context->getTitle(); |
| 77 | if ( !$title || !$title->inNamespace( NS_MEDIAWIKI ) |
| 78 | || $title->getText() !== Constants::CONFIG_PAGE ) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | if ( !$content instanceof JsonContent ) { |
| 83 | // Huh?? |
| 84 | return; |
| 85 | } |
| 86 | if ( !$content->getData()->isGood() ) { |
| 87 | // Will error out anyways |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | $validator = new Validator( $context, $this->urlUtils ); |
| 92 | $errors = []; |
| 93 | $data = wfObjectToArray( $content->getData()->getValue() ); |
| 94 | foreach ( $data as $title => $urls ) { |
| 95 | $errors = array_merge( $errors, $validator->checkTitle( $title ) ); |
| 96 | foreach ( (array)$urls as $url ) { |
| 97 | $errors = array_merge( $errors, $validator->checkUrl( $url ) ); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | if ( $errors ) { |
| 102 | foreach ( $errors as $error ) { |
| 103 | $status->fatal( $error ); |
| 104 | } |
| 105 | return false; |
| 106 | } else { |
| 107 | return true; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** @inheritDoc */ |
| 112 | public function onGetPreferences( $user, &$preferences ) { |
| 113 | $preferences[Constants::PREFERENCE_NAME] = [ |
| 114 | 'type' => 'textarea', |
| 115 | 'section' => 'personal/userpage', |
| 116 | 'label-message' => 'realme-preference-desc', |
| 117 | 'help-message' => 'realme-preference-help', |
| 118 | 'rows' => min( 5, $this->options->get( 'RealMeUserPageUrlLimit' ) ), |
| 119 | |
| 120 | 'validation-callback' => function ( $optionValue, $allData, HTMLForm $form ) { |
| 121 | $urls = explode( "\n", $optionValue ?? '' ); |
| 122 | |
| 123 | $errors = []; |
| 124 | $count = 0; |
| 125 | $validator = new Validator( $form, $this->urlUtils ); |
| 126 | |
| 127 | foreach ( $urls as $url ) { |
| 128 | if ( trim( $url ) === '' ) { |
| 129 | continue; |
| 130 | } |
| 131 | |
| 132 | $count += 1; |
| 133 | |
| 134 | $errors = array_merge( $errors, $validator->checkUrl( $url ) ); |
| 135 | } |
| 136 | |
| 137 | if ( $count > $this->options->get( 'RealMeUserPageUrlLimit' ) ) { |
| 138 | $errors[] = $form->msg( 'realme-preference-error-too-many' ) |
| 139 | ->params( $this->options->get( 'RealMeUserPageUrlLimit' ) ); |
| 140 | } |
| 141 | |
| 142 | if ( $errors ) { |
| 143 | return $errors; |
| 144 | } |
| 145 | |
| 146 | return true; |
| 147 | }, |
| 148 | ]; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Given a Title that corresponds to a User or User talk page, look up the links |
| 153 | * that should be added to that page |
| 154 | * |
| 155 | * @return ?array |
| 156 | */ |
| 157 | private function getLinksForUser( Title $title, ParserOutput $parserOutput ) { |
| 158 | if ( $title->isSubpage() ) { |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | $user = $this->userFactory->newFromName( $title->getText() ); |
| 163 | |
| 164 | if ( !$user ) { |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | $linksPresent = array_keys( $parserOutput->getExternalLinks() ); |
| 169 | |
| 170 | if ( !$linksPresent ) { |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | $option = $this->userOptionsLookup->getOption( $user, Constants::PREFERENCE_NAME, "" ); |
| 175 | $allowedUrls = explode( "\n", $option ); |
| 176 | |
| 177 | return array_intersect( $linksPresent, $allowedUrls ); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Given a non-user Title, look up the links that should be added to that page |
| 182 | * |
| 183 | * @return ?array |
| 184 | */ |
| 185 | private function getLinksFromConfig( Title $title ) { |
| 186 | $config = FormatJson::decode( wfMessage( 'realme-config.json' )->inContentLanguage()->plain(), true ); |
| 187 | if ( !is_array( $config ) ) { |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | $urls = $config[$title->getPrefixedText()] ?? []; |
| 192 | return (array)$urls; |
| 193 | } |
| 194 | |
| 195 | /** @inheritDoc */ |
| 196 | public function onOutputPageParserOutput( $outputPage, $parserOutput ): void { |
| 197 | $title = $outputPage->getTitle(); |
| 198 | if ( !$title ) { |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | if ( !$title->inNamespaces( NS_USER, NS_USER_TALK ) ) { |
| 203 | // Check sitewide config |
| 204 | $urls = $this->getLinksFromConfig( $title ); |
| 205 | } else { |
| 206 | $urls = $this->getLinksForUser( $title, $parserOutput ); |
| 207 | } |
| 208 | |
| 209 | if ( $urls ) { |
| 210 | foreach ( $urls as $url ) { |
| 211 | $outputPage->addLink( [ 'rel' => 'me', 'href' => $url ] ); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |