MediaWiki  master
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 
00099         return false;
00100     }
00101 
00108     private static function cleanIsbn( $isbn ) {
00109         return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
00110     }
00111 
00117     private function makeForm() {
00118         global $wgScript;
00119 
00120         $form = Html::openElement( 'fieldset' ) . "\n";
00121         $form .= Html::element( 'legend', array(), $this->msg( 'booksources-search-legend' )->text() ) . "\n";
00122         $form .= Html::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) . "\n";
00123         $form .= Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) . "\n";
00124         $form .= '<p>' . Xml::inputLabel( $this->msg( 'booksources-isbn' )->text(), 'isbn', 'isbn', 20, $this->isbn, array( 'autofocus' => true ) );
00125         $form .= '&#160;' . Xml::submitButton( $this->msg( 'booksources-go' )->text() ) . "</p>\n";
00126         $form .= Html::closeElement( 'form' ) . "\n";
00127         $form .= Html::closeElement( 'fieldset' ) . "\n";
00128 
00129         return $form;
00130     }
00131 
00139     private function showList() {
00140         global $wgContLang;
00141 
00142         # Hook to allow extensions to insert additional HTML,
00143         # e.g. for API-interacting plugins and so on
00144         wfRunHooks( 'BookInformation', array( $this->isbn, $this->getOutput() ) );
00145 
00146         # Check for a local page such as Project:Book_sources and use that if available
00147         $page = $this->msg( 'booksources' )->inContentLanguage()->text();
00148         $title = Title::makeTitleSafe( NS_PROJECT, $page ); # Show list in content language
00149         if ( is_object( $title ) && $title->exists() ) {
00150             $rev = Revision::newFromTitle( $title, false, Revision::READ_NORMAL );
00151             $content = $rev->getContent();
00152 
00153             if ( $content instanceof TextContent ) {
00154                 //XXX: in the future, this could be stored as structured data, defining a list of book sources
00155 
00156                 $text = $content->getNativeData();
00157                 $this->getOutput()->addWikiText( str_replace( 'MAGICNUMBER', $this->isbn, $text ) );
00158 
00159                 return true;
00160             } else {
00161                 throw new MWException( "Unexpected content type for book sources: " . $content->getModel() );
00162             }
00163         }
00164 
00165         # Fall back to the defaults given in the language file
00166         $this->getOutput()->addWikiMsg( 'booksources-text' );
00167         $this->getOutput()->addHTML( '<ul>' );
00168         $items = $wgContLang->getBookstoreList();
00169         foreach ( $items as $label => $url ) {
00170             $this->getOutput()->addHTML( $this->makeListItem( $label, $url ) );
00171         }
00172         $this->getOutput()->addHTML( '</ul>' );
00173 
00174         return true;
00175     }
00176 
00184     private function makeListItem( $label, $url ) {
00185         $url = str_replace( '$1', $this->isbn, $url );
00186 
00187         return Html::rawElement( 'li', array(),
00188             Html::element( 'a', array( 'href' => $url, 'class' => 'external' ), $label ) );
00189     }
00190 
00191     protected function getGroupName() {
00192         return 'wiki';
00193     }
00194 }