30use UnexpectedValueException;
54 parent::__construct(
'Booksources' );
55 $this->revisionLookup = $revisionLookup;
56 $this->titleFactory = $titleFactory;
69 $isbn = $isbn ?: $this->
getRequest()->getText(
'isbn' );
70 $isbn = trim( $isbn );
72 $this->buildForm( $isbn );
75 if ( !self::isValidISBN( $isbn ) ) {
77 "<div class=\"error\">\n$1\n</div>",
78 'booksources-invalid-isbn'
82 $this->showList( $isbn );
93 $isbn = self::cleanIsbn( $isbn );
95 if ( strlen( $isbn ) == 13 ) {
96 for ( $i = 0; $i < 12; $i++ ) {
97 if ( $isbn[$i] ===
'X' ) {
99 } elseif ( $i % 2 == 0 ) {
100 $sum += (int)$isbn[$i];
102 $sum += 3 * (int)$isbn[$i];
106 $check = ( 10 - ( $sum % 10 ) ) % 10;
107 if ( (
string)$check === $isbn[12] ) {
110 } elseif ( strlen( $isbn ) == 10 ) {
111 for ( $i = 0; $i < 9; $i++ ) {
112 if ( $isbn[$i] ===
'X' ) {
115 $sum += (int)$isbn[$i] * ( $i + 1 );
119 if ( $check == 10 ) {
122 if ( (
string)$check === $isbn[9] ) {
136 private static function cleanIsbn( $isbn ) {
137 return trim( preg_replace(
'![^0-9X]!',
'', $isbn ) );
145 private function buildForm( $isbn ) {
150 'label-message' =>
'booksources-isbn',
157 HTMLForm::factory(
'ooui', $formDescriptor, $this->
getContext() )
159 ->setWrapperLegendMsg(
'booksources-search-legend' )
160 ->setSubmitTextMsg(
'booksources-search' )
163 ->displayForm(
false );
173 private function showList( $isbn ) {
176 $isbn = self::cleanIsbn( $isbn );
177 # Hook to allow extensions to insert additional HTML,
178 # e.g. for API-interacting plugins and so on
181 # Check for a local page such as Project:Book_sources and use that if available
182 $page = $this->
msg(
'booksources' )->inContentLanguage()->text();
184 $title = $this->titleFactory->makeTitleSafe(
NS_PROJECT, $page );
185 if ( is_object( $title ) && $title->exists() ) {
186 $rev = $this->revisionLookup->getRevisionByTitle( $title );
187 $content = $rev->getContent( SlotRecord::MAIN );
189 if ( $content instanceof TextContent ) {
192 $text = $content->getText();
193 $out->addWikiTextAsInterface( str_replace(
'MAGICNUMBER', $isbn, $text ) );
197 throw new UnexpectedValueException(
198 "Unexpected content type for book sources: " . $content->getModel()
203 # Fall back to the defaults given in the language file
204 $out->addWikiMsg(
'booksources-text' );
205 $out->addHTML(
'<ul>' );
207 foreach ( $items as $label =>
$url ) {
208 $out->addHTML( $this->makeListItem( $isbn, $label,
$url ) );
210 $out->addHTML(
'</ul>' );
223 private function makeListItem( $isbn, $label,
$url ) {
224 $url = str_replace(
'$1', $isbn,
$url );
226 return Html::rawElement(
'li', [],
237class_alias( SpecialBookSources::class,
'SpecialBookSources' );
Content object implementation for representing flat text.
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...