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