Example #1
0
 public static function get_surname($sortkeyprefix, $with_substitutions = true)
 {
     if (strpos($sortkeyprefix, "|") !== false) {
         $_frag = explode("|", $sortkeyprefix);
         $_name = trim($_frag[1]);
     } else {
         $_name = trim($sortkeyprefix);
     }
     $_surname = explode(",", $_name);
     $surname = trim($_surname[0]);
     if ($with_substitutions) {
         return wkwFontImage::make_substitutions($surname);
     } else {
         return $surname;
     }
 }
Example #2
0
 /**
  * gets pages from the wiki which are linked to the given page
  */
 public static function get_linked_names($page_id = false)
 {
     if ($page_id === false) {
         return array();
     }
     $cache_key = "links/" . $page_id;
     $linked_names = json_decode(wkwCache::get_cache($cache_key));
     if (!$linked_names) {
         $results = self::query(array('query' => 'links', 'term' => $page_id));
         $linked_names = array();
         if ($results && $results->query && $results->query->pages) {
             $all_names = self::get_all_names();
             foreach ($results->query->pages as $id => $name) {
                 if (isset($name->missing)) {
                     /* no page in wiki yet for this name */
                     $surname = self::get_unlinked_surname($name->title);
                     $id = md5($name->title);
                     $p = array('pageid' => $id, 'title' => $name->title, 'surname' => $surname, 'sortkeyprefix' => $surname, 'urltitle' => false, 'hasDetail' => false);
                     $po = (object) $p;
                     /* generate image for name */
                     $ud = wp_upload_dir();
                     $nametxt = $ud["basedir"] . '/wkw-cache/names/' . $id . '.txt';
                     $nameimg = $ud["basedir"] . '/wkw-cache/names/' . $id . '.png';
                     if (!file_exists($nametext)) {
                         file_put_contents($nametxt, $name->title);
                     }
                     wkwFontImage::generate($po);
                     $po->lastmod = wkwCache::get_last_modified($id);
                     $linked_names[] = $po;
                 } else {
                     /* page exists - get details from $all_names */
                     foreach ($all_names as $n) {
                         if ($n->pageid == $id) {
                             $p = array('pageid' => $n->pageid, 'title' => $n->title, 'surname' => $n->surname, 'urltitle' => $n->urltitle, 'hasDetail' => true, 'lastmod' => wkwCache::get_last_modified($n->pageid));
                             $linked_names[] = (object) $p;
                             break;
                         }
                     }
                 }
             }
             wkwCache::save_cache($cache_key, json_encode($linked_names));
         }
     }
     return $linked_names;
 }
