Remove most named character references from output
[lhc/web/wiklou.git] / includes / specials / SpecialBooksources.php
index ba91289..7733a0d 100644 (file)
@@ -6,7 +6,7 @@
  *
  * @author Rob Church <robchur@gmail.com>
  * @todo Validate ISBNs using the standard check-digit method
- * @ingroup SpecialPages
+ * @ingroup SpecialPage
  */
 class SpecialBookSources extends SpecialPage {
 
@@ -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( !self::isValidISBN( $this->isbn ) ) {
+                               $wgOut->wrapWikiMsg( "<div class=\"error\">\n$1\n</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 ) );
        }
 
@@ -59,7 +101,7 @@ class SpecialBookSources extends SpecialPage {
                $form .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
                $form .= Xml::hidden( 'title', $title->getPrefixedText() );
                $form .= '<p>' . Xml::inputLabel( wfMsg( 'booksources-isbn' ), 'isbn', 'isbn', 20, $this->isbn );
-               $form .= '&nbsp;' . Xml::submitButton( wfMsg( 'booksources-go' ) ) . '</p>';
+               $form .= '&#160;' . Xml::submitButton( wfMsg( 'booksources-go' ) ) . '</p>';
                $form .= Xml::closeElement( 'form' );
                $form .= '</fieldset>';
                return $form;