Extension:Question2Answer

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual
Crystal Clear action run.png
Question2Answer

Release status: experimental

Author(s) Peter Gadjokov
MediaWiki 16.4
Database changes Yes
License Creative Commons
Download No link
Example http://en.wikiaudio.org

Translate the Question2Answer extension if it is available at translatewiki.net

Check usage and version matrix; code metrics

To install q2a with mw signle sign on -

Start with the instructions at

http://www.question2answer.org/single-sign-on.php

Perform all the steps up to and including step 6. For MySQL database name use the database name of your mediawiki install.


For step 7,edit the provided qa-external-users.php and use it in place of the included default question2answer file of the same name. If you're integrating with a mediawiki install that has different cookie settings than than a stock mw install add the correct ones.(you can find your cookie settings using the cookie editor for firefox).

Find the localsettings.php file in the root directory of your mw install and add a line to define the cookie domain. In the case of a website such as en.yourwebsite.org the setting would be

$wgCookieDomain = '.yourwebsite.org';

For local host this setting shouldn't matter and will work by leaving it empty

$wgCookieDomain = ;

Complete the q2a steps 8, 9 and 10 and test.

---

What's in qa-external-users.php -

I've added a few defines 11 to 24 - you can use these to adjust the cookie names and to enable the login/registration/logut links integration feature. Additionally I've added a few utility functions which are all prefixed wka_ - they should all be pretty self-explanatory.

The functions qa_ are the q2a integration interface. I've modified the required parts of the interface, that is, the functions:

function qa_get_mysql_user_column_type() function qa_get_login_links($relative_url_prefix, $redirect_back_to_url) function qa_get_logged_in_user() function qa_get_user_email($userid) function qa_get_userids_from_public($publicusernames)

You should not need to touch function qa_get_mysql_user_column_type()

The rest are largely based on the examples provided in the original - before making significant changes to this code, you should review the original file as it contains extensive documentation and examples.

The real work happens in qa_get_logged_in_user() and this is where the user role mapping happens. Line 80 defines the base level for regular mw users and lines 95 to 104 map mw sysops to q2a 'super admins'. Additional mappings could be added there, say, mw level 'bureaucrat' to q2a admins.

The rest of the qa_ interface is left in its default implementation - there are many more integration options in the interface and it seems fairly well documented.





qa-external-users.php[edit | edit source]

<?php
 
 
 
    if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
        header('Location: ../');
        exit;
    }
 
 
// cookie names for stock mw install. If these are different on your install then change them accordingly
define('MW_UID_COOKIE', 'wikiUserID');
define('MW_UNAME_COOKIE', 'wikiUserName');
define('MW_SESSION_COOKIE', 'wiki_session');
 
 
 
 
 
/*  Set these urls to your mediawiki login and log out urls*/
 
define('MW_LOGIN_URL', '');
define('MW_LOGOUT_URL', '');
define('MW_REG_URL', '');
define('USE_LOGIN_LINKS', false);
 
 
 
function wka_cookie_value($key)
{
    return isset($_COOKIE[$key]) ? $_COOKIE[$key] : NULL;
}
 
function wka_get_uname()
{
    return wka_cookie_value(MW_UNAME_COOKIE);
 
}
 
function wka_get_uid()
{
    return wka_cookie_value(MW_UID_COOKIE);
}
 
