SpecialBookSources.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * Implements Special:Booksources
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup SpecialPage
  22. */
  23. use MediaWiki\MediaWikiServices;
  24. /**
  25. * Special page outputs information on sourcing a book with a particular ISBN
  26. * The parser creates links to this page when dealing with ISBNs in wikitext
  27. *
  28. * @author Rob Church <robchur@gmail.com>
  29. * @ingroup SpecialPage
  30. */
  31. class SpecialBookSources extends SpecialPage {
  32. public function __construct() {
  33. parent::__construct( 'Booksources' );
  34. }
  35. /**
  36. * @param string|null $isbn ISBN passed as a subpage parameter
  37. */
  38. public function execute( $isbn ) {
  39. $out = $this->getOutput();
  40. $this->setHeaders();
  41. $this->outputHeader();
  42. // User provided ISBN
  43. $isbn = $isbn ?: $this->getRequest()->getText( 'isbn' );
  44. $isbn = trim( $isbn );
  45. $this->buildForm( $isbn );
  46. if ( $isbn !== '' ) {
  47. if ( !self::isValidISBN( $isbn ) ) {
  48. $out->wrapWikiMsg(
  49. "<div class=\"error\">\n$1\n</div>",
  50. 'booksources-invalid-isbn'
  51. );
  52. }
  53. $this->showList( $isbn );
  54. }
  55. }
  56. /**
  57. * Return whether a given ISBN (10 or 13) is valid.
  58. *
  59. * @param string $isbn ISBN passed for check
  60. * @return bool
  61. */
  62. public static function isValidISBN( $isbn ) {
  63. $isbn = self::cleanIsbn( $isbn );
  64. $sum = 0;
  65. if ( strlen( $isbn ) == 13 ) {
  66. for ( $i = 0; $i < 12; $i++ ) {
  67. if ( $isbn[$i] === 'X' ) {
  68. return false;
  69. } elseif ( $i % 2 == 0 ) {
  70. $sum += $isbn[$i];
  71. } else {
  72. $sum += 3 * $isbn[$i];
  73. }
  74. }
  75. $check = ( 10 - ( $sum % 10 ) ) % 10;
  76. if ( (string)$check === $isbn[12] ) {
  77. return true;
  78. }
  79. } elseif ( strlen( $isbn ) == 10 ) {
  80. for ( $i = 0; $i < 9; $i++ ) {
  81. if ( $isbn[$i] === 'X' ) {
  82. return false;
  83. }
  84. $sum += $isbn[$i] * ( $i + 1 );
  85. }
  86. $check = $sum % 11;
  87. if ( $check == 10 ) {
  88. $check = "X";
  89. }
  90. if ( (string)$check === $isbn[9] ) {
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96. /**
  97. * Trim ISBN and remove characters which aren't required
  98. *
  99. * @param string $isbn Unclean ISBN
  100. * @return string
  101. */
  102. private static function cleanIsbn( $isbn ) {
  103. return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
  104. }
  105. /**
  106. * Generate a form to allow users to enter an ISBN
  107. *
  108. * @param string $isbn
  109. */
  110. private function buildForm( $isbn ) {
  111. $formDescriptor = [
  112. 'isbn' => [
  113. 'type' => 'text',
  114. 'name' => 'isbn',
  115. 'label-message' => 'booksources-isbn',
  116. 'default' => $isbn,
  117. 'autofocus' => true,
  118. 'required' => true,
  119. ],
  120. ];
  121. $context = new DerivativeContext( $this->getContext() );
  122. $context->setTitle( $this->getPageTitle() );
  123. HTMLForm::factory( 'ooui', $formDescriptor, $context )
  124. ->setWrapperLegendMsg( 'booksources-search-legend' )
  125. ->setSubmitTextMsg( 'booksources-search' )
  126. ->setMethod( 'get' )
  127. ->prepareForm()
  128. ->displayForm( false );
  129. }
  130. /**
  131. * Determine where to get the list of book sources from,
  132. * format and output them
  133. *
  134. * @param string $isbn
  135. * @throws MWException
  136. * @return bool
  137. */
  138. private function showList( $isbn ) {
  139. $out = $this->getOutput();
  140. $isbn = self::cleanIsbn( $isbn );
  141. # Hook to allow extensions to insert additional HTML,
  142. # e.g. for API-interacting plugins and so on
  143. Hooks::run( 'BookInformation', [ $isbn, $out ] );
  144. # Check for a local page such as Project:Book_sources and use that if available
  145. $page = $this->msg( 'booksources' )->inContentLanguage()->text();
  146. $title = Title::makeTitleSafe( NS_PROJECT, $page ); # Show list in content language
  147. if ( is_object( $title ) && $title->exists() ) {
  148. $rev = Revision::newFromTitle( $title, false, Revision::READ_NORMAL );
  149. $content = $rev->getContent();
  150. if ( $content instanceof TextContent ) {
  151. // XXX: in the future, this could be stored as structured data, defining a list of book sources
  152. $text = $content->getText();
  153. $out->addWikiTextAsInterface( str_replace( 'MAGICNUMBER', $isbn, $text ) );
  154. return true;
  155. } else {
  156. throw new MWException( "Unexpected content type for book sources: " . $content->getModel() );
  157. }
  158. }
  159. # Fall back to the defaults given in the language file
  160. $out->addWikiMsg( 'booksources-text' );
  161. $out->addHTML( '<ul>' );
  162. $items = MediaWikiServices::getInstance()->getContentLanguage()->getBookstoreList();
  163. foreach ( $items as $label => $url ) {
  164. $out->addHTML( $this->makeListItem( $isbn, $label, $url ) );
  165. }
  166. $out->addHTML( '</ul>' );
  167. return true;
  168. }
  169. /**
  170. * Format a book source list item
  171. *
  172. * @param string $isbn
  173. * @param string $label Book source label
  174. * @param string $url Book source URL
  175. * @return string
  176. */
  177. private function makeListItem( $isbn, $label, $url ) {
  178. $url = str_replace( '$1', $isbn, $url );
  179. return Html::rawElement( 'li', [],
  180. Html::element( 'a', [ 'href' => $url, 'class' => 'external' ], $label )
  181. );
  182. }
  183. protected function getGroupName() {
  184. return 'wiki';
  185. }
  186. }