Index: trunk/extensions/SacredText/SacredText.lookup.php |
— | — | @@ -2,11 +2,17 @@ |
3 | 3 | class SacredTextLookup { |
4 | 4 | |
5 | 5 | function parseInput( $input, &$book, &$chapternum, &$versenums ) { |
6 | | - if( preg_match( "/^\s*([\s\w]*\w+)\s*(\d+):(\d+)/", $input, $matches) ) { |
| 6 | + if( preg_match( "/^\s*([\s\w]*\w+)\s*(\d+):(\d+)(?:-(\d+))?\s*$/", $input, $matches) ) { |
7 | 7 | $book = $matches[1]; |
8 | 8 | $chapternum = $matches[2]; |
9 | 9 | $versenums = array(); |
10 | | - $versenums[] = $matches[3]; |
| 10 | + if( count( $matches ) <= 4 ) { |
| 11 | + $versenums[] = $matches[3]; |
| 12 | + } |
| 13 | + else for( $x = $matches[3]; $x <= $matches[4]; $x++ ) { |
| 14 | + $versenums[] = $x; |
| 15 | + } |
| 16 | + |
11 | 17 | return true; |
12 | 18 | } else { |
13 | 19 | return false; |
— | — | @@ -40,8 +46,6 @@ |
41 | 47 | } |
42 | 48 | } |
43 | 49 | |
44 | | - |
45 | | - |
46 | 50 | function lookup( $religtext, $book, $chapternum, $versenums, $lang, $ver ) |
47 | 51 | { |
48 | 52 | global $wgSacredChapterAlias; |
— | — | @@ -71,8 +75,51 @@ |
72 | 76 | if( $obj ) { |
73 | 77 | return htmlspecialchars( $obj->st_text ); |
74 | 78 | } else { |
| 79 | + $r = self::fallback( $religtext, $book, $chapternum, $versenums, $lang, $ver ); |
| 80 | + if( $r ) return htmlspecialchars( $r ); |
| 81 | + |
75 | 82 | return htmlspecialchars( "Could not find: ". $book ." ".$chapternum.":".$versenums[0]." in the ". $religtext ); |
76 | 83 | } |
77 | 84 | } |
78 | 85 | |
| 86 | + /* This function makes it possible to look for the verse on other websites, if the requested verse |
| 87 | + cannot be found in the database. |
| 88 | + */ |
| 89 | + function fallback( $religtext, $book, $chapternum, $versenums, $lang, $ver ) |
| 90 | + { |
| 91 | + global $wgSacredFallbackServers; |
| 92 | + if( !array_key_exists($religtext, $wgSacredFallbackServers) ) return false; |
| 93 | + if( !array_key_exists($lang, $wgSacredFallbackServers[$religtext]) ) return false; |
| 94 | + if( !array_key_exists($ver, $wgSacredFallbackServers[$religtext][$lang]) ) return false; |
| 95 | + |
| 96 | + $url0 = $wgSacredFallbackServers[$religtext][$lang][$ver]["url"]; |
| 97 | + $regex = $wgSacredFallbackServers[$religtext][$lang][$ver]["regex"]; |
| 98 | + |
| 99 | + |
| 100 | + $url0 = str_replace( |
| 101 | + array('$(religtext)','$(lang)','$(ver)','$(book)','$(chapternum)'), |
| 102 | + array($religtext,$lang,$ver,$book,$chapternum), |
| 103 | + $url0); |
| 104 | + |
| 105 | + $ret = ""; |
| 106 | + foreach( $versenums as $versenum ) |
| 107 | + { |
| 108 | + $url = str_replace('$(versenum)',$versenum, $url0); |
| 109 | + |
| 110 | + $h = fopen($url,'r' ); |
| 111 | + $str=''; |
| 112 | + $length = 8192; |
| 113 | + while(!feof($h)) $str.=fread($h,$length); |
| 114 | + fclose($h); |
| 115 | + $num = preg_match_all( $regex, $str, $matches, PREG_PATTERN_ORDER ); |
| 116 | + if( $num ) { |
| 117 | + $ret .= implode( " ", $matches[1] ) . " "; |
| 118 | + } |
| 119 | + else { |
| 120 | + return sprintf("Failed to match pattern %s from results from %s.",$regex,$url); |
| 121 | + } |
| 122 | + } |
| 123 | + if( empty($ret) ) return "Failed to retrieve match from '$url'."; |
| 124 | + else return "$ret"; |
| 125 | + } |
79 | 126 | } |
Index: trunk/extensions/SacredText/SacredText.php |
— | — | @@ -22,6 +22,8 @@ |
23 | 23 | |
24 | 24 | // the following are the parameters that can be set in LocalSettings.php |
25 | 25 | $wgSacredUseBibleTag = true; |
| 26 | + |
| 27 | +// There needs to be a database table to hold this information, in order to make it more manageable. |
26 | 28 | $wgSacredChapterAlias = array(); |
27 | 29 | $wgSacredChapterAlias["Christian Bible"] = array(); |
28 | 30 | $wgSacredChapterAlias["Christian Bible"]["1Chronicles"]="1 Chronicles"; |
— | — | @@ -113,7 +115,8 @@ |
114 | 116 | |
115 | 117 | $wgHooks['ParserFirstCallInit'][] = 'efSacredTextParserInit'; |
116 | 118 | $wgHooks['LoadExtensionSchemaUpdates'][] = 'updateSacredTextDB'; |
117 | | - |
| 119 | + |
| 120 | +// Create the hooks for quoting sacred texts |
118 | 121 | function efSacredTextParserInit( &$parser ) { |
119 | 122 | global $wgSacredUseBibleTag; |
120 | 123 | $parser->setHook( 'sacredtext', array('SacredTextLookup','hookSacredText') ); |
— | — | @@ -125,11 +128,28 @@ |
126 | 129 | |
127 | 130 | function updateSacredTextDB() { |
128 | 131 | global $wgExtNewTables; |
| 132 | + |
| 133 | + // create the database structure |
129 | 134 | $wgExtNewTables[] = array( |
130 | 135 | 'sacredtext_verses', |
131 | 136 | dirname( __FILE__ ) . '/SacredText.verses.sql' ); |
132 | | - $wgExtNewTables[] = array( |
133 | | - 'sacredtext_verses_kjv_entire', |
134 | | - dirname( __FILE__ ) . '/data/bible_kjv_entire.sql' ); |
| 137 | + |
| 138 | + // the code below checks if there are any SQL scripts for adding |
| 139 | + // the content of religious scripts to the database |
| 140 | + $dir = dirname( __FILE__ ) . '/data/'; |
| 141 | + |
| 142 | + $h = opendir( $dir ); |
| 143 | + while( $file = readdir( $h ) ) |
| 144 | + { |
| 145 | + if( strcmp($file,'.')==0 || strcmp($file,'..')==0 ) continue; |
| 146 | + if(!is_file($dir.$file)) continue; |
| 147 | + if(!preg_match('/^(.*)\.sql$/', $file, $matches)) continue; |
| 148 | + |
| 149 | + $wgExtNewTables[] = array( |
| 150 | + $matches[1], |
| 151 | + $dir.$file); |
| 152 | + } |
| 153 | + closedir( $h ); |
| 154 | + |
135 | 155 | return true; |
136 | 156 | } |