1

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>';    
}
?>

Makke_
  • 47
  • 6
  • You should not guess, you should actually know what's going on in your code. – svick Apr 26 '13 at 09:01
  • Unfortunately not all of us is that skilled i'm afraid, but thanks for you assistant. – Makke_ Apr 26 '13 at 09:33
  • What I meant is that if you don't know, you should find it out. Either by using a debugger or, if that's not available to you, by writing the actual value somewhere where you can read it. – svick Apr 26 '13 at 09:35
  • It doesn´t work just to use echo 'Username: '$user; so there must be something wrong in the code. I don´t have other possibillity rather than using code step by step to decode the code. – Makke_ Apr 26 '13 at 09:51
  • Have you tried to display $query to see the generated query ? So you can see whether is correct (and generated at all) and run it manually. – Jacopofar Apr 26 '13 at 18:04
  • echo 'Test: '.$query.''; gives me Test: SELECT ID, Name, Date, Score FROM coaching WHERE Name = 'JOHANA3' This is when I specify the username. The problem (and know i´m sure ;) ) is that I don´t know how to get the username from the session. – Makke_ Apr 29 '13 at 06:57

2 Answers2

1

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.

  • I am trying to get the username to an external php-file. I will store the file at the same location as MediaWiki but I don´t want to make an extension of it. I tried to use a hook just to see if I could manage to retrieve the data I needed that way with no luck. IS it possible to retrieve the username from the session to an external php-file? – Makke_ Apr 29 '13 at 06:27
1

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.

Community
  • 1
  • 1
Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
  • I´m not sure If I can use a hook at all. I am trying to get the username from the session to an external php-file but as you allready seen my coding skills are lame. Is it possible to retrieve the username from the session and use it in a external php-file? – Makke_ Apr 29 '13 at 06:29
  • A quick and dirty way to get the MediaWiki user name is to just use `$_COOKIE["xxxUserName"]`, where `xxx` is the [cookie prefix](http://www.mediawiki.org/wiki/Manual:$wgCookiePrefix) for your wiki (which will be based on the database name/prefix, if you haven't set it explicitly). If you're not sure what it is, peek in your browser's cookie jar to check. – Ilmari Karonen Apr 29 '13 at 13:37
  • You're welcome. If your problem has been solved, don't forget to mark one of the answers as accepted by clicking the check mark icon to the left of it. Once you have 15 rep on this site, you can also upvote any answers that you like using the arrow icons. – Ilmari Karonen Apr 30 '13 at 15:53