* (bug 2391) A warning is now shown for invalid ISBN numbers on Special:Booksources.
authorBrion Vibber <brion@users.mediawiki.org>
Fri, 12 Dec 2008 23:59:15 +0000 (23:59 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Fri, 12 Dec 2008 23:59:15 +0000 (23:59 +0000)
This could be called from the parser as well, but I'm a little hesitant just yet.
Parser tests currently contain some bogus ISBN numbers which need to be replaced with
legit ones... there's also the possibility that some books really are published with
bad ISBNs... on the other hand, Amazon won't let you search for them. :)
Plus the feedback as you're hitting preview might be nice... but it's also not clear
what's wrong -- that is, that it's an invalid number rather than that you just didn't
do something right. Do we want better feedback?

RELEASE-NOTES
includes/specials/SpecialBooksources.php
languages/messages/MessagesEn.php

index 6c1cf8a..577916c 100644 (file)
@@ -220,6 +220,7 @@ The following extensions are migrated into MediaWiki 1.14:
   algorithm very similar to IE's content detection algorithm.
 * Cascading protection no longer requires that both edit and move are restricted
   to sysop, just edit=sysop is enough
+* (bug 2391) A warning is now shown for invalid ISBN numbers on Special:Booksources.
 
 === Bug fixes in 1.14 ===
 
index ba91289..12b119d 100644 (file)
@@ -30,20 +30,62 @@ class SpecialBookSources extends SpecialPage {
        public function execute( $isbn ) {
                global $wgOut, $wgRequest;
                $this->setHeaders();
-               $this->isbn = $this->cleanIsbn( $isbn ? $isbn : $wgRequest->getText( 'isbn' ) );
+               $this->isbn = self::cleanIsbn( $isbn ? $isbn : $wgRequest->getText( 'isbn' ) );
                $wgOut->addWikiMsg( 'booksources-summary' );
                $wgOut->addHTML( $this->makeForm() );
-               if( strlen( $this->isbn ) > 0 )
+               if( strlen( $this->isbn ) > 0 ) {
+                       if( !$this->isValidIsbn( $this->isbn ) ) {
+                               $wgOut->wrapWikiMsg( '<div class="error">$1</div>', 'booksources-invalid-isbn' );
+                       }
                        $this->showList();
+               }
        }
 
+       /**
+        * Returns whether a given ISBN (10 or 13) is valid.  True indicates validity.
+        * @param isbn ISBN passed for check
+        */
+       public static function isValidISBN( $isbn ) {
+               $isbn = self::cleanIsbn( $isbn );
+               $sum = 0;
+               $check = -1;
+               if( strlen( $isbn ) == 13 ) {
+                       for( $i = 0; $i < 12; $i++ ) {
+                               if($i % 2 == 0) {
+                                       $sum += $isbn{$i};
+                               } else {
+                                       $sum += 3 * $isbn{$i};
+                               }
+                       }
+               
+                       $check = (10 - ($sum % 10)) % 10;
+                       if ($check == $isbn{12}) {
+                               return true;
+                       }
+               } elseif( strlen( $isbn ) == 10 ) {
+                       for($i = 0; $i < 9; $i++) {
+                               $sum += $isbn{$i} * ($i + 1);
+                       }
+               
+                       $check = $sum % 11;
+                       if($check == 10) {
+                               $check = "X";
+                       }
+                       if($check == $isbn{9}) {
+                               return true;
+                       }
+               }
+               return false;
+       }
+
+
        /**
         * Trim ISBN and remove characters which aren't required
         *
         * @param $isbn Unclean ISBN
         * @return string
         */
-       private function cleanIsbn( $isbn ) {
+       private static function cleanIsbn( $isbn ) {
                return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
        }
 
index 2ee6f5d..a77fdf2 100644 (file)
@@ -2147,6 +2147,7 @@ Each row contains links to the first and second redirect, as well as the target
 'booksources-isbn'          => 'ISBN:', # only translate this message to other languages if you have to change it
 'booksources-go'            => 'Go',
 'booksources-text'          => 'Below is a list of links to other sites that sell new and used books, and may also have further information about books you are looking for:',
+'booksources-invalid-isbn'  => 'The given ISBN number does not appear to be valid; check for errors copying from the original source.',
 
 # Magic words
 'rfcurl'    => 'http://tools.ietf.org/html/rfc$1', # do not translate or duplicate this message to other languages