MediaWiki master
SpecialBookSources.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Specials;
8
16use UnexpectedValueException;
17
28
29 private RevisionLookup $revisionLookup;
30 private TitleFactory $titleFactory;
31
32 public function __construct(
33 RevisionLookup $revisionLookup,
34 TitleFactory $titleFactory
35 ) {
36 parent::__construct( 'Booksources' );
37 $this->revisionLookup = $revisionLookup;
38 $this->titleFactory = $titleFactory;
39 }
40
44 public function execute( $isbn ) {
45 $out = $this->getOutput();
46
47 $this->setHeaders();
48 $this->outputHeader();
49
50 // User provided ISBN
51 $isbn = $isbn ?: $this->getRequest()->getText( 'isbn' );
52 $isbn = trim( $isbn );
53
54 $this->buildForm( $isbn );
55
56 if ( $isbn !== '' ) {
57 if ( !self::isValidISBN( $isbn ) ) {
58 $out->wrapWikiMsg(
59 "<div class=\"error\">\n$1\n</div>",
60 'booksources-invalid-isbn'
61 );
62 }
63
64 $this->showList( $isbn );
65 }
66 }
67
74 public static function isValidISBN( $isbn ) {
75 $isbn = self::cleanIsbn( $isbn );
76 $sum = 0;
77 if ( strlen( $isbn ) == 13 ) {
78 for ( $i = 0; $i < 12; $i++ ) {
79 if ( $isbn[$i] === 'X' ) {
80 return false;
81 } elseif ( $i % 2 == 0 ) {
82 $sum += (int)$isbn[$i];
83 } else {
84 $sum += 3 * (int)$isbn[$i];
85 }
86 }
87
88 $check = ( 10 - ( $sum % 10 ) ) % 10;
89 if ( (string)$check === $isbn[12] ) {
90 return true;
91 }
92 } elseif ( strlen( $isbn ) == 10 ) {
93 for ( $i = 0; $i < 9; $i++ ) {
94 if ( $isbn[$i] === 'X' ) {
95 return false;
96 }
97 $sum += (int)$isbn[$i] * ( $i + 1 );
98 }
99
100 $check = $sum % 11;
101 if ( $check == 10 ) {
102 $check = "X";
103 }
104 if ( (string)$check === $isbn[9] ) {
105 return true;
106 }
107 }
108
109 return false;
110 }
111
118 private static function cleanIsbn( $isbn ) {
119 return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
120 }
121
127 private function buildForm( $isbn ) {
128 $formDescriptor = [
129 'isbn' => [
130 'type' => 'text',
131 'name' => 'isbn',
132 'label-message' => 'booksources-isbn',
133 'default' => $isbn,
134 'autofocus' => true,
135 'required' => true,
136 ],
137 ];
138
139 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
140 ->setTitle( $this->getPageTitle() )
141 ->setWrapperLegendMsg( 'booksources-search-legend' )
142 ->setSubmitTextMsg( 'booksources-search' )
143 ->setMethod( 'get' )
144 ->prepareForm()
145 ->displayForm( false );
146 }
147
155 private function showList( $isbn ) {
156 $out = $this->getOutput();
157
158 $isbn = self::cleanIsbn( $isbn );
159 # Hook to allow extensions to insert additional HTML,
160 # e.g. for API-interacting plugins and so on
161 $this->getHookRunner()->onBookInformation( $isbn, $out );
162
163 # Check for a local page such as Project:Book_sources and use that if available
164 $page = $this->msg( 'booksources' )->inContentLanguage()->text();
165 // Show list in content language
166 $title = $this->titleFactory->makeTitleSafe( NS_PROJECT, $page );
167 if ( is_object( $title ) && $title->exists() ) {
168 $rev = $this->revisionLookup->getRevisionByTitle( $title );
169 $content = $rev->getContent( SlotRecord::MAIN );
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->getText();
175 $out->addWikiTextAsInterface( str_replace( 'MAGICNUMBER', $isbn, $text ) );
176
177 return true;
178 } else {
179 throw new UnexpectedValueException(
180 "Unexpected content type for book sources: " . $content->getModel()
181 );
182 }
183 }
184
185 # Fall back to the defaults given in the language file
186 $out->addWikiMsg( 'booksources-text' );
187 $out->addHTML( '<ul>' );
188 $items = $this->getContentLanguage()->getBookstoreList();
189 foreach ( $items as $label => $url ) {
190 $out->addHTML( $this->makeListItem( $isbn, $label, $url ) );
191 }
192 $out->addHTML( '</ul>' );
193
194 return true;
195 }
196
205 private function makeListItem( $isbn, $label, $url ) {
206 $url = str_replace( '$1', $isbn, $url );
207
208 return Html::rawElement( 'li', [],
209 Html::element( 'a', [ 'href' => $url, 'class' => 'external' ], $label )
210 );
211 }
212
214 protected function getGroupName() {
215 return 'wiki';
216 }
217}
218
220class_alias( SpecialBookSources::class, 'SpecialBookSources' );
const NS_PROJECT
Definition Defines.php:55
Content object implementation for representing flat text.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:195
This class is a collection of static functions that serve two purposes:
Definition Html.php:43
Value object representing a content slot associated with a page revision.
Parent class for all special pages.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getPageTitle( $subpage=false)
Get a self-referential title object.
getContext()
Gets the context this SpecialPage is executed in.
getRequest()
Get the WebRequest being used for this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
getContentLanguage()
Shortcut to get content language.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages By default the message key is the canonical name of...
Information on citing a book with a particular ISBN.
__construct(RevisionLookup $revisionLookup, TitleFactory $titleFactory)
static isValidISBN( $isbn)
Return whether a given ISBN (10 or 13) is valid.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Creates Title objects.
Service for looking up page revisions.
element(SerializerNode $parent, SerializerNode $node, $contents)