Sophivorus has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/316488

Change subject: Add language validation
......................................................................

Add language validation

- Also smarter language guess
- Also link to Wikipedia after every extract

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, 28 insertions(+), 0 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikipediaExtracts 
refs/changes/88/316488/1

diff --git a/WikipediaExtracts.php b/WikipediaExtracts.php
index b8db1ce..5696dec 100644
--- a/WikipediaExtracts.php
+++ b/WikipediaExtracts.php
@@ -25,12 +25,23 @@
                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 ( !is_string( $language ) or strlen( $language ) > 3 ) {
+                       return '<span class="error">' . wfMessage( 
'wikipediaextracts-invalid-language', $language ) . '</span>';
                }
 
                // Query the Wikipedia API
@@ -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 )->plain();
                        return $extract;
                }
        }
@@ -80,14 +93,25 @@
                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 ( !is_string( $language ) or strlen( $language ) > 3 ) {
+                       return '<span class="error">' . wfMessage( 
'wikipediaextracts-invalid-language', $language ) . '</span>';
+               }
+
                // Query the Wikipedia API
                $data = array(
                        'action' => 'query',
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: newchange
Gerrit-Change-Id: Icaf0a20dc74b1fd179bc5e77d9696e2e6507959d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikipediaExtracts
Gerrit-Branch: master
Gerrit-Owner: Sophivorus <scheno...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to