<?php
# =========================================================================== #
#
# Experimental bot script, by Grigor Gatchev.
#
# Relying on the Bgbot mechanisms, created by Borislav Manolov.
#
# =========================================================================== #
require_once 'page_history.php';
require_once 'page_edit.php';
function editors_names_until_page_creation (
$bot_orig, $page_orig, $bot_copy, $page_copy, $majoronly ) {
$orig_page = new Page_History ( $bot_orig, $page_orig );
return $orig_page->editors_names_until_page_creation (
$bot_copy, $page_copy, $majoronly );
}
function editors_names_until_date ( $bot_orig, $page_orig, $date, $majoronly ) {
$orig_page = new Page_History ( $bot_orig, $page_orig );
return $orig_page->editors_names ( MIN_DATETIME, $date, $majoronly );
}
function bases_from_inserted_originals ( $bot_orig, $page_orig ) {
$bases = array();
if ( $bot_orig->edit ( $page_orig ) ) {
$count = preg_match_all ( '/\{\{Производна-Уикипедия(.*)\}\}/Us',
$bot_orig->page_content, $matches );
for ( $i=0; $i<$count; $i++ ) {
$basetext = $matches[1][$i];
$base = array();
$base['lang'] = trim (
extract_element ( '/\|\s*език\s*=\s*(.*)\|/Us', $basetext ) );
$base['page'] = trim (
extract_element ( '/\|\s*страница\s*=\s*(.*)\|/Us', $basetext ) );
$base['date'] = trim (
extract_element ( '/\|\s*дата\s*=\s*(.*)\|/Us', $basetext ) );
$base['list'] = trim (
extract_element ( '/\|\s*съавтори\s*=\s*(.*)$/Us', $basetext ) );
$base['date'] = str_replace ( '[[', '', $base['date'] );
$base['date'] = str_replace ( ']]', '', $base['date'] );
$base['list'] = str_replace ( 'Редактори: ', '', $base['list'] );
$bases[] = $base;
}
} else {
$bot_orig->log ( "Original \"$page_orig\" NOT fetched for editing - cannot extract info about its base" );
}
return $bases;
}
function original_authors_template ( $descendance_type, $descendance_date, $descendance_link,
$original_wiki, $original_wiki_link, $original_page, $original_page_link,
$original_history_link, $authors_names, $original_licence, $original_bases ) {
$template = "{{Производна статия от статия\n";
$template .= "| вид-производност = $descendance_type\n";
$template .= "| производност-дата = $descendance_date\n";
$template .= "| производност-линк = $descendance_link\n"; // към точната версия
$template .= "| оригинал-уики = $original_wiki\n";
$template .= "| оригинал-уики-линк = $original_wiki_link\n";
$template .= "| оригинал-име = $original_page\n";
$template .= "| оригинал-име-линк = $original_page_link\n"; // към актуалната страница
$template .= "| оригинал-история-линк = $original_history_link\n";
$template .= "| оригинал-автори = $authors_names\n";
$template .= "| оригинал-лиценз = $original_licence\n";
$template .= "| оригинал-база = $original_bases\n";
$template .= "}}";
return $template;
}
function origin_template ( $original_wiki, $original_wiki_name,
$original_page, $original_oldid, $authors_list, $descendance_date, $original_base ) {
global $base_pages, $wiki_sites;
$original_wiki_link = 'http://' . $original_wiki;
$original_page_link = $original_wiki_link . '/' .
(isset($base_pages[$wiki]) ? $base_pages[$wiki] : 'w/index.php') .
'?title=' . $original_page;
$original_page_link = str_replace ( " ", "_", $original_page_link );
$original_history_link = $original_page_link . '&action=history';
if ( $original_oldid ) {
$descendance_link = $original_page_link . "&oldid=" . $original_oldid;
}
$descendance_type = "е производна от";
$original_licence = "[[БГ-Фантастика:Лиценз за свободна документация на ГНУ|Лиценз за свободна документация на ГНУ]]";
return original_authors_template ( $descendance_type, $descendance_date, $descendance_link,
$original_wiki, $original_wiki_link, $original_page, $original_page_link,
$original_history_link, $authors_list, $original_licence, $original_base );
}
function bg_wikipedia_base_origin_template ( $base ) {
global $wiki_sites;
$original_wiki = $wiki_sites[$base['lang']];
$original_page = $base['page'];
$authors_list = $base['list'];
$descendance_date = $base['date'];
$original_oldid = false;
switch ( $base['lang'] ) {
case 'bg' : $original_wiki_name = "българоезичната Уикипедия"; break;
case 'en' : $original_wiki_name = "англоезичната Уикипедия"; break;
case 'de' : $original_wiki_name = "немскоезичната Уикипедия"; break;
case 'fr' : $original_wiki_name = "френскоезичната Уикипедия"; break;
default : $original_wiki_name = "---ЗА ПОПЪЛВАНЕ---"; break;
}
return origin_template ( $original_wiki, $original_wiki_name,
$original_page, $original_oldid, $authors_list, $descendance_date, "" );
}
function insert_original_editors_names (
$bot_orig, $page_orig, $bot_copy, $page_copy, $majoronly = false ) {
global $wiki_month_names;
$copy_edit = new Page_Edit ( $bot_copy, $page_copy );
if ( ! ( $copy_edit->active ) ) {
$bot_copy->log ( "Could not open \"$page_copy\" for editing - nothing changed" );
return;
}
if ( $copy_edit->bgbot->page_is_redirect ) {
$bot_copy->log ( "\"$page_copy\" is redirect - skipping it" );
return;
}
if ( strpos ( $copy_edit->bgbot->page_content, "{{Производна статия от статия" ) ) {
$bot_copy->log ( "\"$page_copy\" already contains this template!" );
return;
}
$copy_history = new Page_History ( $bot_copy, $page_copy );
if ( ! ( $copy_history->active ) ) {
$bot_copy->log ( "Copy \"page_copy\" history not found - can't determine descendance date" );
return;
}
$copy_date = $copy_history->page_created_on();
$descendance_date = date ( "d", $copy_date ) . " " .
$wiki_month_names[$bot_copy->wiki][date ( "n", $copy_date )] . " " .
date ( "Y", $copy_date );
$orig_history = new Page_History ( $bot_orig, $page_orig );
if ( ! ( $orig_history->active ) ) {
$bot_copy->log ( "History of \"$page_orig\" not found; nothing added to \"$page_copy\"" );
return;
}
$authors_array = $orig_history->editors_names ( MIN_DATETIME, $copy_date, $majoronly );
if ( count ( $authors_array ) == 0 ) {
$bot_copy->log ( "\"$page_copy\" apparently created before \"$page_orig\" - not based on it" );
return;
}
$original_oldid = $orig_history->rev_oldid (
$orig_history->page_last_updated_no ( $copy_date )
);
for ( $i=0; $i<count($authors_array); $i++ ) {
if ( ! ( ( $authors_array[$i] == "Bgbot" ) ||
( $authors_array[$i] == "JAnDbot" ) ||
( $authors_array[$i] == "Escarbot" ) ||
( $authors_array[$i] == "Thijs!bot" ) ) ) {
if ( $i ) { $authors_list .= ", "; }
$authors_list .= $authors_array[$i];
}
}
$sources_section_no = $copy_edit->section_header_pos ( "Източници" );
if ( is_bool ( $sources_section_no ) ) {
$sources_section_no =
$copy_edit->append_section ( 1, "Източници", "" );
}
$bases_list = bases_from_inserted_originals ( $bot_orig, $page_orig );
foreach ( $bases_list as $base ) {
$original_bases .= str_replace ( "\n|", "\n |",
bg_wikipedia_base_origin_template ( $base ) );
}
$section_text = $copy_edit->section_text ( $sources_section_no ) . "\n" .
origin_template ( $bot_orig->sites[$bot_orig->wiki], "българоезичната Уикипедия",
$page_orig, $original_oldid, $authors_list, $descendance_date,
$original_bases ) .
"\n";
$copy_edit->section_text ( $sources_section_no, $section_text );
#echo $copy_edit->parts_to_wikitext ();
$copy_edit->submit ( "Бот - Шаблон за производност на статията", false );
$bot_copy->log ( "Base of \"$page_orig\" inserted as a template in \"$page_copy\"" );
}
function insert_original_editors_names_inpage ( $pagename, $namespace ) {
global $wiki_special_words;
if ( ! empty ( $namespace ) ) { $divider = ":"; }
$bgf = new Bgbot ( 'bgf', 'Ботчо', '***' );
$bgw = new Bgbot ( 'bg', 'Григор Гачев', '***' );
insert_original_editors_names (
$bgw, $wiki_special_words[$bgw->wiki][$namespace] . $divider . $pagename,
$bgf, $wiki_special_words[$bgf->wiki][$namespace] . $divider . $pagename
);
}
function insert_original_editors_names_bylist ( $listfile, $namespace, $restsecs = 60 ) {
global $wiki_special_words;
if ( ! empty ( $namespace ) ) { $divider = ":"; }
$lf = fopen ( $listfile, "r" );
$bgf = new Bgbot ( 'bgf', 'Ботчо', '***' );
$bgw = new Bgbot ( 'bg', 'Григор Гачев', '***' );
while ( ! feof ( $lf ) ) {
$pagename = trim ( fgets ( $lf ) );
if ( ! ( feof ( $lf ) ) || ( empty ( $name ) ) ) {
$bgf->log ( "Processing \"" . $wiki_special_words[$bgf->wiki][$namespace] .
"$divider $pagename\"..." );
insert_original_editors_names (
$bgw, $wiki_special_words[$bgw->wiki][$namespace] . $divider . $pagename,
$bgf, $wiki_special_words[$bgf->wiki][$namespace] . $divider . $pagename
);
sleep ( $restsecs );
}
}
}
insert_original_editors_names_bylist ( 'bgf-articles.list', 'wiki' );
# insert_original_editors_names_inpage ( 'Пясъчник', 'wiki' );
?>