Sophivorus has submitted this change and it was merged. Change subject: Add language validation ......................................................................
Add language validation - Also smarter language guess - Also link to Wikipedia after every extract - Also fix some encoding issues Change-Id: Icaf0a20dc74b1fd179bc5e77d9696e2e6507959d --- M WikipediaExtracts.php M i18n/en.json M i18n/es.json M i18n/fr.json M i18n/qqq.json 5 files changed, 30 insertions(+), 2 deletions(-) Approvals: Sophivorus: Verified; Looks good to me, approved diff --git a/WikipediaExtracts.php b/WikipediaExtracts.php index b8db1ce..cdfa55a 100644 --- a/WikipediaExtracts.php +++ b/WikipediaExtracts.php @@ -25,18 +25,29 @@ extract( $args ); if ( $input ) { if ( filter_var( $input, FILTER_VALIDATE_URL ) ) { + // Extract the title $path = parse_url( $input, PHP_URL_PATH ); $PATH = explode( '/', $path ); $title = $PATH[2]; + + // Extract the language + $host = parse_url( $input, PHP_URL_HOST ); + $HOST = explode( '.', $host ); + $language = $HOST[0]; } else { $title = $input; } } + // Validate language code + if ( !Language::isValidCode( $language ) ) { + return '<span class="error">' . wfMessage( 'wikipediaextracts-invalid-language', $language ) . '</span>'; + } + // Query the Wikipedia API $data = array( 'action' => 'query', - 'titles' => $title, + 'titles' => urldecode( $title ), 'prop' => 'extracts', 'exchars' => $chars, 'exsentences' => $sentences, @@ -58,6 +69,8 @@ return '<span class="error">' . wfMessage( 'wikipediaextracts-404', $title ) . '</span>'; } $extract = $value->extract; + $url = 'https://' . $language . '.wikipedia.org/wiki/' . urlencode( $title ); + $extract .= wfMessage( 'wikipediaextracts-credits', $url )->parse(); return $extract; } } @@ -80,18 +93,29 @@ extract( $options ); if ( $input ) { if ( filter_var( $input, FILTER_VALIDATE_URL ) ) { + // Extract the title $path = parse_url( $input, PHP_URL_PATH ); $PATH = explode( '/', $path ); $title = $PATH[2]; + + // Extract the language + $host = parse_url( $input, PHP_URL_HOST ); + $HOST = explode( '.', $host ); + $language = $HOST[0]; } else { $title = $input; } } + // Validate language code + if ( !Language::isValidCode( $language ) ) { + return '<span class="error">' . wfMessage( 'wikipediaextracts-invalid-language', $language ) . '</span>'; + } + // Query the Wikipedia API $data = array( 'action' => 'query', - 'titles' => $title, + 'titles' => urldecode( $title ), 'prop' => 'extracts', 'exchars' => $chars, 'exsentences' => $sentences, diff --git a/i18n/en.json b/i18n/en.json index 05fc55a..ba72498 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -6,5 +6,6 @@ }, "wikipediaextracts-desc": "Enables to insert content extracted directly from Wikipedia.", "wikipediaextracts-404": "The page '$1' was not found in the English Wikipedia", + "wikipediaextracts-invalid-language": "The language code '$1' is invalid", "wikipediaextracts-credits": "<small>Extracted from [$1 Wikipedia]</small>" } \ No newline at end of file diff --git a/i18n/es.json b/i18n/es.json index 24fd951..2dcad0c 100644 --- a/i18n/es.json +++ b/i18n/es.json @@ -6,5 +6,6 @@ }, "wikipediaextracts-desc": "Permite insertar contenido extraido directamente de Wikipedia.", "wikipediaextracts-404": "La página '$1' no fue encontrada en la Wikipedia en español", + "wikipediaextracts-invalid-language": "El código de lenguaje '$1' es inválido", "wikipediaextracts-credits": "<small>Extraído de [$1 Wikipedia]</small>" } \ No newline at end of file diff --git a/i18n/fr.json b/i18n/fr.json index c1f926f..7a09a3a 100644 --- a/i18n/fr.json +++ b/i18n/fr.json @@ -7,5 +7,6 @@ }, "wikipediaextracts-desc": "Permet d'insérer du contenu extrait directement de Wikipédia Français.", "wikipediaextracts-404": "La page '$1' n'a pas été trouvée sur Wikipédia", + "wikipediaextracts-invalid-language": "The language code '$1' is invalid", "wikipediaextracts-credits": "<small>Extrait de [$1 Wikipédia]</small>" } \ No newline at end of file diff --git a/i18n/qqq.json b/i18n/qqq.json index fcb3c17..af802b5 100644 --- a/i18n/qqq.json +++ b/i18n/qqq.json @@ -6,5 +6,6 @@ }, "wikipediaextracts-desc": "{{desc|name=WikipediaExtracts|url=https://www.mediawiki.org/wiki/Extension:WikipediaExtracts}}", "wikipediaextracts-404": "Error message when an extract is requested of a Wikipedia page that doesn't exist" + "wikipediaextracts-invalid-language": "Error message when given an invalid language code", "wikipediaextracts-credits": "Text after the extract, crediting Wikipedia" } -- To view, visit https://gerrit.wikimedia.org/r/316488 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Icaf0a20dc74b1fd179bc5e77d9696e2e6507959d Gerrit-PatchSet: 2 Gerrit-Project: mediawiki/extensions/WikipediaExtracts Gerrit-Branch: master Gerrit-Owner: Sophivorus <scheno...@gmail.com> Gerrit-Reviewer: Legoktm <legoktm.wikipe...@gmail.com> Gerrit-Reviewer: Siebrand <siebr...@kitano.nl> Gerrit-Reviewer: Sophivorus <scheno...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits