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