| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Specials; |
| 22 |
|
| 23 |
use MediaWiki\Content\TextContent; |
| 24 |
use MediaWiki\Html\Html; |
| 25 |
use MediaWiki\HTMLForm\HTMLForm; |
| 26 |
use MediaWiki\Revision\RevisionLookup; |
| 27 |
use MediaWiki\Revision\SlotRecord; |
| 28 |
use MediaWiki\SpecialPage\SpecialPage; |
| 29 |
use MediaWiki\Title\TitleFactory; |
| 30 |
use UnexpectedValueException; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class SpecialBookSources extends SpecialPage { |
| 42 |
|
| 43 |
private RevisionLookup $revisionLookup; |
| 44 |
private TitleFactory $titleFactory; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function __construct( |
| 51 |
RevisionLookup $revisionLookup, |
| 52 |
TitleFactory $titleFactory |
| 53 |
) { |
| 54 |
parent::__construct( 'Booksources' ); |
| 55 |
$this->revisionLookup = $revisionLookup; |
| 56 |
$this->titleFactory = $titleFactory; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public function execute( $isbn ) { |
| 63 |
$out = $this->getOutput(); |
| 64 |
|
| 65 |
$this->setHeaders(); |
| 66 |
$this->outputHeader(); |
| 67 |
|
| 68 |
|
| 69 |
$isbn = $isbn ?: $this->getRequest()->getText( 'isbn' ); |
| 70 |
$isbn = trim( $isbn ); |
| 71 |
|
| 72 |
$this->buildForm( $isbn ); |
| 73 |
|
| 74 |
if ( $isbn !== '' ) { |
| 75 |
if ( !self::isValidISBN( $isbn ) ) { |
| 76 |
$out->wrapWikiMsg( |
| 77 |
"<div class=\"error\">\n$1\n</div>", |
| 78 |
'booksources-invalid-isbn' |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
$this->showList( $isbn ); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public static function isValidISBN( $isbn ) { |
| 93 |
$isbn = self::cleanIsbn( $isbn ); |
| 94 |
$sum = 0; |
| 95 |
if ( strlen( $isbn ) == 13 ) { |
| 96 |
for ( $i = 0; $i < 12; $i++ ) { |
| 97 |
if ( $isbn[$i] === 'X' ) { |
| 98 |
return false; |
| 99 |
} elseif ( $i % 2 == 0 ) { |
| 100 |
$sum += (int)$isbn[$i]; |
| 101 |
} else { |
| 102 |
$sum += 3 * (int)$isbn[$i]; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
$check = ( 10 - ( $sum % 10 ) ) % 10; |
| 107 |
if ( (string)$check === $isbn[12] ) { |
| 108 |
return true; |
| 109 |
} |
| 110 |
} elseif ( strlen( $isbn ) == 10 ) { |
| 111 |
for ( $i = 0; $i < 9; $i++ ) { |
| 112 |
if ( $isbn[$i] === 'X' ) { |
| 113 |
return false; |
| 114 |
} |
| 115 |
$sum += (int)$isbn[$i] * ( $i + 1 ); |
| 116 |
} |
| 117 |
|
| 118 |
$check = $sum % 11; |
| 119 |
if ( $check == 10 ) { |
| 120 |
$check = "X"; |
| 121 |
} |
| 122 |
if ( (string)$check === $isbn[9] ) { |
| 123 |
return true; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
private static function cleanIsbn( $isbn ) { |
| 137 |
return trim( preg_replace( '![^0-9X]!', '', $isbn ) ); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
private function buildForm( $isbn ) { |
| 146 |
$formDescriptor = [ |
| 147 |
'isbn' => [ |
| 148 |
'type' => 'text', |
| 149 |
'name' => 'isbn', |
| 150 |
'label-message' => 'booksources-isbn', |
| 151 |
'default' => $isbn, |
| 152 |
'autofocus' => true, |
| 153 |
'required' => true, |
| 154 |
], |
| 155 |
]; |
| 156 |
|
| 157 |
HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() ) |
| 158 |
->setTitle( $this->getPageTitle() ) |
| 159 |
->setWrapperLegendMsg( 'booksources-search-legend' ) |
| 160 |
->setSubmitTextMsg( 'booksources-search' ) |
| 161 |
->setMethod( 'get' ) |
| 162 |
->prepareForm() |
| 163 |
->displayForm( false ); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
private function showList( $isbn ) { |
| 174 |
$out = $this->getOutput(); |
| 175 |
|
| 176 |
$isbn = self::cleanIsbn( $isbn ); |
| 177 |
|
| 178 |
|
| 179 |
$this->getHookRunner()->onBookInformation( $isbn, $out ); |
| 180 |
|
| 181 |
|
| 182 |
$page = $this->msg( 'booksources' )->inContentLanguage()->text(); |
| 183 |
|
| 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 ); |
| 188 |
|
| 189 |
if ( $content instanceof TextContent ) { |
| 190 |
|
| 191 |
|
| 192 |
$text = $content->getText(); |
| 193 |
$out->addWikiTextAsInterface( str_replace( 'MAGICNUMBER', $isbn, $text ) ); |
| 194 |
|
| 195 |
return true; |
| 196 |
} else { |
| 197 |
throw new UnexpectedValueException( |
| 198 |
"Unexpected content type for book sources: " . $content->getModel() |
| 199 |
); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
$out->addWikiMsg( 'booksources-text' ); |
| 205 |
$out->addHTML( '<ul>' ); |
| 206 |
$items = $this->getContentLanguage()->getBookstoreList(); |
| 207 |
foreach ( $items as $label => $url ) { |
| 208 |
$out->addHTML( $this->makeListItem( $isbn, $label, $url ) ); |
| 209 |
} |
| 210 |
$out->addHTML( '</ul>' ); |
| 211 |
|
| 212 |
return true; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
private function makeListItem( $isbn, $label, $url ) { |
| 224 |
$url = str_replace( '$1', $isbn, $url ); |
| 225 |
|
| 226 |
return Html::rawElement( 'li', [], |
| 227 |
Html::element( 'a', [ 'href' => $url, 'class' => 'external' ], $label ) |
| 228 |
); |
| 229 |
} |
| 230 |
|
| 231 |
protected function getGroupName() { |
| 232 |
return 'wiki'; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
class_alias( SpecialBookSources::class, 'SpecialBookSources' ); |