예제 #1
0
파일: cache.php 프로젝트: p-2/wkw-theme
 /**
  * cron job to refresh image and data caches
  */
 public static function refresh_caches()
 {
     set_time_limit(0);
     self::clear_data_cache();
     /* store IDs for names to get data */
     $namedata = array();
     /* number of names to get data for in one hit */
     $namedata_limit = 20;
     $all_names = wkwMediaWiki::get_all_names();
     if ($all_names && count($all_names)) {
         $cacheDir = self::get_cache_dir("images");
         $logtxt = sprintf("\n============================\nCRON JOB STARTED %s\n============================\n", date("r"));
         foreach ($all_names as $name) {
             $filename = $cacheDir . "/" . $name->pageid . ".png";
             $touched = strtotime($name->touched);
             $since = time() - 60 * 60 * 24;
             $refresh = false;
             if (file_exists($filename)) {
                 /* see if there is an old image */
                 $linkimg = $cacheDir . "/" . md5($name->title) . ".png";
                 $linkname = $cacheDir . "/" . md5($name->title) . ",txt";
                 if (file_exists($linkimg) || file_exists($linkname)) {
                     @unlink($linkimg);
                     @unlink($linkname);
                 }
                 /* see if the page has been touched since the image was generated */
                 if (filemtime($filename) < $touched) {
                     $logtxt .= sprintf("%s: file exists but wiki page was touched more recently than the image was generated (filemtime: %s, touched: %s).\n", $name->title, date("d/m/Y H:i:s", filemtime($filename)), date("d/m/Y H:i:s", $touched));
                     $refresh = true;
                 }
             } else {
                 $logtxt .= sprintf("%s: cache file doesn't exist\n", $name->title);
                 $refresh = true;
             }
             if ($refresh) {
                 wkwFontImage::generate($name);
                 wkwCache::clearCacheFor($name);
                 $namedata[] = $name->pageid;
             }
         }
         foreach ($all_names as $name) {
             $bio = wkwCache::get_cache_dir('bios') . $name->pageid . '.json';
             if (!file_exists($bio) && count($namedata) <= $namedata_limit && !in_array($name->pageid, $namedata)) {
                 $namedata[] = $name->pageid;
             }
         }
         if (count($namedata)) {
             foreach ($namedata as $pageid) {
                 $logtxt .= "\nGetting links and biographical data for name " . $pageid . "\n";
                 wkwMediaWiki::get_linked_names($pageid);
                 wkwMediaWiki::get_page_data($nameid);
             }
         }
         $logtxt .= sprintf("\n============================\nCRON JOB FINISHED %s\n============================\n", date("r"));
         self::log($logtxt, "cron_log.txt");
     }
 }