Note: You are currently viewing documentation for Moodle 3.7. 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
(→‎Step A: Create the script: remove old 1.9 code references.)
No edit summary
 
Line 1: Line 1:
{{Performance}}{{Update}}
{{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 context_helper::build_all_paths()
  PHP Notice:  Context id 113 does not have valid path, please use build_context_path()
  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:
 
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==
== Before you start==
Line 15: Line 17:
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 ==


== Step A: Create the script ==
In your favorite editor, open lib/classes/task/context_cleanup_task.php and edit the line that reads


On your server, create a file in the '''/admin''' folder called '''rebuildcontextpaths.php''' with the following content:
\context_helper::build_all_paths(false);
<code php>


<?php
and change it to
require_once(dirname(__FILE__) . '/../config.php');


require_login();
\context_helper::build_all_paths(true);  
require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM));


$strRebuildContextPath = "Rebuild Context Path";
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. 


$PAGE->set_url('/admin/rebuildcontexpath.php');
You can then change
$PAGE->set_title($strRebuildContextPath);
$PAGE->set_heading($strRebuildContextPath);
$PAGE->navbar->add($strRebuildContextPath);


echo $OUTPUT->header();
\context_helper::build_all_paths(true);  
echo '<p>Rebuilding context paths ...</p>';
build_context_path(true);
echo '<p>Done</p>';
echo $OUTPUT->footer();
?>
</code>


(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.)
back to


== Step B: Run the script ==
\context_helper::build_all_paths(false);


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'.
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