30use UnexpectedValueException;
50 parent::__construct(
'Booksources' );
51 $this->revisionLookup = $revisionLookup;
52 $this->titleFactory = $titleFactory;
65 $isbn = $isbn ?: $this->
getRequest()->getText(
'isbn' );
66 $isbn = trim( $isbn );
68 $this->buildForm( $isbn );
71 if ( !self::isValidISBN( $isbn ) ) {
73 "<div class=\"error\">\n$1\n</div>",
74 'booksources-invalid-isbn'
78 $this->showList( $isbn );
89 $isbn = self::cleanIsbn( $isbn );
91 if ( strlen( $isbn ) == 13 ) {
92 for ( $i = 0; $i < 12; $i++ ) {
93 if ( $isbn[$i] ===
'X' ) {
95 } elseif ( $i % 2 == 0 ) {
96 $sum += (int)$isbn[$i];
98 $sum += 3 * (int)$isbn[$i];
102 $check = ( 10 - ( $sum % 10 ) ) % 10;
103 if ( (
string)$check === $isbn[12] ) {
106 } elseif ( strlen( $isbn ) == 10 ) {
107 for ( $i = 0; $i < 9; $i++ ) {
108 if ( $isbn[$i] ===
'X' ) {
111 $sum += (int)$isbn[$i] * ( $i + 1 );
115 if ( $check == 10 ) {
118 if ( (
string)$check === $isbn[9] ) {
132 private static function cleanIsbn( $isbn ) {
133 return trim( preg_replace(
'![^0-9X]!',
'', $isbn ) );
141 private function buildForm( $isbn ) {
146 'label-message' =>
'booksources-isbn',
153 HTMLForm::factory(
'ooui', $formDescriptor, $this->
getContext() )
155 ->setWrapperLegendMsg(
'booksources-search-legend' )
156 ->setSubmitTextMsg(
'booksources-search' )
159 ->displayForm(
false );
169 private function showList( $isbn ) {
172 $isbn = self::cleanIsbn( $isbn );
173 # Hook to allow extensions to insert additional HTML,
174 # e.g. for API-interacting plugins and so on
177 # Check for a local page such as Project:Book_sources and use that if available
178 $page = $this->
msg(
'booksources' )->inContentLanguage()->text();
180 $title = $this->titleFactory->makeTitleSafe(
NS_PROJECT, $page );
181 if ( is_object( $title ) && $title->exists() ) {
182 $rev = $this->revisionLookup->getRevisionByTitle( $title );
183 $content = $rev->getContent( SlotRecord::MAIN );
185 if ( $content instanceof TextContent ) {
188 $text = $content->getText();
189 $out->addWikiTextAsInterface( str_replace(
'MAGICNUMBER', $isbn, $text ) );
193 throw new UnexpectedValueException(
194 "Unexpected content type for book sources: " . $content->getModel()
199 # Fall back to the defaults given in the language file
200 $out->addWikiMsg(
'booksources-text' );
201 $out->addHTML(
'<ul>' );
203 foreach ( $items as $label =>
$url ) {
204 $out->addHTML( $this->makeListItem( $isbn, $label,
$url ) );
206 $out->addHTML(
'</ul>' );
219 private function makeListItem( $isbn, $label,
$url ) {
220 $url = str_replace(
'$1', $isbn,
$url );
222 return Html::rawElement(
'li', [],
233class_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...