User:LeucosticteBot/ChangeWikiaEngine.php: Difference between revisions

From WikiIndex
Jump to navigation Jump to search
(Created page with "This bot looks for pages that have a url with .wikia.com in them, and changes the wiki engine to wikia. <source lang="php"> <?php /* * ChangeWikiaEngine.php * By Leucostict...")
 
No edit summary
Line 1: Line 1:
This bot looks for pages that have a url with .wikia.com in them, and changes the wiki engine to wikia.
This bot 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.


<source lang="php">
<source lang="php">

Revision as of 10:31, 26 November 2012

This bot 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      = new wikipedia;
$wiki->url = "http://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 );

// Each line is a wiki page
foreach ( $lines as $line ) {
    // 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' );
                    }
                }
            }
        }
    }
}