SpecialBooksources.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Special page outputs information on sourcing a book with a particular ISBN
  4. * The parser creates links to this page when dealing with ISBNs in wikitext
  5. *
  6. * @author Rob Church <robchur@gmail.com>
  7. * @todo Validate ISBNs using the standard check-digit method
  8. * @ingroup SpecialPages
  9. */
  10. class SpecialBookSources extends SpecialPage {
  11. /**
  12. * ISBN passed to the page, if any
  13. */
  14. private $isbn = '';
  15. /**
  16. * Constructor
  17. */
  18. public function __construct() {
  19. parent::__construct( 'Booksources' );
  20. }
  21. /**
  22. * Show the special page
  23. *
  24. * @param $isbn ISBN passed as a subpage parameter
  25. */
  26. public function execute( $isbn ) {
  27. global $wgOut, $wgRequest;
  28. $this->setHeaders();
  29. $this->isbn = self::cleanIsbn( $isbn ? $isbn : $wgRequest->getText( 'isbn' ) );
  30. $wgOut->addWikiMsg( 'booksources-summary' );
  31. $wgOut->addHTML( $this->makeForm() );
  32. if( strlen( $this->isbn ) > 0 ) {
  33. if( !self::isValidISBN( $this->isbn ) ) {
  34. $wgOut->wrapWikiMsg( '<div class="error">$1</div>', 'booksources-invalid-isbn' );
  35. }
  36. $this->showList();
  37. }
  38. }
  39. /**
  40. * Returns whether a given ISBN (10 or 13) is valid. True indicates validity.
  41. * @param isbn ISBN passed for check
  42. */
  43. public static function isValidISBN( $isbn ) {
  44. $isbn = self::cleanIsbn( $isbn );
  45. $sum = 0;
  46. $check = -1;
  47. if( strlen( $isbn ) == 13 ) {
  48. for( $i = 0; $i < 12; $i++ ) {
  49. if($i % 2 == 0) {
  50. $sum += $isbn{$i};
  51. } else {
  52. $sum += 3 * $isbn{$i};
  53. }
  54. }
  55. $check = (10 - ($sum % 10)) % 10;
  56. if ($check == $isbn{12}) {
  57. return true;
  58. }
  59. } elseif( strlen( $isbn ) == 10 ) {
  60. for($i = 0; $i < 9; $i++) {
  61. $sum += $isbn{$i} * ($i + 1);
  62. }
  63. $check = $sum % 11;
  64. if($check == 10) {
  65. $check = "X";
  66. }
  67. if($check == $isbn{9}) {
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. /**
  74. * Trim ISBN and remove characters which aren't required
  75. *
  76. * @param $isbn Unclean ISBN
  77. * @return string
  78. */
  79. private static function cleanIsbn( $isbn ) {
  80. return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
  81. }
  82. /**
  83. * Generate a form to allow users to enter an ISBN
  84. *
  85. * @return string
  86. */
  87. private function makeForm() {
  88. global $wgScript;
  89. $title = self::getTitleFor( 'Booksources' );
  90. $form = '<fieldset><legend>' . wfMsgHtml( 'booksources-search-legend' ) . '</legend>';
  91. $form .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
  92. $form .= Xml::hidden( 'title', $title->getPrefixedText() );
  93. $form .= '<p>' . Xml::inputLabel( wfMsg( 'booksources-isbn' ), 'isbn', 'isbn', 20, $this->isbn );
  94. $form .= '&nbsp;' . Xml::submitButton( wfMsg( 'booksources-go' ) ) . '</p>';
  95. $form .= Xml::closeElement( 'form' );
  96. $form .= '</fieldset>';
  97. return $form;
  98. }
  99. /**
  100. * Determine where to get the list of book sources from,
  101. * format and output them
  102. *
  103. * @return string
  104. */
  105. private function showList() {
  106. global $wgOut, $wgContLang;
  107. # Hook to allow extensions to insert additional HTML,
  108. # e.g. for API-interacting plugins and so on
  109. wfRunHooks( 'BookInformation', array( $this->isbn, &$wgOut ) );
  110. # Check for a local page such as Project:Book_sources and use that if available
  111. $title = Title::makeTitleSafe( NS_PROJECT, wfMsgForContent( 'booksources' ) ); # Show list in content language
  112. if( is_object( $title ) && $title->exists() ) {
  113. $rev = Revision::newFromTitle( $title );
  114. $wgOut->addWikiText( str_replace( 'MAGICNUMBER', $this->isbn, $rev->getText() ) );
  115. return true;
  116. }
  117. # Fall back to the defaults given in the language file
  118. $wgOut->addWikiMsg( 'booksources-text' );
  119. $wgOut->addHTML( '<ul>' );
  120. $items = $wgContLang->getBookstoreList();
  121. foreach( $items as $label => $url )
  122. $wgOut->addHTML( $this->makeListItem( $label, $url ) );
  123. $wgOut->addHTML( '</ul>' );
  124. return true;
  125. }
  126. /**
  127. * Format a book source list item
  128. *
  129. * @param $label Book source label
  130. * @param $url Book source URL
  131. * @return string
  132. */
  133. private function makeListItem( $label, $url ) {
  134. $url = str_replace( '$1', $this->isbn, $url );
  135. return '<li><a href="' . htmlspecialchars( $url ) . '">' . htmlspecialchars( $label ) . '</a></li>';
  136. }
  137. }