Makke_
Makke_

Reputation: 49

Get MediaWiki session() username

I have statistic stored in a database. I would like Users in our MediaWiki to view only statistic that is linked to their username. I hav e tried below but it gives me just a blank page (no errors). My guess is that it doesn´t get the username from the session. How should I manage to do that?

<?php
$dbtype           = "mysql";
$dbhost           = "localhost";
$dbname           = "test";
$dbuser           = "username";
$dbpassword       = "passw0rd";

$conn = mysql_connect($dbhost, $dbuser, $dbpassword);

if(! $conn )
{
die('Could not connect: ' . mysql_error());
}   

$wgHooks['UserLoginComplete'][] = 'test::onUserLoginComplete';
class test
{
public static function UserLoginComplete( $user, $password, &$retval, &$msg ) {
    $dbr = wfGetDB( DB_SLAVE );
    $row = $dbr->selectRow(
        'coaching',                                 // $Table
        'Name',                                     // $vars (column of the table)
        array( 'Name' => $user ),                   // $Conitions
        __METHOD__                                  // $fname = 'Database::select'
    );

mysql_select_db($dbname, $conn);

echo '<table border="0" border-color="#CCCCCC" bgcolor="#FFFFFF"><thead><tr><td><b>ID</b></td><td><b>Namn</b></td><td><b>Datum</b></td><td><b>Score</b></td></tr></thead><tbody>';
$query = "SELECT ID, Name, Date, Score FROM coaching WHERE Name = $user";

$result = mysql_query($query) or die(mysql_error());
$color="1";
while($row = mysql_fetch_array($result)){
if($color==1){
echo '<tr bgcolor="#D0D0D0">';
$color="2";
} else {
echo '<tr bgcolor="#E0E0E0">';
$color="1";
}
echo '<td>'.$row['ID'].'</td><td>'.$row['Name'].'</td><td>'.$row['Date'].'</td><td>'.$row['Score'].'</td></tr>';    
}
?>

Upvotes: 1

Views: 918

Answers (2)

Ilmari Karonen
Ilmari Karonen

Reputation: 50328

Your actual problem is that your class is missing a closing curly brace (}), causing a PHP syntax error. (Actually, you're missing two of them.)

To avoid and/or catch such errors, you should:

Anyway, once you've fixed those issues (and the syntax error), you've still got some other problems. One, pointed out by Jamie Thingelstad, is that the parameters for your UserLoginComplete hook are wrong; they should be:

function onUserLoginComplete( &$user, &$inject_html ) {

Second, you should not echo anything in a UserLoginComplete hook (or in any MediaWiki extension code at all, really). What you can do, in this case, is append the HTML you want to output to the $inject_html parameter, like this:

$inject_html .= '<table border="0" ...';

(This output method is peculiar to this specific hook. The usual way to output something in a MediaWiki extension is using the OutputPage class, either through an instance passed to your hook — or obtained via some other object passed to it, such as anything that implements IContextSource — or, if there isn't any, using the global instance $wgOut.)

Also, you're not escaping the $user variable in your SQL query, so your code is potentially vulnerable to SQL injection. Besides, you shouldn't be using the old mysql functions anyway; they've been deprecated.

Finally, I'm not sure if UserLoginComplete is really the right hook for what you want to do, anyway. As the name suggests, it runs only when the user logs in to MediaWiki, and will not run again unless the user logs out and back in. If you would like your users to be able to view your statistics whenever you want, you should either use some hook that runs on every page view, or, perhaps better yet, make a custom special page your users can visit to see the statistics.

Ps. Also, you should never use the closing ?> tag in PHP unless you actually intend to include some HTML code after it. Having a ?> at the end of the file is nothing but an invitation for mysterious errors.

Upvotes: 1

Jamie Thingelstad
Jamie Thingelstad

Reputation: 81

I'm assuming you are doing this inside of a MediaWiki extension. It wasn't clear from your question if that was the case but I see you are registering a hook. Note that the hook you are registering UserLoginComplete shows a different function signature than you are defining. To get the username you need to use the instance of the $wgUser object object you are being passed and then call the getName method on that object.

Something like:

$user->getName();

$user itself is an object reference.

Upvotes: 1

Related Questions