Note: You are currently viewing documentation for Moodle 3.8. Up-to-date documentation for the latest stable version of Moodle may be available here: How to rebuild context paths.

How to rebuild context paths: Difference between revisions

From MoodleDocs
No edit summary
 
(14 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Performance}}
Sometimes, if something has gone wrong in your database, you will get an error like:
Sometimes, if something has gone wrong in your database, you will get an error like:


:[Thu Jan 15 16:20:59 2009] [error] [client 144.32.148.21] PHP Notice:  Context id 113 does not have valid path, please use build_context_path()
PHP Notice:  Context id 113 does not have valid path, please use context_helper::build_all_paths()
:* line 375 of lib/accesslib.php: call to debugging()
:* line 125 of mod/forum/index.php: call to has_capability()
:* in /lib/weblib.php on line 6923, referer: mod/forum/index.php?id=2


This page explains how to follow the instruction to "please use build_context_path". You will need to create a simple PHP script to do this, then run it.
or like:


==Before you start==
Can't find data record in database table context.
Debug info: SELECT * FROM {context} WHERE id = ?[array (0 => '1030367',)]
Error code: invalidrecord
 
This page explains how to follow the instruction to "please use context_helper::build_all_paths()".
 
== Before you start==


This error should never happen, so the fact that it has may be a symptom of something more serious being wrong. If you are paranoid, you may wish to [[How to check your database for corruption|check your database for corruption]] before running the repair script below.
This error should never happen, so the fact that it has may be a symptom of something more serious being wrong. If you are paranoid, you may wish to [[How to check your database for corruption|check your database for corruption]] before running the repair script below.


On the other hand, sometimes these things happens, so you could just run the script below then forget about it.
On the other hand, sometimes these things happen when a course/category delete fails, or is done incorrectly, so you could just run the task as described below to fix the error:
 
== Step A: Verify the \core\task\context_cleanup_task scheduled task is running, and run it manually, if needed  ==
 
Go to Site administration -> Server -> Scheduled tasks and look for the Cleanup contexts (\core\task\context_cleanup_task) task.  If it has not run recently, run it via the "Run Now" link, or via the CLI with:
 
php admin/tool/task/cli/schedule_task.php --execute='\core\task\context_cleanup_task' --showdebugging
 
Once that is done, check if the error has gone away.  If it has, you are done!  If it has not, please move to Step B
 
== Step B: Edit lib/classes/task/context_cleanup_task.php to fix corrupt context paths ==
 
In your favorite editor, open lib/classes/task/context_cleanup_task.php and edit the line that reads


==Step 1: create the script==
\context_helper::build_all_paths(false);


On your server, create a file in the '''admin''' folder called '''rebuildcontextpaths.php''' with contents:
and change it to


  <?php
  \context_helper::build_all_paths(true);  
require_once(dirname(__FILE__) . '/../config.php');
require_login();
require_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM));
print_header();
echo '&lt;p>Rebuilding context paths ...&lt;/p>';
build_context_path(true);
echo '&lt;p>Done&lt;/p>';
print_footer('empty');
?>


(You can either create this file on your own computer using a text editor like Notepad, then upload it to Moodle's admin folder. Or you can edit the file directly on the server. It does not matter how you do it, just that you end up with the file there, with those contents.)
to force the task to rebuild the context paths. Run the \core\task\context_cleanup_task scheduled task again, as you did in Step A.  Your errors should now be gone.


==Step 2: run the script==
You can then change


Go to the URL .../admin/rebuildcontextpaths.php in your web browser (for example, if your Moodle site is at <nowiki>http://example.com/moodle/, then go to the URL http://example.com/moodle/admin/rebuildcontextpaths.php</nowiki>). You need to log in as an administrator. The script will run, and immediately print 'Rebuilding context paths ...'. Some time later (depending on how big your Moodle site is) it will print 'Done'.
\context_helper::build_all_paths(true);


==See also==
back to


* [[Administrator documentation]]
\context_helper::build_all_paths(false);


[[Category: Administrator]]
so the \core\task\context_cleanup_task scheduled task works as intended

Latest revision as of 17:06, 17 October 2019


Sometimes, if something has gone wrong in your database, you will get an error like:

PHP Notice:  Context id 113 does not have valid path, please use context_helper::build_all_paths()

or like:

Can't find data record in database table context.
Debug info: SELECT * FROM {context} WHERE id = ?[array (0 => '1030367',)]
Error code: invalidrecord

This page explains how to follow the instruction to "please use context_helper::build_all_paths()".

Before you start

This error should never happen, so the fact that it has may be a symptom of something more serious being wrong. If you are paranoid, you may wish to check your database for corruption before running the repair script below.

On the other hand, sometimes these things happen when a course/category delete fails, or is done incorrectly, so you could just run the task as described below to fix the error:

Step A: Verify the \core\task\context_cleanup_task scheduled task is running, and run it manually, if needed

Go to Site administration -> Server -> Scheduled tasks and look for the Cleanup contexts (\core\task\context_cleanup_task) task. If it has not run recently, run it via the "Run Now" link, or via the CLI with:

php admin/tool/task/cli/schedule_task.php --execute='\core\task\context_cleanup_task' --showdebugging

Once that is done, check if the error has gone away. If it has, you are done! If it has not, please move to Step B

Step B: Edit lib/classes/task/context_cleanup_task.php to fix corrupt context paths

In your favorite editor, open lib/classes/task/context_cleanup_task.php and edit the line that reads

\context_helper::build_all_paths(false); 

and change it to

\context_helper::build_all_paths(true); 

to force the task to rebuild the context paths. Run the \core\task\context_cleanup_task scheduled task again, as you did in Step A. Your errors should now be gone.

You can then change

\context_helper::build_all_paths(true); 

back to

\context_helper::build_all_paths(false); 

so the \core\task\context_cleanup_task scheduled task works as intended