User:LeucosticteBot/ChangeWikiaEngine.php

From WikiIndex
Jump to navigation Jump to search

This bot script looks for pages that have a url with .wikia.com in them, and changes the wiki engine to Wikia. It iterates over the list of pages contained in CategoryMembers.txt. See mw:Manual:Chris G's botclasses/AllCategoryMembersBot.php for details on generating that file.

<?php
/*
 * ChangeWikiaEngine.php
 * By Leucosticte, https://www.MediaWiki.org/wiki/User:Leucosticte
 * GPL 2.0
 *
 * This bot looks for pages that have a url with .wikia.com in them, and changes the wiki engine to
 * Wikia.
/*

/* Setup my classes. */
include( 'botclasses.php' );
$wiki      = WikiIndex;
$wiki->url = "https://WikiIndex.org/api.php";

/* All the login stuff. */
$user = 'LeucosticteBot';
$pass = 'REMOVED';
$wiki->login( $user, $pass );

// Configuration
$pageTitlesFile = 'CategoryMembers.txt';

// Test file existence
if ( !file_exists ( $pageTitlesFile ) ) {
    die ( "File $pageTitlesFile not found" );
}

// Read files
$lines = file( $pageTitlesFile, FILE_IGNORE_NEW_LINES );
$startHere = 'Belligerent Universe Wiki'; // Resume where we left off

// Each line is a wiki page
$thereYet = false;
foreach ( $lines as $line ) {
    if ( !$startHere || $thereYet || $line == $startHere ) {
        $thereYet = true;
        // Poll to get wiki page content
        $contents = $wiki->getpage ( $line );
        // A template line with data will start with a |, e.g. |URL = WikiIndex.org
        if ( $begin = strpos ( $contents, '|URL' ) ) {
            if ( $equals = strpos ( $contents, '=', $begin ) ) {
                $closingBrace = strpos ( $contents, "}}", $equals );
                // End this string at the newline or the }} (closing the template), whichever
                // comes first
                $newline = strpos ( $contents, "\n", $equals );
                if ( $closingBrace && $closingBrace < $newline ) {
                    $newline = $closingBrace;
                }
                if ( $newline ) {
                    $url = trim( substr ( $contents, $equals + 1,
                        $newline - $equals - 1 ) );
                }
            }
        }
        // If the URL contains .wikia.com, then replace the engine, if necessary
        if ( strpos ( $url, '.wikia.com' ) ) {
            if ( $begin = strpos ( $contents, '|engine' ) ) {
                if ( $equals = strpos ( $contents, '=', $begin ) ) {
                    $closingBrace = strpos ( $contents, "}}", $equals );
                    // End this string at the newline or the }} (closing the template), whichever
                    // comes first
                    $newline = strpos ( $contents, "\n", $equals );
                    if ( $closingBrace && $closingBrace < $newline ) {
                        $newline = $closingBrace;
                    }
                    if ( $newline ) {
                        $mediawiki = strpos ( $contents, 'MediaWiki', $equals );
                        if ( $mediawiki && $mediawiki < $newline ) {
                            $contents = substr_replace ( $contents, 'Wikia', $mediawiki, 9 );
                            echo $contents;
                            $wiki->edit( $line, $contents, 'Robot: Changing engine. MediaWiki -> Wikia' );
                        }
                    }
                }
            }
        }
    }
}