includes/functions.php
changeset 953 323c4cd1aa37
parent 915 91f4da84966f
child 963 b572ce1114f1
equal deleted inserted replaced
952:d52dfa1f08da 953:323c4cd1aa37
   378 function get_page_title($page_id, $show_ns = true)
   378 function get_page_title($page_id, $show_ns = true)
   379 {
   379 {
   380   global $db, $session, $paths, $template, $plugins; // Common objects
   380   global $db, $session, $paths, $template, $plugins; // Common objects
   381 
   381 
   382   $idata = RenderMan::strToPageID($page_id);
   382   $idata = RenderMan::strToPageID($page_id);
   383   $page_id_key = $paths->nslist[ $idata[1] ] . $idata[0];
   383   return get_page_title_ns($idata[0], $idata[1]);
   384   $page_id_key = sanitize_page_id($page_id_key);
       
   385   $page_data = @$paths->pages[$page_id_key];
       
   386   $title = ( isset($page_data['name']) ) ?
       
   387     ( ( $page_data['namespace'] == 'Article' || !$show_ns ) ?
       
   388       '' :
       
   389       $paths->nslist[ $idata[1] ] )
       
   390     . $page_data['name'] :
       
   391     ( $show_ns ? $paths->nslist[$idata[1]] : '' ) . str_replace('_', ' ', dirtify_page_id( $idata[0] ) );
       
   392   return $title;
       
   393 }
   384 }
   394 
   385 
   395 /**
   386 /**
   396  * Tells you the title for the given page ID and namespace
   387  * Tells you the title for the given page ID and namespace
   397  * @param string Page ID
   388  * @param string Page ID
   400  */
   391  */
   401 
   392 
   402 function get_page_title_ns($page_id, $namespace)
   393 function get_page_title_ns($page_id, $namespace)
   403 {
   394 {
   404   global $db, $session, $paths, $template, $plugins; // Common objects
   395   global $db, $session, $paths, $template, $plugins; // Common objects
   405 
   396   
   406   $ns_prefix = ( isset($paths->nslist[ $namespace ]) ) ? $paths->nslist[ $namespace ] : $namespace . substr($paths->nslist['Special'], -1);
   397   $ns = namespace_factory($page_id, $namespace);
   407   $page_id_key = $ns_prefix . $page_id;
   398   return $ns->title;
   408   if ( isPage($page_id_key) )
       
   409   {
       
   410     $page_data = $paths->pages[$page_id_key];
       
   411   }
       
   412   else
       
   413   {
       
   414     $page_data = array();
       
   415   }
       
   416   $title = ( isset($page_data['name']) ) ? $page_data['name'] : $ns_prefix . str_replace('_', ' ', dirtify_page_id( $page_id ) );
       
   417   return $title;
       
   418 }
   399 }
   419 
   400 
   420 /**
   401 /**
   421  * Redirect the user to the specified URL.
   402  * Redirect the user to the specified URL.
   422  * @param string $url The URL, either relative or absolute.
   403  * @param string $url The URL, either relative or absolute.
   576  * Tell me if the page exists or not.
   557  * Tell me if the page exists or not.
   577  * @param string the full page ID (Special:Administration) of the page to check for
   558  * @param string the full page ID (Special:Administration) of the page to check for
   578  * @return bool True if the page exists, false otherwise
   559  * @return bool True if the page exists, false otherwise
   579  */
   560  */
   580 
   561 
   581 function isPage($p) {
   562 function isPage($p)
       
   563 {
   582   global $db, $session, $paths, $template, $plugins; // Common objects
   564   global $db, $session, $paths, $template, $plugins; // Common objects
   583 
   565   static $ispage_cache = array();
   584   // Try the easy way first ;-)
   566   if ( isset($ispage_cache[$p]) )
   585   if ( isset( $paths->pages[ $p ] ) )
   567     return $ispage_cache[$p];
   586   {
   568   
   587     return true;
   569   list($page_id, $namespace) = RenderMan::strToPageID($p);
   588   }
   570   $cdata = $paths->get_cdata($page_id, $namespace);
   589 
   571   if ( !isset($cdata['page_exists']) )
   590   // Special case for Special, Template, and Admin pages that can't have slashes in their URIs
   572   {
   591   $ns_test = RenderMan::strToPageID( $p );
   573     $class = ( class_exists($_ = "Namespace_$namespace") ) ? $_ : "Namespace_Default";
   592 
   574     $page = new $class($page_id, $namespace);
   593   if($ns_test[1] != 'Special' && $ns_test[1] != 'Template' && $ns_test[1] != 'Admin')
   575     return $page->exists();
   594   {
   576   }
   595     return false;
   577   
   596   }
   578   $ispage_cache[$p] = $cdata['page_exists'];
   597 
   579   return $cdata['page_exists'];
   598   $particles = explode('/', $p);
       
   599   if ( isset ( $paths->pages[ $particles[ 0 ] ] ) )
       
   600   {
       
   601     return true;
       
   602   }
       
   603   else
       
   604   {
       
   605     return false;
       
   606   }
       
   607 }
   580 }
   608 
   581 
   609 /**
   582 /**
   610  * Returns the appropriate Namespace_* object for a page.
   583  * Returns the appropriate Namespace_* object for a page.
   611  * @param string Page ID
   584  * @param string Page ID
   613  * @param int Revision ID
   586  * @param int Revision ID
   614  */
   587  */
   615 
   588 
   616 function namespace_factory($page_id, $namespace, $revision_id = 0)
   589 function namespace_factory($page_id, $namespace, $revision_id = 0)
   617 {
   590 {
       
   591   global $db, $session, $paths, $template, $plugins; // Common objects
       
   592   
       
   593   static $objcache = array();
       
   594   $pathskey = $paths->get_pathskey($page_id, $namespace) . ":$revision_id";
       
   595   if ( isset($objcache[$pathskey]) )
       
   596     return $objcache[$pathskey];
       
   597   
   618   if ( !class_exists("Namespace_$namespace") )
   598   if ( !class_exists("Namespace_$namespace") )
   619   {
   599   {
   620     if ( file_exists(ENANO_ROOT . "/includes/namespaces/" . strtolower($namespace) . ".php") )
   600     if ( file_exists(ENANO_ROOT . "/includes/namespaces/" . strtolower($namespace) . ".php") )
   621     {
   601     {
   622       require(ENANO_ROOT . "/includes/namespaces/" . strtolower($namespace) . ".php");
   602       require(ENANO_ROOT . "/includes/namespaces/" . strtolower($namespace) . ".php");
   624   }
   604   }
   625   if ( class_exists("Namespace_$namespace") )
   605   if ( class_exists("Namespace_$namespace") )
   626   {
   606   {
   627     $class = "Namespace_$namespace";
   607     $class = "Namespace_$namespace";
   628     $ns = new $class($page_id, $namespace, $revision_id);
   608     $ns = new $class($page_id, $namespace, $revision_id);
       
   609     $objcache[$pathskey] = $ns;
   629     return $ns;
   610     return $ns;
   630   }
   611   }
   631   else
   612   else
   632   {
   613   {
   633     $ns = new Namespace_Default($page_id, $namespace, $revision_id);
   614     $ns = new Namespace_Default($page_id, $namespace, $revision_id);
       
   615     $objcache[$pathskey] = $ns;
   634     return $ns;
   616     return $ns;
   635   }
   617   }
   636 }
   618 }
   637 
   619 
   638 /**
   620 /**
   913  * Shows header information on the current page. Currently this is only the delete-vote feature. Doesn't take or return anything, but assumes that the page information is already set in $paths.
   895  * Shows header information on the current page. Currently this is only the delete-vote feature. Doesn't take or return anything, but assumes that the page information is already set in $paths.
   914  */
   896  */
   915 
   897 
   916 function display_page_headers()
   898 function display_page_headers()
   917 {
   899 {
   918   global $db, $session, $paths, $template, $plugins; // Common objects
   900   // Deprecated.
   919   global $lang;
       
   920   if($session->get_permissions('vote_reset') && $paths->cpage['delvotes'] > 0)
       
   921   {
       
   922     $delvote_ips = unserialize($paths->cpage['delvote_ips']);
       
   923     $hr = htmlspecialchars(implode(', ', $delvote_ips['u']));
       
   924     
       
   925     $string_id = ( $paths->cpage['delvotes'] == 1 ) ? 'delvote_lbl_votes_one' : 'delvote_lbl_votes_plural';
       
   926     $string = $lang->get($string_id, array('num_users' => $paths->cpage['delvotes']));
       
   927     
       
   928     echo '<div class="info-box" style="margin-left: 0; margin-top: 5px;" id="mdgDeleteVoteNoticeBox">
       
   929             <b>' . $lang->get('etc_lbl_notice') . '</b> ' . $string . '<br />
       
   930             <b>' . $lang->get('delvote_lbl_users_that_voted') . '</b> ' . $hr . '<br />
       
   931             <a href="'.makeUrl($paths->page, 'do=deletepage').'" onclick="ajaxDeletePage(); return false;">' . $lang->get('delvote_btn_deletepage') . '</a>  |  <a href="'.makeUrl($paths->page, 'do=resetvotes').'" onclick="ajaxResetDelVotes(); return false;">' . $lang->get('delvote_btn_resetvotes') . '</a>
       
   932           </div>';
       
   933   }
       
   934 }
   901 }
   935 
   902 
   936 /**
   903 /**
   937  * Displays page footer information including file and category info. This also has the send_page_footers hook. Doesn't take or return anything, but assumes that the page information is already set in $paths.
   904  * Displays page footer information including file and category info. This also has the send_page_footers hook. Doesn't take or return anything, but assumes that the page information is already set in $paths.
   938  */
   905  */
  2360   $num_pages = ceil ( $num_results / $perpage );
  2327   $num_pages = ceil ( $num_results / $perpage );
  2361   $out = '';
  2328   $out = '';
  2362   $this_page = ceil ( $start / $perpage );
  2329   $this_page = ceil ( $start / $perpage );
  2363   $i = 0;
  2330   $i = 0;
  2364   
  2331   
  2365   $paginator = generate_paginator($this_page, $num_pages, $result_url, $perpage, 0);
  2332   if ( $num_results > 0 )
  2366   $out .= $paginator;
  2333   {
       
  2334     $paginator = generate_paginator($this_page, $num_pages, $result_url, $perpage, 0);
       
  2335     $out .= $paginator;
       
  2336   }
  2367 
  2337 
  2368   $cls = 'row2';
  2338   $cls = 'row2';
  2369 
  2339 
  2370   if ( $row = $db->fetchrow($q) )
  2340   if ( $row = $db->fetchrow($q) )
  2371   {
  2341   {
  2384       $cls = ( $cls == 'row1' ) ? 'row2' : 'row1';
  2354       $cls = ( $cls == 'row1' ) ? 'row2' : 'row1';
  2385       foreach ( $row as $j => $val )
  2355       foreach ( $row as $j => $val )
  2386       {
  2356       {
  2387         if ( isset($callers[$j]) )
  2357         if ( isset($callers[$j]) )
  2388         {
  2358         {
  2389           $tmp = ( is_callable($callers[$j]) ) ? @call_user_func($callers[$j], $val, $row) : $val;
  2359           $tmp = ( is_callable($callers[$j]) ) ? call_user_func($callers[$j], $val, $row) : $val;
  2390 
  2360 
  2391           if ( is_string($tmp) )
  2361           if ( is_string($tmp) )
  2392           {
  2362           {
  2393             $row[$j] = $tmp;
  2363             $row[$j] = $tmp;
  2394           }
  2364           }
  2399       $out .= $parser->run();
  2369       $out .= $parser->run();
  2400     } while ( $row = @$db->fetchrow($q) );
  2370     } while ( $row = @$db->fetchrow($q) );
  2401     $out .= $footer;
  2371     $out .= $footer;
  2402   }
  2372   }
  2403 
  2373 
  2404   $out .= $paginator;
  2374   if ( $num_results > 0 )
       
  2375     $out .= $paginator;
  2405 
  2376 
  2406   return $out;
  2377   return $out;
  2407 }
  2378 }
  2408 
  2379 
  2409 /**
  2380 /**
  2427   $out = '';
  2398   $out = '';
  2428   $i = 0;
  2399   $i = 0;
  2429   $this_page = ceil ( $start / $perpage );
  2400   $this_page = ceil ( $start / $perpage );
  2430 
  2401 
  2431   $paginator = generate_paginator($this_page, $num_pages, $result_url, $perpage, 0);
  2402   $paginator = generate_paginator($this_page, $num_pages, $result_url, $perpage, 0);
  2432   $out .= $paginator;
  2403   
  2433   
  2404   if ( $num_results > 1 )
  2434   if ( $total > 1 )
       
  2435   {
  2405   {
  2436     $out .= $paginator;
  2406     $out .= $paginator;
  2437   }
  2407   }
  2438 
  2408 
  2439   $cls = 'row2';
  2409   $cls = 'row2';
  2455       $out .= $val;
  2425       $out .= $val;
  2456     }
  2426     }
  2457     $out .= $footer;
  2427     $out .= $footer;
  2458   }
  2428   }
  2459 
  2429 
  2460   if ( $total > 1 )
  2430   if ( $num_results > 1 )
  2461     $out .= $paginator;
  2431     $out .= $paginator;
  2462 
  2432 
  2463   return $out;
  2433   return $out;
  2464 }
  2434 }
  2465 
  2435 
  4615 
  4585 
  4616 /**
  4586 /**
  4617  * Logs something in the profiler.
  4587  * Logs something in the profiler.
  4618  * @param string Point name or message
  4588  * @param string Point name or message
  4619  * @param bool Optional. If true (default), a backtrace will be generated and added to the profiler data. False disables this, often for security reasons.
  4589  * @param bool Optional. If true (default), a backtrace will be generated and added to the profiler data. False disables this, often for security reasons.
  4620  */
  4590  * @param resource Optional. If specified, bases the time difference off of this event instead of the previous event/
  4621 
  4591  * @return resource Event ID
  4622 function profiler_log($point, $allow_backtrace = true)
  4592  */
       
  4593 
       
  4594 function profiler_log($point, $allow_backtrace = true, $parent_event = false)
  4623 {
  4595 {
  4624   if ( !defined('ENANO_DEBUG') )
  4596   if ( !defined('ENANO_DEBUG') )
  4625     return false;
  4597     return false;
  4626   
  4598   
  4627   global $_profiler;
  4599   global $_profiler;
  4632   }
  4604   }
  4633   $_profiler[] = array(
  4605   $_profiler[] = array(
  4634       'point' => $point,
  4606       'point' => $point,
  4635       'time' => microtime_float(),
  4607       'time' => microtime_float(),
  4636       'backtrace' => $backtrace,
  4608       'backtrace' => $backtrace,
  4637       'mem' => false
  4609       'mem' => false,
       
  4610       'parent_event' => $parent_event
  4638     );
  4611     );
  4639   if ( function_exists('memory_get_usage') )
  4612   if ( function_exists('memory_get_usage') )
  4640   {
  4613   {
  4641     $_profiler[ count($_profiler) - 1 ]['mem'] = memory_get_usage();
  4614     $_profiler[ count($_profiler) - 1 ]['mem'] = memory_get_usage();
  4642   }
  4615   }
  4643   return true;
  4616   return count($_profiler) - 1;
       
  4617 }
       
  4618 
       
  4619 /**
       
  4620  * Insert a message (an event without any time data) into the profiler.
       
  4621  * @param string Message
       
  4622  */
       
  4623 
       
  4624 function profiler_message($message)
       
  4625 {
       
  4626   if ( !defined('ENANO_DEBUG') )
       
  4627     return false;
       
  4628   
       
  4629   global $_profiler;
       
  4630   
       
  4631   $_profiler[] = array(
       
  4632       'message' => $message,
       
  4633     );
  4644 }
  4634 }
  4645 
  4635 
  4646 /**
  4636 /**
  4647  * Returns the profiler's data (so far).
  4637  * Returns the profiler's data (so far).
  4648  * @return array
  4638  * @return array
  4673   foreach ( $profile as $i => $entry )
  4663   foreach ( $profile as $i => $entry )
  4674   {
  4664   {
  4675     // $time_since_last = $entry['time'] - $time_last;
  4665     // $time_since_last = $entry['time'] - $time_last;
  4676     // if ( $time_since_last < 0.01 )
  4666     // if ( $time_since_last < 0.01 )
  4677     //   continue;
  4667     //   continue;
       
  4668     
       
  4669     if ( isset($entry['message']) )
       
  4670     {
       
  4671       $html .= "<!-- ########################################################## -->\n<tr>\n  <th colspan=\"2\">Message $i</th>\n</tr>";
       
  4672       
       
  4673       $html .= '<tr>' . "\n";
       
  4674       $html .= '  <td class="row2">Message:</td>' . "\n";
       
  4675       $html .= '  <td class="row1">' . htmlspecialchars($entry['message']) . '</td>' . "\n";
       
  4676       $html .= '</tr>' . "\n";
       
  4677       continue;
       
  4678     }
  4678     
  4679     
  4679     $html .= "<!-- ########################################################## -->\n<tr>\n  <th colspan=\"2\">Event $i</th>\n</tr>";
  4680     $html .= "<!-- ########################################################## -->\n<tr>\n  <th colspan=\"2\">Event $i</th>\n</tr>";
  4680     
  4681     
  4681     $html .= '<tr>' . "\n";
  4682     $html .= '<tr>' . "\n";
  4682     $html .= '  <td class="row2">Event:</td>' . "\n";
  4683     $html .= '  <td class="row2">Event:</td>' . "\n";
  4688     $html .= '<tr>' . "\n";
  4689     $html .= '<tr>' . "\n";
  4689     $html .= '  <td class="row2">Time since start:</td>' . "\n";
  4690     $html .= '  <td class="row2">Time since start:</td>' . "\n";
  4690     $html .= '  <td class="row1">' . $time . 's</td>' . "\n";
  4691     $html .= '  <td class="row1">' . $time . 's</td>' . "\n";
  4691     $html .= '</tr>' . "\n";
  4692     $html .= '</tr>' . "\n";
  4692     
  4693     
       
  4694     $time_label = 'Time since last event:';
       
  4695     if ( $entry['parent_event'] && is_int($entry['parent_event']) && isset($profile[$entry['parent_event']]) )
       
  4696     {
       
  4697       $time_last = $profile[$entry['parent_event']]['time'];
       
  4698       $time_label = "Time since event #{$entry['parent_event']}:";
       
  4699     }
       
  4700     
  4693     $time = $entry['time'] - $time_last;
  4701     $time = $entry['time'] - $time_last;
  4694     if ( $time < 0.0001 )
  4702     if ( $time < 0.0001 )
  4695       $time = 'Marginal';
  4703       $time_html = 'Marginal';
  4696     else
  4704     else
  4697       $time = "{$time}s";
  4705       $time_html = number_format($time, 6) . "s";
  4698     
  4706     
       
  4707     if ( $time > 0.02 )
       
  4708       $time_html = "<span style=\"background-color: #a00; padding: 4px; color: #fff; font-weight: bold;\">$time_html</span>";
       
  4709       
  4699     $html .= '<tr>' . "\n";
  4710     $html .= '<tr>' . "\n";
  4700     $html .= '  <td class="row2">Time since last event:</td>' . "\n";
  4711     $html .= '  <td class="row2">' . $time_label . '</td>' . "\n";
  4701     $html .= '  <td class="row1">' . $time . '</td>' . "\n";
  4712     $html .= '  <td class="row1">' . $time_html . '</td>' . "\n";
  4702     $html .= '</tr>' . "\n";
  4713     $html .= '</tr>' . "\n";
  4703     
  4714     
  4704     if ( $entry['backtrace'] )
  4715     if ( $entry['backtrace'] )
  4705     {
  4716     {
  4706       $html .= '<tr>' . "\n";
  4717       $html .= '<tr>' . "\n";
  4901   if ( $dh = opendir(ENANO_ROOT . '/cache') )
  4912   if ( $dh = opendir(ENANO_ROOT . '/cache') )
  4902   {
  4913   {
  4903     $cache->purge('page_meta');
  4914     $cache->purge('page_meta');
  4904     $cache->purge('anon_sidebar');
  4915     $cache->purge('anon_sidebar');
  4905     $cache->purge('plugins');
  4916     $cache->purge('plugins');
       
  4917     $cache->purge('wiki_edit_notice');
  4906     
  4918     
  4907     $data_files = array(
  4919     $data_files = array(
  4908         'aes_decrypt.php',
  4920         'aes_decrypt.php',
  4909         // ranks cache is stored using a custom engine (not enano's default cache)
  4921         // ranks cache is stored using a custom engine (not enano's default cache)
  4910         'cache_ranks.php'
  4922         'cache_ranks.php'