Extension:YAnswersStats
|
YAnswersStats Release status: stable |
|||
|---|---|---|---|
| Implementation | Parser function | ||
| Description | Obtains a user's statistics from a Yahoo! Answers Profile | ||
| Author(s) | Lojjik Braughler (PseudoOneTalk) | ||
| Last version | 1.0.0 (23 August 2011) | ||
| MediaWiki | 1.16 | ||
| PHP | 5.2 | ||
| License | GNU GPL v3.0+ | ||
| Download | download | ||
| Example | {{#ya:AA10030221|1}} | ||
|
|||
|
|||
|
Check usage (experimental) |
|||
Contents |
[edit] Usage
This extension uses regex matches to find user statistics from their Yahoo! Answers profile. In order to use this extension, you must first find their profile ID. If you visit a Yahoo! Answers profile, you should see a link like this: http://answers.yahoo.com/activity?show=AA10030221. The profile ID is the string after "show=". Unfortunately, this is the only way to identify Yahoo! Answers users. Next, you need to know what statistic you want. This extension matches 4:
- User's level (parameter "1")
- User's total points (parameter "2")
- User's points until the next level (parameter "3")
- User's points this week (parameter "4")
Now, to display the statistic on a page, you use: {{#ya:PROFILEID|parameter}}, where the parameter is of the options above. You could write a template to make it easier to use this extension. Example, a template with the name "Template:YAStat" (used like {{YAStat|PROFILE_ID|level}} would be:
<nowiki> {{#ya:{{{1}}}| {{#switch: {{{2}}} | level = 1 | points = 2 | pointslevel = 3 | pointsweek = 4 | 1 }}}} </nowiki>
[edit] Download
Please cut and paste the code found below and place it in $IP/extensions/YAnswersStats.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.
[edit] Installation
To install this extension, add the following to LocalSettings.php:
require_once("$IP/extensions/YAnswersStats.php");
[edit] Configuration parameters
There is only one configuration parameter for this extension: $wgYALimit. Set this to the maximum number of calls allowed per page. Defaults to 5. Example:
$wgYALimit = 8;
[edit] Code
<?php /** * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ # Only execute extension through MediaWiki if ( !defined( 'MEDIAWIKI' ) ) { die(); } // Extension credits that will show up on Special:Version $wgExtensionCredits['parserhook'][] = array( 'name' => 'YAnswersStats', 'version' => '1.0.0', 'author' => 'Lojjik Braughler', 'description' => 'A parser function returning user data from a Yahoo! Answers profile', 'url' => 'http://www.mediawiki.org/wiki/Extension:YAnswersStats', ); # We define the setup function $wgHooks['ParserFirstCallInit'][] = 'wfYAnswers'; # We should set a limit to prevent making too many calls $wgYALimit = 5; $wgYATimes = 0; # Cache of the YA page, so we don't have to make a call every time someone loads the page (update on purge) $wgYAnswersCache = array(); # Initialise the parser function $wgHooks['LanguageGetMagic'][] = 'wfYAnswers_Magic'; # Setup parser function function wfYAnswers( &$parser ) { $parser->setFunctionHook( 'ya', 'wfYAnswers_Render' ); return true; } # Parser function function wfYAnswers_Magic( &$magicWords ) { $magicWords['ya'] = array( 0, 'ya' ); return true; } # Render and match function wfYAnswers_Render( &$parser, $profile, $option = 1 ) { global $wgYAch, $wgYAnswersCache, $wgYALimit, $wgYATimes, $wgHTTPTimeout; $profile = trim( $profile ); if( $profile == '' ) { return 0; } elseif( array_key_exists( $profile, $wgYAnswersCache ) ) { $data = $wgYAnswersCache[$profile]; // We're matching html in the source of the page and applying it to the regex array preg_match( '/\<div class="member-current-level"\>\<span\>Level (?<level>\d+)\<\/span\>/i', $data, $regex['level'] ); preg_match( '/\<td class="count"\>(?<points>\d+)\<\/td\>\<th scope="row"\>Points\<\/th\>/i', $data, $regex['points'] ); preg_match( '/\<td class="count"\>(?<pointslevel>\d+)\<\/td\>\<th scope="row"\>Points to next level\<\/th\>/i', $data, $regex['pointslevel'] ); preg_match( '/\<td class="count"\>(?<pointsweek>\d+)\<\/td\>\<th scope="row"\>Points this week\<\/th\>/i', $data, $regex['pointsweek'] ); // To allow retrieval of different statistics switch( $option ) { case 1: /* level */ return $regex['level']['level']; break; case 2: /* points */ return $regex['points']['points']; break; case 3: return $regex['pointslevel']['pointslevel']; break; case 4: return $regex['pointsweek']['pointsweek']; break; default: return $regex['level']['level']; break; } } elseif( $wgYATimes < $wgYALimit || $wgYALimit == 0 ) { $wgYATimes++; if( !isset( $wgYAch ) ) { # Setup cURL $wgYAch = curl_init(); curl_setopt( $wgYAch, CURLOPT_TIMEOUT, $wgHTTPTimeout ); curl_setopt( $wgYAch, CURLOPT_RETURNTRANSFER, true ); } /* Note: This is where we fetch a copy of the page and parse it, in case it's not in the cache or it's time to update*/ curl_setopt( $wgYAch, CURLOPT_URL, 'http://answers.yahoo.com/activity?show=' . urlencode( $profile ) ); if( $data = curl_exec( $wgYAch ) ) { $wgYAnswersCache[$profile] = $data; $status = curl_getinfo( $wgYAch, CURLINFO_HTTP_CODE ); if( $status == 200 ) { $data = $wgYAnswersCache[$profile]; preg_match( '/\<div class="member-current-level"\>\<span\>Level (?<level>\d+)\<\/span\>/i', $data, $regex['level'] ); preg_match( '/\<td class="count"\>(?<points>\d+)\<\/td\>\<th scope="row"\>Points\<\/th\>/i', $data, $regex['points'] ); preg_match( '/\<td class="count"\>(?<pointslevel>\d+)\<\/td\>\<th scope="row"\>Points to next level\<\/th\>/i', $data, $regex['pointslvl'] ); preg_match( '/\<td class="count"\>(?<pointsweek>\d+)\<\/td\>\<th scope="row"\>Points this week\<\/th\>/i', $data, $regex['pointsweek'] ); switch( $option ) { case 1: /* level */ return $regex['level']['level']; break; case 2: /* points */ return $regex['points']['points']; break; case 3: /* points to level */ return $regex['pointslvl']['pointslevel']; break; case 4: /* points this week */ return $regex['pointsweek']['pointsweek']; break; default: /* defaults to user's level */ return $regex['level']['level']; break; } } elseif( $status == 404 ) { return $wgYAnswersCache[$profile] = 1; } } return $wgYAnswersCache[$profile] = 2; } else { return 3; } }
