37 private $revisionLookup;
45 parent::__construct(
'Booksources' );
46 $this->revisionLookup = $revisionLookup;
59 $isbn = $isbn ?: $this->
getRequest()->getText(
'isbn' );
60 $isbn = trim( $isbn );
62 $this->buildForm( $isbn );
65 if ( !self::isValidISBN( $isbn ) ) {
67 "<div class=\"error\">\n$1\n</div>",
68 'booksources-invalid-isbn'
72 $this->showList( $isbn );
83 $isbn = self::cleanIsbn( $isbn );
85 if ( strlen( $isbn ) == 13 ) {
86 for ( $i = 0; $i < 12; $i++ ) {
87 if ( $isbn[$i] ===
'X' ) {
89 } elseif ( $i % 2 == 0 ) {
90 $sum += (int)$isbn[$i];
92 $sum += 3 * (int)$isbn[$i];
96 $check = ( 10 - ( $sum % 10 ) ) % 10;
97 if ( (
string)$check === $isbn[12] ) {
100 } elseif ( strlen( $isbn ) == 10 ) {
101 for ( $i = 0; $i < 9; $i++ ) {
102 if ( $isbn[$i] ===
'X' ) {
105 $sum += (int)$isbn[$i] * ( $i + 1 );
109 if ( $check == 10 ) {
112 if ( (
string)$check === $isbn[9] ) {
126 private static function cleanIsbn( $isbn ) {
127 return trim( preg_replace(
'![^0-9X]!',
'', $isbn ) );
135 private function buildForm( $isbn ) {
140 'label-message' =>
'booksources-isbn',
149 ->setWrapperLegendMsg(
'booksources-search-legend' )
150 ->setSubmitTextMsg(
'booksources-search' )
153 ->displayForm(
false );
164 private function showList( $isbn ) {
167 $isbn = self::cleanIsbn( $isbn );
168 # Hook to allow extensions to insert additional HTML,
169 # e.g. for API-interacting plugins and so on
172 # Check for a local page such as Project:Book_sources and use that if available
173 $page = $this->
msg(
'booksources' )->inContentLanguage()->text();
176 $rev = $this->revisionLookup->getRevisionByTitle(
$title );
177 $content = $rev->getContent( SlotRecord::MAIN );
183 $out->addWikiTextAsInterface( str_replace(
'MAGICNUMBER', $isbn, $text ) );
187 throw new MWException(
"Unexpected content type for book sources: " .
$content->getModel() );
191 # Fall back to the defaults given in the language file
192 $out->addWikiMsg(
'booksources-text' );
193 $out->addHTML(
'<ul>' );
195 foreach ( $items as $label => $url ) {
196 $out->addHTML( $this->makeListItem( $isbn, $label, $url ) );
198 $out->addHTML(
'</ul>' );
211 private function makeListItem( $isbn, $label, $url ) {
212 $url = str_replace(
'$1', $isbn, $url );
215 Html::element(
'a', [
'href' => $url,
'class' =>
'external' ], $label )
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Special page outputs information on sourcing a book with a particular ISBN The parser creates links t...
static isValidISBN( $isbn)
Return whether a given ISBN (10 or 13) is valid.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
__construct(RevisionLookup $revisionLookup)
Parent class for all special pages.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getRequest()
Get the WebRequest being used for this instance.
getPageTitle( $subpage=false)
Get a self-referential title object.
getContentLanguage()
Shortcut to get content language.
Content object implementation for representing flat text.
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.