The Golden Rules
First things to set in your mind...
- The <search> tag can only be a single line, though doesn't have to be a whole line, as partial matches work too. You should learn how to use the offset attribute to properly encompass all needed lines, but that doesn't mean you should encompass many lines. Instead of trying to include many lines from top down, try starting at the bottom and finding lines going up.
- Less is more! Whenever possible, try to match the smallest, yet still unique part of the code. That will improve the chances of being more future-resistant and avoid breaking other mods that may have changes close to yours.
- Pay attention to all the attribute options for each tag. There is a lot of power that is overlooked by people skimming over the syntax
How to make vQmod Scripts
UKSB from the OpenCart forums has created a great vQmod script generator here. This greatly simplifies an already simple creation process.
vQmod currently uses an xml formatted parser by default but other parsers can be created in the future as well.
A simple replace script example looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<modification>
<id>Replace 123 with ABC</id>
<version>1.0.0</version>
<vqmver>1.0.9</vqmver>
<author>qphoria</author>
<file name="relative/path/myfile.php">
<operation>
<search position="replace"><![CDATA[
$var = '123';
]]></search>
<add><![CDATA[
$var = 'ABC';
]]></add>
</operation>
</file>
</modification>
This script simply changes the value of a variable of "myfile.php" from "123" to "ABC" before it gets included.
Syntax
vQmod supports a number of parameters:
modification
- This is the highest level of the file and there can only be one
modification / id
- This is the name and description of the mod.
- Format: Free form text. (Informational)
modification / version
- This is the version of the mod.
- Format: Number and Decimal (1.0.0) (Informational)
modification / vqmver
- This is the minimum required version of VirtualQMod needed for the script to work.
- Format: Number and Decimal (1.0.0) (Informational)
modification / author
- This is the author of the mod.
- Format: Free form text (Informational)
modification / file
- This is the name of the file to modify.
- Requires attribute "name" as relative filename to the location of the main index.php file (e.g. catalog/controller/product/product.php). Name attribute supports a wildcard (*) character for dynamic path building. Each wildcard is limited to a single directory level.
- catalog/view/theme/*/template/product/product.tpl
- catalog/view/theme/*/*/product/product.tpl
- etcThere can be multiple file tags in a single xml file. Each file can have its own set of operations Optional attribute "error" set to log|skip|abort - skip means it will simply ignore this file.
- log is the same as skip, but logs the error. (default)
- abort means to log the error and cancel the remaining operations in that particular xml script. It does not revert changes to other files already made in that script and doesn't stop other xml files.
- Wildcard paths will ignore the "error" attribute completely
modification / file / operation
- This is the wrapper of the actual operation occurring.
- There can be multiple operations to the same file tag.
- Optional attribute "error" set to skip|log|abort
- skip means all other operations will be applied even if one cannot. There will be no error in the log.
- log is the same as skip, but logs the error.
- abort means to log the error and revert to the original source. (default)
modification / file / operation / search
- This is the first required step of the operation.
- Can only search single lines, both fully and partially. But offset and index attributes will assist.
- Automatically trims whitespace and linebreaks
- One per operation tag
- Recommended to use CDATA tags to wrap code.
- Required attribute "position" set to before|after|replace|top|bottom|all.
- replace will replace the data in the search tag with the data in the add tag. (default)
- before will insert the add data before the search data
- after will insert the add data after the search data
- top will insert the add data at the top of the file. The search data is ignored.
- bottom will insert the add data at the bottom of the file. The search data is ignored.
- all will completely replace all the code in the file with the add data. The search data is ignored.
- Optional attribute "offset" to work with the position
- if the search position is before and offset 3 it will put the add data before the line, 3 lines above the searched line
- if the search position is after and offset 3 it will put the add data after the line, 3 lines below the searched line
- if the search position is replace and offset 3 it will remove the code from the search line and the next 3 lines and replace it with the add data
- if the search position is top and offset 3 it will put the code before the line, 3 lines below the top of the file
- if the search position is bottom and offset 3 it will put the code after the line, 3 lines above the bottom of the file
- Optional attribute "index" for specifying which instances of a search tag should be acted on
- If the search string is "echo" and there are 5 echos in the file, but only want to replace the 1st and 3rd, use index="1,3"
- Comma delimited for multiple instances starting with "1"
- Leave out or set to FALSE to replace all instances. (default)
- Optional attribute "regex" for specifying whether or not to search a regex pattern.
- If true, the search data should be a valid regex pattern
- Leave out or set to FALSE to use normal string search (default)
- Optional attribute "trim" set to true|false
- true will trim away whitespace and linebreaks.
- leave out or set to true to trim. (default is true)
modification / file / operation / add
- This is the second required step of the operation.
- Can be multiple lines
- One per operation.
- Location of added data depends on the position attribute of the search command.
- Use CDATA tags to wrap code.
- Optional attribute "trim" set to true|false
- true will trim away whitespace and linebreaks.
- leave out or set to false to not trim. (default is false)
<![CDATA[ ]]>
- These are called CDATA tags and they are used by xml to specify that the data between should not be evaluated. It's recommended you always use these for data between the search and add operation tags
What happens if the search/replace text contains a <![CDATA[ ]]> tag? Is it possible to escape the text?
This should not be done really, but it can be done sure http://stackoverflow.com/questions/223652/is-there-a-way-to-escape-a-cdata-end-token-in-xml
how can I replace text contains with each language?
ex: english language replace to "contact" Vietnamese language replace to "lien he"
how can do it ?
Indeed. Can we use wildcards like language/*/file.php and have language files ignored? Or maybe this is already made easier??
Anyone? Thanks.
@Avano... you forgot the last directory in the file path (wildcard is limited for a single directory level)
it should be catalog/language//folder/file.php, where folder is account, module, etc.
and then what? where do I put this xml file?
I think that what is missing here is how to apply the changes??? I have my file copied in the xml folder. then what?
Once your file is in the xml folder, it will apply it next time the file is requested for anything
Qphoria, why did you modified 2.1.6 to ignore "error" when using wildcards?
What will be the action for files with wildcard path now?
I didn't "modify" it to do that. It has always ignored the file exist for wildcards. I simply made it optional for non-wildcard paths. Wildcard paths can't be validated because they are dynamic.
I have some lines of the same code for several extensions... Can I somehow check if the lines were added already, and skip an entire section of the vQMod script?
You can't check for it, however you can remove the original code and replace it with something custom that means the second will not match it
When i use position = 'all', it generates error : Fatal error: Class 'Controllerproductproduct' not found in vqmod\vqcache\vq2-system_engine_front.php on line 46
Here's the code
<file name="catalog/controller/product/product.php">
position="all" replaces all code with the code you give it. Clearly you're not setting any code, or don't have the class ControllerProductProduct? defined. Note that position="all" is a position you should rarely if at all use. There are usually far better ways than replacing a whole file
Hi DJ, yes i have 2 position that need to be replaced, what is your best suggestion? I only pasted the first three lines since that's where it generate error. Class controller product should be there since it comes with OC for product controller => catalog/controller/product/product.php so it's impossible it doesn't have the class. If i change the search position to "before" or "after" it works good.
Thanks.
I'm unsure why you are using position="all" ???
If its not meant to be there, remove it
My mistake, i thought position all means replace any offset found in the file. Btw if there are fews offset on search, how to set vqmod to replace only the first offset found?
use index="1"