|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00033 abstract class XMLDBCheckAction extends XMLDBAction { 00038 protected $introstr = ''; 00039 00043 function init() { 00044 parent::init(); 00045 00046 // Set own core attributes 00047 00048 // Set own custom attributes 00049 00050 // Get needed strings 00051 $this->loadStrings(array( 00052 $this->introstr => 'tool_xmldb', 00053 'ok' => '', 00054 'wrong' => 'tool_xmldb', 00055 'table' => 'tool_xmldb', 00056 'field' => 'tool_xmldb', 00057 'searchresults' => 'tool_xmldb', 00058 'completelogbelow' => 'tool_xmldb', 00059 'yes' => '', 00060 'no' => '', 00061 'error' => '', 00062 'back' => 'tool_xmldb' 00063 )); 00064 } 00065 00071 function invoke() { 00072 parent::invoke(); 00073 00074 $result = true; 00075 00076 // Set own core attributes 00077 $this->does_generate = ACTION_GENERATE_HTML; 00078 00079 // These are always here 00080 global $CFG, $XMLDB, $DB, $OUTPUT; 00081 00082 // And we nedd some ddl suff 00083 $dbman = $DB->get_manager(); 00084 00085 // Here we'll acummulate all the wrong fields found 00086 $problemsfound = array(); 00087 00088 // Do the job, setting $result as needed 00089 00090 // Get the confirmed to decide what to do 00091 $confirmed = optional_param('confirmed', false, PARAM_BOOL); 00092 00093 // If not confirmed, show confirmation box 00094 if (!$confirmed) { 00095 $o = '<table class="generalbox" border="0" cellpadding="5" cellspacing="0" id="notice">'; 00096 $o.= ' <tr><td class="generalboxcontent">'; 00097 $o.= ' <p class="centerpara">' . $this->str[$this->introstr] . '</p>'; 00098 $o.= ' <table class="boxaligncenter" cellpadding="20"><tr><td>'; 00099 $o.= ' <div class="singlebutton">'; 00100 $o.= ' <form action="index.php?action=' . $this->title . '&confirmed=yes&sesskey=' . sesskey() . '" method="post"><fieldset class="invisiblefieldset">'; 00101 $o.= ' <input type="submit" value="'. $this->str['yes'] .'" /></fieldset></form></div>'; 00102 $o.= ' </td><td>'; 00103 $o.= ' <div class="singlebutton">'; 00104 $o.= ' <form action="index.php?action=main_view" method="post"><fieldset class="invisiblefieldset">'; 00105 $o.= ' <input type="submit" value="'. $this->str['no'] .'" /></fieldset></form></div>'; 00106 $o.= ' </td></tr>'; 00107 $o.= ' </table>'; 00108 $o.= ' </td></tr>'; 00109 $o.= '</table>'; 00110 00111 $this->output = $o; 00112 } else { 00113 // The back to edit table button 00114 $b = ' <p class="centerpara buttons">'; 00115 $b .= '<a href="index.php">[' . $this->str['back'] . ']</a>'; 00116 $b .= '</p>'; 00117 00118 // Iterate over $XMLDB->dbdirs, loading their XML data to memory 00119 if ($XMLDB->dbdirs) { 00120 $dbdirs =& $XMLDB->dbdirs; 00121 $o='<ul>'; 00122 foreach ($dbdirs as $dbdir) { 00123 // Only if the directory exists 00124 if (!$dbdir->path_exists) { 00125 continue; 00126 } 00127 // Load the XML file 00128 $xmldb_file = new xmldb_file($dbdir->path . '/install.xml'); 00129 00130 // Only if the file exists 00131 if (!$xmldb_file->fileExists()) { 00132 continue; 00133 } 00134 // Load the XML contents to structure 00135 $loaded = $xmldb_file->loadXMLStructure(); 00136 if (!$loaded || !$xmldb_file->isLoaded()) { 00137 echo $OUTPUT->notification('Errors found in XMLDB file: '. $dbdir->path . '/install.xml'); 00138 continue; 00139 } 00140 // Arriving here, everything is ok, get the XMLDB structure 00141 $structure = $xmldb_file->getStructure(); 00142 00143 $o.=' <li>' . str_replace($CFG->dirroot . '/', '', $dbdir->path . '/install.xml'); 00144 // Getting tables 00145 if ($xmldb_tables = $structure->getTables()) { 00146 $o.=' <ul>'; 00147 // Foreach table, process its fields 00148 foreach ($xmldb_tables as $xmldb_table) { 00149 // Skip table if not exists 00150 if (!$dbman->table_exists($xmldb_table)) { 00151 continue; 00152 } 00153 // Fetch metadata from physical DB. All the columns info. 00154 if (!$metacolumns = $DB->get_columns($xmldb_table->getName())) { 00155 // / Skip table if no metacolumns is available for it 00156 continue; 00157 } 00158 // Table processing starts here 00159 $o.=' <li>' . $xmldb_table->getName(); 00160 // Do the specific check. 00161 list($output, $newproblems) = $this->check_table($xmldb_table, $metacolumns); 00162 $o.=$output; 00163 $problemsfound = array_merge($problemsfound, $newproblems); 00164 $o.=' </li>'; 00165 // Give the script some more time (resetting to current if exists) 00166 if ($currenttl = @ini_get('max_execution_time')) { 00167 @ini_set('max_execution_time',$currenttl); 00168 } 00169 } 00170 $o.=' </ul>'; 00171 } 00172 $o.=' </li>'; 00173 } 00174 $o.='</ul>'; 00175 } 00176 00177 // Create a report of the problems found. 00178 $r = $this->display_results($problemsfound); 00179 00180 // Combine the various bits of output. 00181 $this->output = $b . $r . $o; 00182 } 00183 00184 // Launch postaction if exists (leave this here!) 00185 if ($this->getPostAction() && $result) { 00186 return $this->launch($this->getPostAction()); 00187 } 00188 00189 // Return ok if arrived here 00190 return $result; 00191 } 00192 00203 abstract protected function check_table(xmldb_table $xmldb_table, array $metacolumns); 00204 00212 abstract protected function display_results(array $problems_found); 00213 }