16use UnexpectedValueException;
33 parent::__construct(
'Booksources' );
46 $isbn = $isbn ?: $this->
getRequest()->getText(
'isbn' );
47 $isbn = trim( $isbn );
49 $this->buildForm( $isbn );
52 if ( !self::isValidISBN( $isbn ) ) {
54 "<div class=\"error\">\n$1\n</div>",
55 'booksources-invalid-isbn'
59 $this->showList( $isbn );
70 $isbn = self::cleanIsbn( $isbn );
72 if ( strlen( $isbn ) == 13 ) {
73 for ( $i = 0; $i < 12; $i++ ) {
74 if ( $isbn[$i] ===
'X' ) {
76 } elseif ( $i % 2 == 0 ) {
77 $sum += (int)$isbn[$i];
79 $sum += 3 * (int)$isbn[$i];
83 $check = ( 10 - ( $sum % 10 ) ) % 10;
84 if ( (
string)$check === $isbn[12] ) {
87 } elseif ( strlen( $isbn ) == 10 ) {
88 for ( $i = 0; $i < 9; $i++ ) {
89 if ( $isbn[$i] ===
'X' ) {
92 $sum += (int)$isbn[$i] * ( $i + 1 );
99 if ( (
string)$check === $isbn[9] ) {
113 private static function cleanIsbn( $isbn ) {
114 return trim( preg_replace(
'![^0-9X]!',
'', $isbn ) );
122 private function buildForm( $isbn ) {
127 'label-message' =>
'booksources-isbn',
134 HTMLForm::factory(
'ooui', $formDescriptor, $this->
getContext() )
136 ->setWrapperLegendMsg(
'booksources-search-legend' )
137 ->setSubmitTextMsg(
'booksources-search' )
140 ->displayForm(
false );
150 private function showList( $isbn ) {
153 $isbn = self::cleanIsbn( $isbn );
154 # Hook to allow extensions to insert additional HTML,
155 # e.g. for API-interacting plugins and so on
158 # Check for a local page such as Project:Book_sources and use that if available
159 $page = $this->
msg(
'booksources' )->inContentLanguage()->text();
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 );
166 if ( $content instanceof TextContent ) {
169 $text = $content->getText();
170 $out->addWikiTextAsInterface( str_replace(
'MAGICNUMBER', $isbn, $text ) );
174 throw new UnexpectedValueException(
175 "Unexpected content type for book sources: " . $content->getModel()
180 # Fall back to the defaults given in the language file
181 $out->addWikiMsg(
'booksources-text' );
182 $out->addHTML(
'<ul>' );
184 foreach ( $items as $label =>
$url ) {
185 $out->addHTML( $this->makeListItem( $isbn, $label,
$url ) );
187 $out->addHTML(
'</ul>' );
200 private function makeListItem( $isbn, $label,
$url ) {
201 $url = str_replace(
'$1', $isbn,
$url );
203 return Html::rawElement(
'li', [],
215class_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...