http://www.mediawiki.org/wiki/Special:Code/MediaWiki/61751

Revision: 61751
Author:   jonwilliford
Date:     2010-01-31 19:04:01 +0000 (Sun, 31 Jan 2010)

Log Message:
-----------
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
    trunk/extensions/SacredText/SacredText.php

Removed Paths:
-------------
    trunk/extensions/SacredText/data/

Modified: trunk/extensions/SacredText/SacredText.lookup.php
===================================================================
--- trunk/extensions/SacredText/SacredText.lookup.php   2010-01-31 18:45:10 UTC 
(rev 61750)
+++ trunk/extensions/SacredText/SacredText.lookup.php   2010-01-31 19:04:01 UTC 
(rev 61751)
@@ -2,11 +2,17 @@
 class SacredTextLookup {
 
        function parseInput( $input, &$book, &$chapternum, &$versenums ) {
-           if( preg_match( "/^\s*([\s\w]*\w+)\s*(\d+):(\d+)/", $input, 
$matches) ) {
+           if( preg_match( "/^\s*([\s\w]*\w+)\s*(\d+):(\d+)(?:-(\d+))?\s*$/", 
$input, $matches) ) {
                $book = $matches[1];
                $chapternum = $matches[2];
                $versenums = array();
-               $versenums[] = $matches[3];
+                       if( count( $matches ) <= 4 ) {
+                       $versenums[] = $matches[3];
+                       }
+                       else for( $x = $matches[3]; $x <= $matches[4]; $x++ ) {
+                               $versenums[] = $x;
+                       }
+                               
                return true;
            } else {
                return false;
@@ -40,8 +46,6 @@
            }
        }
        
-       
-
        function lookup( $religtext, $book, $chapternum, $versenums, $lang, 
$ver ) 
        {
            global $wgSacredChapterAlias;
@@ -71,8 +75,51 @@
            if( $obj ) {
                return htmlspecialchars( $obj->st_text );
            } else {
+                       $r = self::fallback( $religtext, $book, $chapternum, 
$versenums, $lang, $ver );
+                       if( $r ) return htmlspecialchars( $r );
+                       
                return htmlspecialchars( "Could not find: ". $book ." 
".$chapternum.":".$versenums[0]." in the ". $religtext );
        }
        }
 
+       /* This function makes it possible to look for the verse on other 
websites, if the requested verse 
+               cannot be found in the database.
+       */
+       function fallback( $religtext, $book, $chapternum, $versenums, $lang, 
$ver ) 
+       {
+               global $wgSacredFallbackServers;
+           if( !array_key_exists($religtext, $wgSacredFallbackServers) ) 
return false;
+           if( !array_key_exists($lang, $wgSacredFallbackServers[$religtext]) 
) return false;
+               if( !array_key_exists($ver, 
$wgSacredFallbackServers[$religtext][$lang]) ) return false;
+
+               $url0 = 
$wgSacredFallbackServers[$religtext][$lang][$ver]["url"];
+               $regex = 
$wgSacredFallbackServers[$religtext][$lang][$ver]["regex"];
+
+
+               $url0 = str_replace(
+                       
array('$(religtext)','$(lang)','$(ver)','$(book)','$(chapternum)'),
+                       array($religtext,$lang,$ver,$book,$chapternum),
+                       $url0);
+
+               $ret = "";
+               foreach( $versenums as $versenum )
+               {
+                       $url = str_replace('$(versenum)',$versenum, $url0);
+
+                       $h = fopen($url,'r' );
+               $str='';
+                       $length = 8192;
+               while(!feof($h)) $str.=fread($h,$length);
+               fclose($h);
+               $num = preg_match_all( $regex, $str, $matches, 
PREG_PATTERN_ORDER );
+               if( $num ) {
+               $ret .= implode( " ", $matches[1] ) . " ";
+               }
+                       else {
+                               return sprintf("Failed to match pattern %s from 
results from %s.",$regex,$url);
+                       }
+               }
+               if( empty($ret) ) return "Failed to retrieve match from 
'$url'.";
+               else return "$ret";
+       }
 }

Modified: trunk/extensions/SacredText/SacredText.php
===================================================================
--- trunk/extensions/SacredText/SacredText.php  2010-01-31 18:45:10 UTC (rev 
61750)
+++ trunk/extensions/SacredText/SacredText.php  2010-01-31 19:04:01 UTC (rev 
61751)
@@ -22,6 +22,8 @@
 
 // the following are the parameters that can be set in LocalSettings.php
 $wgSacredUseBibleTag = true;
+
+// There needs to be a database table to hold this information, in order to 
make it more manageable.
 $wgSacredChapterAlias = array();
 $wgSacredChapterAlias["Christian Bible"] = array();
 $wgSacredChapterAlias["Christian Bible"]["1Chronicles"]="1 Chronicles";
@@ -113,7 +115,8 @@
 
 $wgHooks['ParserFirstCallInit'][] = 'efSacredTextParserInit';
 $wgHooks['LoadExtensionSchemaUpdates'][] = 'updateSacredTextDB';
- 
+
+// Create the hooks for quoting sacred texts 
 function efSacredTextParserInit( &$parser ) {
        global $wgSacredUseBibleTag;
        $parser->setHook( 'sacredtext', 
array('SacredTextLookup','hookSacredText') );
@@ -125,11 +128,28 @@
 
 function updateSacredTextDB() {
        global $wgExtNewTables;
+
+       // create the database structure
        $wgExtNewTables[] = array(
                'sacredtext_verses',
                dirname( __FILE__ ) . '/SacredText.verses.sql' );
-       $wgExtNewTables[] = array(
-               'sacredtext_verses_kjv_entire',
-               dirname( __FILE__ ) . '/data/bible_kjv_entire.sql' );
+
+       // the code below checks if there are any SQL scripts for adding
+       //  the content of religious scripts to the database
+       $dir = dirname( __FILE__ ) . '/data/';
+
+    $h = opendir( $dir );
+    while( $file = readdir( $h ) )
+    {
+        if( strcmp($file,'.')==0 || strcmp($file,'..')==0 ) continue; 
+        if(!is_file($dir.$file)) continue;
+        if(!preg_match('/^(.*)\.sql$/', $file, $matches)) continue;  
+
+        $wgExtNewTables[] = array(
+            $matches[1],
+            $dir.$file);
+    }
+    closedir( $h );
+
        return true;
 }



_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to