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