Topic on Extension talk:TitleKey

Method to setup without shell access

6
Jamesmontalvo3 (talkcontribs)

On my production server I do not yet have shell access, so I had to work around this. I wrote the following script to be used over HTTP to add the required MySQL table and populate it with data. This script worked in my environment and is not guaranteed to work on yours. It is certainly not the most robust code possible. MediaWiki/Titlekey developer gurus: please feel free to comment on any erroneous content. Hope this helps!

<?php

/**
 *  
 *  Before you use this script please note that this was created by a person
 *      inexperienced with the inner workings of MediaWiki. It was created to
 *      get around the problem of not having command line access on a 
 *      particular server with a specific MediaWiki load with specific 
 *      extensions. THIS MAY NOT WORK ON YOUR COMPUTER. Anyone who knows more
 *      about MediaWiki please feel free to edit mercilessly.
 *  
 *  I've tried to write this script without any dependencies so it should run
 *      on just about any PHP load. However, this abysmally slower than using
 *      the standard rebuildTitleKeys.php script from the command line. If your
 *      wiki is large this script may not work for you (without vastly 
 *      increasing max_execution_time in php.ini).
 *  
 *  Note on implementation: titlekey.sql says tk_key is "with namespace prefix"
 *      which seems to me that titles should be recorded like with the prefix
 *      like "Template:Citation needed" instead of just "Citation needed".
 *      However, my inspection of the Titlekey extension indicates that prefix
 *		is not included.
 *  
 *  Input your database host name, username, password and database name below.
 *  
 **/

$hostname = "YOUR HOST NAME";
$username = "YOUR USERNAME";
$password = "YOUR PASSWORD";
$db_name  = "YOUR DATABASE NAME";

/** 
 *  No changes should be required below this point...unless I made a mistake.
 *  
 *  USE AT YOUR OWN RISK! BACKUP YOUR WIKI FIRST!
 **/

echo "Connecting to database...";

// Connect to database 
$db = mysql_connect($hostname, $username, $password)
    or die ('Unable to connect. Check you connection parameters.');

// Select particular database
mysql_select_db($db_name, $db)
    or die(mysql_error($db));

echo "connection successful.<br />";
	
// Construct query to retrieve all relevant page information from standard
// MediaWiki table 'page'
$query = "SELECT page_id, page_namespace, page_title FROM page";

echo "Retrieving page information...";

// Perform query to retrieve page information
$result = mysql_query($query, $db) or die (mysql_error($db));

echo "page information retrieval successful.<br />";

// Loop through pages (for each result create associative array $page)
while( $page = mysql_fetch_assoc($result) ) {

    /**
     *  FORMATTING PAGE NAMES SO THEY CAN BE SEARCHED WITH CASE-INSENSITIVITY
     *
     *  Page names from the standard MediaWiki table 'page' must be reformatted
     *      to be searched quickly and easily with case-insensitivity. Three
     *      actions must be performed on the page title:
     *
     *      1) Replace all underscores with spaces using PHP's str_replace
     *      2) Convert all characters to upper case using PHP's strtoupper
     *      3) Always escape characters before inserting into a database
     **/ 
    $titlekey_page_name = mysql_real_escape_string(
        strtoupper(
            str_replace("_", " ", $page['page_title'])
        )
    );

    // Create a string of SQL values to be inserted into the new titlekey
    //      table. String is pushed to $inserts array to be used later.
    $inserts[] = '(' 
        . $page['page_id'] . ', ' 
        . $page['page_namespace'] . ', ' 
        . '"' . $titlekey_page_name . '"'
        . ')';
        
}

// Take $inserts array (array of strings) and glue them together with ", "
//      in between each string.
$inserts = implode(', ', $inserts);

/**
 *  Build the full insert query. The new table 'titlekey' has columns:
 *      1) titlekey.tk_page corresponds to page.page_id
 *      2) titlekey.tk_namespace corresponds to page.page_namespace
 *      3) titlekey.tk_key corresponds to page.page_title
 *          (in all caps, no underscores)
 **/
$insert_query = 
    'INSERT INTO titlekey
        (tk_page, tk_namespace, tk_key)
    VALUES ' . $inserts . ';';
	
// if titlekey table does not yet exist, create it
$create_table_query = 
    'CREATE TABLE IF NOT EXISTS titlekey (
        -- Ref to page_id
        tk_page int unsigned NOT NULL,

        -- Keep a denormalized copy of the namespace for filtering
        tk_namespace int NOT NULL,

        -- Normalized title.
        -- With namespace prefix, case-folded, in space form.
        tk_key varchar(255) binary NOT NULL,
	  
        PRIMARY KEY tk_page (tk_page),
        INDEX name_key (tk_namespace, tk_key)

    );';

echo "Creating table 'titlekey' (if required)...";

// Execute query to add titlekey table
mysql_query($create_table_query, $db) or die (mysql_error($db));

// In case titlekey already existed and had data in it, remove all data prior
//     to repopulating.
mysql_query("TRUNCATE TABLE titlekey;", $db) or die (mysql_error($db));

echo "table created.<br />Inserting pages into 'titlekey' table.";

// Execute query to insert values into new titlekey table
mysql_query($insert_query, $db) or die (mysql_error($db));

// Close database connection
mysql_close();

echo "<br /><br />Update operations complete.";
Nakohdo (talkcontribs)
128.157.160.13 (talkcontribs)

Yeah I looked into MaintenanceShell, but in general I don't use extensions marked "experimental" on my production server.

This post was posted by 128.157.160.13, but signed as Jamesmontalvo3.

Nakohdo (talkcontribs)

But if the alternative is using a PHP script for direct database access with database credentials in plain text this might be an option to be considered ;-)

Jamesmontalvo3 (talkcontribs)

This is intended as a run-once-and-delete script...as opposed to having MaintenanceShell, an experimental extension which exposes shell-like privileges, running full time on your production server. It's a trade off of whether you trust yourself to scan through my intentionally simplified code or trust the creators of MaintenanceShell to not leave security holes and possible data-corrupting bugs.

Nakohdo (talkcontribs)

I tried another extension which works with MediaWiki 1.20.2, Extension:Maintenance (beta).

As the rebuildTitleKeys.php file doesn't reside in the default /maintenance folder you have to add the following to your LocalSettings.php:

$wgMaintenanceScripts = array(
  'rebuildTitleKeys' => "$IP/extensions/TitleKey/rebuildTitleKeys.php",
);

http://www.mediawiki.org/wiki/Manual:$wgMaintenanceScripts

Then you can add the following section to the Maintenance extension's metadata.ini file:

[rebuildTitleKeys]
enabled = 1

http://www.mediawiki.org/wiki/Extension:Maintenance#Extending_the_list_of_scripts

Then you should be able to run the Title Key update from the Maintenance extension page.

Reply to "Method to setup without shell access"