MediaWiki
REL1_24
|
00001 <?php 00032 class SpecialBookSources extends SpecialPage { 00036 private $isbn = ''; 00037 00041 public function __construct() { 00042 parent::__construct( 'Booksources' ); 00043 } 00044 00050 public function execute( $isbn ) { 00051 $this->setHeaders(); 00052 $this->outputHeader(); 00053 $this->isbn = self::cleanIsbn( $isbn ? $isbn : $this->getRequest()->getText( 'isbn' ) ); 00054 $this->getOutput()->addHTML( $this->makeForm() ); 00055 if ( strlen( $this->isbn ) > 0 ) { 00056 if ( !self::isValidISBN( $this->isbn ) ) { 00057 $this->getOutput()->wrapWikiMsg( 00058 "<div class=\"error\">\n$1\n</div>", 00059 'booksources-invalid-isbn' 00060 ); 00061 } 00062 $this->showList(); 00063 } 00064 } 00065 00071 public static function isValidISBN( $isbn ) { 00072 $isbn = self::cleanIsbn( $isbn ); 00073 $sum = 0; 00074 if ( strlen( $isbn ) == 13 ) { 00075 for ( $i = 0; $i < 12; $i++ ) { 00076 if ( $i % 2 == 0 ) { 00077 $sum += $isbn[$i]; 00078 } else { 00079 $sum += 3 * $isbn[$i]; 00080 } 00081 } 00082 00083 $check = ( 10 - ( $sum % 10 ) ) % 10; 00084 if ( $check == $isbn[12] ) { 00085 return true; 00086 } 00087 } elseif ( strlen( $isbn ) == 10 ) { 00088 for ( $i = 0; $i < 9; $i++ ) { 00089 $sum += $isbn[$i] * ( $i + 1 ); 00090 } 00091 00092 $check = $sum % 11; 00093 if ( $check == 10 ) { 00094 $check = "X"; 00095 } 00096 if ( $check == $isbn[9] ) { 00097 return true; 00098 } 00099 } 00100 00101 return false; 00102 } 00103 00110 private static function cleanIsbn( $isbn ) { 00111 return trim( preg_replace( '![^0-9X]!', '', $isbn ) ); 00112 } 00113 00119 private function makeForm() { 00120 $form = Html::openElement( 'fieldset' ) . "\n"; 00121 $form .= Html::element( 00122 'legend', 00123 array(), 00124 $this->msg( 'booksources-search-legend' )->text() 00125 ) . "\n"; 00126 $form .= Html::openElement( 'form', array( 'method' => 'get', 'action' => wfScript() ) ) . "\n"; 00127 $form .= Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) . "\n"; 00128 $form .= '<p>' . Xml::inputLabel( 00129 $this->msg( 'booksources-isbn' )->text(), 00130 'isbn', 00131 'isbn', 00132 20, 00133 $this->isbn, 00134 array( 'autofocus' => true ) 00135 ); 00136 $form .= ' ' . Xml::submitButton( $this->msg( 'booksources-go' )->text() ) . "</p>\n"; 00137 $form .= Html::closeElement( 'form' ) . "\n"; 00138 $form .= Html::closeElement( 'fieldset' ) . "\n"; 00139 00140 return $form; 00141 } 00142 00150 private function showList() { 00151 global $wgContLang; 00152 00153 # Hook to allow extensions to insert additional HTML, 00154 # e.g. for API-interacting plugins and so on 00155 wfRunHooks( 'BookInformation', array( $this->isbn, $this->getOutput() ) ); 00156 00157 # Check for a local page such as Project:Book_sources and use that if available 00158 $page = $this->msg( 'booksources' )->inContentLanguage()->text(); 00159 $title = Title::makeTitleSafe( NS_PROJECT, $page ); # Show list in content language 00160 if ( is_object( $title ) && $title->exists() ) { 00161 $rev = Revision::newFromTitle( $title, false, Revision::READ_NORMAL ); 00162 $content = $rev->getContent(); 00163 00164 if ( $content instanceof TextContent ) { 00165 //XXX: in the future, this could be stored as structured data, defining a list of book sources 00166 00167 $text = $content->getNativeData(); 00168 $this->getOutput()->addWikiText( str_replace( 'MAGICNUMBER', $this->isbn, $text ) ); 00169 00170 return true; 00171 } else { 00172 throw new MWException( "Unexpected content type for book sources: " . $content->getModel() ); 00173 } 00174 } 00175 00176 # Fall back to the defaults given in the language file 00177 $this->getOutput()->addWikiMsg( 'booksources-text' ); 00178 $this->getOutput()->addHTML( '<ul>' ); 00179 $items = $wgContLang->getBookstoreList(); 00180 foreach ( $items as $label => $url ) { 00181 $this->getOutput()->addHTML( $this->makeListItem( $label, $url ) ); 00182 } 00183 $this->getOutput()->addHTML( '</ul>' ); 00184 00185 return true; 00186 } 00187 00195 private function makeListItem( $label, $url ) { 00196 $url = str_replace( '$1', $this->isbn, $url ); 00197 00198 return Html::rawElement( 'li', array(), 00199 Html::element( 'a', array( 'href' => $url, 'class' => 'external' ), $label ) ); 00200 } 00201 00202 protected function getGroupName() { 00203 return 'wiki'; 00204 } 00205 }