<?php
# Copyright (C) 2007 Grigor Gatchev
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# http://www.gnu.org/copyleft/gpl.html
#
# Author: Grigor Gatchev, grigor: gatchev.info, www.gatchev.info
#############################################################################
# Tested on MediaWiki 1.10. #
require_once 'bgbot.php';
function is_interwiki ( $wikimark ) {
global $wiki_special_nss;
return ( strlen ( $wiki_special_nss[$wikimark] ) > 0 );
}
function is_category ( $wikimark ) {
global $wiki_special_words;
$wikimark = trim ( $wikimark, $wiki );
return ( ( $wikimark == $wiki_special_words[$wiki]['category'] ) || ( $wikimark == "Category" ) );
}
function extract_interwikis ( $page_wikitext, $wiki ) {
global $wiki_special_words;
$count = preg_match_all('/\n(\[\[.*:.+\]\])/U', $page_wikitext, $matches);
$wikis = array();
for ($i = 0; $i < $count; $i++) {
if ( ! preg_match (
'/\[\[\s*(Category|' . $wiki_special_words[$wiki]['category'] . ')\s*:.+\]\]/Us',
$matches[1][$i], $dummy ) ) {
if ( ! strpos ( $matches[1][$i], ':|' ) ) {
$wikis[] = $matches[1][$i];
}
}
}
sort($wikis);
return $wikis;
}
function strip_interwikis ( $page_wikitext, $wiki ) {
$interwikis = extract_interwikis ( $page_wikitext, $wiki );
foreach ( $interwikis as $interwiki ) {
$page_wikitext = str_replace ( "\n" . $interwiki, '', $page_wikitext );
}
return $page_wikitext;
}
function extract_categories ( $page_wikitext, $wiki ) {
global $wiki_special_words;
$count = preg_match_all('/\n(\[\[.*:.+\]\])/U', $page_wikitext, $matches);
$categories = array();
for ($i = 0; $i < $count; $i++) {
if ( preg_match (
'/\[\[\s*(Category|' . $wiki_special_words[$wiki]['category'] . ')\s*:.*\]\]/Us',
$matches[1][$i], $dummy ) ) {
$categories[] = $matches[1][$i];
}
}
sort($categories);
return $categories;
}
function strip_categories ( $page_wikitext, $wiki ) {
$categories = extract_categories ( $page_wikitext, $wiki );
foreach ( $categories as $category ) {
$page_wikitext = str_replace ( "\n" . $category, '', $page_wikitext );
}
return $page_wikitext;
}
class Page_Edit {
var $bgbot = "";
var $page_name = "";
var $page_timestamp = "";
var $edit_token = "";
var $page_is_watched = "";
var $parts = "";
var $hierarchy = "";
var $active = "";
# --- Getting and submitting --- #
function Page_Edit ( $Bgbot, $pagename ) {
$this->bgbot = $Bgbot;
if ( $this->bgbot->edit ( $pagename ) ) {
$this->parts = $this->wikitext_to_parts();
$this->page_name = $this->bgbot->page_name;
$this->page_timestamp = $this->bgbot->page_timestamp;
$this->edit_token = $this->bgbot->edit_token;
$this->page_is_watched = $this->bgbot->page_is_watched;
$this->active = true;
} else {
$this->active = false;
}
}
function submit ( $summary, $isminor = true, $purge = false, $bgbot = '' ) {
if ( empty ( $bgbot ) ) { $bgbot = $this->bgbot; }
$bgbot->page_name = $this->page_name;
$bgbot->page_timestamp = $this->page_timestamp;
$bgbot->edit_token = $this->edit_token;
$bgbot->page_is_watched = $this->page_is_watched;
$bgbot->submit ( $this->parts_to_wikitext(),
$summary, $bgbot->wiki, $isminor, $purge );
}
# --- Services --- #
function extract_attributes() {
$count = preg_match_all('/(\n\[\[.*:.+\]\])/U', $this->bgbot->page_content, $matches);
$this->parts['categories'] = array();
$this->parts['interwikis'] = array();
foreach ( $matches[1] as $match ) {
preg_match ( '/\n\[\[\s*(.*)\s*:\s*(.*)\s*\|\s*(.*)\s*\]\]/Us', $match, $parts );
if ( is_category ( $parts[1] ) ) {
$category = array();
$category['name'] = $parts[1];
$category['sort'] = $parts[2];
$this->parts['categories'][] = $category;
$this->bgbot->page_content = str_replace ( $match, '', $this->bgbot->page_content );
}
if ( is_interwiki ( $parts[1] ) ) {
$interwiki = array();
$interwiki['code'] = $parts[1];
$interwiki['page'] = $parts[2];
$this->parts['interwikis'][] = $interwiki;
$this->bgbot->page_content = str_replace ( $match, '', $this->bgbot->page_content );
}
}
return $this->bgbot->page_content;
}
function wikitext_to_parts () {
$this->parts = array();
$this->extract_attributes();
$split = preg_split ( '/(\s*==+.*==+\s*)/Us', $this->bgbot->page_content,
-1, PREG_SPLIT_DELIM_CAPTURE + PREG_SPLIT_OFFSET_CAPTURE );
$this->parts['opener'] = $split[0][0];
$this->parts['sections'] = array();
for ( $pos=1; $pos<count($split); $pos++ ) {
$section = array();
$section['start'] = $split[$pos][1];
$section['header'] = $split[$pos][0];
$pos++;
$section['text_start'] = $split[$pos][1];
$section['text'] = $split[$pos][0];
$section['end'] = $section['text_start'] + strlen ( $section['text'] );
$this->parts['sections'][] = $section;
}
return $this->parts;
}
function parts_to_wikitext () {
global $wiki_special_nss, $wiki_special_words;
$text = $this->parts['opener'];
foreach ( $this->parts['sections'] as $section ) {
$text .= $section['header'] . $section['text'];
}
foreach ( $this->parts['categories'] as $category ) {
$text .= "[[" . $wiki_special_words[$this->bgbot->wiki]['category'] .
":" . $category['name'];
if ( ! empty ( $category['sort'] ) ) {
$text .= "|" . $category['sort'];
}
$text .= "]]\n";
}
$text .= "\n";
foreach ( $this->parts['interwikis'] as $interwiki ) {
$text .= "[[" . $interwiki['code'] . ":" . $interwiki['page'] . "\n";
}
return $text;
}
# --- Sections - Global --- #
function sections_count() {
return count ( $this->parts['sections'] );
}
function remove_section ( $pos ) {
return array_splice ( $this->parts['sections'], $pos, 1 );
}
function insert_section ( $pos, $headerlevel, $header, $text ) {
$headermark = str_repeat ( "=", $headerlevel + 1 );
$section = array (
'header' => "\n\n$headermark $header $headermark\n",
'text' => $text
);
array_splice ( $this->parts['sections'], $pos, 0, array ( $section ) );
}
function append_section ( $headerlevel, $header, $text ) {
$this->insert_section ( $this->sections_count(), $headerlevel, $header, $text );
return $this->sections_count() - 1;
}
# --- Sections - by headers --- #
function section_header ( $pos, $newheader = false ) {
$header = $this->parts['sections'][$pos]['header'];
if ( ! is_bool ( $newheader ) ) {
$this->parts['sections'][$pos]['header'] = $newheader;
}
return $header;
}
function section_header_text ( $pos, $newtext = false ) {
preg_match ( '/==+(?!=)(.*)==+/U',
$this->parts['sections'][$pos]['header'], $matches );
if ( ! is_bool ( $newtext ) ) {
$this->parts['sections'][$pos]['header'] =
preg_replace ( '/(==+)(?!=).*(==+)/Us', "$1$newtext$2",
$this->parts['sections'][$pos]['header'] );
}
return trim ( $matches[1] );
}
function section_header_level ( $pos, $newlevel = false ) {
preg_match ( '/(==+)(?!=).*(==+)/Us',
$this->parts['sections'][$pos]['header'], $matches );
if ( ! is_bool ( $newlevel ) ) {
$headermark = str_repeat ( "=", $newlevel + 1 );
$this->parts['sections'][$pos]['header'] =
preg_replace ( '/(\s*)==+(?!=)(\s*.*\s*)==+(\s*)/Us', "$1$headermark$2$headermark$3",
$this->parts['sections'][$pos]['header'] );
}
return max ( strlen ( $matches[1][0] ), strlen ( $matches[1][1] ) ) - 1;
}
function section_header_pos ( $headertext, $startpos = 0 ) {
for ( $pos=$startpos; $pos<$this->sections_count(); $pos++ ) {
$postext = $this->section_header_text( $pos );
if ( $postext == $headertext ) { return $pos; }
}
return false;
}
# --- Sections - by texts --- #
function section_text ( $pos, $newtext = false ) {
$text = $this->parts['sections'][$pos]['text'];
if ( ! is_bool ( $newtext ) ) {
$this->parts['sections'][$pos]['text'] = $newtext;
}
return $text;
}
}
?>