// check the user is logged into mw - that is, has a mw session and
// the UID cookie is set. 
function wka_is_logged_in()
{
    return (wka_cookie_value(MW_SESSION_COOKIE) && wka_cookie_value(MW_UID_COOKIE));
}
 
 
function qa_get_mysql_user_column_type()
{
    return 'INT';
}
 
 
function qa_get_login_links($relative_url_prefix, $redirect_back_to_url)
{
    if (USE_LOGIN_LINKS)
    {
        return array(
            'login' => MW_LOGIN_URL,
            'register' => MW_REG_URL,
            'logout' => MW_LOGOUT_URL
        );
    }
    else
    {
        return null;
    }
} 
 
 
function qa_get_logged_in_user()
{
    session_start();
 
    // default q2a access level for regular mw users
    $level = QA_USER_LEVEL_BASIC;
 
    $uid   = wka_get_uid();
    $uname = wka_get_uname();
 
 
    if (wka_is_logged_in()) 
    {
        $qa_db_connection = qa_db_connection();
 
        // grab user data from mw db (just email in this case)
        $udata = mysql_fetch_assoc(
            mysql_query("SELECT user_email FROM user WHERE user_id=" . mysql_real_escape_string($uid, $qa_db_connection), $qa_db_connection)
        );
 
        // see if user is in the mw sysop group
        $uperms = mysql_fetch_assoc(
            mysql_query("SELECT ug_user from user_groups where ug_group = 'sysop' and ug_user = " . mysql_real_escape_string($uid, $qa_db_connection), $qa_db_connection)
        );
 
        // if a sysop, set QA user level to super admin
        if (is_array($uperms) && isset($uperms['ug_user']))
        {
            $level = QA_USER_LEVEL_SUPER;
        }
 
        if (is_array($udata))
        {
            return array(
                'userid' => $uid,
                'publicusername' => $uname,
                'email' => $udata['user_email'],
                'level' => $level
            );
        }
    }
 
    return null;       
}
 
 
function qa_get_user_email($userid)
{
 
    $qa_db_connection=qa_db_connection();
 
    $result=mysql_fetch_assoc(
        mysql_query(
            "SELECT user_email FROM user WHERE user_id='".mysql_real_escape_string($userid, $qa_db_connection)."'",
            $qa_db_connection
        )
    );
 
    if (is_array($result))
        return $result['user_email'];
 
    return null;
}
 
 
function qa_get_userids_from_public($publicusernames)
{
 
    $publictouserid=array();
 
    if (count($publicusernames)) {
        $qa_db_connection=qa_db_connection();
 
        $escapedusernames=array();
        foreach ($publicusernames as $publicusername)
            $escapedusernames[]="'".mysql_real_escape_string($publicusername, $qa_db_connection)."'";
 
        $results=mysql_query(
            'SELECT user_name, user_id FROM user WHERE user_name IN ('.implode(',', $escapedusernames).')',
            $qa_db_connection
        );
 
        while ($result=mysql_fetch_assoc($results))
            $publictouserid[$result['user_name']]=$result['userid'];
    }
 
    return $publictouserid;
}
 
 
function qa_get_public_from_userids($userids)
{
    $useridtopublic=array();
 
    if (count($userids)) {
        $qa_db_connection=qa_db_connection();
 
        $escapeduserids=array();
        foreach ($userids as $userid)
            $escapeduserids[]="'".mysql_real_escape_string($userid, $qa_db_connection)."'";
 
        $results=mysql_query(
            'SELECT user_name, user_id FROM user WHERE user_id IN ('.implode(',', $escapeduserids).')',
            $qa_db_connection
        );
 
        while ($result=mysql_fetch_assoc($results))
            $useridtopublic[$result['user_id']]=$result['user_name'];
    }
 
    return $useridtopublic;
}
 
 
    function qa_get_logged_in_user_html($logged_in_user, $relative_url_prefix)
/*
    ==========================================================================
         YOU MAY MODIFY THIS FUNCTION, BUT THE DEFAULT BELOW WILL WORK OK
    ==========================================================================
 
    qa_get_logged_in_user_html($logged_in_user, $relative_url_prefix)
 
    You should return HTML code which identifies the logged in user, to be displayed next to the
    logout link on the QA pages. This HTML will only be shown to the logged in user themselves.
 
    $logged_in_user is the array that you returned from qa_get_logged_in_user(). Hopefully this
    contains enough information to generate the HTML without another database query, but if not,
    call qa_db_connection() to get the connection to the QA database.
 
    $relative_url_prefix is a relative URL to the root of the QA site, which may be useful if
    you want to include a link that uses relative URLs. If the QA site is in a subdirectory of
    your site, $relative_url_prefix.'../' refers to your site root (see example 1).
 
    If you don't know what to display for a user, you can leave the default below. This will
    show the public username, linked to the QA profile page for the user.
*/
    {
 
    //  By default, show the public username linked to the QA profile page for the user
 
        $publicusername=$logged_in_user['publicusername'];
 
        return '<A HREF="'.htmlspecialchars($relative_url_prefix.'user/'.urlencode($publicusername)).
            '" CLASS="qa-user-link">'.htmlspecialchars($publicusername).'</A>';
 
    /*
        Example 1 - suitable if:
 
        * Your QA site:        http://www.mysite.com/qa/
        * Your user pages:     http://www.mysite.com/user/[username]
 
        $publicusername=$logged_in_user['publicusername'];
 
        return '<A HREF="'.htmlspecialchars($relative_url_prefix.'../user/'.urlencode($publicusername)).
            '" CLASS="qa-user-link">'.htmlspecialchars($publicusername).'</A>';
    */
 
    /*
        Example 2 - suitable if:
 
        * Your QA site:        http://qa.mysite.com/
        * Your user pages:     http://www.mysite.com/[username]/
        * 16x16 user photos:   http://www.mysite.com/[username]/photo-small.jpeg
 
        $publicusername=$logged_in_user['publicusername'];
 
        return '<A HREF="http://www.mysite.com/'.htmlspecialchars(urlencode($publicusername)).'/" CLASS="qa-user-link">'.
            '<IMG SRC="http://www.mysite.com/'.htmlspecialchars(urlencode($publicusername)).'/photo-small.jpeg" '.
            'STYLE="width:16px; height:16px; border:none; margin-right:4px;">'.htmlspecialchars($publicusername).'</A>';
    */
 
    }
 
 
    function qa_get_users_html($userids, $should_include_link, $relative_url_prefix)
