f29b580e184e458f2a163c6762fad2d0d2eba532
[lhc/web/wiklou.git] / includes / specials / SpecialBooksources.php
1 <?php
2 /**
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 */
19
20 /**
21 * Special page outputs information on sourcing a book with a particular ISBN
22 * The parser creates links to this page when dealing with ISBNs in wikitext
23 *
24 * @author Rob Church <robchur@gmail.com>
25 * @todo Validate ISBNs using the standard check-digit method
26 * @ingroup SpecialPage
27 */
28 class SpecialBookSources extends SpecialPage {
29
30 /**
31 * ISBN passed to the page, if any
32 */
33 private $isbn = '';
34
35 /**
36 * Constructor
37 */
38 public function __construct() {
39 parent::__construct( 'Booksources' );
40 }
41
42 /**
43 * Show the special page
44 *
45 * @param $isbn ISBN passed as a subpage parameter
46 */
47 public function execute( $isbn ) {
48 global $wgOut, $wgRequest;
49 $this->setHeaders();
50 $this->isbn = self::cleanIsbn( $isbn ? $isbn : $wgRequest->getText( 'isbn' ) );
51 $wgOut->addWikiMsg( 'booksources-summary' );
52 $wgOut->addHTML( $this->makeForm() );
53 if( strlen( $this->isbn ) > 0 ) {
54 if( !self::isValidISBN( $this->isbn ) ) {
55 $wgOut->wrapWikiMsg( "<div class=\"error\">\n$1\n</div>", 'booksources-invalid-isbn' );
56 }
57 $this->showList();
58 }
59 }
60
61 /**
62 * Returns whether a given ISBN (10 or 13) is valid. True indicates validity.
63 * @param isbn ISBN passed for check
64 */
65 public static function isValidISBN( $isbn ) {
66 $isbn = self::cleanIsbn( $isbn );
67 $sum = 0;
68 $check = -1;
69 if( strlen( $isbn ) == 13 ) {
70 for( $i = 0; $i < 12; $i++ ) {
71 if($i % 2 == 0) {
72 $sum += $isbn{$i};
73 } else {
74 $sum += 3 * $isbn{$i};
75 }
76 }
77
78 $check = (10 - ($sum % 10)) % 10;
79 if ($check == $isbn{12}) {
80 return true;
81 }
82 } elseif( strlen( $isbn ) == 10 ) {
83 for($i = 0; $i < 9; $i++) {
84 $sum += $isbn{$i} * ($i + 1);
85 }
86
87 $check = $sum % 11;
88 if($check == 10) {
89 $check = "X";
90 }
91 if($check == $isbn{9}) {
92 return true;
93 }
94 }
95 return false;
96 }
97
98
99 /**
100 * Trim ISBN and remove characters which aren't required
101 *
102 * @param $isbn Unclean ISBN
103 * @return string
104 */
105 private static function cleanIsbn( $isbn ) {
106 return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
107 }
108
109 /**
110 * Generate a form to allow users to enter an ISBN
111 *
112 * @return string
113 */
114 private function makeForm() {
115 global $wgScript;
116 $title = self::getTitleFor( 'Booksources' );
117 $form = '<fieldset><legend>' . wfMsgHtml( 'booksources-search-legend' ) . '</legend>';
118 $form .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
119 $form .= Xml::hidden( 'title', $title->getPrefixedText() );
120 $form .= '<p>' . Xml::inputLabel( wfMsg( 'booksources-isbn' ), 'isbn', 'isbn', 20, $this->isbn );
121 $form .= '&#160;' . Xml::submitButton( wfMsg( 'booksources-go' ) ) . '</p>';
122 $form .= Xml::closeElement( 'form' );
123 $form .= '</fieldset>';
124 return $form;
125 }
126
127 /**
128 * Determine where to get the list of book sources from,
129 * format and output them
130 *
131 * @return string
132 */
133 private function showList() {
134 global $wgOut, $wgContLang;
135
136 # Hook to allow extensions to insert additional HTML,
137 # e.g. for API-interacting plugins and so on
138 wfRunHooks( 'BookInformation', array( $this->isbn, &$wgOut ) );
139
140 # Check for a local page such as Project:Book_sources and use that if available
141 $title = Title::makeTitleSafe( NS_PROJECT, wfMsgForContent( 'booksources' ) ); # Show list in content language
142 if( is_object( $title ) && $title->exists() ) {
143 $rev = Revision::newFromTitle( $title );
144 $wgOut->addWikiText( str_replace( 'MAGICNUMBER', $this->isbn, $rev->getText() ) );
145 return true;
146 }
147
148 # Fall back to the defaults given in the language file
149 $wgOut->addWikiMsg( 'booksources-text' );
150 $wgOut->addHTML( '<ul>' );
151 $items = $wgContLang->getBookstoreList();
152 foreach( $items as $label => $url )
153 $wgOut->addHTML( $this->makeListItem( $label, $url ) );
154 $wgOut->addHTML( '</ul>' );
155 return true;
156 }
157
158 /**
159 * Format a book source list item
160 *
161 * @param $label Book source label
162 * @param $url Book source URL
163 * @return string
164 */
165 private function makeListItem( $label, $url ) {
166 $url = str_replace( '$1', $this->isbn, $url );
167 return '<li><a href="' . htmlspecialchars( $url ) . '" class="external">' . htmlspecialchars( $label ) . '</a></li>';
168 }
169 }