r61751 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r61750‎ | r61751 | r61752 >
Date:19:04, 31 January 2010
Author:jonwilliford
Status:resolved (Comments)
Tags:
Comment:
Removing the scripture texts from the MediaWiki database. Modified the code in SacredText.php to automatically add any sql scripts from the data directory (to make it easier to add the SQL scripts from other repositories). Made a fallback mechanism that can query other websites for missing verses or translations. Still have one fix from r61323 remaining.
Modified paths:
  • /trunk/extensions/SacredText/SacredText.lookup.php (modified) (history)
  • /trunk/extensions/SacredText/SacredText.php (modified) (history)
  • /trunk/extensions/SacredText/data (deleted) (history)

Diff [purge]

Index: trunk/extensions/SacredText/SacredText.lookup.php
@@ -2,11 +2,17 @@
33 class SacredTextLookup {
44
55 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) ) {
77 $book = $matches[1];
88 $chapternum = $matches[2];
99 $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+
1117 return true;
1218 } else {
1319 return false;
@@ -40,8 +46,6 @@
4147 }
4248 }
4349
44 -
45 -
4650 function lookup( $religtext, $book, $chapternum, $versenums, $lang, $ver )
4751 {
4852 global $wgSacredChapterAlias;
@@ -71,8 +75,51 @@
7276 if( $obj ) {
7377 return htmlspecialchars( $obj->st_text );
7478 } else {
 79+ $r = self::fallback( $religtext, $book, $chapternum, $versenums, $lang, $ver );
 80+ if( $r ) return htmlspecialchars( $r );
 81+
7582 return htmlspecialchars( "Could not find: ". $book ." ".$chapternum.":".$versenums[0]." in the ". $religtext );
7683 }
7784 }
7885
 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+ }
79126 }
Index: trunk/extensions/SacredText/SacredText.php
@@ -22,6 +22,8 @@
2323
2424 // the following are the parameters that can be set in LocalSettings.php
2525 $wgSacredUseBibleTag = true;
 26+
 27+// There needs to be a database table to hold this information, in order to make it more manageable.
2628 $wgSacredChapterAlias = array();
2729 $wgSacredChapterAlias["Christian Bible"] = array();
2830 $wgSacredChapterAlias["Christian Bible"]["1Chronicles"]="1 Chronicles";
@@ -113,7 +115,8 @@
114116
115117 $wgHooks['ParserFirstCallInit'][] = 'efSacredTextParserInit';
116118 $wgHooks['LoadExtensionSchemaUpdates'][] = 'updateSacredTextDB';
117 -
 119+
 120+// Create the hooks for quoting sacred texts
118121 function efSacredTextParserInit( &$parser ) {
119122 global $wgSacredUseBibleTag;
120123 $parser->setHook( 'sacredtext', array('SacredTextLookup','hookSacredText') );
@@ -125,11 +128,28 @@
126129
127130 function updateSacredTextDB() {
128131 global $wgExtNewTables;
 132+
 133+ // create the database structure
129134 $wgExtNewTables[] = array(
130135 'sacredtext_verses',
131136 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+
135155 return true;
136156 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r62209Moving code back to googlecode.com repository until I get time to address r61...jonwilliford00:58, 10 February 2010

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r61323Initial commit of SacredText project.jonwilliford04:05, 21 January 2010

Comments

#Comment by JonathanWilliford (talk | contribs)   19:29, 31 January 2010

I still need to fix this -> Special:Code/MediaWiki/61323#c5392.

#Comment by 😂 (talk | contribs)   19:35, 31 January 2010
  1. Please read Manual:Coding conventions and follow its suggestions regarding formatting
  2. Do not use array_key_exists(), use isset()
  3. Don't use fopen() on remote URLs. Firstly because it might be disabled and you will throw errors if it is with the way you've written it. Secondly, cURL is much better. So we have easy-to-use Http::get() and Http::post() methods to do all the heavy lifting for you. They return false or your content as a string.
  4. If you're introducing new configuration variables, you need to set defaults and document them

Status & tagging log