16use UnexpectedValueException;
36 parent::__construct(
'Booksources' );
37 $this->revisionLookup = $revisionLookup;
38 $this->titleFactory = $titleFactory;
51 $isbn = $isbn ?: $this->
getRequest()->getText(
'isbn' );
52 $isbn = trim( $isbn );
54 $this->buildForm( $isbn );
57 if ( !self::isValidISBN( $isbn ) ) {
59 "<div class=\"error\">\n$1\n</div>",
60 'booksources-invalid-isbn'
64 $this->showList( $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 ) {
82 $sum += (int)$isbn[$i];
84 $sum += 3 * (int)$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 += (int)$isbn[$i] * ( $i + 1 );
101 if ( $check == 10 ) {
104 if ( (
string)$check === $isbn[9] ) {
118 private static function cleanIsbn( $isbn ) {
119 return trim( preg_replace(
'![^0-9X]!',
'', $isbn ) );
127 private function buildForm( $isbn ) {
132 'label-message' =>
'booksources-isbn',
139 HTMLForm::factory(
'ooui', $formDescriptor, $this->
getContext() )
141 ->setWrapperLegendMsg(
'booksources-search-legend' )
142 ->setSubmitTextMsg(
'booksources-search' )
145 ->displayForm(
false );
155 private function showList( $isbn ) {
158 $isbn = self::cleanIsbn( $isbn );
159 # Hook to allow extensions to insert additional HTML,
160 # e.g. for API-interacting plugins and so on
163 # Check for a local page such as Project:Book_sources and use that if available
164 $page = $this->
msg(
'booksources' )->inContentLanguage()->text();
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 );
171 if ( $content instanceof TextContent ) {
174 $text = $content->getText();
175 $out->addWikiTextAsInterface( str_replace(
'MAGICNUMBER', $isbn, $text ) );
179 throw new UnexpectedValueException(
180 "Unexpected content type for book sources: " . $content->getModel()
185 # Fall back to the defaults given in the language file
186 $out->addWikiMsg(
'booksources-text' );
187 $out->addHTML(
'<ul>' );
189 foreach ( $items as $label =>
$url ) {
190 $out->addHTML( $this->makeListItem( $isbn, $label,
$url ) );
192 $out->addHTML(
'</ul>' );
205 private function makeListItem( $isbn, $label,
$url ) {
206 $url = str_replace(
'$1', $isbn,
$url );
208 return Html::rawElement(
'li', [],
220class_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...