Legoktm has uploaded a new change for review.

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

Change subject: Add ISBN parser function
......................................................................

Add ISBN parser function

This is intended to ease linking to Special:Booksources as an
alternative to magic links.

{{isbn:....}} is basically equivalent to
[[Special:Booksources/...|ISBN ...]], but it will also validate the
ISBN, and allow for localization of the link text.

Bug: T148274
Change-Id: I12dfdf70e09f4847e68bc7d88683ea62c40b29a1
---
M includes/parser/CoreParserFunctions.php
M languages/i18n/en.json
M languages/i18n/qqq.json
M languages/messages/MessagesEn.php
M tests/parser/parserTests.txt
5 files changed, 43 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/63/319263/1

diff --git a/includes/parser/CoreParserFunctions.php 
b/includes/parser/CoreParserFunctions.php
index ef26db6..952e59d 100644
--- a/includes/parser/CoreParserFunctions.php
+++ b/includes/parser/CoreParserFunctions.php
@@ -54,7 +54,7 @@
                        'talkpagename', 'talkpagenamee', 'subjectpagename',
                        'subjectpagenamee', 'pageid', 'revisionid', 
'revisionday',
                        'revisionday2', 'revisionmonth', 'revisionmonth1', 
'revisionyear',
-                       'revisiontimestamp', 'revisionuser', 'cascadingsources',
+                       'revisiontimestamp', 'revisionuser', 
'cascadingsources', 'isbn',
                ];
                foreach ( $noHashFunctions as $func ) {
                        $parser->setFunctionHook( $func, [ __CLASS__, $func ], 
Parser::SFH_NO_HASH );
@@ -1334,4 +1334,30 @@
                return '';
        }
 
+       /**
+        * ISBN parser function to ease linking to Special:Booksources
+        *
+        * @param Parser $parser
+        * @param string $isbn
+        * @return array|string
+        */
+       public static function isbn( Parser $parser, $isbn = '' ) {
+               if ( !SpecialBookSources::isValidISBN( $isbn ) ) {
+                       $converter = 
$parser->getConverterLanguage()->getConverter();
+                       return '<span class="error">' .
+                               wfMessage( 'isbn-invalid' )
+                                       ->params( $converter->markNoConversion( 
wfEscapeWikiText( $isbn ) ) )
+                                       ->inContentLanguage()->text() .
+                       '</span>';
+               }
+
+               return [
+                       $parser->getLinkRenderer()->makeLink(
+                               SpecialPage::getTitleFor( 'Booksources', $isbn 
),
+                               wfMessage( 'isbn-linktext' )->params( $isbn 
)->text()
+                       ),
+                       'isHTML' => true
+               ];
+       }
+
 }
diff --git a/languages/i18n/en.json b/languages/i18n/en.json
index c759984..aeea50c 100644
--- a/languages/i18n/en.json
+++ b/languages/i18n/en.json
@@ -1937,6 +1937,8 @@
        "booksources-invalid-isbn": "The given ISBN does not appear to be 
valid; check for errors copying from the original source.",
        "rfcurl": "//tools.ietf.org/html/rfc$1",
        "pubmedurl": "//www.ncbi.nlm.nih.gov/pubmed/$1?dopt=Abstract",
+       "isbn-invalid": "<strong>Error:</strong> Invalid ISBN \"$1\".",
+       "isbn-linktext": "ISBN $1",
        "specialloguserlabel": "Performer:",
        "speciallogtitlelabel": "Target (title or {{ns:user}}:username for 
user):",
        "log": "Logs",
diff --git a/languages/i18n/qqq.json b/languages/i18n/qqq.json
index fe44a2b..5cdb52f 100644
--- a/languages/i18n/qqq.json
+++ b/languages/i18n/qqq.json
@@ -2121,6 +2121,8 @@
        "booksources-invalid-isbn": "This message is displayed after an invalid 
ISBN is entered on [[Special:Booksources]].",
        "rfcurl": "{{notranslate}}\nParameters:\n* $1 - RFC number\nSee 
also:\n* {{msg-mw|Pubmedurl}}",
        "pubmedurl": "{{notranslate}}\nParameters:\n* $1 - Pubmed number\nSee 
also:\n* {{msg-mw|Rfcurl}}",
+       "isbn-invalid": "Error message shown if the provided ISBN number is 
invalid. $1 is the user-provided ISBN number.",
+       "isbn-linktext": "Link text used when generating a link to 
Special:Booksources. ISBN refers to [[:w:International Standard Book Number]]. 
$1 is the ISBN identifier.",
        "specialloguserlabel": "Used in [[Special:Log]] as a label for an input 
field with which the log can be filtered for entries describing actions 
''performed'' by the specified user.  \"Carried out\" and \"done\" are possible 
alternatives for \"performed\".",
        "speciallogtitlelabel": "Used in [[Special:Log]] as a label for an 
input field with which the log can be filtered.  This filter selects for pages 
or users on which a log action was performed.",
        "log": "{{doc-special|Log}}\n{{Identical|Log}}",
diff --git a/languages/messages/MessagesEn.php 
b/languages/messages/MessagesEn.php
index 67369e2..83c5de3 100644
--- a/languages/messages/MessagesEn.php
+++ b/languages/messages/MessagesEn.php
@@ -381,6 +381,7 @@
        'pagesincategory_pages'   => [ 0, 'pages' ],
        'pagesincategory_subcats' => [ 0, 'subcats' ],
        'pagesincategory_files'   => [ 0, 'files' ],
+       'isbn'                    => [ 0, 'isbn' ],
 ];
 
 /**
diff --git a/tests/parser/parserTests.txt b/tests/parser/parserTests.txt
index 103acc6..1cf38dd 100644
--- a/tests/parser/parserTests.txt
+++ b/tests/parser/parserTests.txt
@@ -10570,6 +10570,17 @@
 </p>
 !! end
 
+!! test
+ISBN parser function
+!! wikitext
+{{isbn:1234}}
+{{isbn:0-7475-3269-9}}
+!! html/php
+<p><span class="error"><strong>Error:</strong> Invalid ISBN "1234".</span>
+<a href="/wiki/Special:BookSources/0-7475-3269-9" 
title="Special:BookSources/0-7475-3269-9">ISBN 0-7475-3269-9</a>
+</p>
+!! end
+
 ###
 ### Templates
 ####

-- 
To view, visit https://gerrit.wikimedia.org/r/319263
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I12dfdf70e09f4847e68bc7d88683ea62c40b29a1
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Legoktm <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to