This repository has been archived on 2024-03-20. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
GitSnax/GitSnax.body.php
2024-03-20 09:26:54 -05:00

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