00001 <?php 00002 00011 class SpecialBookSources extends SpecialPage { 00012 00016 private $isbn = ''; 00017 00021 public function __construct() { 00022 parent::__construct( 'Booksources' ); 00023 } 00024 00030 public function execute( $isbn ) { 00031 global $wgOut, $wgRequest; 00032 $this->setHeaders(); 00033 $this->isbn = self::cleanIsbn( $isbn ? $isbn : $wgRequest->getText( 'isbn' ) ); 00034 $wgOut->addWikiMsg( 'booksources-summary' ); 00035 $wgOut->addHTML( $this->makeForm() ); 00036 if( strlen( $this->isbn ) > 0 ) { 00037 if( !self::isValidISBN( $this->isbn ) ) { 00038 $wgOut->wrapWikiMsg( '<div class="error">$1</div>', 'booksources-invalid-isbn' ); 00039 } 00040 $this->showList(); 00041 } 00042 } 00043 00048 public static function isValidISBN( $isbn ) { 00049 $isbn = self::cleanIsbn( $isbn ); 00050 $sum = 0; 00051 $check = -1; 00052 if( strlen( $isbn ) == 13 ) { 00053 for( $i = 0; $i < 12; $i++ ) { 00054 if($i % 2 == 0) { 00055 $sum += $isbn{$i}; 00056 } else { 00057 $sum += 3 * $isbn{$i}; 00058 } 00059 } 00060 00061 $check = (10 - ($sum % 10)) % 10; 00062 if ($check == $isbn{12}) { 00063 return true; 00064 } 00065 } elseif( strlen( $isbn ) == 10 ) { 00066 for($i = 0; $i < 9; $i++) { 00067 $sum += $isbn{$i} * ($i + 1); 00068 } 00069 00070 $check = $sum % 11; 00071 if($check == 10) { 00072 $check = "X"; 00073 } 00074 if($check == $isbn{9}) { 00075 return true; 00076 } 00077 } 00078 return false; 00079 } 00080 00081 00088 private static function cleanIsbn( $isbn ) { 00089 return trim( preg_replace( '![^0-9X]!', '', $isbn ) ); 00090 } 00091 00097 private function makeForm() { 00098 global $wgScript; 00099 $title = self::getTitleFor( 'Booksources' ); 00100 $form = '<fieldset><legend>' . wfMsgHtml( 'booksources-search-legend' ) . '</legend>'; 00101 $form .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ); 00102 $form .= Xml::hidden( 'title', $title->getPrefixedText() ); 00103 $form .= '<p>' . Xml::inputLabel( wfMsg( 'booksources-isbn' ), 'isbn', 'isbn', 20, $this->isbn ); 00104 $form .= ' ' . Xml::submitButton( wfMsg( 'booksources-go' ) ) . '</p>'; 00105 $form .= Xml::closeElement( 'form' ); 00106 $form .= '</fieldset>'; 00107 return $form; 00108 } 00109 00116 private function showList() { 00117 global $wgOut, $wgContLang; 00118 00119 # Hook to allow extensions to insert additional HTML, 00120 # e.g. for API-interacting plugins and so on 00121 wfRunHooks( 'BookInformation', array( $this->isbn, &$wgOut ) ); 00122 00123 # Check for a local page such as Project:Book_sources and use that if available 00124 $title = Title::makeTitleSafe( NS_PROJECT, wfMsgForContent( 'booksources' ) ); # Show list in content language 00125 if( is_object( $title ) && $title->exists() ) { 00126 $rev = Revision::newFromTitle( $title ); 00127 $wgOut->addWikiText( str_replace( 'MAGICNUMBER', $this->isbn, $rev->getText() ) ); 00128 return true; 00129 } 00130 00131 # Fall back to the defaults given in the language file 00132 $wgOut->addWikiMsg( 'booksources-text' ); 00133 $wgOut->addHTML( '<ul>' ); 00134 $items = $wgContLang->getBookstoreList(); 00135 foreach( $items as $label => $url ) 00136 $wgOut->addHTML( $this->makeListItem( $label, $url ) ); 00137 $wgOut->addHTML( '</ul>' ); 00138 return true; 00139 } 00140 00148 private function makeListItem( $label, $url ) { 00149 $url = str_replace( '$1', $this->isbn, $url ); 00150 return '<li><a href="' . htmlspecialchars( $url ) . '">' . htmlspecialchars( $label ) . '</a></li>'; 00151 } 00152 }