MediaWiki  REL1_20
SpecialBooksources.php
Go to the documentation of this file.
00001 <?php
00032 class SpecialBookSources extends SpecialPage {
00033 
00037         private $isbn = '';
00038 
00042         public function __construct() {
00043                 parent::__construct( 'Booksources' );
00044         }
00045 
00051         public function execute( $isbn ) {
00052                 $this->setHeaders();
00053                 $this->outputHeader();
00054                 $this->isbn = self::cleanIsbn( $isbn ? $isbn : $this->getRequest()->getText( 'isbn' ) );
00055                 $this->getOutput()->addHTML( $this->makeForm() );
00056                 if( strlen( $this->isbn ) > 0 ) {
00057                         if( !self::isValidISBN( $this->isbn ) ) {
00058                                 $this->getOutput()->wrapWikiMsg( "<div class=\"error\">\n$1\n</div>", 'booksources-invalid-isbn' );
00059                         }
00060                         $this->showList();
00061                 }
00062         }
00063 
00069         public static function isValidISBN( $isbn ) {
00070                 $isbn = self::cleanIsbn( $isbn );
00071                 $sum = 0;
00072                 if( strlen( $isbn ) == 13 ) {
00073                         for( $i = 0; $i < 12; $i++ ) {
00074                                 if($i % 2 == 0) {
00075                                         $sum += $isbn[$i];
00076                                 } else {
00077                                         $sum += 3 * $isbn[$i];
00078                                 }
00079                         }
00080 
00081                         $check = (10 - ($sum % 10)) % 10;
00082                         if ($check == $isbn[12]) {
00083                                 return true;
00084                         }
00085                 } elseif( strlen( $isbn ) == 10 ) {
00086                         for($i = 0; $i < 9; $i++) {
00087                                 $sum += $isbn[$i] * ($i + 1);
00088                         }
00089 
00090                         $check = $sum % 11;
00091                         if($check == 10) {
00092                                 $check = "X";
00093                         }
00094                         if($check == $isbn[9]) {
00095                                 return true;
00096                         }
00097                 }
00098                 return false;
00099         }
00100 
00107         private static function cleanIsbn( $isbn ) {
00108                 return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
00109         }
00110 
00116         private function makeForm() {
00117                 global $wgScript;
00118 
00119                 $form  = '<fieldset><legend>' . $this->msg( 'booksources-search-legend' )->escaped() . '</legend>';
00120                 $form .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
00121                 $form .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() );
00122                 $form .= '<p>' . Xml::inputLabel( $this->msg( 'booksources-isbn' )->text(), 'isbn', 'isbn', 20, $this->isbn );
00123                 $form .= '&#160;' . Xml::submitButton( $this->msg( 'booksources-go' )->text() ) . '</p>';
00124                 $form .= Xml::closeElement( 'form' );
00125                 $form .= '</fieldset>';
00126                 return $form;
00127         }
00128 
00135         private function showList() {
00136                 global $wgContLang;
00137 
00138                 # Hook to allow extensions to insert additional HTML,
00139                 # e.g. for API-interacting plugins and so on
00140                 wfRunHooks( 'BookInformation', array( $this->isbn, $this->getOutput() ) );
00141 
00142                 # Check for a local page such as Project:Book_sources and use that if available
00143                 $page = $this->msg( 'booksources' )->inContentLanguage()->text();
00144                 $title = Title::makeTitleSafe( NS_PROJECT, $page ); # Show list in content language
00145                 if( is_object( $title ) && $title->exists() ) {
00146                         $rev = Revision::newFromTitle( $title, false, Revision::READ_NORMAL );
00147                         $this->getOutput()->addWikiText( str_replace( 'MAGICNUMBER', $this->isbn, $rev->getText() ) );
00148                         return true;
00149                 }
00150 
00151                 # Fall back to the defaults given in the language file
00152                 $this->getOutput()->addWikiMsg( 'booksources-text' );
00153                 $this->getOutput()->addHTML( '<ul>' );
00154                 $items = $wgContLang->getBookstoreList();
00155                 foreach( $items as $label => $url )
00156                         $this->getOutput()->addHTML( $this->makeListItem( $label, $url ) );
00157                 $this->getOutput()->addHTML( '</ul>' );
00158                 return true;
00159         }
00160 
00168         private function makeListItem( $label, $url ) {
00169                 $url = str_replace( '$1', $this->isbn, $url );
00170                 return '<li><a href="' . htmlspecialchars( $url ) . '" class="external">' . htmlspecialchars( $label ) . '</a></li>';
00171         }
00172 }