3 * Implements Special:Booksources
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.
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.
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
21 * @ingroup SpecialPage
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
28 * @author Rob Church <robchur@gmail.com>
29 * @ingroup SpecialPage
31 class SpecialBookSources
extends SpecialPage
{
33 * ISBN passed to the page, if any
37 public function __construct() {
38 parent
::__construct( 'Booksources' );
42 * Show the special page
44 * @param string $isbn ISBN passed as a subpage parameter
46 public function execute( $isbn ) {
47 $out = $this->getOutput();
50 $this->outputHeader();
52 $this->isbn
= self
::cleanIsbn( $isbn ?
: $this->getRequest()->getText( 'isbn' ) );
56 if ( $this->isbn
!== '' ) {
57 if ( !self
::isValidISBN( $this->isbn
) ) {
59 "<div class=\"error\">\n$1\n</div>",
60 'booksources-invalid-isbn'
69 * Return whether a given ISBN (10 or 13) is valid.
71 * @param string $isbn ISBN passed for check
74 public static function isValidISBN( $isbn ) {
75 $isbn = self
::cleanIsbn( $isbn );
77 if ( strlen( $isbn ) == 13 ) {
78 for ( $i = 0; $i < 12; $i++
) {
79 if ( $isbn[$i] === 'X' ) {
81 } elseif ( $i %
2 == 0 ) {
84 $sum +
= 3 * $isbn[$i];
88 $check = ( 10 - ( $sum %
10 ) ) %
10;
89 if ( (string)$check === $isbn[12] ) {
92 } elseif ( strlen( $isbn ) == 10 ) {
93 for ( $i = 0; $i < 9; $i++
) {
94 if ( $isbn[$i] === 'X' ) {
97 $sum +
= $isbn[$i] * ( $i +
1 );
101 if ( $check == 10 ) {
104 if ( (string)$check === $isbn[9] ) {
113 * Trim ISBN and remove characters which aren't required
115 * @param string $isbn Unclean ISBN
118 private static function cleanIsbn( $isbn ) {
119 return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
123 * Generate a form to allow users to enter an ISBN
125 private function buildForm() {
130 'label-message' => 'booksources-isbn',
131 'default' => $this->isbn
,
137 $htmlForm = HTMLForm
::factory( 'ooui', $formDescriptor, $this->getContext() )
138 ->setWrapperLegendMsg( 'booksources-search-legend' )
139 ->setSubmitTextMsg( 'booksources-search' )
142 ->displayForm( false );
146 * Determine where to get the list of book sources from,
147 * format and output them
149 * @throws MWException
152 private function showList() {
153 $out = $this->getOutput();
157 # Hook to allow extensions to insert additional HTML,
158 # e.g. for API-interacting plugins and so on
159 Hooks
::run( 'BookInformation', [ $this->isbn
, $out ] );
161 # Check for a local page such as Project:Book_sources and use that if available
162 $page = $this->msg( 'booksources' )->inContentLanguage()->text();
163 $title = Title
::makeTitleSafe( NS_PROJECT
, $page ); # Show list in content language
164 if ( is_object( $title ) && $title->exists() ) {
165 $rev = Revision
::newFromTitle( $title, false, Revision
::READ_NORMAL
);
166 $content = $rev->getContent();
168 if ( $content instanceof TextContent
) {
169 // XXX: in the future, this could be stored as structured data, defining a list of book sources
171 $text = $content->getNativeData();
172 $out->addWikiText( str_replace( 'MAGICNUMBER', $this->isbn
, $text ) );
176 throw new MWException( "Unexpected content type for book sources: " . $content->getModel() );
180 # Fall back to the defaults given in the language file
181 $out->addWikiMsg( 'booksources-text' );
182 $out->addHTML( '<ul>' );
183 $items = $wgContLang->getBookstoreList();
184 foreach ( $items as $label => $url ) {
185 $out->addHTML( $this->makeListItem( $label, $url ) );
187 $out->addHTML( '</ul>' );
193 * Format a book source list item
195 * @param string $label Book source label
196 * @param string $url Book source URL
199 private function makeListItem( $label, $url ) {
200 $url = str_replace( '$1', $this->isbn
, $url );
202 return Html
::rawElement( 'li', [],
203 Html
::element( 'a', [ 'href' => $url, 'class' => 'external' ], $label )
207 protected function getGroupName() {