X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FSpecialBooksources.php;h=5f103495498bb9f5d8000fb5c372b949309dfcd1;hb=8c3b4616cdf2bcd9c8cc1264b806c47b2523f163;hp=f527276cb168bec19c3cbe381231661aceae4c34;hpb=09e87fed729e135d50366587f0fa94dbd0155577;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/SpecialBooksources.php b/includes/SpecialBooksources.php index f527276cb1..5f10349549 100644 --- a/includes/SpecialBooksources.php +++ b/includes/SpecialBooksources.php @@ -1,81 +1,113 @@ + * @todo Validate ISBNs using the standard check-digit method + */ +class SpecialBookSources extends SpecialPage { -function wfSpecialBooksources( $par ) -{ - global $wgRequest; - - $isbn = $par; - if( empty( $par ) ) { - $isbn = $wgRequest->getVal( 'isbn' ); + /** + * ISBN passed to the page, if any + */ + private $isbn = ''; + + /** + * Constructor + */ + public function __construct() { + parent::__construct( 'Booksources' ); } - $isbn = preg_replace( '/[^0-9X]/', '', $isbn ); - - $bsl = new BookSourceList( $isbn ); - $bsl->show(); -} -class BookSourceList { + /** + * 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(); + } - var $mIsbn; + /** + * 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 ) ); + } - function BookSourceList( $isbn ) - { - $this->mIsbn = $isbn; + /** + * 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 show() - { - global $wgOut, $wgUser, $wgLang; - $fname = "BookSourceList::show()"; - $noautolist = false; + /** + * Determine where to get the list of book sources from, + * format and output them + * + * @return string + */ + private function showList() { + global $wgOut, $wgContLang; - $wgOut->setPagetitle( wfMsg( "booksources" ) ); - $bstext = wfMsg( "booksourcetext" ); + # Hook to allow extensions to insert additional HTML, + # e.g. for API-interacting plugins and so on + wfRunHooks( 'BookInformation', array( $this->isbn, &$wgOut ) ); - if( $this->mIsbn ) { - $bstitle = Title::newFromText( wfmsg( "booksources" ) ); - $sql = "SELECT cur_text FROM cur " . - "WHERE cur_namespace=4 and cur_title='" . - wfStrencode( $bstitle->getDBkey() ) . "'"; - $res = wfQuery( $sql, DB_READ, $fname ); - if( ( $s = wfFetchObject( $res ) ) and ( $s->cur_text != "" ) ) { - $bstext = $s->cur_text; - $bstext = str_replace( "MAGICNUMBER", $this->mIsbn, $bstext ); - $noautolist = true; - } + # Check for a local page such as Project:Book_sources and use that if available + $title = Title::makeTitleSafe( NS_PROJECT, wfMsgForContent( 'booksources' ) ); # Show list in content language + if( is_object( $title ) && $title->exists() ) { + $rev = Revision::newFromTitle( $title ); + $wgOut->addWikiText( str_replace( 'MAGICNUMBER', $this->isbn, $rev->getText() ) ); + return true; } - $wgOut->addWikiText( $bstext ); - - # If ISBN is blank, just show a list of links to the - # home page of the various book sites. Otherwise, show - # a list of links directly to the book. - - if( !$noautolist ) { # only do this if we haven't already shown [[Wikipedia:Book sources]] - $s = "\n"; - - $wgOut->addHTML( $s ); - } + # Fall back to the defaults given in the language file + $wgOut->addWikiText( wfMsgNoTrans( 'booksources-text' ) ); + $wgOut->addHtml( '' ); + return true; + } + + /** + * 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 ) . '
  • '; } + } -?> +