function do_proc_interest($intrestKey, $intrestName)
{
    global $mvMaxContribPerInterest, $mvMaxForAgainstBills;
    include_once 'scrape_and_insert.inc.php';
    $mvScrape = new MV_BillScraper();
    $raw_results = $mvScrape->doRequest('http://www.maplight.org/map/us/interest/' . $intrestKey . '/view/all');
    $page_body = '{{Interest Group|' . "\n";
    $page_body .= 'MAPLight Interest ID=' . $intrestKey . "|\n";
    // get all people contributions:
    preg_match_all('/\\/map\\/us\\/legislator\\/([^"]*)">.*\\$([^<]*)</U', $raw_results, $matches);
    if (isset($matches[2])) {
        $i = 0;
        foreach ($matches[1] as $i => $person_id) {
            $hr_inx = $i + 1;
            // we have to lookup the name:
            $personName = $mvScrape->get_wiki_name_from_maplightid($person_id);
            if ($personName) {
                $page_body .= "Funded Name {$hr_inx}=" . $personName . "|\n";
                $page_body .= "Funded Amount {$hr_inx}=" . str_replace(',', '', $matches[2][$i]) . "|\n";
            }
            if ($hr_inx == $mvMaxContribPerInterest) {
                break;
            }
            $i++;
        }
    }
    $intrest_bills_url = 'http://maplight.org/map/us/interest/' . $intrestKey . '/bills';
    $raw_results = $mvScrape->doRequest($intrest_bills_url);
    // get all bills supported or opposed
    preg_match_all('/\\/map\\/us\\/bill\\/([^"]*)".*\\/map\\/us\\/legislator.*<td>([^<]*)</U', $raw_results, $matches);
    print 'bill:' . $intrest_bills_url . "\n";
    //die;
    $sinx = $oinx = 1;
    if (isset($matches[1][0])) {
        $support_count = $oppse_count = 0;
        foreach ($matches[1] as $i => $bill_id) {
            // skip if we are maxed out
            if ($support_count == $mvMaxForAgainstBills) {
                continue;
            }
            if ($oppse_count == $mvMaxForAgainstBills) {
                continue;
            }
            $hr_inx = $i + 1;
            $bill_name = $mvScrape->get_bill_name_from_mapLight_id($bill_id);
            if ($matches[2][$i] == 'Support') {
                $page_body .= "Supported Bill {$sinx}=" . str_replace('_', ' ', $bill_name) . "|\n";
                $sinx++;
            } elseif ($matches[2][$i] == 'Oppose') {
                $page_body .= "Opposed Bill {$oinx}=" . str_replace('_', ' ', $bill_name) . "|\n";
                $oinx++;
            }
        }
    }
    $page_body .= '}}';
    $wTitle = Title::makeTitle(NS_MAIN, $intrestName);
    print "Interest: ";
    do_update_wiki_page($wTitle, $page_body);
    print "\n";
}
function append_to_wiki_page($wgTitle, $append_text, $unique = true)
{
    global $botUserName;
    if ($wgTitle->exists()) {
        $wgArticle = new Article($wgTitle);
        $cur_text = $wgArticle->getContent();
        if ($unique) {
            if (strpos($cur_text, $append_text) !== false) {
                print "no insert {$append_text} already present\n";
                return;
            }
        }
        $cur_text .= "\n\n" . $append_text;
        //do the edit:
        $sum_txt = 'metavid append';
        $wgArticle->doEdit($cur_text, $sum_txt);
        print "did append on " . $wgTitle->getDBkey() . "\n";
    } else {
        print "append request to empty page... creating\n";
        do_update_wiki_page($wgTitle, $append_text);
    }
}
function upTemplates( $templateName, $force = false ) {
	global $valid_attributes;

if($templateName=='Ht_en'){
/***************************************************
 * Transcripts:
 * updates transcript templates
 ***************************************************/
 	$wgTemplateTitle = Title :: makeTitle( NS_TEMPLATE, 'Ht_en' );
	do_update_wiki_page( $wgTemplateTitle, '<noinclude>
		This is the default Template for the display of transcript text.
		</noinclude><includeonly>{{ #if:  {{{PersonName|}}} | {{ #ifexist: Image:{{{PersonName}}}.jpg | [[Image:{{{PersonName}}}.jpg|44px|left]]|[[Image:Missing person.jpg|44px|left]]}} |}}{{ #if:{{{PersonName|}}}|[[{{{PersonName}}}]]: |}}{{{BodyText}}}
		</includeonly>', null, $force );
}
if($templateName=='archive_org_ftypes'){
/****************************************************
 * Archive.org file type semantics
 ****************************************************/
	$archive_org_ftypes = array( '64Kb_MPEG4', '256Kb_MPEG4', 'MPEG1', 'MPEG2', 'flash_flv' );
	foreach ( $archive_org_ftypes as $ftype ) {
		$pTitle = Title::makeTitle( SMW_NS_PROPERTY, 'Ao_file_' . $ftype );
		do_update_wiki_page( $pTitle, '[[has type::URL]]', null, $force );
	}
}
if($templateName=='Bill'){
/*****************************************************
 * Bill Templates
 ****************************************************/
	$bill_template = '<noinclude>Bill Person Template simplifies the structure of articles about Bills.
	<pre>{{Bill|
	GovTrackID=The GovTrack Bill ID (used to key-into GovTracks Bill info)|
	ThomasID=The bill\'s Thomas id (used for Thomas linkback)|
	MAPLightBillID=The Map light Bill ID (used for supporting and opposing interest)|
	OpenCongressBillID=The open congress bill id (used for bill rss feeds)|
	Title Description=The short title/description of the bill|
	Date Introduced=The date the bill was introduced|
	Session=The session of congress (110 for 2007-08, 109 for 2005-2006 etc)|
	Bill Key=The short bill name ie: H.R. #|
	Sponsor=Who the Bill was Sponsored By|
	Cosponsor #=Cosponsor, Where # is 1-70 for listing all cosponsors|
	Supporting Interest #=Interest, Where # is 1-20 for listing top supporting interests|
	Opposing Interest #=Interest, Where # is 1-20 for listing top opposing interests|
	}}</pre>The template name (Bill) should be given as the \'\'first\'\' thing on a page. The Cosponsored list should come at the end.
	</noinclude><includeonly>
	==Bill [[Bill Key:={{{Bill Key}}}]] in the {{ #if: {{{Session|}}}| [[Congress Session:={{{Session}}}]] |}} of Congress==
	<span style="float:right">{{navimg|xsize=50|ysize=50|image=Crystal_Clear_mimetype_video_search.png|link=Category:{{{Bill Key}}}}}</span>
	{{ #if: {{{Title Description|}}}|{{{Title Description}}} |}}
	{{ #if: {{{Bill Key|}}}| <br />Media in [[:Category:{{{Bill Key}}}]] |}}
	{{ #if: {{{Date Introduced|}}}|* Date Introduced: [[Date Bill Introduced:={{{Date Introduced}}}]] |}}
	{{ #if: {{{Sponsor|}}}|* Sponsor: [[Bill Sponsor:={{{Sponsor}}}]] |}}';
	$bill_template .= '
	{{ #if: {{{Cosponsor 1|}}}|* Cosponsor: [[Bill Cosponsor:={{{Cosponsor 1}}}]] |}}';

	// $bill_template.='{{ #for: {{{n}}} | {{{Cosponsor $n$}}}<br /> }}';
	for ( $i = 2; $i < 70; $i++ ) {
	$bill_template .= '{{ #if: {{{Cosponsor ' . $i . '|}}}|, [[Bill Cosponsor:={{{Cosponsor ' . $i . '}}}]] |}}';
	}
	// output mapLight info if present:
	$bill_template .= '{{ #if: {{{Supporting Interest 1|}}}|<h2>Intrests who <span style="color:green">support</span> bill becoming law</h2>' . "\n[[Data_Source_URL:=http://maplight.org/map/us/bill/{{{MapLightBillID}}}|MAPLight Source]]" . ' |}}';
	for ( $i = 1; $i < 20; $i++ ) {
	$bill_template .= '{{ #if: {{{Supporting Interest ' . $i . '|}}}|* [[Supporting Interest:={{{Supporting Interest ' . $i . '}}}]]' . "\n" . ' |}}';
	}
	$bill_template .= '{{ #if: {{{Opposing Interest 1|}}}|<h2>Interests who <span style="color:red">oppose</span> bill becoming law</h2>' . "\n" . ' |}}';
	for ( $i = 1; $i < 20; $i++ ) {
	$bill_template .= '{{ #if: {{{Opposing Interest ' . $i . '|}}}|* [[Opposing Interest:={{{Opposing Interest ' . $i . '}}}]]' . "\n" . '|}}';
	}
	// @@todo could do inline rss once we get a good cache model for http://www.mediawiki.org/wiki/Extension:RSS_Reader
	// maybe just action=purge on as a cron job, with $parser->disableCache(); commented out
	$bill_template .= '
	{{ #if: {{{OpenCongressBillID|}}}|==Bill RSS Feeds==
	* In the News [http://www.opencongress.org/bill/{{{OpenCongressBillID|}}}/atom_news]
	* Blog Coverage [http://www.opencongress.org/bill/{{{OpenCongressBillID|}}}/atom_blogs]
	* Bill Actions [http://www.opencongress.org/bill/{{{OpenCongressBillID|}}}/atom][[Open Congress Bill ID:={{{OpenCongressBillID|}}}|]]
	|}}';

	$bill_template .= '
	==Data Sources==
	{{ #if: {{{ThomasID|}}}|* [[Metavid Sources#Thomas|Thomas]] Official Bill Information:[[Data_Source_URL:=http://thomas.loc.gov/cgi-bin/bdquery/z?{{{ThomasID}}}:]] [[Thomas Bill ID:={{{ThomasID}}}| ]]
	|}}{{ #if: {{{GovTrackID|}}}|* [[Metavid Sources#GovTrack|GovTrack]] Bill Overview:[[Data_Source_URL:=http://www.govtrack.us/congress/bill.xpd?bill={{{GovTrackID}}}]] [[GovTrack Bill ID:={{{GovTrackID}}}| ]]
	|}}{{ #if: {{{MapLightBillID|}}}|* [[Metavid Sources#MapLight|MapLight]] Bill Overview:[[Data_Source_URL:=http://maplight.org/map/us/bill/{{{MapLightBillID}}}]] [[MAPLight_Bill_ID:={{{MapLightBillID}}}| ]]
	|}}
	[[Category:Bill]]
	</includeonly>';
	// update bill template:
	$wgTemplateTitle = Title :: makeTitle( NS_TEMPLATE, 'Bill' );
		do_update_wiki_page( $wgTemplateTitle, $bill_template , null, $force );

	// update semnatic property types:
	foreach ( array( 'Page' => array( 'Bill Key', 'Bill Sponsor', 'Bill Cosponsor', 'Congress Session' ),
				  'String' => array( 'Thomas Bill ID', 'Open_Congress_Bill_ID', 'GovTrack_Bill_ID', 'MAPLight_Bill_ID' ),
				  'URL' => array( 'Data_Source_URL' ),
				  'Date' => array( 'Date_Bill_Introduced' )
	) as $type => $type_set ) {
		foreach ( $type_set as $propName ) {
				$wgPropTitle = Title::newFromText( $propName, SMW_NS_PROPERTY );
					do_update_wiki_page( $wgPropTitle, '[[has type::' . $type . ']]', null, $force );
			}
	}
}
if($templateName=='Interest Group'){
/***************************************
 * Interest Group templates:
 **************************************/

	  global $mvMaxContribPerInterest, $mvMaxForAgainstBills;
	 $interest_template = '<noinclude>Interest Group Template simplifies the structure of articles about Interest Groups.
	<pre>{{Interest Group|
	MAPLight Interest ID=The MapLight Interest ID|
	Funded Name #=funded name where 1 is 1-' . $mvMaxContribPerInterest . ' for top ' . $mvMaxContribPerInterest . ' contributions|
	Funded Amount #=funded amount to name 1 (required pair to funded name #)|
	Supported Bill #=Bills the Interest group supported (long name) 1-' . $mvMaxForAgainstBills . '|
	Opposed Bill #=Bills Interest group Opposed (long name) 1-' . $mvMaxForAgainstBills . '|
	}}</pre>
	</noinclude><includeonly>
	{{ #if: {{{Funded Name 1|}}}|==Recipients Funded==
	Showing contributions 2001-2008 Senate / 2005-2008 House [[Data_Source_URL:=http://maplight.org/map/us/interest/{{{MAPLight Interest ID}}}/view/all|MAPLight source]]
	|}}';
	/**
	 * output top $mvMaxContribPerInterest contributers
	 */
	for ( $i = 1; $i <= $mvMaxContribPerInterest; $i++ ) {
		if ( $i <= 10 ) { // only display 10:
			$interest_template .= '{{ #if: {{{Funded Name ' . $i . '|}}}|*[[Funded:={{{Funded Name ' . $i . '}}};{{{Funded Amount ' . $i . '}}}]] |}}';
			if ( $i != 10 )$interest_template .= "\n";
		} else {
			$interest_template .= '{{ #if: {{{Funded Name ' . $i . '|}}}|*[[Funded:={{{Funded Name ' . $i . '}}};{{{Funded Amount ' . $i . '}}}|]] |}}';
		}
	}

	/**
	 * output bills supported / opposed template vars:
	 */
	foreach ( array( 'Supported', 'Opposed' ) as $pos ) {
		// $interest_template.='\nShowing contributions 2001-2008 Senate / 2005-2008 House [[Data_Source_URL:=http://maplight.org/map/us/interest/{{{MAPLight Interest ID}}}|MAPLight source]]';
		$interest_template .= '{{ #if: {{{' . $pos . ' Bill 1|}}}|<h3>' . $pos . ' Bills</h3>
	Pulled from maplight [[Data_Source_URL:=http://maplight.org/map/us/interest/{{{MAPLight Interest ID}}}/bills|source]]
	|}}';
		for ( $i = 1; $i <= $mvMaxForAgainstBills; $i++ ) {
			$interest_template .= '{{ #if: {{{' . $pos . ' Bill ' . $i . '|}}}|*[[' . $pos . ' Bill:={{{' . $pos . ' Bill ' . $i . '}}}]]
	|}}';
		}
	}
	$interest_template .= '[[Category:Interest Group]]
	</includeonly>';

	$wgTemplateTitle = Title :: makeTitle( NS_TEMPLATE, 'Interest Group' );
	do_update_wiki_page( $wgTemplateTitle, $interest_template , null, $force );

	$wgPropTitle = Title::newFromText( 'Funded', SMW_NS_PROPERTY );
	do_update_wiki_page( $wgPropTitle, '[[has type:=Page;Number]]', null, $force );
}
if($templateName=='Congress_Person'){
	$template_body = '<noinclude>Congress Person template simplifies
			the structure of articles about Congress People.
			<pre>{{Congress Person|'."\n";
	foreach ( $valid_attributes as $dbKey => $attr ) {
		list ( $name, $desc ) = $attr;
		$template_body .= $name . '=' . $desc . "|\n";
	}
$template_body .='
}}</pre>The order of the fields is not relevant. The template name (Congress Person) should be given as the \'\'first\'\' thing on a page.
</noinclude><includeonly> __NOTOC__ __NOEDITSECTION__ <div id="NOTITLEHACK"></div><div id="profile"><p>{{ #if: {Image:{{PAGENAME}}.jpg}| [[Image:{{PAGENAME}}.jpg]]
|}}<span>{{PAGENAME}} ({{{Title}}} - {{{Party}}} - {{{State}}})</span></p></div>
<div id="resultsArea">{{#mvData:PERSONSPEECHES|num_results=5|person={{PAGENAME}}}}</div>
<div id="searchSideBar">
<div id="searchSideBar2Top"></div>
<div id="searchSideBarInner" class="suggestionsBox">
<div class="block first_block">
   <h6 class="profile">Overview</h6>
</div><div class="block wide_block">
{{ #if: {{{GovTrack Person ID|}}}|
<ul><li>[[Data_Source_URL:=http://www.opencongress.org/people/show/{{{GovTrack Person ID}}}|]][http://www.opencongress.org/people/show/{{{GovTrack Person ID}}} Profile on OpenCongress]
<ul>
<li>
[http://www.opencongress.org/people/atom_news/{{{GovTrack Person ID}}} In the News RSS]
</li>
<li>
[http://www.opencongress.org/people/atom_blogs/{{{GovTrack Person ID}}} In the Blogs RSS]
</li>
</ul>
</li>
{{ #if: {{{Bio Guide ID|}}}|<li>[http://bioguide.congress.gov/scripts/biodisplay.pl?index={{{Bio Guide ID}}} Official Biography] </li>
|}}
</ul>
|}}
</div>
<div class="block">
<h6 class="profile">Recent Bills Sponsored</h6>
</div>
{{#ask: [[Bill Sponsor::{{PAGENAME}}]]
| format=ul
| limit=4
}}
<div class="block">
<h6 class="profile">Recent Bills Co-Sponsored</h6>
</div>
{{#ask: [[Bill Cosponsor::{{PAGENAME}}]]
| format=ul
| limit=4
}}
<div class="block">
<h6 class="profile">Top Funding Sources</h6>
</div>
{{ #if: {{{MAPLight Person ID|}}}|[[Data_Source_URL:=http://www.maplight.org/map/us/legislator/{{{MAPLight Person ID}}}|MAPLight Source]]
 |}}{{ #if: {{{Total Received|}}}|Total Campaign Contributions for {{PAGENAME}}: {{{Total Received}}} <br />|}}
{{ #if: {{{Contributions Date Range|}}}|Contributions Date Range: {{{Contributions Date Range}}} |}}
<ul>
{{ #if: {{{Funding Interest 1|}}}|<li>[[Funding Interest:={{{Funding Interest 1}}};{{{Funding Amount 1}}}]]</li>
|}}{{ #if: {{{Funding Interest 2|}}}|<li>[[Funding Interest:={{{Funding Interest 2}}};{{{Funding Amount 2}}}]]</li>
|}}{{ #if: {{{Funding Interest 3|}}}|<li>[[Funding Interest:={{{Funding Interest 3}}};{{{Funding Amount 3}}}]]</li>
|}}{{ #if: {{{Funding Interest 4|}}}|<li>[[Funding Interest:={{{Funding Interest 4}}};{{{Funding Amount 4}}}]]</li>
|}}</ul>
{{ #if: {{{Funding Interest 5|}}}|[[Funding Interest:={{{Funding Interest 5}}};{{{Funding Amount 5}}}|]]|}}{{ #if: {{{Funding Interest 6|}}}|*[[Funding Interest:={{{Funding Interest 6}}};{{{Funding Amount 6}}}| ]]|}}{{ #if: {{{Funding Interest 7|}}}|*[[Funding Interest:={{{Funding Interest 7}}};{{{Funding Amount 7}}}| ]]|}}{{ #if: {{{Funding Interest 8|}}}|[[Funding Interest:={{{Funding Interest 8}}};{{{Funding Amount 8}}}|]]|}}{{ #if: {{{Funding Interest 9|}}}|[[Funding Interest:={{{Funding Interest 9}}};{{{Funding Amount 9}}}|]]|}}{{ #if: {{{Funding Interest 10|}}}|[[Funding Interest:={{{Funding Interest 10}}};{{{Funding Amount 10}}}|]]|}}
</div>


{{ #if: {{{Committee Member 1|}}}|
===Committee Membership===
|}}{{ #if: {{{Committee Member 1|}}}|*[[Committee Member:={{{Member of Committee 1}}}]]
|}}{{ #if: {{{Committee Member 2|}}}|*[[Committee Member:={{{Member of Committee 2}}}]]
|}}{{ #if: {{{Committee Member 3|}}}|*[[Committee Member:={{{Member of Committee 3}}}]]
|}}{{ #if: {{{Committee Member 4|}}}|*[[Committee Member:={{{Member of Committee 4}}}]]
|}}{{ #if: {{{Committee Member 5|}}}|*[[Committee Member:={{{Member of Committee 5}}}]]
|}}{{ #if: {{{Committee Member 6|}}}|*[[Committee Member:={{{Member of Committee 6}}}]]
|}}{{ #if: {{{Committee Member 7|}}}|*[[Committee Member:={{{Member of Committee 7}}}]]
|}}{{ #if: {{{Committee Member 8|}}}|*[[Committee Member:={{{Member of Committee 8}}}]]
|}}{{ #if: {{{Committee Member 9|}}}|*[[Committee Member:={{{Member of Committee 9}}}]]
|}}{{ #if: {{{Committee Member 10|}}}|*[[Committee Member:={{{Member of Committee 10}}}]]
|}}{{ #if: {{{Committee Member 11|}}}|*[[Committee Member:={{{Member of Committee 11}}}]]
|}}{{ #if: {{{Committee Member 12|}}}|*[[Committee Member:={{{Member of Committee 12}}}]]
|}}{{ #if: {{{Committee Member 13|}}}|*[[Committee Member:={{{Member of Committee 13}}}]]
|}}{{ #if: {{{Committee Member 14|}}}|*[[Committee Member:={{{Member of Committee 14}}}]]
|}}{{ #if: {{{Committee Member 15|}}}|*[[Committee Member:={{{Member of Committee 15}}}]]

|}}
</div>';
		//output all the database fileds (hidden):
		foreach ( $valid_attributes as $dbKey => $attr ) {
			list ( $name, $desc ) = $attr;
				$template_body .= "{{ #if: {{{" . $name . "|}}}| [[$name:={{{" . $name . "}}}| ]] |}}";
		}
		//finish up template output:
$template_body .='
[[Category:Congress Person]] [[Category:Person]]
<div style="clear:both"></div>
</includeonly>';
		//now insert template body:
		$wgTemplateTitle = Title :: makeTitle( NS_TEMPLATE, 'Congress_Person' );
		do_update_wiki_page( $wgTemplateTitle, $template_body , null, $force );

		//update attribute string types:
		foreach ( $valid_attributes as $dbKey => $attr ) {
			list ( $name, $desc, $type ) = $attr;
			$wgPropTitle = Title::newFromText( $name, SMW_NS_PROPERTY );
			do_update_wiki_page( $wgPropTitle, "[[has type:=$type]]", null, $force );
		}
	}
}
 function processBill($govTrackBillId, $bill_key, $openCongBillId = false, $mapLightBillId = false, $forceUpdate = false, $doIntrestLookup = false)
 {
     // get the bill title & its sponsor / co-sponsors:
     $rawGovTrackPage = $this->doRequest($this->govTrack_bill_url . $govTrackBillId);
     if ($rawGovTrackPage === false) {
         return false;
     }
     /*****************************
      * Process Bill GovTrack info
      *****************************/
     print "gov_track id: " . $govTrackBillId . " from: " . $this->govTrack_bill_url . $govTrackBillId . "\n";
     // get title:
     $patern = '/<title>(.*)<\\/title>/';
     preg_match($patern, $rawGovTrackPage, $title_match);
     if (isset($title_match[1])) {
         //strip govtrack.us
         $title_match[1] = str_replace('(GovTrack.us)', '', $title_match[1]);
         if (trim($title_match[1]) == '') {
             print "empty title\n";
             return false;
         }
         $title_short = str_replace(array('_', '...', ' [110th]', ' [109th]', ' [108th]', ' [107th]'), array(' ', '', '', '', '', ''), $title_match[1]);
         $this->cur_bill_short_title = $title_short;
         // set the desc if present:
         preg_match('/<meta name="description" content="([^">]*)"/', $rawGovTrackPage, $desc_match);
         if (isset($desc_match[1])) {
             $title_desc = $desc_match[1];
         } else {
             die('could not find title desc: ' . $title_desc);
         }
         $this->bill_titles[$bill_key] = $title_short;
     } else {
         print $this->govTrack_bill_url . $govTrackBillId . "\n" . $patern . "\n" . $rawGovTrackPage;
         die('could not get title for bill: ' . $govTrackBillId);
     }
     // print "raw govtrack:\n $rawGovTrackPage";
     // get the $thomas_match
     preg_match('/thomas\\.loc\\.gov\\/cgi-bin\\/bdquery\\/z\\?([^\\"]*)/', $rawGovTrackPage, $thomas_match);
     // get introduced: //strange .* does not seem to work :(
     preg_match('/Introduced<\\/nobr>[^>]*>[^>]*>[^>]*>([^<]*)/', $rawGovTrackPage, $date_intro_match);
     // get sponsor govtrack_id:
     preg_match('/usbill:sponsor[^<]*<a href="person.xpd\\?id=([^"]*)/i', $rawGovTrackPage, $sponsor_match);
     // lookup govtrack_id
     // print_r($sponsor_match);
     if (isset($sponsor_match[1])) {
         $sponsor_name = str_replace('_', ' ', $this->get_wiki_name_from_govid($sponsor_match[1]));
     }
     // get cosponsor chunk:
     $scospon = strpos($rawGovTrackPage, 'Cosponsors [as of');
     $cochunk = substr($rawGovTrackPage, $scospon, strpos($rawGovTrackPage, '<a href="/faq.xpd#cosponsors">') - $scospon);
     preg_match_all('/person.xpd\\?id=([^"]*)/', $cochunk, $cosponsor_match);
     $bp = "{{Bill|\n" . 'GovTrackID=' . $govTrackBillId . "|\n";
     if (isset($thomas_match[1])) {
         $bp .= 'ThomasID=' . $thomas_match[1] . "|\n";
     }
     if ($openCongBillId) {
         $bp .= 'OpenCongressBillID=' . $openCongBillId . "|\n";
     }
     if ($mapLightBillId) {
         $bp .= 'MapLightBillID=' . $mapLightBillId . "|\n";
     }
     if (isset($this->cur_session)) {
         $bp .= 'Session=' . $this->cur_session . "th session|\n";
     }
     $bp .= 'Bill Key=' . $bill_key . "|\n";
     if (isset($date_intro_match[1])) {
         $bp .= 'Date Introduced=' . $date_intro_match[1] . "|\n";
     }
     if ($title_desc) {
         $bp .= 'Title Description=' . $title_desc . "|\n";
     }
     if ($sponsor_name) {
         $bp .= 'Sponsor=' . $sponsor_name . "|\n";
     }
     if (isset($cosponsor_match[1])) {
         foreach ($cosponsor_match[1] as $k => $govid) {
             $cosponsor_name = $this->get_wiki_name_from_govid($govid);
             if ($cosponsor_name) {
                 $bp .= 'Cosponsor ' . ($k + 1) . '=' . $cosponsor_name . "|\n";
             }
         }
     }
     /*****************************
      * Process MapLight Info
      *****************************/
     if ($mapLightBillId) {
         $bill_interest = $this->proccMapLightBillIntrests($mapLightBillId);
         if ($bill_interest) {
             $i = 1;
             foreach ($bill_interest['support'] as $interest) {
                 $this->procMapLightInterest($interest);
                 $bp .= 'Supporting Interest ' . $i . '=' . $interest['name'] . "|\n";
                 $i++;
                 //process interest
                 if ($doIntrestLookup) {
                     $this->procMapLightInterest($interest);
                 }
             }
             $i = 1;
             foreach ($bill_interest['oppose'] as $interest) {
                 $bp .= 'Opposing Interest ' . $i . '=' . $interest['name'] . "|\n";
                 $i++;
                 //process interest
                 if ($doIntrestLookup) {
                     $this->procMapLightInterest($interest);
                 }
             }
         }
     }
     $bp .= "}}\n";
     // print 'page : '.$title_short.' ' . $bp . "\n";
     // incorporated into the template:
     // $body.="\n\n".'Source: [[Data Source Name:=GovTrack]] [[Data Source URL:='.$this->govTrack_bill_url . $govTrackBillId.']]';
     // set up the base bill page:
     $wgBillTitle = Title::newFromText($title_short);
     //print $bp;
     //die;
     do_update_wiki_page($wgBillTitle, $bp);
     // set up a redirect for the bill key, and a link for the category page:
     //print "\ndo redirect for: $title_short \n";
     global $mvForceUpdate;
     //$wgBillKeyTitle = Title::newFromText( $bill_key );
     //do_update_wiki_page( $wgBillKeyTitle, '#REDIRECT [[' . $title_short . ']]', null, $mvForceUpdate );
     // set up link on the category page:
     //$wgCatPageTitle = Title::newFromText( $bill_key, NS_CATEGORY );
     //do_update_wiki_page( $wgCatPageTitle, 'See Bill Page For More Info: [[:' . $wgBillTitle->getText() . ']]', null, $mvForceUpdate );
 }
function upTemplates($force=false){
/***************************************************
 * Transcripts: 
 * updates transcript templates
 ***************************************************/
 	$wgTemplateTitle = Title :: makeTitle(NS_TEMPLATE, 'Ht_en');
	do_update_wiki_page($wgTemplateTitle, '<noinclude>
		This is the default Template for the display of transcript text. 
		</noinclude><includeonly>{{ #if:  {{{PersonName|}}} | {{ #ifexist: Image:{{{PersonName}}}.jpg | [[Image:{{{PersonName}}}.jpg|44px|left]]|[[Image:Missing person.jpg|44px|left]]}} |}}{{ #if:{{{PersonName|}}}|[[{{{PersonName}}}]]: |}}{{{BodyText}}}
		</includeonly>',null, $force);
/****************************************************
 * Archive.org file type semantics
 ****************************************************/  
	$archive_org_ftypes = array('64Kb_MPEG4','256Kb_MPEG4','MPEG1','MPEG2','flash_flv');
	foreach($archive_org_ftypes as $ftype){
		$pTitle= Title::makeTitle(SMW_NS_PROPERTY, 'Ao_file_'.$ftype );
		do_update_wiki_page($pTitle, '[[has type::URL]]',null, $force);
	}
/*****************************************************
 * Bill Templates
 ****************************************************/	
	$bill_template='<noinclude>Bill Person Template simplifies the structure of articles about Bills.
<pre>{{Bill|
GovTrackID=The GovTrack Bill ID (used to key-into GovTracks Bill info)|
ThomasID=The bill\'s Thomas id (used for Thomas linkback)|
MAPLightBillID=The Map light Bill ID (used for supporting and opposing interest)|
OpenCongressBillID=The open congress bill id (used for bill rss feeds)|
Title Description=The short title/description of the bill|
Date Introduced=The date the bill was introduced|
Session=The session of congress (110 for 2007-08, 109 for 2005-2006 etc)|
Bill Key=The short bill name ie: H.R. #|
Sponsor=Who the Bill was Sponsored By|
Cosponsor #=Cosponsor, Where # is 1-70 for listing all cosponsors|
Supporting Interest #=Interest, Where # is 1-20 for listing top supporting interests|
Opposing Interest #=Interest, Where # is 1-20 for listing top opposing interests|				
}}</pre>The template name (Bill) should be given as the \'\'first\'\' thing on a page. The Cosponsored list should come at the end.
</noinclude><includeonly>
==Bill [[Bill Key:={{{Bill Key}}}]] in the {{ #if: {{{Session|}}}| [[Congress Session:={{{Session}}}]] |}} of Congress==
<span style="float:right">{{navimg|xsize=50|ysize=50|image=Crystal_Clear_mimetype_video_search.png|link=Category:{{{Bill Key}}}}}</span>
{{ #if: {{{Title Description|}}}|{{{Title Description}}} |}}
{{ #if: {{{Bill Key|}}}| <br>Media in [[:Category:{{{Bill Key}}}]] |}}
{{ #if: {{{Date Introduced|}}}|* Date Introduced: [[Date Bill Introduced:={{{Date Introduced}}}]] |}}
{{ #if: {{{Sponsor|}}}|* Sponsor: [[Bill Sponsor:={{{Sponsor}}}]] |}}';
$bill_template.='
{{ #if: {{{Cosponsor 1|}}}|* Cosponsor: [[Bill Cosponsor:={{{Cosponsor 1}}}]] |}}';

//$bill_template.='{{ #for: {{{n}}} | {{{Cosponsor $n$}}}<br /> }}';
for($i=2;$i<70;$i++){	
	$bill_template.='{{ #if: {{{Cosponsor '.$i.'|}}}|, [[Bill Cosponsor:={{{Cosponsor '.$i.'}}}]] |}}';
}
//output mapLight info if present:
$bill_template.='{{ #if: {{{Supporting Interest 1|}}}|<h2>Intrests who <span style="color:green">support</span> bill becoming law</h2>'."\n[[Data_Source_URL:=http://maplight.org/map/us/bill/{{{MapLightBillID}}}|MAPLight Source]]".' |}}';
for($i=1;$i<20;$i++){
	$bill_template.='{{ #if: {{{Supporting Interest '.$i.'|}}}|* [[Supporting Interest:={{{Supporting Interest '.$i.'}}}]]'."\n".' |}}';
}
$bill_template.='{{ #if: {{{Opposing Interest 1|}}}|<h2>Interests who <span style="color:red">oppose</span> bill becoming law</h2>'."\n".' |}}';
for($i=1;$i<20;$i++){
	$bill_template.='{{ #if: {{{Opposing Interest '.$i.'|}}}|* [[Opposing Interest:={{{Opposing Interest '.$i.'}}}]]'."\n".'|}}';
}
//@@todo could do inline rss once we get a good cache model for http://www.mediawiki.org/wiki/Extension:RSS_Reader
// maybe just action=purge on as a cron job, with $parser->disableCache(); commented out 
$bill_template.='
{{ #if: {{{OpenCongressBillID|}}}|==Bill RSS Feeds==
* In the News [http://www.opencongress.org/bill/{{{OpenCongressBillID|}}}/atom_news]
* Blog Coverage [http://www.opencongress.org/bill/{{{OpenCongressBillID|}}}/atom_blogs]
* Bill Actions [http://www.opencongress.org/bill/{{{OpenCongressBillID|}}}/atom][[Open Congress Bill ID:={{{OpenCongressBillID|}}}|]]
|}}';

$bill_template.='
==Data Sources==  		
{{ #if: {{{ThomasID|}}}|* [[Metavid Sources#Thomas|Thomas]] Official Bill Information:[[Data_Source_URL:=http://thomas.loc.gov/cgi-bin/bdquery/z?{{{ThomasID}}}:]] [[Thomas Bill ID:={{{ThomasID}}}| ]] 
|}}{{ #if: {{{GovTrackID|}}}|* [[Metavid Sources#GovTrack|GovTrack]] Bill Overview:[[Data_Source_URL:=http://www.govtrack.us/congress/bill.xpd?bill={{{GovTrackID}}}]] [[GovTrack Bill ID:={{{GovTrackID}}}| ]] 
|}}{{ #if: {{{MapLightBillID|}}}|* [[Metavid Sources#MapLight|MapLight]] Bill Overview:[[Data_Source_URL:=http://maplight.org/map/us/bill/{{{MapLightBillID}}}]] [[MAPLight_Bill_ID:={{{MapLightBillID}}}| ]] 
|}}
[[Category:Bill]]
</includeonly>';	
	//update bill template:
	$wgTemplateTitle = Title :: makeTitle(NS_TEMPLATE, 'Bill');
		do_update_wiki_page($wgTemplateTitle,$bill_template ,null, $force);
			
	//update semnatic property types:
	foreach(array('Page'=>array('Bill Key', 'Bill Sponsor', 'Bill Cosponsor', 'Congress Session'),
				  'String'=>array('Thomas Bill ID','Open_Congress_Bill_ID','GovTrack_Bill_ID','MAPLight_Bill_ID'),
				  'URL'=>array('Data_Source_URL'),
				  'Date'=>array('Date_Bill_Introduced')
	) as $type=>$type_set){
		foreach($type_set as $propName){
				$wgPropTitle = Title::newFromText($propName, SMW_NS_PROPERTY);
					do_update_wiki_page($wgPropTitle, '[[has type::'.$type.']]',null, $force);
			}
	}
/***************************************
 * Interest Group templates:
 **************************************/
 
  global $mvMaxContribPerInterest, $mvMaxForAgainstBills;
 $interest_template = '<noinclude>Interest Group Template simplifies the structure of articles about Interest Groups.
<pre>{{Interest Group|
MAPLight Interest ID=The MapLight Interest ID|
Funded Name #=funded name where 1 is 1-'.$mvMaxContribPerInterest.' for top '.$mvMaxContribPerInterest.' contributions|
Funded Amount #=funded amount to name 1 (required pair to funded name #)|	
Supported Bill #=Bills the Interest group supported (long name) 1-'.$mvMaxForAgainstBills.'|
Opposed Bill #=Bills Interest group Opposed (long name) 1-'.$mvMaxForAgainstBills.'|
}}</pre>
</noinclude><includeonly>
{{ #if: {{{Funded Name 1|}}}|==Recipients Funded==
Showing contributions 2001-2008 Senate / 2005-2008 House [[Data_Source_URL:=http://maplight.org/map/us/interest/{{{MAPLight Interest ID}}}/view/all|MAPLight source]]
|}}';
/*
 * output top $mvMaxContribPerInterest contributers
 */
for($i=1;$i<=$mvMaxContribPerInterest;$i++){
	if($i<=10){ //only display 10:
		$interest_template.='{{ #if: {{{Funded Name '.$i.'|}}}|*[[Funded:={{{Funded Name '.$i.'}}};{{{Funded Amount '.$i.'}}}]] |}}';
		if($i!=10)$interest_template.="\n";
	}else{
		$interest_template.='{{ #if: {{{Funded Name '.$i.'|}}}|*[[Funded:={{{Funded Name '.$i.'}}};{{{Funded Amount '.$i.'}}}|]] |}}';
	}
}

/*
 * output bills supported / opposed template vars:
 */
foreach(array('Supported','Opposed') as $pos){
	//$interest_template.='\nShowing contributions 2001-2008 Senate / 2005-2008 House [[Data_Source_URL:=http://maplight.org/map/us/interest/{{{MAPLight Interest ID}}}|MAPLight source]]';
	$interest_template.='{{ #if: {{{'.$pos.' Bill 1|}}}|<h3>'.$pos.' Bills</h3>
Pulled from maplight [[Data_Source_URL:=http://maplight.org/map/us/interest/{{{MAPLight Interest ID}}}/bills|source]]
|}}'; 
	for($i=1;$i<=$mvMaxForAgainstBills;$i++){	 
		$interest_template.='{{ #if: {{{'.$pos.' Bill '.$i.'|}}}|*[['.$pos.' Bill:={{{'.$pos.' Bill '.$i.'}}}]]
|}}';
	}
}
$interest_template.='[[Category:Interest Group]]
</includeonly>';

$wgTemplateTitle = Title :: makeTitle(NS_TEMPLATE, 'Interest Group');
do_update_wiki_page($wgTemplateTitle,$interest_template ,null, $force);

$wgPropTitle = Title::newFromText('Funded', SMW_NS_PROPERTY);
do_update_wiki_page($wgPropTitle, '[[has type:=Page;Number]]',null, $force);

/***************************************
 *  Update people templates
 ***************************************/ 
	global $valid_attributes;
	$wgTemplateTitle = Title :: makeTitle(NS_TEMPLATE, 'Congress Person');
	$wgTemplateArticle = new Article($wgTemplateTitle);
	$template_body = '<noinclude>Congress Person template simplifies 
			the structure of articles about Congress People.
			<pre>{{Congress Person|' . "\n";
	foreach ($valid_attributes as $dbKey => $attr) {
		list ($name, $desc) = $attr;				 
		$template_body .= $name . '=' . $desc . "|\n";
	}
	//$template_body .= "Committee Member #= The name of the Committee (where # is 1-15)|\n";
	//$template_body .= "Committee Chair #= The name of the Committee (where # is 1-15)|\n";
	$template_body .= "Funding Interest #= Funding Interest Name (where # is 1-10)|\n";
	$template_body .= "Funding Amount #= Funding Amount for Interest (where # is 1-10 and aligns with Funding Interest Name)|\n";
	$template_body .= '}}</pre>' .
	'The order of the fields is not relevant. The template name (Congress Person) should be given as the \'\'first\'\' thing on a page.
			</noinclude>' .
	'<includeonly>' . "\n";
	//hide the table of contents:
	$template_body .= '__NOTOC__ ';
	//include the image if present: 
	$template_body .= '{{ #if: {Image:{{PAGENAME}}.jpg}| [[Image:{{PAGENAME}}.jpg|left]]'."\n".'|}}';
	foreach ($valid_attributes as $dbKey => $attr) {
		list ($name, $desc) = $attr;	
			$template_body .= "{{ #if: {{{" . $name . "|}}}| [[$name:={{{" . $name . "}}}| ]] |}}";		
	}	
	//$template_body.='<span style="width:40%;float:left;">';
	$template_body.='<span style="float:right">{{navimg|xsize=50|ysize=50|image=Crystal_Clear_mimetype_video_search.png|link=Special:MediaSearch/person/{{PAGENAME}} }}</span>';	
	//ask for speeches media~	
	$template_body.="\n".'{{#ask: [[Speech_by::{{PAGENAME}}]] 
|?Category | ?Bill
| intro=<h3>Speeches By {{PAGENAME}}</h3>
| default= | limit=5}}';	
	/*$template_body.='{{#ask: [[Spoken_By::{{PAGENAME}}]] |
| intro=<h3>[[Special:MediaSearch/person/{{PAGENAME}}|Spoken Text]] By {{PAGENAME}}</h3> 
| default= | limit=5}}';	 
*/
	//$template_body.='</span>';
	//$template_body.='<span style="width:40%;float:left;">';
	//ask for inverse relations (bills) : 
	$template_body.='{{#ask: [[Bill Sponsor::{{PAGENAME}}]] |
| intro=<h3>Bills Sponsored</h3>
| default= | limit=5}}';

	$template_body.='{{#ask: [[Bill Cosponsor::{{PAGENAME}}]] |
| intro=<h3>Bills Cosponsored</h3>
| default= | limit=5}}';


	//$template_body.="\n".'[[Special:MediaSearch/person/{{PAGENAME}}|Media Search]]<br>'; 
	//include space for Top 10 Interests Funding	
	$template_body.="\n===Interests Funding===\n";
	$template_body.="{{ #if: {{{MAPLight Person ID|}}}|[[Data_Source_URL:=http://www.maplight.org/map/us/legislator/{{{MAPLight Person ID}}}|MAPLight Source]] \n|}}";
	$template_body.="{{ #if: {{{Total Received|}}}| Total Campaign Contributions for {{PAGENAME}}: {{{Total Received}}} <br>|}}\n";
	$template_body.="{{ #if: {{{Contributions Date Range|}}}|Contributions Date Range: {{{Contributions Date Range}}} |}}\n";	
	for($i=1;$i<=10;$i++){
		$template_body.="{{ #if: {{{Funding Interest $i|}}}|*[[Funding Interest:={{{Funding Interest $i}}};{{{Funding Amount $i}}}]] \n|}}";
	}
	$template_body.="\n";
	
	//comm
	$template_body.="{{ #if: {{{Committee Member $i|}}}|\n===Committee Membership===\n|}}";
	for($i=1;$i<=15;$i++){
		$template_body.="{{ #if: {{{Committee Member $i|}}}|*[[Committee Member:={{{Member of Committee $i}}}]] \n|}}";
	}
	//$template_body.='</span>';
	//$template_body.='<div style="clear:both"></div>';
	//news feeds:
	$template_body.="\n===RSS feeds===\n";
	$template_body.='[[Data_Source_URL:=http://www.opencongress.org/people/show/{{{GovTrack Person ID}}}|OpenCongress Source]]'."\n";
	$template_body.="*[http://www.opencongress.org/people/atom_news/{{{GovTrack Person ID}}} In the News]
*[http://www.opencongress.org/people/atom_blogs/{{{GovTrack Person ID}}} In the Blogs]";
	
	//include some external links:
	$template_body .="\n===External Links===\n".
'{{ #if: {{{Bio Guide ID|}}}|* [http://bioguide.congress.gov/scripts/biodisplay.pl?index={{{Bio Guide ID}}} Offical Biography] |}}'."\n";
'{{ #if: {{{MAPLight Person ID|}}}|* [http://www.maplight.org/map/us/legislator/{{{MAPLight Person ID}}} MapLight Page] |}}'."\n";
'{{ #if: {{{Open Secrets ID|}}}|* [http://www.opensecrets.org/politicians/summary.asp?CID={{{Open Secrets ID}}} Open Secrets Page] |}}'."\n";
'{{ #if: {{{GovTrack Person ID|}}}|* [http://www.govtrack.us/congress/person.xpd?id={{{GovTrack Person ID}}} GovTrack Page] |}}'."\n";
	$template_body .= '[[Category:Congress Person]] [[Category:Person]]
			</includeonly>';
	echo "updated 'Congress Person' template\n";
	do_update_wiki_page($wgTemplateTitle, $template_body,null, $force);
	
	//update types for attr: 
	foreach ($valid_attributes as $dbKey => $attr) {
		list ($name, $desc, $type) = $attr;
		$wgPropTitle = Title::newFromText($name, SMW_NS_PROPERTY);
		do_update_wiki_page($wgPropTitle, "[[has type:=$type]]",null, $force);
	}
	//update special types: 
	$wgPropTitle = Title::newFromText('Funding Interest',SMW_NS_PROPERTY);
	do_update_wiki_page($wgPropTitle, "[[has type:=Page;Number]]",null,$force); 

/************************************
 * some more types: 
 ************************************/
$wgPropTitle = Title::newFromText('Speech by', SMW_NS_PROPERTY);
do_update_wiki_page($wgPropTitle, "[[has type:=Page]]",null,$force); 
		
$wgPropTitle = Title::newFromText('Spoken by', SMW_NS_PROPERTY);
do_update_wiki_page($wgPropTitle, "[[has type:=Page]]",null,$force); 
/************************************
 *  page helpers
 ************************************/
 $wgTempNavImg = Title::makeTitle(NS_TEMPLATE, 'Navimg');
 do_update_wiki_page($wgTempNavImg, '<div style="position: relative; width: {{{xsize|{{{size|}}}}}}px; height: {{{ysize|{{{size|}}}}}}px; overflow: hidden;"><div style="position: absolute; top: 0; left: 0; font-size: 200pt; width: {{{xsize|{{{size|}}}}}}px; height: {{{ysize|{{{size|}}}}}}px;  overflow: hidden; line-height: {{{ysize|{{{size|}}}}}}px; z-index: 3;">[[:{{{link|}}}|{{{linktext|&nbsp;}}}]]</div><div style="position: absolute; top: 0; left: 0; z-index: 2;">[[Image:{{{image|}}}|{{{xsize|{{{size|}}}}}}px|image page]]</div></div><noinclude>
Simple Usage example:<nowiki> {{navimg|xsize=50|ysize=50|image=Wikimedia-logo.svg|link=MediaWiki}} </nowiki>
</noinclude>
');


}
function strip_speech_by()
{
    global $mvDryRun;
    $dbr = wfGetDB(DB_SLAVE);
    $streams_res = $dbr->select('mv_mvd_index', '*', $conds = array('mvd_type' => 'Anno_en'), $fname = 'strip_speech_by', $options = array('LIMIT' => 10000));
    $inx = 0;
    while ($mvd_row = $dbr->fetchObject($streams_res)) {
        $mvdTitle = Title::newFromText($mvd_row->wiki_title, MV_NS_MVD);
        $mvdArticle = new Article($mvdTitle);
        $cur_text = trim($mvdArticle->getContent());
        // print "old text: "
        $st = 'Speech By:';
        if (substr($cur_text, 0, strlen($st)) == $st) {
            print "{$inx} :up: " . $mvd_row->wiki_title . "\n";
            $new_text = trim(substr($cur_text, strlen($st)));
            // print "new text: $new_text\n";
            if (!$mvDryRun) {
                do_update_wiki_page($mvdTitle, $new_text, MV_NS_MVD, $force = true);
            }
        }
        $inx++;
    }
}