Advertisement
Guest User

wiki

a guest
Aug 31st, 2011
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.03 KB | None | 0 0
  1. <?php
  2.  /**
  3.  * required functions are here
  4.  *
  5.  * @package wp-wikibox
  6.  */
  7.     /**
  8.      *  wikibox_summary function
  9.      *  gets and optionally echoes a box with summary from wikipedia
  10.     */
  11.     function wikibox_summary( $keyword, $lang = 'en', $echo = true ) {
  12.         global $wikibox_options;
  13.        
  14.         $article = wikibox_get_summary( $keyword, $lang );
  15.        
  16.         if ( $article ) {
  17.             // prepare keyword title
  18.             $keyword = str_replace( ' ', '_', $article['title'] );
  19.             // add wikipedia copyright notice
  20.             $copyright = '<p class="wikiright">';
  21.             // include wikibox link
  22.             if ( $wikibox_options['include_bslink'] == 'yes' ) {
  23.                 $copyright .= '<a href="http://bsurprised.com/2010/09/wp-wikibox-wordpress-plugin/" title="Powered by BSurprised WP-WikiBox"><img src="' . get_settings( 'siteurl' ) . WIKIBOX_WEBDIR . '/images/wb.png" alt="Powered by BSurprised WP-WikiBox" style="border-width:0" title="Powered by BSurprised WP-WikiBox" /></a> ';
  24.             }
  25.             $copyright .= '<a href="http://creativecommons.org/licenses/by-sa/3.0/" title="Wikipedia article licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License"><img src="' . get_settings( 'siteurl' ) . WIKIBOX_WEBDIR . '/images/cc.png" alt="Creative Commons License" style="border-width:0" title="Wikipedia article licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License" /></a> ';
  26.             $copyright .= sprintf( __( 'From <a rel="nofollow" title="Wikipedia" href="http://%s.wikipedia.org/">Wikipedia</a>, the free encyclopedia', 'wikibox' ), $lang );
  27.             // include view original link
  28.             if ( $wikibox_options['include_originallink'] == 'yes' ) {
  29.                 $copyright .= " <a rel=\"nofollow\" class=\"wikilink\" title=\"" . __( 'View on Wikipedia', 'wikibox' ) . "\" href=\"http://$lang.wikipedia.org/wiki/$keyword\">" . __( '[+]', 'wikibox' ) . "</a>";
  30.             }
  31.             // include edit link
  32.             if ( $wikibox_options['include_editlink'] == 'yes' ) {
  33.                 $copyright .= " <a rel=\"nofollow\" class=\"wikilink\" title=\"" . __( 'Edit Article', 'wikibox' ) . "\" href=\"http://$lang.wikipedia.org/w/index.php?title=$keyword&action=edit\">" . __( '[edit]', 'wikibox' ) . "</a>";
  34.             }
  35.             $copyright .= '</p>';
  36.             $header_meta = false;
  37.             $box = '<div class="wikibox">';
  38.             // title and copyright
  39.             if ( $wikibox_options['include_title'] == 'yes' ) {
  40.                 $box .= "<h3 class=\"wikititle\">$article[title]</h3>";
  41.                 $box .= $copyright;
  42.                 $header_meta = true;
  43.             }
  44.             //add content
  45.             $box .= $article['content'];
  46.             // include more link
  47.             if ( $wikibox_options['include_morelink'] == 'yes' ) {
  48.                 $last_tag = strrpos( $box, '</' );
  49.                 $box = substr_replace( $box, " <span class=\"wikimore\"><a rel=\"nofollow\" class=\"wikilink\" title=\"" . __( 'Read More', 'wikibox' ) . "\" href=\"http://$lang.wikipedia.org/wiki/$keyword\">" . __( '[...]', 'wikibox' ) . "</a></span>", $last_tag, 0);
  50.             }
  51.             // no title so include the copyright in footer
  52.             if ( ! $header_meta ) {
  53.                 $box .= $copyright;
  54.             }
  55.             $box .= '</div>';
  56.            
  57.             if ( $echo ) echo $box; else return $box;
  58.         }
  59.     } // end wikibox_summary
  60.  
  61.     /**
  62.      *  wikibox_get_summary function
  63.      *  retrieves and refines a page summary from wikipedia
  64.     */
  65.     function wikibox_get_summary( $keyword, $lang = 'en' ) {
  66.         global $wikibox_options;
  67.        
  68.         //add wiki underscores instead of spaces
  69.         $keyword = urlencode( str_replace( ' ', '_', $keyword ) );
  70.        
  71.         // check whick version of caching we have and return it if any
  72.         if ( $wikibox_options['cache_type'] == 'transient' ) {
  73.             if ( false !== ( $transient = get_transient( WIKIBOX_PREFIX . substr( $keyword, 0, 25 ) ) ) ) {
  74.                 // return the transient, we have this content
  75.                 return $transient;
  76.             }
  77.         } else if ( $wikibox_options['cache_type'] == 'file' ) {
  78.             $cache = new wikibox_cache( ABSPATH . WIKIBOX_WEBDIR . '/cache' );
  79.             $file_data = $cache->get( $keyword, $wikibox_options['cache_timespan'] );
  80.             if ( $file_data !== false ) {
  81.                 // return the data, we have this content
  82.                 return $file_data;
  83.             }
  84.         }
  85.        
  86.         // build the page name url
  87.         $file = sprintf( WIKIBOX_RAW_URL , $lang, $keyword );
  88.        
  89.         // get the page
  90.         $result = wikibox_get_data( $file );
  91.  
  92.         if ( $result !== false ) {
  93.             if ( substr( $result, 0, strlen( __( "Error", "wikibox" ) ) ) == __( "Error", "wikibox" ) ) return array( 'title' => 'Error', 'content' => $result );
  94.            
  95.             // load the result string
  96.             $xml = @simplexml_load_string( $result );  //or die ( __( "Unable to load Wikipedia XML string!", "wikibox" ) );
  97.            
  98.             if ( $xml ) {
  99.                 $title = (string)$xml->query->pages->page['title'];
  100.                 $content = $xml->query->pages->page->revisions->rev;
  101.                 $headstart = explode( "\n==", $content );
  102.                
  103.                 $summary = $headstart[0];
  104.                
  105.                 //echo '||'.$summary.'||';
  106.                
  107.                 // recursive remove boxes
  108.                 $summary = preg_replace( "/\{\{((?>[^{{}}]+)|(?R))*\}\}/", "", $summary );
  109.  
  110.                 // remove refs as they will not have ref list
  111.                 $summary = preg_replace( "#\<ref.*\<\/ref\>#imseU", "", $summary );
  112.                 $summary = preg_replace( "#\<ref.*\/\>#imseU", "", $summary );
  113.                 // remove remained dummy \n \t etc.
  114.                 $summary = trim( $summary );
  115.            
  116.                 if ( $summary != '' ) {
  117.                     $file = sprintf( WIKIBOX_PARSE_URL , $lang );
  118.                     // parse using wiki template engine, this one sends the text in POST as url may not handle the length
  119.                     $result = wikibox_get_data( $file, true, array( 'text' => $summary ) );
  120.                    
  121.                     if ( $result !== false ) {
  122.                         if ( substr( $result, 0, strlen( __( "Error", "wikibox" ) ) ) == __( "Error", "wikibox" ) ) return array( 'title' => 'Error', 'content' => $result );
  123.                        
  124.                         // load the result string
  125.                         $xml = @simplexml_load_string( $result ); //or die ( __( "Unable to load Wikipedia parsed XML string!", "wikibox" ) );
  126.                        
  127.                         if ( $xml ) {
  128.                             $content = $xml->parse->text;
  129.                            
  130.                             //some cleanup and url repairing
  131.                             $content = wikibox_clean_html ( $content );
  132.                             // correcting the wiki urls and add http host
  133.                             $content = wikibox_repair_urls ( $content, $lang );
  134.                            
  135.                             // add transient for a day to avoid calling wikipedia too much
  136.                             if ( $content != '' && $wikibox_options['cache_type'] == 'transient' ) set_transient( WIKIBOX_PREFIX . substr( $keyword, 0, 25 ), array( 'title' => $title, 'content' => $content ), $wikibox_options['cache_timespan'] );
  137.                             if ( $content != '' && $wikibox_options['cache_type'] == 'file' ) { $cache->set( $keyword, array( 'title' => $title, 'content' => $content ) ); }
  138.                            
  139.                             //echo $content;
  140.                             return array( 'title' => $title, 'content' => $content );
  141.                             //} else {
  142.                             //  return __( 'The keyword did not match any Wikipedia page title.', 'wikibox' );
  143.                             //}
  144.                         }
  145.                     }
  146.                 }
  147.             }
  148.         }
  149.     } // end wikibox_get_summary
  150.    
  151.     /**
  152.      *  wikibox_clean_html function
  153.      *  cleans unwanted things, this can help preventing regex from acting strangely
  154.     */
  155.     function wikibox_clean_html( $str ) {
  156.         global $wikibox_options;
  157.         $str = preg_replace( "/([\t\n]|\(\)|\( \)|\(\, \))/", "", $str );
  158.         $str = str_replace( "<p><br />", "<p>", $str );
  159.         // remove html comments
  160.         $str = preg_replace( "#\<\!\-\-.*\-\-\>#imseU", "", $str );
  161.         // strip images
  162.         if ( $wikibox_options['include_images'] != 'yes' ) {
  163.             $str = preg_replace( "#<div class=\"thumb\b[^>]*>(.*?)<\/div>#imseU", "", $str );
  164.         }
  165.         if ( $wikibox_options['include_links'] != 'yes' ) {
  166.             $str = strip_tags( $str, '<p><span><b><i><u><em><strong><strike><small><ul><li><ol><dl><dd><dt><code><pre><blockquote><cite><abbr><acronym><br>' );
  167.         }
  168.         return $str;
  169.     } // end wikibox_clean_html
  170.    
  171.     /**
  172.      *  wikibox_repair_urls function
  173.      *  adds wikipedia host to urls
  174.     */
  175.     function wikibox_repair_urls( $str, $lang ) {
  176.         global $wikibox_options;
  177.         $str = str_replace( '"/w/', '"http://' . "$lang.wikipedia.org/w/", $str );
  178.         $str = str_replace( '"/wiki/', '"http://' . "$lang.wikipedia.org/wiki/", $str );
  179.         if ( $wikibox_options['add_nofollow'] == 'yes' ) {
  180.             $str = str_replace( 'href=', 'rel="nofollow" href=', $str );
  181.         }
  182.         return $str;
  183.     } // end wikibox_repair_urls
  184.    
  185.     /**
  186.      *  wikibox_get_data function
  187.      *  creates a stream context to retrieve data and returns it
  188.     */
  189.     function wikibox_get_data( $url, $post = false, $post_content='' ) {
  190.         global $wikibox_options;
  191.        
  192.         try {
  193.             if( function_exists( 'curl_init' ) ) {
  194.                 $curl = curl_init(); // init the curl
  195.                 curl_setopt( $curl, CURLOPT_HTTPHEADER, array( "Cache-Control: max-age=0",
  196.                                                              "Connection: keep-alive",
  197.                                                              "Keep-Alive: 300",
  198.                                                              "Accept-Charset: ISO-8859-1,utf-8",
  199.                                                              "Accept-Language: $wikibox_options[language]",
  200.                                                              "Pragma: " ) );
  201.                 curl_setopt( $curl, CURLOPT_URL, $url );
  202.                 curl_setopt( $curl, CURLOPT_USERAGENT, WIKIBOX_AGENT );
  203.                 curl_setopt( $curl, CURLOPT_ENCODING, 'gzip,deflate' );
  204.                 curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
  205.                 curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 120 );
  206.                 curl_setopt( $curl, CURLOPT_TIMEOUT, 120 );
  207.                 curl_setopt( $curl, CURLOPT_MAXREDIRS, 5 );
  208.                 if ( $post ) {
  209.                     curl_setopt ( $curl, CURLOPT_POST, 1 );
  210.                     curl_setopt ( $curl, CURLOPT_POSTFIELDS, http_build_query( $post_content ) );
  211.                 }
  212.                 $result = curl_exec( $curl ); // execute the curl command
  213.                 curl_close( $curl ); // close the connection
  214.                
  215.             } else {
  216.                
  217.                 // create a stream to add user agent to request, as wiki requires a valid descriptive user agent
  218.                 if ( $post ) {
  219.                     $opts = array(
  220.                               'http'=>array(
  221.                                         'method' => "POST",
  222.                                         'header' => "Accept-language: $wikibox_options[language]\r\n" .
  223.                                                     ( ($wikibox_options['enable_gzip'] === 'yes') ? "Accept-Encoding: gzip\r\n" : "" ) .
  224.                                                     "User-Agent: " . WIKIBOX_AGENT . "\r\n",
  225.                                         'content' => http_build_query( $post_content )
  226.                                         )
  227.                             );
  228.                 } else {
  229.                     $opts = array(
  230.                               'http' => array(
  231.                                         'method' => "GET",
  232.                                         'header' => "Accept-language: $wikibox_options[language]\r\n" .
  233.                                                     ( ($wikibox_options['enable_gzip'] === 'yes') ? "Accept-Encoding: gzip\r\n" : "" ) .
  234.                                                     "User-Agent: " . WIKIBOX_AGENT . "\r\n"
  235.                                         )
  236.                             );
  237.                 }
  238.                 $context = stream_context_create( $opts );
  239.                
  240.                 // get the page
  241.                 $result = file_get_contents( $url, false, $context );
  242.                 //uncompress the result if we are using gzip, be sure to strip the first 10 bytes
  243.                 if ( $wikibox_options['enable_gzip'] === 'yes' ) $result = gzinflate( substr( $result, 10 ) );
  244.             }
  245.            
  246.         } catch ( Exception $e ) {
  247.             echo $result = __( "Error", "wikibox" ) . ': ' . __( "Failed to contact Wikipedia server.", "wikibox" );
  248.         }
  249.        
  250.         return $result;
  251.        
  252.     } // end wikibox_get_data
  253.    
  254.     /**
  255.      *  wikibox_delete_cache function
  256.      *  deletes transient data on editing options
  257.     */
  258.     function wikibox_delete_cache() {
  259.         global $wpdb, $wikibox_options;
  260.         if ( $wikibox_options['cache_type'] == 'transient' ) {
  261.             $query = "DELETE FROM $wpdb->options WHERE option_name LIKE '_transient%wikibox%'";
  262.             $wpdb->query( $query );
  263.         } else if ( $wikibox_options['cache_type'] == 'file' ) {
  264.             clearstatcache();
  265.             $dir = ABSPATH . WIKIBOX_WEBDIR . '/cache/';
  266.             if ( $dh = opendir( $dir ) ) {
  267.                 while ( ( $file = readdir( $dh ) ) !== false ) {
  268.                     unlink ( $dir . $file );
  269.                 }
  270.                 closedir( $dh );
  271.             }
  272.         }
  273.     } // end wikibox_delete_cache
  274.  
  275.     /**
  276.      *  wikibox_lang_listbox function
  277.      *  load wikipedia language list into an html options list
  278.     */
  279.     function wikibox_lang_listbox( $name, $echo = true ) {
  280.         global $wikibox_options;
  281.         // load the xml file
  282.         $xml = simplexml_load_file( ABSPATH . WIKIBOX_WEBDIR . '/languages/wiki-languages.xml' )  or die ( __( "Unable to load Language XML file!", "wikibox" ) );
  283.        
  284.         $html = '';
  285.         $cur_lang = $wikibox_options['language'];
  286.         if ( $xml ) {
  287.             $html = "<select name=\"$name\">";
  288.             foreach ( $xml as $lang ) {
  289.                 $html .= "<option value=\"$lang->code\"" . ( $cur_lang == $lang->code ? ' selected="selected"' : '' ) . ">$lang->name</option>";
  290.             }
  291.             $html .= '</select>';
  292.         }
  293.         if ( $echo ) echo $html; else return $html;
  294.     } // end wikibox_lang_listbox
  295.    
  296. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement