PageRenderTime 58ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/webroot/updates/concrete5.6.0.2/concrete/core/models/job.php

https://bitbucket.org/microwebedu/registratie_carem
PHP | 345 lines | 242 code | 55 blank | 48 comment | 30 complexity | 32e0a6f44856c717440dc1de266105f0 MD5 | raw file
Possible License(s): MIT, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. defined('C5_EXECUTE') or die("Access Denied.");
  3. /**
  4. *
  5. * Contains the job class.
  6. * @package Utilities
  7. * @author Andrew Embler <andrew@concrete5.org>
  8. * @author Tony Trupp <tony@concrete5.org>
  9. * @link http://www.concrete5.org
  10. * @license http://www.opensource.org/licenses/mit-license.php MIT
  11. *
  12. */
  13. /**
  14. *
  15. * The job class is essentially sub-dispatcher for certain maintenance tasks that need to be run at specified intervals. Examples include indexing a search engine or generating a sitemap page.
  16. * @package Utilities
  17. * @author Andrew Embler <andrew@concrete5.org>
  18. * @author Tony Trupp <tony@concrete5.org>
  19. * @link http://www.concrete5.org
  20. * @license http://www.opensource.org/licenses/mit-license.php MIT
  21. *
  22. */
  23. class Concrete5_Model_Job extends Object {
  24. //Required Job Variables - Override these variables in your child job class
  25. public $jName="Job base class";
  26. public $jDescription="";
  27. //you must override this method
  28. function run(){
  29. throw new Exception(t('Error: The Job::run() method must be overridden by your child Job class.'));
  30. }
  31. public function getJobName() {return $this->jName;}
  32. public function getJobDescription() {return $this->jDescription;}
  33. public function getJobHandle() {return $this->jHandle;}
  34. public function getPackageHandle() {
  35. return PackageList::getHandle($this->pkgID);
  36. }
  37. //==========================================================
  38. // JOB MANAGEMENT - do not override anything below this line
  39. //==========================================================
  40. //meta variables
  41. public $errors=array();
  42. protected $jobClassLocations=array();
  43. //Other Job Variables
  44. public $jID=0;
  45. public $jStatus='ENABLED';
  46. public $availableJStatus=array( 'ENABLED','RUNNING','DISABLED_ERROR','DISABLED' );
  47. public $jDateLastRun=' default ';
  48. public $jHandle='';
  49. public $jNotUninstallable=0;
  50. /*
  51. final public __construct(){
  52. //$this->jHandle="example_job_file.php";
  53. }
  54. */
  55. public static function jobClassLocations(){
  56. return array(DIR_FILES_JOBS, DIR_FILES_JOBS_CORE);
  57. }
  58. // authenticateRequest checks against your site's salt and a custom auth field to make
  59. // sure that this is a request that is coming either from something cronned by the site owner
  60. // or from the dashboard
  61. public static function authenticateRequest($auth) {
  62. $val = PASSWORD_SALT . ':' . DIRNAME_JOBS;
  63. return md5($val) == $auth;
  64. }
  65. public static function generateAuth() {
  66. $val = PASSWORD_SALT . ':' . DIRNAME_JOBS;
  67. return md5($val);
  68. }
  69. public static function exportList($xml) {
  70. $jl = self::getList();
  71. if ($jl->numRows() > 0) {
  72. $jx = $xml->addChild('jobs');
  73. while($r = $jl->FetchRow()) {
  74. $j = Job::getByID($r['jID']);
  75. $ch = $jx->addChild('job');
  76. $ch->addAttribute('handle',$j->getJobHandle());
  77. $ch->addAttribute('package',$j->getPackageHandle());
  78. }
  79. }
  80. }
  81. // Job Retrieval
  82. // ==============
  83. public static function getList(){
  84. $db = Loader::db();
  85. $v=array();
  86. $q = "SELECT * FROM Jobs ORDER BY jDateLastRun";
  87. $rs = $db->query($q, $v);
  88. return $rs;
  89. }
  90. public static function resetRunningJobs() {
  91. $db = Loader::db();
  92. $db->Execute('update Jobs set jStatus = \'ENABLED\' where jStatus = \'RUNNING\'');
  93. }
  94. public static function getByID( $jID=0 ){
  95. $db = Loader::db();
  96. $jobData = $db->getRow("SELECT * FROM Jobs WHERE jID=".intval($jID));
  97. if( !$jobData || !$jobData['jHandle'] ) return NULL;
  98. return Job::getJobObjByHandle( $jobData['jHandle'], $jobData );
  99. }
  100. public static function getByHandle( $jHandle='' ){
  101. $db = Loader::db();
  102. $jobData = $db->getRow( 'SELECT * FROM Jobs WHERE jHandle=?', array($jHandle) );
  103. if( !$jobData || !$jobData['jHandle'] ) return NULL;
  104. return Job::getJobObjByHandle( $jobData['jHandle'], $jobData );
  105. }
  106. public static function getJobObjByHandle( $jHandle='', $jobData=array() ){
  107. $jcl = Job::jobClassLocations();
  108. //check for the job file in the various locations
  109. $db = Loader::db();
  110. $pkgID = $db->GetOne('select pkgID from Jobs where jHandle = ?', $jHandle);
  111. if ($pkgID > 0) {
  112. $pkgHandle = PackageList::getHandle($pkgID);
  113. if ($pkgHandle) {
  114. $jcl[] = DIR_PACKAGES . '/' . $pkgHandle . '/' . DIRNAME_JOBS;
  115. $jcl[] = DIR_PACKAGES_CORE . '/' . $pkgHandle . '/' . DIRNAME_JOBS;
  116. }
  117. }
  118. foreach( $jcl as $jobClassLocation ){
  119. //load the file & class, then run the job
  120. $path=$jobClassLocation.'/'.$jHandle.'.php';
  121. if( file_exists($path) ){
  122. require_once($path);
  123. $className=Object::camelcase( $jHandle );
  124. $j = new $className();
  125. $j->jHandle=$jHandle;
  126. if(intval($jobData['jID'])>0){
  127. $j->jID=intval($jobData['jID']);
  128. $j->jStatus=$jobData['jStatus'];
  129. $j->jDateLastRun=$jobData['jDateLastRun'];
  130. $j->jLastStatusText=$jobData['jLastStatusText'];
  131. $j->pkgID=$jobData['pkgID'];
  132. $j->jDateInstalled=$jobData['jDateInstalled'];
  133. $j->jNotUninstallable=$jobData['jNotUninstallable'];
  134. }
  135. return $j;
  136. }
  137. }
  138. return NULL;
  139. }
  140. //Scan job directories for job classes
  141. public static function getAvailableList($includeConcreteDirJobs=1){
  142. $jobObjs=array();
  143. //get existing jobs
  144. $existingJobHandles=array();
  145. $existingJobsRS = Job::getList();
  146. while($existingJobsRow = $existingJobsRS->fetchRow() )
  147. $existingJobHandles[]=$existingJobsRow['jHandle'];
  148. if(!$includeConcreteDirJobs)
  149. $jobClassLocations = array( DIR_FILES_JOBS );
  150. else $jobClassLocations = Job::jobClassLocations();
  151. foreach( $jobClassLocations as $jobClassLocation){
  152. // Open a known directory, and proceed to read its contents
  153. if (is_dir($jobClassLocation)) {
  154. if ($dh = opendir($jobClassLocation)) {
  155. while (($file = readdir($dh)) !== false) {
  156. if( substr($file,strlen($file)-4)!='.php' ) continue;
  157. $alreadyInstalled=0;
  158. foreach($existingJobHandles as $existingJobHandle){
  159. if( substr($file,0,strlen($file)-4)==$existingJobHandle){
  160. $alreadyInstalled=1;
  161. break;
  162. }
  163. }
  164. if($alreadyInstalled) continue;
  165. $path=$jobClassLocation .'/'. $file;
  166. require_once( $jobClassLocation .'/'. $file );
  167. $jHandle = substr($file,0,strlen($file)-4);
  168. $className=Object::camelcase( $jHandle );
  169. if(class_exists($className)){
  170. $jobObjs[$jHandle]=new $className();
  171. $jobObjs[$jHandle]->jHandle=$jHandle;
  172. if(!$jobObjs[$jHandle] instanceof Job){
  173. $jobObjs[$jHandle]->jDescription= t('Error: The Job class must be a child class of Job.');
  174. $jobObjs[$jHandle]->invalid=1;
  175. }
  176. }else{
  177. $invalidJob = new Job();
  178. $invalidJob->jName = $className;
  179. $invalidJob->jHandle=$jHandle;
  180. $invalidJob->jDescription = t('Error: Invalid Job file. The class %s was not found in %s .', $className, $path);
  181. $invalidJob->invalid=1;
  182. $jobObjs[$jHandle] = $invalidJob;
  183. }
  184. }
  185. closedir($dh);
  186. }
  187. }else throw new Exception( t('Error: Invalid Jobs Directory %s', $jobClassLocation) );
  188. }
  189. return $jobObjs;
  190. }
  191. // Running Jobs
  192. // ==============
  193. public static function runAllJobs(){
  194. //loop through all installed jobs
  195. $jobListRS=Job::getList();
  196. while( $jobItem = $jobListRS->fetchRow() ){
  197. $jobObj = Job::getJobObjByHandle($jobItem['jHandle']);
  198. $jobObj->executeJob();
  199. }
  200. }
  201. public function executeJob(){
  202. Events::fire('on_before_job_execute', $this);
  203. $db = Loader::db();
  204. $timestampH =date('Y-m-d g:i:s A');
  205. $timestamp=date('Y-m-d H:i:s');
  206. $this->jDateLastRun = $timestampH;
  207. $rs = $db->query( "UPDATE Jobs SET jStatus='RUNNING', jDateLastRun=? WHERE jHandle=?", array( $timestamp, $this->jHandle ) );
  208. try{
  209. $resultMsg=$this->run();
  210. if(strlen($resultMsg)==0)
  211. $resultMsg= t('The Job was run successfully.');
  212. }catch(Exception $e){
  213. $resultMsg=$e->getMessage();
  214. $this->loadError(2);
  215. }
  216. if( !$this->isError() ) $jStatus='ENABLED';
  217. else $jStatus='DISABLED_ERROR';
  218. $rs = $db->query( "UPDATE Jobs SET jStatus=?, jLastStatusText=? WHERE jHandle=?", array( $jStatus, $resultMsg, $this->jHandle ) );
  219. $enum = 0;
  220. if ($this->getError() > 0) {
  221. $enum = $this->getError();
  222. }
  223. $rs = $db->query( "INSERT INTO JobsLog (jID, jlMessage, jlTimestamp, jlError) VALUES(?,?,?,?)", array( $this->jID, $resultMsg, $timestamp, $enum ) );
  224. Events::fire('on_job_execute', $this);
  225. return $resultMsg;
  226. }
  227. public function setJobStatus($jStatus='ENABLED'){
  228. $db = Loader::db();
  229. if( !in_array($jStatus,$this->availableJStatus) )
  230. $jStatus='ENABLED';
  231. $rs = $db->query( "UPDATE Jobs SET jStatus=? WHERE jHandle=?", array( $jStatus, $this->jHandle ) );
  232. }
  233. public function installByHandle($jHandle=''){
  234. $availableJobs=Job::getAvailableList();
  235. foreach( $availableJobs as $availableJobHandle=>$availableJobObj ){
  236. if( $availableJobObj->jHandle!=$jHandle ) continue;
  237. $availableJobObj->install();
  238. }
  239. }
  240. public static function getListByPackage($pkg) {
  241. $db = Loader::db();
  242. $list = array();
  243. $r = $db->Execute('select jHandle from Jobs where pkgID = ? order by jHandle asc', array($pkg->getPackageID()));
  244. while ($row = $r->FetchRow()) {
  245. $list[] = Job::getJobObjByHandle($row['jHandle']);
  246. }
  247. $r->Close();
  248. return $list;
  249. }
  250. public function installByPackage($jHandle, $pkg) {
  251. $dir = is_dir(DIR_PACKAGES . '/' . $pkg->getPackageHandle()) ? DIR_PACKAGES . '/' . $pkg->getPackageHandle() : DIR_PACKAGES_CORE . '/' . $pkg->getPackageHandle();
  252. require_once( $dir .'/'. DIRNAME_JOBS . '/' . $jHandle . '.php');
  253. $className=Object::camelcase( $jHandle );
  254. if(class_exists($className)){
  255. $j = new $className();
  256. $db = Loader::db();
  257. $db->Execute('insert into Jobs (jName, jDescription, jDateInstalled, jNotUninstallable, jHandle, pkgID) values (?, ?, ?, ?, ?, ?)',
  258. array($j->getJobName(), $j->getJobDescription(), Loader::helper('date')->getLocalDateTime(), 0, $jHandle, $pkg->getPackageID()));
  259. Events::fire('on_job_install', $j);
  260. return $j;
  261. }
  262. }
  263. public function install(){
  264. $db = Loader::db();
  265. $jobExists=$db->getOne( 'SELECT count(*) FROM Jobs WHERE jHandle=?', array($this->jHandle) );
  266. $vals=array($this->getJobName(),$this->getJobDescription(), date('Y-m-d H:i:s'), $this->jNotUninstallable, $this->jHandle);
  267. if($jobExists){
  268. $db->query('UPDATE Jobs SET jName=?, jDescription=?, jDateInstalled=?, jNotUninstallable=? WHERE jHandle=?',$vals);
  269. }else{
  270. $db->query('INSERT INTO Jobs (jName, jDescription, jDateInstalled, jNotUninstallable, jHandle) VALUES(?,?,?,?,?)',$vals);
  271. }
  272. Events::fire('on_job_install', $this);
  273. }
  274. public function uninstall(){
  275. $ret = Events::fire('on_job_uninstall', $this);
  276. if($ret < 0) {
  277. return $ret;
  278. }
  279. $db = Loader::db();
  280. $db->query( 'DELETE FROM Jobs WHERE jHandle=?', array($this->jHandle) );
  281. }
  282. /**
  283. * Removes Job log entries
  284. */
  285. public static function clearLog() {
  286. $db = Loader::db();
  287. $db->Execute("delete from JobsLog");
  288. }
  289. }
  290. ?>