MediaWiki master
SpecialBookSources.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Specials;
8
16use UnexpectedValueException;
17
28
29 public function __construct(
30 private readonly RevisionLookup $revisionLookup,
31 private readonly TitleFactory $titleFactory
32 ) {
33 parent::__construct( 'Booksources' );
34 }
35
39 public function execute( $isbn ) {
40 $out = $this->getOutput();
41
42 $this->setHeaders();
43 $this->outputHeader();
44
45 // User provided ISBN
46 $isbn = $isbn ?: $this->getRequest()->getText( 'isbn' );
47 $isbn = trim( $isbn );
48
49 $this->buildForm( $isbn );
50
51 if ( $isbn !== '' ) {
52 if ( !self::isValidISBN( $isbn ) ) {
53 $out->wrapWikiMsg(
54 "<div class=\"error\">\n$1\n</div>",
55 'booksources-invalid-isbn'
56 );
57 }
58
59 $this->showList( $isbn );
60 }
61 }
62
69 public static function isValidISBN( $isbn ) {
70 $isbn = self::cleanIsbn( $isbn );
71 $sum = 0;
72 if ( strlen( $isbn ) == 13 ) {
73 for ( $i = 0; $i < 12; $i++ ) {
74 if ( $isbn[$i] === 'X' ) {
75 return false;
76 } elseif ( $i % 2 == 0 ) {
77 $sum += (int)$isbn[$i];
78 } else {
79 $sum += 3 * (int)$isbn[$i];
80 }
81 }
82
83 $check = ( 10 - ( $sum % 10 ) ) % 10;
84 if ( (string)$check === $isbn[12] ) {
85 return true;
86 }
87 } elseif ( strlen( $isbn ) == 10 ) {
88 for ( $i = 0; $i < 9; $i++ ) {
89 if ( $isbn[$i] === 'X' ) {
90 return false;
91 }
92 $sum += (int)$isbn[$i] * ( $i + 1 );
93 }
94
95 $check = $sum % 11;
96 if ( $check == 10 ) {
97 $check = "X";
98 }
99 if ( (string)$check === $isbn[9] ) {
100 return true;
101 }
102 }
103
104 return false;
105 }
106
113 private static function cleanIsbn( $isbn ) {
114 return trim( preg_replace( '![^0-9X]!', '', $isbn ) );
115 }
116
122 private function buildForm( $isbn ) {
123 $formDescriptor = [
124 'isbn' => [
125 'type' => 'text',
126 'name' => 'isbn',
127 'label-message' => 'booksources-isbn',
128 'default' => $isbn,
129 'autofocus' => true,
130 'required' => true,
131 ],
132 ];
133
134 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
135 ->setTitle( $this->getPageTitle() )
136 ->setWrapperLegendMsg( 'booksources-search-legend' )
137 ->setSubmitTextMsg( 'booksources-search' )
138 ->setMethod( 'get' )
139 ->prepareForm()
140 ->displayForm( false );
141 }
142
150 private function showList( $isbn ) {
151 $out = $this->getOutput();
152
153 $isbn = self::cleanIsbn( $isbn );
154 # Hook to allow extensions to insert additional HTML,
155 # e.g. for API-interacting plugins and so on
156 $this->getHookRunner()->onBookInformation( $isbn, $out );
157
158 # Check for a local page such as Project:Book_sources and use that if available
159 $page = $this->msg( 'booksources' )->inContentLanguage()->text();
160 // Show list in content language
161 $title = $this->titleFactory->makeTitleSafe( NS_PROJECT, $page );
162 if ( is_object( $title ) && $title->exists() ) {
163 $rev = $this->revisionLookup->getRevisionByTitle( $title );
164 $content = $rev->getContent( SlotRecord::MAIN );
165
166 if ( $content instanceof TextContent ) {
167 // XXX: in the future, this could be stored as structured data, defining a list of book sources
168
169 $text = $content->getText();
170 $out->addWikiTextAsInterface( str_replace( 'MAGICNUMBER', $isbn, $text ) );
171
172 return true;
173 } else {
174 throw new UnexpectedValueException(
175 "Unexpected content type for book sources: " . $content->getModel()
176 );
177 }
178 }
179
180 # Fall back to the defaults given in the language file
181 $out->addWikiMsg( 'booksources-text' );
182 $out->addHTML( '<ul>' );
183 $items = $this->getContentLanguage()->getBookstoreList();
184 foreach ( $items as $label => $url ) {
185 $out->addHTML( $this->makeListItem( $isbn, $label, $url ) );
186 }
187 $out->addHTML( '</ul>' );
188
189 return true;
190 }
191
200 private function makeListItem( $isbn, $label, $url ) {
201 $url = str_replace( '$1', $isbn, $url );
202
203 return Html::rawElement( 'li', [],
204 Html::element( 'a', [ 'href' => $url, 'class' => 'external' ], $label )
205 );
206 }
207
209 protected function getGroupName() {
210 return 'wiki';
211 }
212}
213
215class_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:207
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.
static isValidISBN( $isbn)
Return whether a given ISBN (10 or 13) is valid.
__construct(private readonly RevisionLookup $revisionLookup, private readonly TitleFactory $titleFactory)
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)