0d7688316e4a62cf35fd29ee4d72beda36967a75
[lhc/web/wiklou.git] / includes / specials / SpecialBooksources.php
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
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 * @todo Validate ISBNs using the standard check-digit method
30 * @ingroup SpecialPage
31 */
32 class SpecialBookSources extends SpecialPage {
33
34 /**
35 * ISBN passed to the page, if any
36 */
37 private $isbn = '';
38
39 /**
40 * Constructor
41 */
42 public function __construct() {
43 parent::__construct( 'Booksources' );
44 }
45
46 /**
47 * Show the special page
48 *
49 * @param string $isbn ISBN passed as a subpage parameter
50 */
51 public function execute( $isbn ) {
52 $this->setHeaders();
53 $this->outputHeader();
54 $this->isbn = self::cleanIsbn( $isbn ? $isbn : $this->getRequest()->getText( 'isbn' ) );
55 $this->getOutput()->addHTML( $this->makeForm() );
56 if ( strlen( $this->isbn ) > 0 ) {
57 if ( !self::isValidISBN( $this->isbn ) ) {
58 $this->getOutput()->wrapWikiMsg( "<div class=\"error\">\n$1\n</div>", 'booksources-invalid-isbn' );
59 }
60 $this->showList();
61 }
62 }
63
64 /**
65 * Returns whether a given ISBN (10 or 13) is valid. True indicates validity.
66 * @param string $isbn ISBN passed for check
67 * @return bool
68 */
69 public static function isValidISBN( $isbn ) {
70 $isbn = self::cleanIsbn( $isbn );
71 $sum = 0;
72 if ( strlen( $isbn ) == 13 ) {
73 for ( $i = 0; $i < 12; $i++ ) {
74 if ( $i % 2 == 0 ) {
75 $sum += $isbn[$i];
76 } else {
77 $sum += 3 * $isbn[$i];
78 }
79 }
80
81 $check = ( 10 - ( $sum % 10 ) ) % 10;
82 if ( $check == $isbn[12] ) {
83 return true;
84 }
85 } elseif ( strlen( $isbn ) == 10 ) {
86 for ( $i = 0; $i < 9; $i++ ) {
87 $sum += $isbn[$i] * ( $i + 1 );
88 }
89
90 $check = $sum % 11;
91 if ( $check == 10 ) {
92 $check = "X";
93 }
94 if ( $check == $isbn[9] ) {
95 return true;
96 }
97 }
98
99 return false;
100 }
101
102 /**
103 * Trim ISBN and remove characters which aren't required
104 *
105 * @param string $isbn Unclean ISBN
106 * @return string
107 */
108 private static function cleanIsbn( $isbn ) {
109 return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
110 }
111
112 /**
113 * Generate a form to allow users to enter an ISBN
114 *
115 * @return string
116 */
117 private function makeForm() {
118 global $wgScript;
119
120 $form = Html::openElement( 'fieldset' ) . "\n";
121 $form .= Html::element( 'legend', array(), $this->msg( 'booksources-search-legend' )->text() ) . "\n";
122 $form .= Html::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) . "\n";
123 $form .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
124 $form .= '<p>' . Xml::inputLabel( $this->msg( 'booksources-isbn' )->text(), 'isbn', 'isbn', 20, $this->isbn );
125 $form .= '&#160;' . Xml::submitButton( $this->msg( 'booksources-go' )->text() ) . "</p>\n";
126 $form .= Html::closeElement( 'form' ) . "\n";
127 $form .= Html::closeElement( 'fieldset' ) . "\n";
128
129 return $form;
130 }
131
132 /**
133 * Determine where to get the list of book sources from,
134 * format and output them
135 *
136 * @throws MWException
137 * @return string
138 */
139 private function showList() {
140 global $wgContLang;
141
142 # Hook to allow extensions to insert additional HTML,
143 # e.g. for API-interacting plugins and so on
144 wfRunHooks( 'BookInformation', array( $this->isbn, $this->getOutput() ) );
145
146 # Check for a local page such as Project:Book_sources and use that if available
147 $page = $this->msg( 'booksources' )->inContentLanguage()->text();
148 $title = Title::makeTitleSafe( NS_PROJECT, $page ); # Show list in content language
149 if ( is_object( $title ) && $title->exists() ) {
150 $rev = Revision::newFromTitle( $title, false, Revision::READ_NORMAL );
151 $content = $rev->getContent();
152
153 if ( $content instanceof TextContent ) {
154 //XXX: in the future, this could be stored as structured data, defining a list of book sources
155
156 $text = $content->getNativeData();
157 $this->getOutput()->addWikiText( str_replace( 'MAGICNUMBER', $this->isbn, $text ) );
158
159 return true;
160 } else {
161 throw new MWException( "Unexpected content type for book sources: " . $content->getModel() );
162 }
163 }
164
165 # Fall back to the defaults given in the language file
166 $this->getOutput()->addWikiMsg( 'booksources-text' );
167 $this->getOutput()->addHTML( '<ul>' );
168 $items = $wgContLang->getBookstoreList();
169 foreach ( $items as $label => $url ) {
170 $this->getOutput()->addHTML( $this->makeListItem( $label, $url ) );
171 }
172 $this->getOutput()->addHTML( '</ul>' );
173
174 return true;
175 }
176
177 /**
178 * Format a book source list item
179 *
180 * @param string $label Book source label
181 * @param string $url Book source URL
182 * @return string
183 */
184 private function makeListItem( $label, $url ) {
185 $url = str_replace( '$1', $this->isbn, $url );
186
187 return Html::rawElement( 'li', array(),
188 Html::element( 'a', array( 'href' => $url, 'class' => 'external' ), $label ) );
189 }
190
191 protected function getGroupName() {
192 return 'other';
193 }
194 }