Jump to content
bora

Processwire 2 to 3 namespace migration script

Recommended Posts

Hello everyone,

Last night as a "saturday night shitty weather stay at home" project I attempted to migrate a project that was on PW 2.7.x to PW 3.x 

I use a lot of partials on the project so I have around 200 php files that needs to be namespaced. Compiler was giving me trouble with "Call undefined function" errors.

Being the lazy developer I attempted to wrote a script after getting bored over pasting  the namespace Processwire; line into around 10 files.

Below you can find the script, that is very basically adds <?php namespace Processwire;?> as the first line of every .php and .module file in the given directory and shows you the results. Of course it checks for namespace Processwire first :)

By default it assumes ./site/templates folder but I tried it with a module which is giving errors due to namespaces and worked fine.

Be careful and remember to take backups first :D

https://gist.github.com/borantula/e41c4b6ba36f78b1110d400a16754691

  • Like 2

Share this post


Link to post
Share on other sites

Yes, I use wireRenderFile method extensively. Now I tried again by changing all calls to wireRenderFile function to absolute from the wrapper function I use. I only put namespace on root folder of templates. But even though I use full paths on wireRenderFile, the .php files in templates/partials still throws errors like 

Fatal error: Call to undefined function getCatalogues() in /var/www/dev.com/pw3/site/templates/partials/main.php on line 9

