MediaWiki  REL1_19
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 
00068         public static function isValidISBN( $isbn ) {
00069                 $isbn = self::cleanIsbn( $isbn );
00070                 $sum = 0;
00071                 if( strlen( $isbn ) == 13 ) {
00072                         for( $i = 0; $i < 12; $i++ ) {
00073                                 if($i % 2 == 0) {
00074                                         $sum += $isbn[$i];
00075                                 } else {
00076                                         $sum += 3 * $isbn[$i];
00077                                 }
00078                         }
00079 
00080                         $check = (10 - ($sum % 10)) % 10;
00081                         if ($check == $isbn[12]) {
00082                                 return true;
00083                         }
00084                 } elseif( strlen( $isbn ) == 10 ) {
00085                         for($i = 0; $i < 9; $i++) {
00086                                 $sum += $isbn[$i] * ($i + 1);
00087                         }
00088 
00089                         $check = $sum % 11;
00090                         if($check == 10) {
00091                                 $check = "X";
00092                         }
00093                         if($check == $isbn[9]) {
00094                                 return true;
00095                         }
00096                 }
00097                 return false;
00098         }
00099 
00106         private static function cleanIsbn( $isbn ) {
00107                 return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
00108         }
00109 
00115         private function makeForm() {
00116                 global $wgScript;
00117 
00118                 $form  = '<fieldset><legend>' . $this->msg( 'booksources-search-legend' )->escaped() . '</legend>';
00119                 $form .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
00120                 $form .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() );
00121                 $form .= '<p>' . Xml::inputLabel( $this->msg( 'booksources-isbn' )->text(), 'isbn', 'isbn', 20, $this->isbn );
00122                 $form .= '&#160;' . Xml::submitButton( $this->msg( 'booksources-go' )->text() ) . '</p>';
00123                 $form .= Xml::closeElement( 'form' );
00124                 $form .= '</fieldset>';
00125                 return $form;
00126         }
00127 
00134         private function showList() {
00135                 global $wgContLang;
00136 
00137                 # Hook to allow extensions to insert additional HTML,
00138                 # e.g. for API-interacting plugins and so on
00139                 wfRunHooks( 'BookInformation', array( $this->isbn, $this->getOutput() ) );
00140 
00141                 # Check for a local page such as Project:Book_sources and use that if available
00142                 $page = $this->msg( 'booksources' )->inContentLanguage()->text();
00143                 $title = Title::makeTitleSafe( NS_PROJECT, $page ); # Show list in content language
00144                 if( is_object( $title ) && $title->exists() ) {
00145                         $rev = Revision::newFromTitle( $title );
00146                         $this->getOutput()->addWikiText( str_replace( 'MAGICNUMBER', $this->isbn, $rev->getText() ) );
00147                         return true;
00148                 }
00149 
00150                 # Fall back to the defaults given in the language file
00151                 $this->getOutput()->addWikiMsg( 'booksources-text' );
00152                 $this->getOutput()->addHTML( '<ul>' );
00153                 $items = $wgContLang->getBookstoreList();
00154                 foreach( $items as $label => $url )
00155                         $this->getOutput()->addHTML( $this->makeListItem( $label, $url ) );
00156                 $this->getOutput()->addHTML( '</ul>' );
00157                 return true;
00158         }
00159 
00167         private function makeListItem( $label, $url ) {
00168                 $url = str_replace( '$1', $this->isbn, $url );
00169                 return '<li><a href="' . htmlspecialchars( $url ) . '" class="external">' . htmlspecialchars( $label ) . '</a></li>';
00170         }
00171 }