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