/*
    ==========================================================================
         YOU MAY MODIFY THIS FUNCTION, BUT THE DEFAULT BELOW WILL WORK OK
    ==========================================================================
 
    qa_get_users_html($userids, $should_include_link, $relative_url_prefix)
 
    You should return an array of HTML to display for each user in $userids. For each element of
    this array, the userid should be in the key, with the corresponding HTML in the value.
 
    Call qa_db_connection() to get the connection to the QA database. If your database is shared with
    QA, you can use this with PHP's MySQL functions such as mysql_query() to run queries. If you
    access this database or any other, try to use a single query instead of one per user.
 
    If $should_include_link is true, the HTML may include links to user profile pages.
    If $should_include_link is false, links should not be included in the HTML.
 
    $relative_url_prefix is a relative URL to the root of the QA site, which may be useful if
    you want to include links that uses relative URLs. If the QA site is in a subdirectory of
    your site, $relative_url_prefix.'../' refers to your site root (see example 1).
 
    If you don't know what to display for a user, you can leave the default below. This will
    show the public username, linked to the QA profile page for each user.
*/
    {
 
    //  By default, show the public username linked to the QA profile page for each user
 
        $useridtopublic=qa_get_public_from_userids($userids);
 
        $usershtml=array();
 
        foreach ($userids as $userid) {
            $publicusername=$useridtopublic[$userid];
 
            $usershtml[$userid]=htmlspecialchars($publicusername);
 
            if ($should_include_link)
                $usershtml[$userid]='<A HREF="'.htmlspecialchars($relative_url_prefix.'user/'.urlencode($publicusername)).
                    '" CLASS="qa-user-link">'.$usershtml[$userid].'</A>';
        }
 
        return $usershtml;
 
    /*
        Example 1 - suitable if:
 
        * Your QA site:        http://www.mysite.com/qa/
        * Your user pages:     http://www.mysite.com/user/[username]
 
        $useridtopublic=qa_get_public_from_userids($userids);
 
        foreach ($userids as $userid) {
            $publicusername=$useridtopublic[$userid];
 
            $usershtml[$userid]=htmlspecialchars($publicusername);
 
            if ($should_include_link)
                $usershtml[$userid]='<A HREF="'.htmlspecialchars($relative_url_prefix.'../user/'.urlencode($publicusername)).
                    '" CLASS="qa-user-link">'.$usershtml[$userid].'</A>';
        }
 
        return $usershtml;
    */
 
    /*
        Example 2 - suitable if:
 
        * Your QA site:        http://qa.mysite.com/
        * Your user pages:     http://www.mysite.com/[username]/
        * User photos (16x16): http://www.mysite.com/[username]/photo-small.jpeg
 
        $useridtopublic=qa_get_public_from_userids($userids);
 
        foreach ($userids as $userid) {
            $publicusername=$useridtopublic[$userid];
 
            $usershtml[$userid]='<IMG SRC="http://www.mysite.com/'.htmlspecialchars(urlencode($publicusername)).'/photo-small.jpeg" '.
                'STYLE="width:16px; height:16px; border:0; margin-right:4px;">'.htmlspecialchars($publicusername);
 
            if ($should_include_link)
                $usershtml[$userid]='<A HREF="http://www.mysite.com/'.htmlspecialchars(urlencode($publicusername)).
                    '/" CLASS="qa-user-link">'.$usershtml[$userid].'</A>';
        }
 
        return $usershtml;
    */
 
    }
 
 
    function qa_user_report_action($userid, $action, $questionid, $answerid, $commentid)
/*
    ==========================================================================
         YOU MAY MODIFY THIS FUNCTION, BUT THE DEFAULT BELOW WILL WORK OK
    ==========================================================================
 
    qa_user_report_action($userid, $action, $questionid, $answerid, $commentid)
 
    Informs you about an action by user $userid that modified the database, such as posting,
    voting, etc... If you wish, you may use this to log user activity or monitor for abuse.
 
    Call qa_db_connection() to get the connection to the QA database. If your database is shared with
    QA, you can use this with PHP's MySQL functions such as mysql_query() to run queries.
 
    $action is one of:
    q_post, q_edit, q_hide, q_reshow, q_delete, q_claim, q_vote_up, q_vote_down, q_vote_nil
    a_post, a_edit, a_hide, a_reshow, a_delete, a_claim, a_vote_up, a_vote_down, a_vote_nil, a_select, a_unselect, a_to_c
    c_post, c_edit, c_hide, c_reshow, c_delete, c_claim
 
    $questionid and/or $answerid and/or $commentid contain the ID of the relevant question or answer
    or comment affected, or null if this information is not appropriate for $action.
 
    FYI, you can get the IP address of the user from $_SERVER['REMOTE_ADDR'].
*/
    {
        // do nothing by default
    }
 
 
/*
    Omit PHP closing tag to help avoid accidental output
*/