diff options
author | Pierre Schmitz <pierre@archlinux.de> | 2009-02-22 13:37:51 +0100 |
---|---|---|
committer | Pierre Schmitz <pierre@archlinux.de> | 2009-02-22 13:37:51 +0100 |
commit | b9b85843572bf283f48285001e276ba7e61b63f6 (patch) | |
tree | 4c6f4571552ada9ccfb4030481dcf77308f8b254 /includes/specials/SpecialBooksources.php | |
parent | d9a20acc4e789cca747ad360d87ee3f3e7aa58c1 (diff) |
updated to MediaWiki 1.14.0
Diffstat (limited to 'includes/specials/SpecialBooksources.php')
-rw-r--r-- | includes/specials/SpecialBooksources.php | 56 |
1 files changed, 49 insertions, 7 deletions
diff --git a/includes/specials/SpecialBooksources.php b/includes/specials/SpecialBooksources.php index 0690c5c0..12b119d8 100644 --- a/includes/specials/SpecialBooksources.php +++ b/includes/specials/SpecialBooksources.php @@ -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 ) + $wgOut->addHTML( $this->makeForm() ); + 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 ) ); } @@ -88,11 +130,11 @@ class SpecialBookSources extends SpecialPage { # Fall back to the defaults given in the language file $wgOut->addWikiMsg( 'booksources-text' ); - $wgOut->addHtml( '<ul>' ); + $wgOut->addHTML( '<ul>' ); $items = $wgContLang->getBookstoreList(); foreach( $items as $label => $url ) - $wgOut->addHtml( $this->makeListItem( $label, $url ) ); - $wgOut->addHtml( '</ul>' ); + $wgOut->addHTML( $this->makeListItem( $label, $url ) ); + $wgOut->addHTML( '</ul>' ); return true; } |