X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FSpecialBooksources.php;h=dad4cffc2c7b6c5a01a9895aea0b6fe27176c207;hb=a7e366665c449832a2e07047683a50476da2cdf0;hp=960f622469219fa860a1674473fff7442e5c07e3;hpb=8dd2ee8057e809873e2715f17177a690c560d2d6;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/SpecialBooksources.php b/includes/SpecialBooksources.php index 960f622469..dad4cffc2c 100644 --- a/includes/SpecialBooksources.php +++ b/includes/SpecialBooksources.php @@ -1,109 +1,113 @@ + * @todo Validate ISBNs using the standard check-digit method */ +class SpecialBookSources extends SpecialPage { -/** - * Constructor - */ -function wfSpecialBooksources( $par ) { - global $wgRequest; + /** + * ISBN passed to the page, if any + */ + private $isbn = ''; - $isbn = $par; - if( empty( $par ) ) { - $isbn = $wgRequest->getVal( 'isbn' ); + /** + * Constructor + */ + public function __construct() { + parent::__construct( 'Booksources' ); } - $isbn = preg_replace( '/[^0-9X]/', '', $isbn ); - - $bsl = new BookSourceList( $isbn ); - $bsl->show(); -} - -/** - * - * @package MediaWiki - * @subpackage SpecialPage - */ -class BookSourceList { - var $mIsbn; - function BookSourceList( $isbn ) { - $this->mIsbn = $isbn; + /** + * Show the special page + * + * @param $isbn ISBN passed as a subpage parameter + */ + public function execute( $isbn ) { + global $wgOut, $wgRequest; + $this->setHeaders(); + $this->isbn = $this->cleanIsbn( $isbn ? $isbn : $wgRequest->getText( 'isbn' ) ); + $wgOut->addWikiText( wfMsgNoTrans( 'booksources-summary' ) ); + $wgOut->addHtml( $this->makeForm() ); + if( strlen( $this->isbn ) > 0 ) + $this->showList(); } - function show() { - global $wgOut; + /** + * Trim ISBN and remove characters which aren't required + * + * @param $isbn Unclean ISBN + * @return string + */ + private function cleanIsbn( $isbn ) { + return trim( preg_replace( '![^0-9X]!', '', $isbn ) ); + } - $wgOut->setPagetitle( wfMsg( "booksources" ) ); - if( $this->mIsbn == '' ) { - $this->askForm(); - } else { - $this->showList(); - } + /** + * Generate a form to allow users to enter an ISBN + * + * @return string + */ + private function makeForm() { + global $wgScript; + $title = self::getTitleFor( 'Booksources' ); + $form = '
' . wfMsgHtml( 'booksources-search-legend' ) . ''; + $form .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ); + $form .= Xml::hidden( 'title', $title->getPrefixedText() ); + $form .= '

' . Xml::inputLabel( wfMsg( 'booksources-isbn' ), 'isbn', 'isbn', 20, $this->isbn ); + $form .= ' ' . Xml::submitButton( wfMsg( 'booksources-go' ) ) . '

'; + $form .= Xml::closeElement( 'form' ); + $form .= '
'; + return $form; } - function showList() { + /** + * Determine where to get the list of book sources from, + * format and output them + * + * @return string + */ + private function showList() { global $wgOut, $wgContLang; - $fname = "BookSourceList::showList()"; - # First, see if we have a custom list setup in - # [[Wikipedia:Book sources]] or equivalent. - $bstitle = Title::makeTitleSafe( NS_PROJECT, wfMsg( "booksources" ) ); - if( $bstitle ) { - $revision = Revision::newFromTitle( $bstitle ); - if( $revision ) { - $bstext = $revision->getText(); - if( $bstext ) { - $bstext = str_replace( "MAGICNUMBER", $this->mIsbn, $bstext ); - $wgOut->addWikiText( $bstext ); - return; - } - } - } + # Hook to allow extensions to insert additional HTML, + # e.g. for API-interacting plugins and so on + wfRunHooks( 'BookInformation', array( $this->isbn, &$wgOut ) ); - # Otherwise, use the list of links in the default Language.php file. - $s = wfMsgWikiHtml( 'booksourcetext' ) . "\n"; - $wgOut->addHTML( $s ); + # Fall back to the defaults given in the language file + $wgOut->addWikiText( wfMsgNoTrans( 'booksources-text' ) ); + $wgOut->addHtml( '' ); + return true; } - - function askForm() { - global $wgOut, $wgTitle; - $fname = "BookSourceList::askForm()"; - - $action = $wgTitle->escapeLocalUrl(); - $isbn = htmlspecialchars( wfMsg( "isbn" ) ); - $go = htmlspecialchars( wfMsg( "go" ) ); - $out = "
- $isbn: - -
"; - $wgOut->addHTML( $out ); + + /** + * Format a book source list item + * + * @param $label Book source label + * @param $url Book source URL + * @return string + */ + private function makeListItem( $label, $url ) { + $url = str_replace( '$1', $this->isbn, $url ); + return '
  • ' . htmlspecialchars( $label ) . '
  • '; } + } ?>