.
/**
* Check that, as in the coding guidelines, every to-do comment links to a tracker issue.
*
* As required by http://docs.moodle.org/dev/Coding_style.
*
* @package core
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require(__DIR__ . '/../../../config.php');
require_login();
$context = context_system::instance();
require_capability('moodle/site:config', $context);
$PAGE->set_url('/lib/tests/other/todochecker.php');
$PAGE->set_context($context);
$PAGE->set_title('To-do checker');
$PAGE->set_heading('To-do checker');
$thirdparty = load_third_party_lib_list();
$extensionstotest = array('php');
$extensionsregex = '/\.(?:' . implode('|', $extensionstotest) . ')$/';
$patterntofind = 'TO' . 'DO'; // Make it not match the regex.
$found = array();
echo $OUTPUT->header();
echo $OUTPUT->heading('To-do checker', 2);
echo $OUTPUT->box_start();
echo 'Checking code ...';
flush();
recurseFolders($CFG->dirroot, 'check_to_dos', $extensionsregex, false, array_keys($thirdparty));
echo ' done.';
echo $OUTPUT->box_end();
if (empty($found)) {
echo '
No to-dos found.
';
} else {
$total = 0;
foreach ($found as $filepath => $matches) {
$total += count($matches);
}
echo '' . $total . ' to-dos found:
';
foreach ($found as $filepath => $matches) {
echo '- ' . $filepath . ' (' . count($matches) . ')
';
foreach ($matches as $lineno => $line) {
$url = 'http://cvs.moodle.org/moodle/' . $filepath . '?view=annotate#l' . $lineno;
$error = '';
// Make sure there is a tracker issue id mentioned
$matches = array();
if (preg_match('/\bTODO\b.*?\b(MDL-\d+)/', $line, $matches)) {
$issueid = $matches[1];
$issueurl = 'http://tracker.moodle.org/browse/' . $issueid;
// Make sure the issue is still open.
list($issueopen, $issuesummary) = issue_info($issueid);
if ($issueopen) {
$issuename = $issueid;
} else {
$issuename = '' . $issueid . '';
$error = 'The associated tracker issue is Resolved.';
}
$line = str_replace($issueid, '' . $issuename . '', htmlspecialchars($line));
} else {
$line = htmlspecialchars($line);
$error = 'No associated tracker issue.';
}
if ($error) {
$error = '' . $error . '';
}
echo '- ' . $lineno . ': ' . $line . $error . '
';
}
echo '
';
}
echo '
';
}
echo $OUTPUT->footer();
function check_to_dos($filepath) {
global $CFG, $found, $thirdparty;
if (isset($thirdparty[$filepath])) {
return; // Skip this file.
}
$lines = file($filepath);
$matchesinfile = array();
foreach ($lines as $lineno => $line) {
if (preg_match('/(?|\$)\bTODO\b/i', $line)) {
$matchesinfile[$lineno] = $line;
}
}
if (!empty($matchesinfile)) {
$shortpath = str_replace($CFG->dirroot . '/', '', $filepath);
$found[$shortpath] = $matchesinfile;
}
}
function issue_info($issueid) {
static $cache = array();
if (array_key_exists($issueid, $cache)) {
return $cache[$issueid];
}
$xmlurl = 'http://tracker.moodle.org/si/jira.issueviews:issue-xml/' . $issueid . '/' . $issueid . '.xml';
$content = download_file_content($xmlurl);
// Get the status.
$open = preg_match('/Unresolved<\/resolution>/', $content);
// Get the summary.
$matches = array();
preg_match('/\[' . $issueid . '\]\s+(.*?)<\/title>/', $content, $matches);
$summary = $matches[1];
preg_match('/]*>(.*?)<\/assignee>/', $content, $matches);
$summary .= ' - Assignee: ' . $matches[1];
$cache[$issueid] = array($open, $summary);
return $cache[$issueid];
}
function load_third_party_lib_list() {
global $CFG;
$libs = array();
$xml = simplexml_load_file($CFG->libdir . '/thirdpartylibs.xml');
foreach ($xml->library as $libobject) {
$libs[$CFG->libdir . '/' . $libobject->location] = 1;
}
return $libs;
}
function recurseFolders($path, $callback, $fileregexp = '/.*/', $exclude = false, $ignorefolders = array()) {
$files = scandir($path);
foreach ($files as $file) {
$filepath = $path .'/'. $file;
if (strpos($file, '.') === 0) {
/// Don't check hidden files.
continue;
} else if (is_dir($filepath)) {
if (!in_array($filepath, $ignorefolders)) {
recurseFolders($filepath, $callback, $fileregexp, $exclude, $ignorefolders);
}
} else if ($exclude xor preg_match($fileregexp, $filepath)) {
call_user_func($callback, $filepath);
}
}
}