Extension talk:TitleKey
Contents
| Thread title | Replies | Last modified |
|---|---|---|
| Method to setup without shell access | 5 | 12:38, 28 December 2012 |
| Hack to make it work when you put it in a directory other than $IP/extensions | 3 | 10:07, 26 September 2012 |
| One step further: search suggestions for matching '''any word''' in the title | 0 | 14:15, 10 August 2012 |
| Include in Release | 1 | 18:04, 20 July 2012 |
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.";
You might also have a look at http://www.mediawiki.org/wiki/Extension_talk:MaintenanceShell#Workaround_for_Extension_TitleKey for using TitleKey with the MaintenanceShell extension.
hth Frank
Yeah I looked into MaintenanceShell, but in general I don't use extensions marked "experimental" on my production server.
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 ;-)
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.
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.
If you put TitleKeys in a directory other than $IP/extensions, it won't be able to create the tables. In fact, it'll crash. So, change the beginning lines of rebuildTitleKeys.php to:
<?php if ( !defined( 'MEDIAWIKI' ) ) { die( 'This file is a MediaWiki extension, it is not a valid entry point' ); } $IP = getenv( 'MW_INSTALL_PATH' ); if ( $IP === false ) $IP = dirname( __FILE__ ) . '/../..'; if ( !isset ( $wgTitleKeyMaintenancePath ) ) { require_once( "$IP/maintenance/Maintenance.php" ); } else { require_once( "$wgTitleKeyMaintenancePath/maintenance/Maintenance.php" ); }
Then, in LocalSettings.php, set that new global variable to your wiki path; e.g. on mine, I use:
$wgTitleKeyMaintenancePath = "/home/stauffenbergssh/en.rationalwikiwikiwiki.org/w/";
I've been told that isset isn't all that safe to use, so if anyone has a better way, please let me know. Thanks.
Did you actually try this? Because if it isn't able to locate $IP then where does LocalSettings.php (and wgTitleKeyMaintenancePath) come from?
Well, on a new MediaWiki installation, first I had to comment out the require_once( "$IP/extensions/TitleKey/TitleKey.php" ); and run update.php, and then I had to uncomment it and run update.php again. I guess you're saying, perhaps the hack did nothing, eh? Hard to say; I'd have to run some experiments to isolate what is making it work or not work. I keep my extensions in /home/stauffenbergssh/extensions/ , and it seemed like that was what was messing it up, because it was using the /../.. and giving me error messages with that path in it. We can userfy this thread if it's desirable to not put that suggestion out there. Leucosticte (talk) 06:43, 26 September 2012 (UTC)
You might also have a look at http://www.mediawiki.org/wiki/Extension_talk:MaintenanceShell#Workaround_for_Extension_TitleKey
hth Frank