(it's defined in _func.php)

Share this post


Link to post
Share on other sites

I would probably solve this using a search & replace on files.

  • Like 1

Share this post


Link to post
Share on other sites
Yes, I use wireRenderFile method extensively. Now I tried again by changing all calls to wireRenderFile function to absolute from the wrapper function I use. I only put namespace on root folder of templates. But even though I use full paths on wireRenderFile, the .php files in templates/partials still throws errors like 
Fatal error: Call to undefined function getCatalogues() in /var/www/dev.com/pw3/site/templates/partials/main.php on line 9
(it's defined in _func.php)
I actually don't think this is a matter of file paths. That error points to one file (like _func.php) having a namespace, and another file (like main.php) not having a namespace (or not referring to the function in the namespace that it exists). If you are letting PW decide whether to compile a file based on whether it has a namespace or not, then you've got to take those same considerations for any files it might include(). A file that doesn't get compiled isn't going to have files include()'d from it compiled either. So it sounds like you probably do have a namespace defined in your _func.php file, but not in your main.php file (or the opposite). For your case, since you are adding a namespace to the to of all your .php files, I'd suggest disabling the compiler by setting $config->templateCompile=false; in your /site/config.php. 
  • Like 3

Share this post


Link to post
Share on other sites
I actually don't think this is a matter of file paths. That error points to one file (like _func.php) having a namespace, and another file (like main.php) not having a namespace (or not referring to the function in the namespace that it exists). If you are letting PW decide whether to compile a file based on whether it has a namespace or not, then you've got to take those same considerations for any files it might include(). A file that doesn't get compiled isn't going to have files include()'d from it compiled either. So it sounds like you probably do have a namespace defined in your _func.php file, but not in your main.php file (or the opposite). For your case, since you are adding a namespace to the to of all your .php files, I'd suggest disabling the compiler by setting $config->templateCompile=false; in your /site/config.php. 

That was actually what I did, disable templateCompile from config.php after running this script. Added this recommendation to gist as well to help anyone willing to try it.

Adding namespace to all files and disabling templateCompile is, as far as I understand, is the cleanest state for PW3.

By the way FileCompiler worked for modules flawlessly, except two small exceptions (one was my fault one was 3rd party module). This week I'll test further before migrating to PW3 on production as well. So far on my local copy I enjoy the perks of PW3 already :)

On the other hand, adding namespace to every little view partial doesn't look like very ideal and practical. I will look into other possible ways of using partials instead of wireRenderFile. 

Error generally occured due to using global functions like wire() or functions from _func.php on non-namespaced files. Out of habit from Laravel, CakePHP etc. I assumed that global functions would continue working without a namespace but then I realized I should be thinking everything within Processwire.

Share this post


Link to post
Share on other sites
... Adding namespace to all files and disabling templateCompile is, as far as I understand, is the cleanest state for PW3...

Considering the new "File Compiler modules" feature of PW, it actually depends on your requirements:

https://processwire.com/blog/posts/processwire-3.0-alpha-2-and-2.6.22-rc1/#file-compiler-modules

However, most of us will rarely use this tool, I suppose...

Share this post


Link to post
Share on other sites
On 4/24/2016 at 11:34 PM, tpr said:

I would probably solve this using a search & replace on files.

That is what I did.

You can get every file in directory templates that ends in .php. Then look if the file starts on the first line with <?php. If <?php is not followed by namespace ProcessWire, it will be replaced.

The regex expression for the find operation

\A^<\?php(?!\ namespace\ ProcessWire;)

My Sublime Find&Replace panel looks like this

2017-01-18 11:13:49.png

You can adjust file paths and endings to catch other files in other directories (e.g. modules) as well.

Should work in other editors that support regex search/replace across files, too.

Share this post


Link to post
Share on other sites

@Mike Rockett Thanks for the pointer. Didn't think about it. I was looking at the default site profile template files and they all have it in the first line.

My post was meant as an example on how you can approach the problem with search/replace and regex. People can adjust the regex to fit their needs.

However, I put together a new regex, that searches for <?php in the first line of a document which is not followed by any number of spaces, tabs or newlines and 'namespace ProcessWire;'.

You can see it in action here.  remove the first line and then add any number of new lines between '<?php' and 'namespace ProcessWire' to see that it works. It only picks up <?php  in the first line of the document.

Here it is for convenience

(^<\?php(?![ \t\n]*namespace ProcessWire;)[ \t]*)/g

 

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By DooM
      Hello guys,
      I'm trying to figure out how to sync fields and templates between staging and production environments.
      I've found Migrations module by Lostkobrakai, but with use of it all the fields and templates must be created by API, which is kind of uncomfortable.
      I also tried ProcessDatabaseBackups module which can export only certain tables, but I don't think it's the best practice to do that.
      How do you guys solve this problem? It's very annoying to setup everything three times (dev, staging, production).
      Thanks a lot :)
    • By modifiedcontent
      I had upgraded my Apache configuration to include PHP7.2 and PHP7.3 for a Laravel-based script on the same server. Somehow it/I messed up a previously fine Processwire site, in a very confusing way.
      The site still looks fine, but editing template files has no effect whatsoever. It is stuck on some kind of cached version. I have already disabled PHP7's OPcache, cleared browser caches, etc, with no effect.
      The pages now apparently come from PW's assets/cache/FileCompiler folder, even though I never enabled template caching for this site.
      I have tried adding "namespace ProcessWire;" to the top of the homepage template file, but then I get this fatal error:
      My functions.php file pulls data in from another Processwire installation on the same VPS with the following line:
      $othersitedata = new ProcessWire('/home/myaccount/public_html/myothersite/site/', 'https://myothersite.com/'); That apparently still works fine; the site still displays data from the other installation, but via the "cached" template that I am now unable to change.
       
      I don't know where to start with this mess. Does any of this sound familiar to anyone? Any pointers in the right direction would be much appreciated. 
       
      Edit:
      Adding "$config->templateCompile = false;" to config.php results in the same fatal error as above. 
    • By dweeda
      How do I migrate a template with all its fields and values to a new site? Is their an export/import process?
    • By joeck
      Hi Guys,
      I'm trying to do my first migration to the customers existing server (IIS 10) . I ran the site as a subdirectory on my website for test purposes (everything works fine).
      Following the tutorial of Joss, I tryed the site on a local xampp server to make sure, it also works on a root directory. So far so good, everything works.
      Now I moved the files (from the xampp) to the customers server. The root/index page is shown but for every subpage i get 404 Errors...
      Hence I followed the troubleshooting guide for not working URLs:
      On the first sight, the .htaccess file is not recognized, therefore I contacted the host support. They said, it is recognized but not all modules are supported in the processwire .htaccess file. I did the "öalskjfdoal" test in the .htaccess file and didn't get a 500 Error.... BUT the rewrite rule from the hosts support, to proof the file is read, DID work... The support claims, they do not provide debugging... so basically the .htaccess file is recognized and working, but not throwing any errors (for whatever reason).
      Working rewrite rule (from support):
      RewriteEngine On RewriteBase / RewriteRule ^test\.asp$ index.html [NC,L] RewriteRule ^test\.html$ konzept.html [NC,L] RewriteRule ^test2\.html$ team.html [NC,L] The support said, a couple modules are not supported in the htaccess file, the supported ones are listed here: http://www.helicontech.com/ape/ (I think mod_rewrite is supported)
      As I do not completely understand what exactly is happening in the htaccess file, I'm stuck. I tried all suggestions I found regarding this topic on the forum, but none of them solved the problem.
       
      .htaccess.txt
    • By Maxplex
      Hey there, 
      i have a big Problem. I migrate Processwire with a Plugin from my local Mama Server to a 1&1 web server. After few Problems with Internal Error (.htaccess), the site looks greta and its work. But my Admin Panel are not working correctly. Before I installed the AdminThemeUIKit but now I can't install ist anymore and I can't refresh my modules. Nothing happens after a mouse click on it. If I am going to the pages, I can't see anything. There isn't a Site Tree or anything else. I can not change anything there. Please help me, it is a huge Problem for me... The admin looks like the very beginning of Processwire !!!


×
×
  • Create New...