Gospel Translations:Technology/Multi-wiki project/trans bot.php

From Gospel Translations

Revision as of 17:24, 2 September 2008 by Mahra (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to:navigation, search
  1. <?php
  2. // I'm a DEMO bot. You don't have to use this file unless you want to.
  3.  
  4. // Runs recently changed summaries through some filters.
  5. // Just run me once each day or so--less often on less busy sites.
  6.  
  7. // define these BEFORE including BasicBot.php if you want to override the settings in BasicBot.php:
  8. // define('USERID','2');
  9. // define('USERNAME','RCBot');
  10. // define('PASSWORD','RCBotPassword');
  11.  
  12. require_once('basic_bot_intl.php');
  13. require_once('basic_bot.php');
  14. require_once('Snoopy.class.php');
  15. require_once('/home/gospeltr/public_html/w/languages/Names.php');
  16. global $wgLanguageNames;
  17. $phpFile = '/home/gospeltr/public_html/w/extensions/dabble/cache/db.php';
  18. $TITLES;
  19.  
  20. // print form if no data was submitted
  21. if(!isset($_GET['whattodo'])) {
  22. 	print <<<EOT
  23. 	<h2>What language should we process?</h2>
  24. 	<form action="trans_bot.php" method="GET" name="bot">
  25. 	<input type="text" name="lang" value="language">
  26. 	<input type="text" name="lc" size="2" value="LC">
  27. 	<input type="text" name="page" value="page to scrape">
  28. 	<input type="text" name="whattodo" value="(blank)/run/print/titles">
  29. 	<input type="submit" value="submit">
  30. 	</form>
  31. EOT;
  32.  
  33. 	exit; 
  34. } 
  35.  
  36. if($_GET['whattodo'] === 'print') {
  37. 	// get the library of translations loaded up
  38. 	$ldb = unserialize(file_get_contents($phpFile)); 
  39. 	$LANGS = $ldb['l'];
  40. 	$TERMS = $ldb['g'];
  41. 	$db = $ldb['t'];
  42. 	$RL = $_GET['lang'];
  43. 	$allTranslations;
  44.  
  45. 	// go grab the list of articles and titles
  46. 	$fetcher = new BasicBotIntl();
  47. 	$fetcher->wikiFilter($_GET['page'],'ScrapePage',$summary='');
  48.  
  49. 	print_r($allTranslations);
  50.  
  51. 	exit; 
  52. } 
  53.  
  54. if($_GET['whattodo'] === 'titles') {
  55.  
  56. 	// get the library of translations loaded up
  57. 	$TITLES = unserialize(file_get_contents('/home/gospeltr/public_html/w/extensions/dabble/cache/titles.php')); 
  58. 	$RL = $_GET['lang'];
  59. 	$botpage = "Category:" . $RL;
  60.  
  61. 	// go grab the list of articles and titles
  62. 	$gatherTitles = new BasicBot();
  63. 	$gatherTitles->SpecialFilterAll($botpage,'GatherTitles',$summary='');
  64.  
  65. 	exit; 
  66. } 
  67.  
  68. // get the library of translations loaded up
  69. $ldb = unserialize(file_get_contents($phpFile)); 
  70. $LANGS = $ldb['l'];
  71. $TERMS = $ldb['g'];
  72. $db = $ldb['t'];
  73. $RL = $_GET['lang'];
  74. $allTranslations;
  75.  
  76. // go grab the list of articles and titles
  77. $fetcher = new BasicBotIntl();
  78. $fetcher->wikiFilter($_GET['page'],'ScrapePage',$summary='');
  79.  
  80. $myl = array_keys($allTranslations);
  81. $flip = array_flip($allTranslations);  
  82.  
  83. $copyBot = new BasicBotIntl();
  84. $copyBot->ArrayFilterAll($myl,'PostCode',$summary='Automated: Added transclusion reference and categories to article source',$allTranslations);
  85.  
  86. function PostCode( $content, $params ){
  87.  
  88. 	global $wgLanguageNames;
  89. 	global $db;
  90. 	global $TERMS;
  91. 	global $RL;
  92. 	$resLang = $RL;
  93. 	global $passv;
  94. 	$res = $params[$passv];
  95.  
  96. 	/////////////////////
  97. 	// break the language code off the title
  98. 	$titleParts = explode('/', $res);
  99. 	$lastPart = end($titleParts);
  100. 	// test for any 2-3 letter sequence ending the title after a /
  101. 	if(isset($wgLanguageNames[$lastPart])) {
  102. 			$old_res = $res;
  103. 			$res = str_ireplace('/'.$lastPart, '', $res);
  104. 	}
  105. 	else {
  106. 		print "Error: $res is not a foreign resource"; 
  107. 		exit;
  108. 	}
  109. 	// Decide whether this belongs to a book or a series
  110. 	if ($db[$resLang][$res]['Media Type'] == 'Chapter' || $db[$resLang][$res]['Media Type'] == 'Book')
  111. 			$stext = 'Book';
  112. 	else
  113. 			$stext = 'Series'; 
  114.  
  115. 	$flip = array_flip($params);
  116. 	if ($db[$resLang][$res]['Media Type'] == 'Book'){
  117. 			$book = $db[$resLang][$res]['Book/series'];
  118. 			$toc = "===".$TERMS['Table of Contents'][$resLang] . "=== \n";
  119. 			$chaps = count($db['TOC'][$book]);
  120. 			for ($i=1; $i<=$chaps; $i++) {
  121. 					$chapter = $db['TOC'][$book][$i];
  122. 					//$chapter = str_ireplace($book.'/', '', $chapter);
  123. 					$chapter = $chapter . '/' . $lastPart;
  124. 					$tch = $flip[$chapter];
  125. 					$tch = str_replace('_', ' ', $tch);
  126. 					$short_tch = explode('/', $tch);
  127. 					$toc = $toc . "[[$tch|{$short_tch[1]}]] \n\n";
  128. 			}
  129.  
  130. //			print $allTranslations;
  131. 	//		print "\n\n";
  132. 	//		print $toc;
  133. 	//		exit;
  134.  
  135. 			return <<<TOC
  136. 	<div id="tran-mh">
  137. <div style="float:right">
  138. <table style="border: 1px solid rgb(148, 148, 148); margin: 0 0pt 10px 10px; padding: 3px 4px 5px 4px; background-color: rgb(233, 242, 254); color: rgb(51, 51, 51); font-family: Verdana,Arial,Helvetica,sans-serif; font-size: 10px; line-height: 13px; width: 200px;" cellspacing="0" cellpadding="0">
  139.  <tr>
  140.  <td colspan="2" style="padding: 10px 0; text-align:center"><font style="font-size: 12px; font-weight: bold;">{$TERMS['About This'][$resLang]} {$TERMS[$db[$resLang][$res]['Media Type']][$resLang]}</font></td>
  141.  </tr>
  142.  <tr>
  143.  <td valign="top" width="85"><strong>{$TERMS['Publisher'][$resLang]}:</strong></td>
  144.  <td valign="top">[[:Category:{$db[$resLang][$res]['Publisher']}|{$db[$resLang][$res]['Publisher']}]]</td>
  145.  </tr>
  146.  <tr>
  147.  <td valign="top"><strong>{$TERMS['Author'][$resLang]}:</strong></td>
  148.  <td valign="top">[[:Category:{$db[$resLang][$res]['Author']}|{$db[$resLang][$res]['Author']}]] $satext</td>
  149.  </tr> 
  150.  <tr> 
  151.  <td valign="top"><strong>{$TERMS[$stext][$resLang]}:</strong></td>
  152.  <td valign="top">[[:Category:{$db[$resLang][$res]['Book/series']}|{$db[$resLang][$res]['Book/series']}]]</td>
  153.  </tr>
  154.  <tr>       
  155.  <td valign="top"><strong>{$TERMS['Topic'][$resLang]}:</strong></td>
  156.  <td valign="top">[[:Category:{$TERMS[$db[$resLang][$res]['Topic']][$resLang]}|{$TERMS[$db[$resLang][$res]['Topic']][$resLang]}]]</td>
  157.  </tr>
  158.  <tr>
  159.  <td valign="top"><strong>{$TERMS['Subtopic'][$resLang]}:</strong></td>
  160.  <td valign="top">[[:Category:{$TERMS[$db[$resLang][$res]['Subtopic']][$resLang]}|{$TERMS[$db[$resLang][$res]['Subtopic']][$resLang]}]]</td>
  161.  </tr>
  162.  <tr>
  163.  <td valign="top" style="padding-bottom: 4px;"><strong>{$TERMS['Date'][$resLang]}:</strong></td>
  164.  <td valign="top" style="padding-bottom: 4px;">[[:Category:{$db[$resLang][$res]['Date']}|{$db[$resLang][$res]['Date']}]]</td>
  165.  </tr>
  166.  <tr>
  167.  <td valign="top" style="border-top: 1px solid #999; padding-top: 4px; margin-top: 4px;"><strong>{$TERMS['Status'][$resLang]}:</strong></td>
  168.  <td valign="top" style="border-top: 1px solid #999; padding-top: 4px; margin-top: 4px;">[[:Category:{$db[$resLang][$res]['Review Status']}|{$db[$resLang][$res]['Review Status']}]]</td>
  169.  </tr>
  170.  <tr>
  171.  <td valign="top"><strong>{$TERMS['Translator'][$resLang]}:</strong></td>
  172.  <td valign="top">[[:Category:{$db[$resLang][$res]['Translator']}|{$db[$resLang][$res]['Translator']}]]</td>
  173.  </tr>
  174.  <tr>
  175.  <td style="padding-bottom: 4px;"><strong>{$TERMS['Reviewer'][$resLang]}:</strong></td>
  176.  <td style="padding-bottom: 4px;">[[:Category:{$db[$resLang][$res]['Reviewer']}|{$db[$resLang][$res]['Reviewer']}]]</td>
  177.  </tr>
  178. </table>
  179. </div>
  180. [[Category:{$db[$resLang][$res]['Review Status']}]]
  181. [[Category:{$TERMS[$db[$resLang][$res]['Media Type']][$resLang]}]]
  182. [[Category:{$TERMS[$db[$resLang][$res]['Topic']][$resLang]}]]
  183. [[Category:{$TERMS[$db[$resLang][$res]['Subtopic']][$resLang]}]]
  184. [[Category:{$db[$resLang][$res]['Author']}]] 
  185. [[Category:{$db[$resLang][$res]['Publisher']}]]
  186. [[Category:{$db[$resLang][$res]['Book/series']}]]<!--
  187. ###### Needs Review template
  188. {{ #ifeq: {{#language:{{#titleparts:{{PAGENAME}}|{{{levels}}}|{{#expr: {{{levels}}} +1 }}}}}} | English | |{{ #ifeq: {{{reviewed}}} | Final Version | | {{ #ifeq: {{{reviewed}}} | Peer Reviewed | {{PeerReviewed}} | {{NeedsReview}}}}}}}}
  189. --></div>
  190. $toc
  191.  
  192. TOC;
  193. 	}
  194. 	else {
  195. 		return <<<TRB
  196. 	<div id="tran-mh">
  197. <div style="float:right">
  198. <table style="border: 1px solid rgb(148, 148, 148); margin: 0 0pt 10px 10px; padding: 3px 4px 5px 4px; background-color: rgb(233, 242, 254); color: rgb(51, 51, 51); font-family: Verdana,Arial,Helvetica,sans-serif; font-size: 10px; line-height: 13px; width: 200px;" cellspacing="0" cellpadding="0">
  199.  <tr>
  200.  <td colspan="2" style="padding: 10px 0; text-align:center"><font style="font-size: 12px; font-weight: bold;">{$TERMS['About This'][$resLang]} {$TERMS[$db[$resLang][$res]['Media Type']][$resLang]}</font></td>
  201.  </tr>
  202.  <tr>
  203.  <td valign="top" width="85"><strong>{$TERMS['Publisher'][$resLang]}:</strong></td>
  204.  <td valign="top">[[:Category:{$db[$resLang][$res]['Publisher']}|{$db[$resLang][$res]['Publisher']}]]</td>
  205.  </tr>
  206.  <tr>
  207.  <td valign="top"><strong>{$TERMS['Author'][$resLang]}:</strong></td>
  208.  <td valign="top">[[:Category:{$db[$resLang][$res]['Author']}|{$db[$resLang][$res]['Author']}]] $satext</td>
  209.  </tr> 
  210.  <tr> 
  211.  <td valign="top"><strong>{$TERMS[$stext][$resLang]}:</strong></td>
  212.  <td valign="top">[[:Category:{$db[$resLang][$res]['Book/series']}|{$db[$resLang][$res]['Book/series']}]]</td>
  213.  </tr>
  214.  <tr>       
  215.  <td valign="top"><strong>{$TERMS['Topic'][$resLang]}:</strong></td>
  216.  <td valign="top">[[:Category:{$TERMS[$db[$resLang][$res]['Topic']][$resLang]}|{$TERMS[$db[$resLang][$res]['Topic']][$resLang]}]]</td>
  217.  </tr>
  218.  <tr>
  219.  <td valign="top"><strong>{$TERMS['Subtopic'][$resLang]}:</strong></td>
  220.  <td valign="top">[[:Category:{$TERMS[$db[$resLang][$res]['Subtopic']][$resLang]}|{$TERMS[$db[$resLang][$res]['Subtopic']][$resLang]}]]</td>
  221.  </tr>
  222.  <tr>
  223.  <td valign="top" style="padding-bottom: 4px;"><strong>{$TERMS['Date'][$resLang]}:</strong></td>
  224.  <td valign="top" style="padding-bottom: 4px;">[[:Category:{$db[$resLang][$res]['Date']}|{$db[$resLang][$res]['Date']}]]</td>
  225.  </tr>
  226.  <tr>
  227.  <td valign="top" style="border-top: 1px solid #999; padding-top: 4px; margin-top: 4px;"><strong>{$TERMS['Status'][$resLang]}:</strong></td>
  228.  <td valign="top" style="border-top: 1px solid #999; padding-top: 4px; margin-top: 4px;">[[:Category:{$db[$resLang][$res]['Review Status']}|{$db[$resLang][$res]['Review Status']}]]</td>
  229.  </tr>
  230.  <tr>
  231.  <td valign="top"><strong>{$TERMS['Translator'][$resLang]}:</strong></td>
  232.  <td valign="top">[[:Category:{$db[$resLang][$res]['Translator']}|{$db[$resLang][$res]['Translator']}]]</td>
  233.  </tr>
  234.  <tr>
  235.  <td style="padding-bottom: 4px;"><strong>{$TERMS['Reviewer'][$resLang]}:</strong></td>
  236.  <td style="padding-bottom: 4px;">[[:Category:{$db[$resLang][$res]['Reviewer']}|{$db[$resLang][$res]['Reviewer']}]]</td>
  237.  </tr>
  238. </table>
  239. </div>
  240. [[Category:{$db[$resLang][$res]['Review Status']}]]
  241. [[Category:{$TERMS[$db[$resLang][$res]['Media Type']][$resLang]}]]
  242. [[Category:{$TERMS[$db[$resLang][$res]['Topic']][$resLang]}]]
  243. [[Category:{$TERMS[$db[$resLang][$res]['Subtopic']][$resLang]}]]
  244. [[Category:{$db[$resLang][$res]['Author']}]] 
  245. [[Category:{$db[$resLang][$res]['Publisher']}]]
  246. [[Category:{$db[$resLang][$res]['Book/series']}]]<!--
  247. ###### Needs Review template
  248. {{ #ifeq: {{#language:{{#titleparts:{{PAGENAME}}|{{{levels}}}|{{#expr: {{{levels}}} +1 }}}}}} | English | |{{ #ifeq: {{{reviewed}}} | Final Version | | {{ #ifeq: {{{reviewed}}} | Peer Reviewed | {{PeerReviewed}} | {{NeedsReview}}}}}}}}
  249. --></div>
  250. {{:gt_en:{$params[$passv]}}}
  251.  
  252. TRB;
  253. 	}
  254. }
  255.  
  256. function ScrapePage($content){
  257. 	global $allTranslations;
  258. 	global $wgLanguageNames;
  259. 	global $db;
  260. 	global $RL;
  261. 	$data_lines = explode(']]]', $content);
  262. 	array_pop($data_lines);
  263.  
  264. 	foreach ($data_lines as $tvalue){
  265. 		// $data_line is english_title:::trans_title
  266. 		$translation = explode(':::', $tvalue);
  267. 		// load up $allTranslations[foreign] = english
  268. 		$foreign = str_ireplace(" ","_", $translation[1]);
  269.  
  270. 		$titleParts = explode('/', $translation[0]);
  271. 		$lastPart = end($titleParts);
  272. 		$res = str_ireplace('/'.$lastPart, '', $translation[0]);
  273. 		// set aside the resource temporarily if it is a book part
  274. 		$short_res = $titleParts[0]."/".$titleParts[1];
  275. 		if(isset($db[$RL][$short_res]['Media Type'])){
  276. 			if ($db[$RL][$short_res]['Media Type'] == 'Chapter') {
  277. 				$temp_array[$translation[0]] = $foreign;
  278. 			}
  279. 		}
  280. 		// ........otherwise just load it up
  281. 		else
  282. 			$allTranslations[$foreign] = $translation[0];
  283.   	}	
  284.  
  285. 	// $flip will be $..[eng] => foreign
  286. 	$flip = array_flip($allTranslations);
  287. 	foreach ($temp_array as $eng=>$for){
  288. 		$temp_res_array = explode('/', $eng);
  289. 		$temp_book = $temp_res_array[0].'/'.$temp_res_array[2];
  290. 		$for_book = $flip[$temp_book];
  291. 		$for_full_title = $for_book.'/'.$for;
  292. 		$allTranslations[$for_full_title] = $eng;
  293. 	}
  294.  
  295. 		//print_r($temp_array);
  296.  
  297. 	return $content;
  298. }
  299.  
  300. function GatherTitles ( $content ) {
  301.  
  302. 		global $TITLES;
  303. 		global $passv;
  304. 		$final; // holds the article title (translated)
  305.  
  306. 		preg_match('/{{info(.)*(}){2,}/i', $content, $match);
  307. 		$final = str_replace('{{info|', '', $match[0]);
  308. 		$final = str_replace('}', '', $final);
  309. 		$TITLES[$passv] = $final;
  310.  
  311.  
  312. 		// save the stuff
  313. 		$fp = fopen('/home/gospeltr/public_html/w/extensions/dabble/cache/titles.php', 'w+') or die("I could not open the file.");
  314. 		fwrite($fp, serialize($TITLES));
  315. 		fclose($fp) or die("I could save the file.");
  316.  
  317. 		return $content;
  318. }
  319.  
  320. echo "Done --" . $passv;
  321.  
  322. ?>
Navigation
Volunteer Tools
Other Wikis
Toolbox