I've found this script for converting wiki syntax to HTML in php and i've tried to integrate it into Codeigniter. it seems really easy to use. However, it's not working, and instead producing around 8 of these errors:
Message: Use of undefined constant LS_NONE - assumed 'LS_NONE'
I think this is because Codeigniter helpers are not a class but rather functions, and this bit of code is a class, or does this issue lie with something else? I've also tried to use it as a model without success.
It also seems horribly outdated (2007). Could somebody suggest a really simple alternative or maybe give an idea of how to convert this to a simple function if that is possible? It's a very short piece of code. I'm not sure how these constants work in relationship to a function versus a class.
I've given the Text_Wiki from Pear ago, but the use and complexity well exceeds both my requirements and knowledge :)
//Any help would be greatly appreciated
Loaded using:
$row = $query->row();
$content=$row->course_content;
$this->load->helper('wiki');
$content=explode("\n", $content);
$output = WikiTextToHTML::convertWikiTextToHTML($content);
$html=array_merge($output);
$data['contents'][]= $html;
$this->load->view('default/a',$data);
It looks like the script is actually a class. put it in the libraries folder and load it with $this->load->library(). That will allow it to properly initialize and define the constants that it uses.
something like:
$this->load->library('wikitexttohtml');
$this->wikitexttohtml->convertWikiTextToHTML($wiki_text);
Related
I work in a php project with multiple independent developers and recently we had case where a function getmicrotime() was twice defined.
all worked fine, because they were defined in different files that were not both included in a single call ... until some refactory.
in the standardcase php would just output a fatal error, but here the output was blocked. (because a thirdparty website called a website ...) so we did not get the output, just the information that nothing worked anymore.
To the point:
Is there any method, external script, etc to check if functions with the same name are defined twice in the project?
i thought about reg. expr search, but ofcourse class methods can have the same name like a::meth1 and b:meth1 .... so its not that easy.
i am talking about a project with ~100.000 lines of ugly code ... so manual checking is not possible
Thanks in advance.
Consider static code analysis. I would suggest Sonar + PHP plugin: http://docs.codehaus.org/display/SONAR/PHP+Plugin
Here is the life example how it works:
http://nemo.sonarqube.org/dashboard/index/net.php.pear.phpcodesniffer
You can always write a simple script (i.e. perl or python) which will find all duplicates. The algorithm would be simple...
I have been recently playing around with HHVM. Went through a lot of trouble getting it to work on my computer. I know that not all PHP functions are available. As a test, I am writing a new website using it instead of using my current code. I ran into a problem when trying to use
filter_var($var,FILTER_SANITIZE_URL);
From the error.log file, it turns out that this function is undefined. Is the filter_var function not available for use in HHVM or am I just doing something wrong here. I like to keep things DRY, this would mean I have to do a lot more validation than I expected.
filter_var is now implemented in hhvm. Open github issues if you have any problems with it.
This function appears to not have been implemented on HHVM See http://comments.gmane.org/gmane.science.linguistics.wikipedia.technical/70038
An option if you want to rely on this functionality with the hopes that it will enter the fold is to polyfill it in (partial implementation to inspire the motivated).
if (!function_exists("filter_var")){
// define the constants used by the function
define("FILTER_VALIDATE_EMAIL", "email");
function filter_var(){
$args = func_get_args();
// $args[1] is the filter type (second parameter)
switch ($args[1]){
case FILTER_VALIDATE_EMAIL:
if (preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $args[0])?$args[0]:false;
break;
}
}
}
In order to localize strings used within my javascript, I want scan all my js files for such strings.
I am using a t() function to request string translations as follows:
t("Hello world");
or with dynamic portions:
t("Hello #user", {"#user": "d_inevitable"});
I want to detect all calls to the t() function and thus gather the strings contained in the first argument in a php "build" script, but skipping the following:
function foo(t) {
t("This is not the real t, do not localize this!");
}
function bar() {
var t = function(){}; //not the real t either...
}
function zoo() {
function t() {
//This also isn't the real t() function.
}
}
t("Translate this string, because this is the real t() in its global scope");
So the simple rule here is that the t function being invokes must be in global scope in order for the first argument to qualify as a translation string.
As a rule, dynamic runtime data as first argument is not allowed. The first argument to t() must always be a "constant" literal string.
I think php codesniffer will help me do it, however all the documentation I could find on it is about enforcing code standard (or detecting violations of it). I need lower level access to its js lexer.
My question is:
Would the php codesniffer's js lexer be able to help me solve my problem?
If so how do I access that lexer?
Are there any other php libs that could help me find the calls to t()?
Please do not suggest stand-alone regular expressions as they cannot possibly solve my problem in full.
Thank you in advance.
What you are describing is basically a coding standard. Certainly, ensuring strings are localised correctly is part of many project standards. So I think PHPCS is the right tool for you, but you will need to write a custom sniff for it because nothing exists to do exactly what you are after.
The best thing to do is probably clone the PHPCS Git repo from Github and then create a new directory under CodeSniffer/Standards to contain your custom sniff. Let's say you call it MyStandard. Make sure you create a Sniffs directory under it and then a subdirectory to house your new sniff. Take a look at the other standards in there to see how they work. You'll also find it easier to copy an existing ruleset.xml file from another standard and just change the cotent to suit you. if you don't want to include any other sniffs from anywhere (you just want to run this one check over your code) then you can just specify a name and description and leave the rest blank.
There is a basic tutorial that covers that.
Inside your sniff, you'll obviously want it to check JS files only, so make sure you specify that in the supportedTokenizers member var (also in the docs). This will ensure PHP and CSS files are always ignored.
When you get down to the actual checking, you'll have full low-level access to the parsed and tokenised content of your file. There are a lot of helper functions to check things like if the code inside other scopes, or to help you move backwards and forwards through the stack looking for bits of code you need.
TIP: run PHPCS using the -v option to see the token output on your file. It should help you see the structure more easily.
If you want to really do things properly, you can even create a nice unit test for your sniff to make sure it keeps running over time.
After all this, you'd check your code like this:
phpcs --standard=MyStandard /path/to/code
And you can use a lot of integrations that exist for PHPCS inside code editors.
You might decide to add a new more sniffs to the standard to check other things, which you can then do easily using your ruleset.xml file or by writing more custom sniff classes.
I hope that helps a bit. If you do decide to write your own sniff and need help, just let me know.
In my codeigniter i created a library in library folder.I want to load view pages in that library.How can i do this?
This is my code:
$this->load->view('view_page');
But when iam using this code i get an error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_theme_lib::$load
Filename: libraries/theme_lib.php
Line Number: 9
What is the problem in mycode?
In Line number 9 in library the code is :
$this->load->view('view_page');
To do what you're trying to do you need to get an instance of CI, and use that.
e.g.
$CI =& get_instance();
Within your library functions you could then use this variable to load the view:
$CI->load->view('view_page');
I would question though why you want to call a view, in the form that you have done, within a library. I suspect that you would be better to get the view call to return data (setting the 3rd parameter 'true') and then have your library return the display data to the controller.... Your approach seems messy, but then I have no idea what your library is trying to do.....
I have came across your question for different reason, I seem to have problem passing variables to views instead. Let me explain before I tell you answer to your problem.
Imagine you have an Emailer library to send emails rather than sorting that out in controller.
Emailer than within itself builds email string using views. My problem is that when I make my call from controller something like Emailer::send_mail($data,$template) it passes the variables correctly but when I do it from another library the view fails to register the variables. LOL
So yes Stéphane Bourzeix you do sometimes want to use output from view in a different way than just returning to client browser.
The solution is here.
https://www.codeigniter.com/userguide2/general/views.html
the last section of that page has something like
$string = $this->load->view('myfile', '', true);
but something like
$string = $this->load->view('myfile', $view_data, true);
should work too
in case of doing this from other places than controllers you will need to:
$this->ci = & get_instance();
$string = $this->ci->load->view("myfile",$view_data,true);
it seems like the last argument in the list (true) is the one that tells it not to render to browser but instead just create string with template content
I know it's a bit too late but hope it still helps to some. Good luck with your code.
tomhre
You simply DON'T load pages (aka Views) in a Library.
I don't see any need for doing this.
I'd like to use Smarty in conjuction with the Zend Framework, especially some of it's View Helpers.
Now i got to the point, where i implemented a Zend_View that uses Smarty to display templates. I can assign values as usual. So far so good.
Now I would really like to use Zend View Helpers in Smarty. I asssigned the Zend_View object as "this" and tried this in the template:
{$this->layout()->setLayout('default')}
As this will print the return value of the setLayout() method (which is a Zend_Layout), there is an error:
Catchable fatal error: Object of class Zend_Layout could not be converted to string in /path/to/templates_c/089c3d67082722c7cabc028fa92a077f8d8b4af5.file.default.tpl.cache.php on line 27
This is clear to me, so I went into Smarty's core to fix this:
The generated code did look like this:
<?php
echo $_smarty_tpl->tpl_vars['this']
->value->layout()
->setLayout('default');
?>
And now it reads:
<?php
$hack = $_smarty_tpl->tpl_vars['this']
->value
->layout()
->setLayout('default');
if( is_string($hack) ||
( is_object($hack) && method_exists($hack, '__toString') ) )
echo $hack;
?>
Now this is probably the worst fix i can think of, for several reasons (Smarty compatibility loss, performance). Sadly, it's the only one. Is there a way to stop Smarty from trying to print the output of the expression? Also, i want the syntax to stay as intuitive as possible, and i don't want to write Smarty functions for all the Helpers, because I want to use this code with a third-party application (Pimcore) that might add new helpers.
Thanks in advance for any suggestions!
Some suggestions (only ideas, nothing so good):
Create a __toString() in Zend_Layout who returns null or empty string (big/ugly/worse workaround).
Create a variable modifier/filter who return null or empty string, so your call will be something like {$this->layout()->setLayout('default')|noreturn} (you can use it with other things too and noreturn can be called with an friendly name like definition or define to indicate the purpose of the instruction, but a workaround too)
Using the assign to build a expression who set all this thing to another var (workaround too).
Maybe this can give you some good ideas =)