f527276cb168bec19c3cbe381231661aceae4c34
[lhc/web/wiklou.git] / includes / SpecialBooksources.php
1 <?php
2
3 # ISBNs in wiki pages will create links to this page, with
4 # the ISBN passed in via the query string.
5
6 function wfSpecialBooksources( $par )
7 {
8 global $wgRequest;
9
10 $isbn = $par;
11 if( empty( $par ) ) {
12 $isbn = $wgRequest->getVal( 'isbn' );
13 }
14 $isbn = preg_replace( '/[^0-9X]/', '', $isbn );
15
16 $bsl = new BookSourceList( $isbn );
17 $bsl->show();
18 }
19
20 class BookSourceList {
21
22 var $mIsbn;
23
24 function BookSourceList( $isbn )
25 {
26 $this->mIsbn = $isbn;
27 }
28
29 function show()
30 {
31 global $wgOut, $wgUser, $wgLang;
32 $fname = "BookSourceList::show()";
33 $noautolist = false;
34
35 $wgOut->setPagetitle( wfMsg( "booksources" ) );
36 $bstext = wfMsg( "booksourcetext" );
37
38 if( $this->mIsbn ) {
39 $bstitle = Title::newFromText( wfmsg( "booksources" ) );
40 $sql = "SELECT cur_text FROM cur " .
41 "WHERE cur_namespace=4 and cur_title='" .
42 wfStrencode( $bstitle->getDBkey() ) . "'";
43 $res = wfQuery( $sql, DB_READ, $fname );
44 if( ( $s = wfFetchObject( $res ) ) and ( $s->cur_text != "" ) ) {
45 $bstext = $s->cur_text;
46 $bstext = str_replace( "MAGICNUMBER", $this->mIsbn, $bstext );
47 $noautolist = true;
48 }
49 }
50
51 $wgOut->addWikiText( $bstext );
52
53 # If ISBN is blank, just show a list of links to the
54 # home page of the various book sites. Otherwise, show
55 # a list of links directly to the book.
56
57 if( !$noautolist ) { # only do this if we haven't already shown [[Wikipedia:Book sources]]
58 $s = "<ul>\n";
59 $bs = $wgLang->getBookstoreList() ;
60 $bsn = array_keys ( $bs ) ;
61 foreach ( $bsn as $name ) {
62 $adr = $bs[$name] ;
63 if ( ! $this->mIsbn ) {
64 $adr = explode ( ":" , $adr , 2 ) ;
65 $adr = explode ( "/" , $adr[1] ) ;
66 $a = "" ;
67 while ( $a == "" ) $a = array_shift ( $adr ) ;
68 $adr = "http://".$a ;
69 } else {
70 $adr = str_replace ( "$1" , $this->mIsbn , $adr ) ;
71 }
72 $s .= "<li><a href=\"{$adr}\" class=\"external\">{$name}</a></li>\n" ;
73 }
74 $s .= "</ul>\n";
75
76 $wgOut->addHTML( $s );
77 }
78 }
79 }
80
81 ?>