33use UnexpectedValueException;
55 parent::__construct(
'Booksources' );
56 $this->revisionLookup = $revisionLookup;
57 $this->titleFactory = $titleFactory;
70 $isbn = $isbn ?: $this->
getRequest()->getText(
'isbn' );
71 $isbn = trim( $isbn );
73 $this->buildForm( $isbn );
76 if ( !self::isValidISBN( $isbn ) ) {
78 "<div class=\"error\">\n$1\n</div>",
79 'booksources-invalid-isbn'
83 $this->showList( $isbn );
94 $isbn = self::cleanIsbn( $isbn );
96 if ( strlen( $isbn ) == 13 ) {
97 for ( $i = 0; $i < 12; $i++ ) {
98 if ( $isbn[$i] ===
'X' ) {
100 } elseif ( $i % 2 == 0 ) {
101 $sum += (int)$isbn[$i];
103 $sum += 3 * (int)$isbn[$i];
107 $check = ( 10 - ( $sum % 10 ) ) % 10;
108 if ( (
string)$check === $isbn[12] ) {
111 } elseif ( strlen( $isbn ) == 10 ) {
112 for ( $i = 0; $i < 9; $i++ ) {
113 if ( $isbn[$i] ===
'X' ) {
116 $sum += (int)$isbn[$i] * ( $i + 1 );
120 if ( $check == 10 ) {
123 if ( (
string)$check === $isbn[9] ) {
137 private static function cleanIsbn( $isbn ) {
138 return trim( preg_replace(
'![^0-9X]!',
'', $isbn ) );
146 private function buildForm( $isbn ) {
151 'label-message' =>
'booksources-isbn',
158 HTMLForm::factory(
'ooui', $formDescriptor, $this->
getContext() )
160 ->setWrapperLegendMsg(
'booksources-search-legend' )
161 ->setSubmitTextMsg(
'booksources-search' )
164 ->displayForm(
false );
174 private function showList( $isbn ) {
177 $isbn = self::cleanIsbn( $isbn );
178 # Hook to allow extensions to insert additional HTML,
179 # e.g. for API-interacting plugins and so on
182 # Check for a local page such as Project:Book_sources and use that if available
183 $page = $this->
msg(
'booksources' )->inContentLanguage()->text();
185 $title = $this->titleFactory->makeTitleSafe(
NS_PROJECT, $page );
186 if ( is_object( $title ) && $title->exists() ) {
187 $rev = $this->revisionLookup->getRevisionByTitle( $title );
188 $content = $rev->getContent( SlotRecord::MAIN );
194 $out->addWikiTextAsInterface( str_replace(
'MAGICNUMBER', $isbn, $text ) );
198 throw new UnexpectedValueException(
199 "Unexpected content type for book sources: " .
$content->getModel()
204 # Fall back to the defaults given in the language file
205 $out->addWikiMsg(
'booksources-text' );
206 $out->addHTML(
'<ul>' );
208 foreach ( $items as $label => $url ) {
209 $out->addHTML( $this->makeListItem( $isbn, $label, $url ) );
211 $out->addHTML(
'</ul>' );
224 private function makeListItem( $isbn, $label, $url ) {
225 $url = str_replace(
'$1', $isbn, $url );
227 return Html::rawElement(
'li', [],
228 Html::element(
'a', [
'href' => $url,
'class' =>
'external' ], $label )
240class_alias( SpecialBookSources::class,
'SpecialBookSources' );
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 Per default the message key is the canonical name o...
Content object implementation for representing flat text.