]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - includes/specials/SpecialBooksources.php
MediaWiki 1.14.0
[autoinstallsdev/mediawiki.git] / includes / specials / SpecialBooksources.php
similarity index 68%
rename from includes/SpecialBooksources.php
rename to includes/specials/SpecialBooksources.php
index 5f103495498bb9f5d8000fb5c372b949309dfcd1..12b119d84d0bf95c41926236f477e0e77822caae 100644 (file)
@@ -4,9 +4,9 @@
  * Special page outputs information on sourcing a book with a particular ISBN
  * The parser creates links to this page when dealing with ISBNs in wikitext
  *
- * @addtogroup SpecialPage
  * @author Rob Church <robchur@gmail.com>
  * @todo Validate ISBNs using the standard check-digit method
+ * @ingroup SpecialPages
  */
 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' ) );
-               $wgOut->addWikiText( wfMsgNoTrans( 'booksources-summary' ) );
-               $wgOut->addHtml( $this->makeForm() );
-               if( strlen( $this->isbn ) > 0 )
+               $this->isbn = self::cleanIsbn( $isbn ? $isbn : $wgRequest->getText( 'isbn' ) );
+               $wgOut->addWikiMsg( 'booksources-summary' );
+               $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 ) );
        }
 
@@ -87,15 +129,15 @@ class SpecialBookSources extends SpecialPage {
                }
 
                # Fall back to the defaults given in the language file
-               $wgOut->addWikiText( wfMsgNoTrans( 'booksources-text' ) );
-               $wgOut->addHtml( '<ul>' );
+               $wgOut->addWikiMsg( 'booksources-text' );
+               $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;
        }
-       
+
        /**
         * Format a book source list item
         *
@@ -107,7 +149,4 @@ class SpecialBookSources extends SpecialPage {
                $url = str_replace( '$1', $this->isbn, $url );
                return '<li><a href="' . htmlspecialchars( $url ) . '">' . htmlspecialchars( $label ) . '</a></li>';
        }
-
 }
-
-