I'm using my own cms from scratch, so, i'm adding useful functions for my system, but i got stuck on this:
A phrase is being loaded from lang file on array, in this case, $lang['sign']['server'] = 'Sign in with your {{servername}} registered account:';, and then, by a function, {{servername}} must be replaced by $config['servername'].
What i have so far on my functions class is the following:
public function replaceTags($text)
{
global $config;
return preg_replace("/{{(.*?)}}/" , $config[strtolower("$1")], $text) ;
}
Im calling this function here: $main->set('ssocial', $FUNC->replaceTags($lang['sign']['social']));, but the result is Sign in with your registered account: instead of Sign in with your "Server Name Goes Here" registered account.
Any ideas about why the preg_replace is not retrieving the value?
Also, when $config[”$1”] is inside '' like this '$config[”$1”]', the output is Sign in with your $config[”servername”] registered account:, so i have no clues about what's wrong.
Thanks in advance.
This is a quick and dirty working example using preg_replace_callback
<?php
$config = array('server' => 'my custom text');
function handler($matches){
global $config;
return $config[$matches[1]];
}
function replaceTags($text)
{
return preg_replace_callback("/{{(.*?)}}/" , 'handler', $text) ;
}
print replaceTags("Hello {{server}}");
Output:
Hello my custom text
As for why your code doesn't work: the second parameter of preg_replace is $config[strtolower("$1")], so php will literally look for key "$1" in $config, which probably doesn't exist.
Related
I'm hacking an existing MediaWiki extension, ProcessCite, that adds a custom hook to the Cite extension. Since migrating to PHP 5.4 and MW 1.22 (from PHP 5.3 and MW 1.19.2), the extension does not appear to work correctly. The problem is with the custom hook not returning the data it should do.
Here are the relevant parts of the code:
ProcessCite.php
# Declare the hook:
$wgHooks['CiteBeforeStackEntry'][] = 'wfProcessCite';
# the function itself
function wfProcessCite($str, $argv){
# process $argv and $str to create a new version of $str
# $argv remains unchanged, $str is set to new value
...
$str = "new string";
return true;
}
in Cite_body.php
function stack( $str, $key = null, $group, $follow, $call ) {
# add the call to the CiteBeforeStackEntry hook
wfRunHooks( 'CiteBeforeStackEntry', array( &$str, &$call ) );
I've added debugging statements to the beginning and end of wfProcessCite, which show that $str is being altered; however, debug statements before and after wfRunHooksshow no change to $str.
Can anyone help?
Got the answer from Andru Vallance on the Mediawiki mailing list:
Prepending the function argument with an ampersand character causes it to be passed it in by reference.
That means you are directly manipulating the original variable inside your function, rather than a copy.
function wfProcessCite( &$str, $argv ){
$str = ‘new value’;
return true;
I am currently creating a web app and I would like to allow my users to create a template. I would only allow them to use HTML and some functions to get some values, so I have some functions like
getDescription(); but since its PHP I also have other function (e.g. phpinfo();) which I don't want them to use.
Is it possible to set a filter (like in_array) to check if functions other than declared are used?
Or is there an Template engine or something else which does that.
I am very new to templating and I couldn't find anything.
If they are only creating HTML templates, you could allow them to put for example;
<div>
[PHP]getDescriptions()[PHP]
</div>
<div>
[PHP]phpinfo()[/PHP]
</div>
Then in your parsing file when they save or whatever, you could have
$allowedFunctions = array('getDescriptions');
$input = '';//html from the template
foreach($allowedFunctions as $key => $value){
$myVal = $value();
$input = str_replace('[PHP]'.$value.'()[/PHP]',$myVal,$input);
}
This would replace [PHP]getDescriptions()[/PHP] with whatever is returned from getDescriptions()...
and phpinfo() wouldnt change.
you can check if a function exists with function_exists. If you want them to use the functions you defined for that purpose only you could prefix those function with something like 'tpl_*". like this:
function tpl_getDescription() {/*code here*/}
and then when you user tries to implement a function like getDescription you add "tpl_" to it and check if that function exists with function_exists().
if(function_exists('tpl_' . $userFuncName))
{
call_user_func('tpl_' . $userFuncName)
}
that way even if the user tries to evoke a native php function tpl_ will be prefixed and if will return false.
Yes, you could easily make a script that enumerates all user functions in an external file. Lets say you have this "template", template.php :
<?
function getDescription() {
}
function userFunc() {
}
function anotherFunction() {
}
?>
then you could get a list of all functions in template.php this way :
<?
include('template.php');
$functions = get_defined_functions();
echo '<pre>';
print_r($functions['user']);
echo '</pre>';
?>
would output :
Array
(
[0] => getdescription
[1] => userfunc
[2] => anotherfunction
)
I would call this script through AJAX, like getfunctions.php?file=template.php which returned a JSON with all user functions inside template.php.
I want to develop a plugin to add the ability to do something with shortcode. I want it to function like this:
[shortcode]Content[/shortcode]
Here is the code I use:
function quote( $atts, $content = null ) {
return '<div class="right text">"'.$content.'"</div>';
}
add_shortcode("quote", "quote");
The $content variable, which returns the value of the shortcode, in this case Content, cannot be used outside of the function. I want to use it on some other part of the PHP code, but I can't get it to work. I am not experienced with PHP, so if you have any solution, please try to be as clear as possible.
Thanks.
You'd have to declare it as a global variable otherwise it's scope (where you can access it) is limited to the function you're using it in.
function quote( $atts, $content = null ) {
global $content;
return '<div class="right text">"'.$content.'"</div>';
}
add_shortcode("quote", "quote");
echo "Using content somewhere else $content";
FYI though, it can lead to potential problems. $content, for example, is a pretty common variable and could conflict if the same variable is being used elsewhere. You'd be better off giving it a unique name like: global $my_global_content = $content. Then use $my_global_$content in the other areas of your code.
I have an issue with rendering wikitext in hook for tag processing.
public static function onTagRender( $input, array $args, $parser, $frame ) {
...
$text = $parser->recursiveTagParse($sometext, $frame);
...
return $text;
}
If $sometext contains e.g.
"Example from page [[XYZ]]"
then I expect returned $text should contain
"Example from page XYZ"
But I get only
"Example from page <!--LINK 0:0-->"
I have tried also $parser->replaceInternalLinks(), but with same result. What have I overlooked?
If some people run into the same problem, try calling replaceLinkHolders after recursiveTagParse. (I didn't have the same problem so I didn't test it.)
So in OP's code snippet, that would be:
public static function onTagRender( $input, array $args, $parser, $frame ) {
...
$text = $parser->recursiveTagParse($sometext, $frame);
$text = $parser->replaceLinkHolders($text);
...
return $text;
}
Explanation according to my understanding:
Actually, the usual parse method calls the internalParse method -- which does most of the job -- and then do some other stuff. On the other hand, recursiveTagParse is almost only calling internalParse, so it doesn't execute the other stuff from parse.
Problem is, links are parsed in two steps:
Links are first extracted into LinkHolderArray and they are replaced with <!--LINK $ns:$key--> in the text.
(This is done by replaceInternalLinks, called by internalParse, so that's fine.)
Then <!--LINK $ns:$key--> markers are parsed into HTML links.
(This is done by replaceLinkHolders which is called by parse, not by internalParse, and thus not by recursiveTagParse.)
Parser::recursiveTagParse only do partial rendering, afaik. That may or may not be the problem. To fully render any user input, you will have to create a parser function (http://www.mediawiki.org/wiki/Manual:Parser_functions) instead of a tag function.
See http://www.mediawiki.org/wiki/Manual:Tag_extensions#How_do_I_render_wikitext_in_my_extension.3F
I have a very strange problem in my wordpress development,
in fucntions.php I have the following code
//mytheme/functions.php
$arg = "HELP ME";
add_action('admin_menu', 'my_function', 10, 1);
do_action('admin_menu',$arg );
function my_function($arg)
{
echo "the var is:".$arg."<br>";
}
the output is
the var is:HELP ME
the var is:
Why the function repeated 2 times? Why has the argument "help me" been passed correctly and the 2nd time it havent been passed?
I have been trying all my best for 2 days and searched in many places to find a solution but I had no luck.
What I am trying to do is simple! I just want to pass argument to a function using add_action?
Inside "my_function" (albeit it's yours :)), write line:
print_r(debug_backtrace());
http://php.net/manual/en/function.debug-backtrace.php
It will help you to know, what are going on.
Or, you can use XDebug (on development server).
Well, first off, in your my_function() function, you're not defining $arg. You're trying to echo something out that isn't there - so when it's returned, it's empty. So you need to define it. (edited to add: you're trying to define it outside the function - but to make the function inside recognize it, you have to globalize the argument.)
function my_function($arg) {
if(!$arg) $arg = 'some value';
echo "the var is:".$arg."<br>";
}
when you add_action, you need to define the $arg value:
add_action('admin_menu', 'my_function', 10, 'my value');
Did you tried to put your function before add_action ?
Use an anonymous function like this:
function my_function($arg) {
echo "the var is: $arg<br>";
}
$arg = "HELP ME";
add_action('admin_menu', function() { global $arg; my_function($arg); }, 10);
See this answer for details.