87 lines
2.3 KiB
PHP
87 lines
2.3 KiB
PHP
<?php
|
|
|
|
/**
|
|
* GitSnaxHooks class
|
|
*
|
|
* @section LICENSE
|
|
* 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 2 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, write to the Free Software Foundation, Inc.,
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
*
|
|
* @file
|
|
* @ingroup Extensions
|
|
* @author Troy Engel
|
|
* @license GNU GPL v2+
|
|
*/
|
|
|
|
class GitSnaxHooks {
|
|
/**
|
|
* Initialize the callback parser hook
|
|
*
|
|
* @param Parser $parser: the Parser object
|
|
* @return true: always return true to not block MW
|
|
*/
|
|
public static function setupGitSnax( $parser ) {
|
|
$parser->setFunctionHook( 'gitsnack', array( 'GitSnaxHooks',
|
|
'getGitSnack' ) );
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Retrieve the github content
|
|
*
|
|
* @param Parser $parser: the Parser object
|
|
* @param string $mode: 'code' or 'gist' link
|
|
* @param string $file: the URL part minus hostname, no leading /
|
|
* @return string|false: either the content or false
|
|
*/
|
|
public static function getGitSnack( $parser, $mode = '', $file = '' ) {
|
|
|
|
global $wgGitSnaxCode, $wgGitSnaxGist, $wgGitSnaxCTTL;
|
|
|
|
switch ( $mode ) {
|
|
case "gist":
|
|
$gitURL = $wgGitSnaxGist;
|
|
break;
|
|
case "code":
|
|
$gitURL = $wgGitSnaxCode;
|
|
break;
|
|
default:
|
|
return false;
|
|
}
|
|
|
|
if ( $file === '' ) {
|
|
return false;
|
|
} else {
|
|
$gitURL = $gitURL . $file;
|
|
$cacheKey = wfMemcKey( __METHOD__, $gitURL );
|
|
$cachedValue = wfGetMainCache()->get( $cacheKey );
|
|
|
|
if ( is_string( $cachedValue ) ) {
|
|
$data = $cachedValue;
|
|
} else {
|
|
$data = \Http::get( $gitURL );
|
|
wfGetMainCache()->set( $cacheKey, $data, $wgGitSnaxCTTL );
|
|
}
|
|
|
|
$result = "<pre style=\"white-space:pre-wrap;\">$data</pre>";
|
|
|
|
return array( $result, 'nowiki' => true, 'noparse' => true,
|
|
'noargs' => true, 'isHTML' => true, );
|
|
}
|
|
|
|
} // getGitSnack
|
|
|
|
} // GitSnaxHooks
|
|
|