Example #3
0
 /**
  * creates the image management page
  */
 public static function datascape_options_page()
 {
     $datascape_options = self::get_datascape_options();
     print '<div class="wrap"><div class="icon32" id="icon-tools"><br /></div><h2>Datascape Options</h2>';
     self::process_datascape_options();
     /* cache management buttons */
     print '<h3>Cache management</h3><p>';
     $settings_url = admin_url('themes.php?page=datascape_options');
     $actions = array("refreshDataCache" => "Refresh data cache", "refreshBiogCache" => "Refresh biography cache", "refreshLinkCache" => "Refresh links cache", "refreshImageCache" => "Refresh Image cache");
     foreach ($actions as $slug => $title) {
         $action_url = $settings_url . '&amp;action=' . $slug . '&amp;wkw_nonce=' . wp_create_nonce($slug);
         printf('<a class="button-primary" style="margin:0 2em 1em 0" id="%s" href="%s">%s</a>', $slug, $action_url, $title);
     }
     print '</p>';
     /* datascape options */
     print '<form method="post" action="options.php">';
     settings_fields('wkw_datascape_options');
     do_settings_sections('wkw-datascape');
     print self::get_palette();
     print '<p><input type="submit" class="button-primary" name="submit" value="Save settings" /><input type="reset" id="hide_datascape_options" class="button-secondary" value="Cancel" /></p></div></form>';
     /* names management */
     print '<h3 id="wkw_manage_names">Manage names</h3>';
     $all_names = wkwMediaWiki::get_all_names();
     $total_names = count($all_names);
     $page = isset($_REQUEST["wkw_name"]) ? $_REQUEST["wkw_name"] : 'a';
     $ud = wp_upload_dir();
     print '<p>There are currently <strong>' . count($all_names) . "</strong> names in the Wiki.</p>";
     /* make navigation */
     $nav = '<div class="atoz"><ul>';
     $abc = range("a", "z");
     $abc[] = 'unlinked';
     $abc[] = 'imagedata';
     foreach ($abc as $letter) {
         $cls = $page == $letter ? ' class="active"' : '';
         $nav .= sprintf('<li><a href="%s"%s#wkw_manage_names>%s</a></li>', admin_url("themes.php?page=datascape_options&wkw_name=" . $letter), $cls, strtoupper($letter));
     }
     $nav .= '</ul></div>';
     $headers = '<tr><th>Name</th><th>added</th><th>last edited</th><th>image</th></tr>';
     $table_top = sprintf('%s<table class="widefat"><thead>%s</thead><tfoot>%s</tfoot><tbody>', $nav, $headers, $headers);
     $table_end = sprintf('</tbody></table>%s<br style="clear:both;" />', $nav);
     switch ($page) {
         case "unlinked":
             $cachedir = $ud["basedir"] . '/wkw-cache/names';
             if ($dh = opendir($cachedir)) {
                 print $table_top;
                 while (($file = readdir($dh)) !== false) {
                     if (!is_dir($cachedir . '/' . $file) && !preg_match('/^[0-9]+\\.png$/', $file) && !preg_match('/.*\\.txt$/', $file)) {
                         $imageurl = $ud["baseurl"] . '/wkw-cache/names/' . $file . '?mtime=' . filemtime($ud["basedir"] . '/wkw-cache/names/' . $file);
                         $fname = $cachedir . "/" . substr($file, 0, -3) . 'txt';
                         $name = file_get_contents($fname);
                         printf('<tr><td colspan="3">%s<br /><a href="/wiki/index.php?title=%s&amp;action=edit" class="button-secondary" target="wiki">create page in wiki</a> <a class="button-secondary" href="%s%s#wkw_manage_names">regenerate image</a></td><td><img src="%s" /></td></tr>', $name, str_replace(' ', '_', $name), admin_url('themes.php?page=datascape_options&amp;action=refreshImage&wkw_linkid='), substr($file, 0, -3), $imageurl);
                     }
                 }
                 print $table_end;
                 closedir($dh);
             }
             break;
         case 'imagedata':
             $datacsv = '"width","height","name","surname","filesize","url"' . "\n";
             $cachedir = $ud["basedir"] . '/wkw-cache/names';
             $total_width = $total_size = $no_images = 0;
             if ($dh = opendir($cachedir)) {
                 $stats = array('largest' => '', 'smallest' => '');
                 $largest = 0;
                 $smallest = 100000000;
                 while (($file = readdir($dh)) !== false) {
                     $fn = $cachedir . '/' . $file;
                     if (!is_dir($fn) && preg_match('/.*png$/', $fn)) {
                         $imginfo = @getimagesize($fn);
                         if ($imginfo !== false) {
                             $no_images++;
                             $name = '';
                             $surname = '';
                             if (file_exists($cachedir . "/" . substr($file, 0, -3) . 'txt')) {
                                 $name = file_get_contents($cachedir . "/" . substr($file, 0, -3) . 'txt');
                                 $surname = wkwMediaWiki::get_unlinked_surname($name);
                             } else {
                                 for ($i = 0; $i < $total_names; $i++) {
                                     if (substr($file, 0, -4) == $all_names[$i]->pageid) {
                                         $name = $all_names[$i]->sortkeyprefix;
                                         $surname = wkwFontImage::get_surname($all_names[$i]->sortkeyprefix, false);
                                         break;
                                     }
                                 }
                             }
                             $imginfo[2] = '"' . str_replace('"', '\\"', $name) . '"';
                             $imginfo[3] = '"' . str_replace('"', '\\"', $surname) . '"';
                             $imginfo[4] = filesize($fn);
                             $imginfo[5] = $ud["baseurl"] . '/wkw-cache/names/' . $file;
                             unset($imginfo['mime']);
                             unset($imginfo['bits']);
                             $datacsv .= implode(',', $imginfo) . "\n";
                             $area = intval($imginfo[0] * $imginfo[1]);
                             if ($area > 0) {
                                 if ($area > $largest) {
                                     $stats["largest"] = $imginfo;
                                     $largest = $area;
                                 }
                                 if ($area < $smallest) {
                                     $stats["smallest"] = $imginfo;
                                     $smallest = $area;
                                 }
                                 $total_width += $imginfo[0];
                                 $total_size += $imginfo[4];
                             }
                         }
                     }
                 }
             }
             print $nav;
             print '<h3 style="clear:both;">Image stats</h3>';
             printf('<p>Largest image (%spx x %spx, %s bytes):<br /><img src="%s" /></p>', $stats["largest"][0], $stats["largest"][1], $stats["largest"][4], $stats["largest"][5]);
             //printf('<p>Smallest image (%spx x %spx, %s bytes):<br /><img src="%s" /></p>', $stats["smallest"][0], $stats["smallest"][1], $stats["smallest"][4], $stats["smallest"][5]);
             printf('<p>Average width of images: %spx</p><p>Average size of images: %s bytes - current settings indicate a download of %s Kb to reach name saturation</p>', floor($total_width / $no_images), floor($total_size / $no_images), floor(floor($total_size / $no_images) * $datascape_options["img_max"] / 1024));
             print '<h3>All image data</h3>';
             printf('<div style="clear:both"><textarea cols="100" rows="100">%s</textarea></div>', $datacsv);
             print $nav;
             break;
         default:
             print $table_top;
             for ($i = 0; $i < $total_names; $i++) {
                 if (stripos($all_names[$i]->sortkeyprefix, $page) === 0) {
                     $imageurl = $ud["baseurl"] . '/wkw-cache/names/' . $all_names[$i]->pageid . '.png?mtime=' . filemtime($ud["basedir"] . '/wkw-cache/names/' . $all_names[$i]->pageid . '.png');
                     printf('<tr><td><strong>Name:</strong> %s<br /><strong>Sortkey:</strong> %s<br /><strong>Surname:</strong> %s<br /><a href="%s" class="button-secondary" target="wiki">view in wiki</a> <a href="%s" class="button-secondary" target="wiki">edit in wiki</a> <a class="button-secondary" href="%s%s#wkw_manage_names">regenerate image</a></td><td>%s</td><td>%s</td><td><img src="%s" title="%s" /></td></tr>', $all_names[$i]->title, $all_names[$i]->sortkeyprefix, wkwFontImage::get_surname($all_names[$i]->sortkeyprefix, false), $all_names[$i]->fullurl, $all_names[$i]->editurl, admin_url('themes.php?page=datascape_options&amp;action=refreshImage&wkw_pageid='), $all_names[$i]->pageid, date("d/m/Y", strtotime($all_names[$i]->timestamp)), date("d/m/Y", strtotime($all_names[$i]->touched)), $imageurl, esc_attr($all_names[$i]->title));
                 }
             }
             print $table_end;
             break;
     }
     print '</div>';
 }
Example #4
0
 /**
  * cache freshener - called from theme option screen
  */
 public static function force_refresh($which = "data")
 {
     set_time_limit(0);
     $all_names = wkwMediaWiki::get_all_names();
     if ($all_names && count($all_names)) {
         switch ($which) {
             case "images":
                 self::clear_image_cache();
                 foreach ($all_names as $name) {
                     wkwFontImage::generate($name);
                 }
                 break;
             case "bios":
             case "links":
                 foreach ($all_names as $name) {
                     $getdata = true;
                     $data = wkwCache::get_cache_dir($which) . $name->pageid . '.json';
                     $touched = strtotime($name->touched);
                     if (file_exists($data) && filemtime($data) >= $touched) {
                         $getdata = false;
                     }
                     if ($getdata) {
                         @unlink($data);
                         $method = $which == "bios" ? "get_page_data" : "get_linked_names";
                         wkwMediaWiki::$method($name->pageid);
                     }
                 }
                 break;
             default:
                 self::clear_data_cache();
                 $all_names = wkwMediaWiki::get_all_names(true);
                 break;
         }
     }
 }