source: wiki-plugin/trunk/wiki_plugin.php @ 48212

Revision 48212, 8.1 KB checked in by fozylet, 4 years ago (diff)

v 1.1 release

Line 
1<?php
2/*
3Plugin Name: GetWIKI
4Version: 1.1
5Plugin URI: http://saj.in/blog/plugins
6Author: Sajin Kunhambu
7Author URI: http://saj.in/
8Description: Get a WIKI article anywhere on yout blog (e.g. ~GetWIKI(Your_Search_Term)~ )
9*/
10
11
12function cleanUp( $article ) {
13    global $getwiki_settings;
14    $article = str_replace("\n","",$article);
15    if(preg_match("@(?<content>\<\!\-\- start content \-\-\>.*\<\!\-\- end content \-\-\>)@i",$article,$match)!=0) $article = $match[content];
16//print "[[[".$article."]]]";die();
17    $article = preg_replace("#\<\!\-\-.*\-\-\>#imseU","",$article);
18    $article = preg_replace("#\[\!\&\#.*\]#imseU","",$article);
19    if(!$getwiki_settings['show_retrieved']) $article = preg_replace("#\<div\sclass=\"printfooter\".*\<\/div\>#imseU","",$article);
20    if(!$getwiki_settings['show_edit']) $article = preg_replace("#\s*\<div\s*class=\"editsection\".*\<\/div\>\s*#imseU","",$article);
21    if(!$getwiki_settings['show_edit']) $article = preg_replace("#\s*\<span\s*class=\"editsection\".*\<\/span\>\s*#imseU","",$article);
22    $article = addHost( $article, "/w/" );
23    $article = addHost( $article, "/wiki/" );
24    $article = addHost( $article, "/skins-1.5/" );
25    $article = "<div class=\"wiki\">".$article.$getwiki_settings['copyleft']."</div>";
26    return $article;
27}
28
29function addHost( $article, $keyword )
30{
31    global $getwiki_settings;
32    return str_replace($keyword,"http://".$getwiki_settings['host'].$keyword,$article);
33}
34
35function getArticleFromHost( $title ) {
36    global $getwiki_settings;
37    if($use_cache) {
38                if(!function_exists('cache_recall')) return("Cache not installed");
39        $function_string = "getArticle(".$title.")";
40        if($article = cache_recall($function_string,$getwiki_settings['cache_life'])) return $article;
41    }
42    $out = "GET ".$getwiki_settings['path'].$title." HTTP/1.0\r\nHost: ".$getwiki_settings['host']."\r\nUser-Agent: GetWiki for WordPress\r\n\r\n";
43    $fp = fsockopen($getwiki_settings['host'], $getwiki_settings['port'], $errno, $errstr, 30);
44    fwrite($fp, $out);
45    $article = "";
46    while (!feof($fp)) {
47        $article .= fgets($fp, 128);
48    }
49    if(substr($article,0,12)=="HTTP/1.0 301")
50    {
51        if(preg_match("/^.*Location\:\s(\S*).*$/im",$article,$match)!=0) {
52            $article = str_replace("http://en.wikipedia.org/wiki/","",$match[1]);
53            $article = getArticleFromHost( $article );
54        } else {
55            $article = "== WIKI Error ==";
56        }
57    }
58    fclose($fp);
59        $article = cleanUp($article);
60    if($use_cache) cache_store($function_string,$article);
61    return $article;
62}
63
64function getArticle( $title ) {
65    return getArticleFromHost( $title );
66}
67
68function wikify( $text ) {
69    $text = preg_replace(
70        "#\~GetWIKI\((\S*)\,(\S*)\)\~#imseU",
71        "getArticleFromHost('$1','$2')",
72        $text
73    );
74    $text = preg_replace(
75        "#\~GetWIKI\((\S*)\)\~#imseU",
76        "getArticle('$1')",
77        $text
78    );
79    return $text;
80}
81
82function wiki_css() {
83    echo "
84    <style type='text/css'>
85    div.wiki {
86        border: 1px dashed silver;
87        background-color: #f0f0f0;
88    }
89    div.gfdl {
90        font-size: 80%;
91    }
92    </style>
93    ";
94}
95
96
97//Options Section - Begin
98$copyleft = "<div class=\"gfdl\">&copy; This material from <a href=\"http://en.wikipedia.org\">Wikipedia</a> is licensed under the <a href=\"http://www.gnu.org/copyleft/fdl.html\">GFDL</a>.</div>";
99if(!get_option('getwiki_settings')) {
100  $getwiki_settings = array(
101          'host' => "en.wikipedia.org",
102          'path' => "/wiki/",
103          'port' => 80,
104          'cache' => (function_exists(cache_recall) || function_exists(cache_store)),
105          'cache_life' => 10080,
106          'show_edit' => false,
107          'show_retrieved' => false,
108          'copyleft' => $copyleft
109  );
110        add_option('getwiki_settings', $getwiki_settings);
111} else {
112  $getwiki_settings = get_option('getwiki_settings');
113  $getwiki_settings['copyleft'] = $copyleft;
114}
115
116if( !function_exists(cache_recall) || !function_exists(cache_store) ) {
117        // caching function not available
118        $getwiki_settings['cache'] = false;
119}
120
121function getwiki_add_page()
122{
123     add_options_page("GetWIKI Options", "GetWIKI", 8, "wiki-plugin", getwiki_options);
124}
125
126function getwiki_options() {
127        global $getwiki_settings,$copyleft;
128        if( isset( $_POST['update_options'] ) )
129        {
130    $getwiki_settings = $_POST['getwiki_settings'];
131    $getwiki_settings['copyleft'] = $copyleft;
132                update_option('getwiki_settings', $_POST['getwiki_settings']);
133        }
134        ?>
135<div class="wrap">
136  <h2>GetWIKI Options</h2>
137  <form name="getwiki_form" method="post">
138    <fieldset class="options">
139      <legend>Server</legend>
140      <table width="100%" cellspacing="2" cellpadding="5" class="editform">
141        <tr valign="top">
142          <th width="45%" scope="row">WIKI Domain (en.wikipedia.org)</th>
143          <td>
144            <input type="text" name="getwiki_settings[host]" value="<?php echo $getwiki_settings['host'] ?>" size="25" />
145          </td>
146        </tr>
147        <tr valign="top">
148          <th width="45%" scope="row">Path (/path/)</th>
149          <td>
150            <input type="text" name="getwiki_settings[path]" value="<?php echo $getwiki_settings['path'] ?>" size="25" />
151          </td>
152        </tr>
153        <tr valign="top">
154          <th width="45%" scope="row">Port (80)</th>
155          <td>
156            <input type="text" name="getwiki_settings[port]" value="<?php echo $getwiki_settings['port'] ?>" size="5" maxlength="5" />
157          </td>
158        </tr>
159      </table>
160    </fieldset>
161    <fieldset class="options">
162      <legend>Cache</legend>
163      <table width="100%" cellspacing="2" cellpadding="5" class="editform">
164        <tr valign="top">
165          <th width="45%" scope="row">Enable Cache</th>
166          <td>
167            <select size="1" name="getwiki_settings[cache]">
168              <option value="1"
169                <?php if($getwiki_settings['cache']==true) echo "selected"; ?>>True
170              </option>
171              <option value="0"
172                <?php if($getwiki_settings['cache']==false) echo "selected"; ?>>False
173              </option>
174            </select>
175          </td>
176        </tr>
177        <tr valign="top">
178          <th width="45%" scope="row">Cache Lifetime</th>
179          <td>
180            <input type="text" name="getwiki_settings[cache_life]" value="<?php echo $getwiki_settings['cache_life'] ?>" size="4" />
181          </td>
182        </tr>
183      </table>
184    </fieldset>
185    <fieldset class="options">
186      <legend>Presentation</legend>
187      <table width="100%" cellspacing="2" cellpadding="5" class="editform">
188        <tr valign="top">
189          <th width="45%" scope="row">Show 'Edit' Links</th>
190          <td>
191            <select size="1" name="getwiki_settings[show_edit]">
192              <option value="1"
193                <?php if($getwiki_settings['show_edit']==true) echo "selected"; ?>>True
194              </option>
195              <option value="0"
196                <?php if($getwiki_settings['show_edit']==false) echo "selected"; ?>>False
197              </option>
198            </select>
199          </td>
200        </tr>
201        <tr valign="top">
202          <th width="45%" scope="row">Show 'Retrieved' Links</th>
203          <td>
204            <select size="1" name="getwiki_settings[show_retrieved]">
205              <option value="1"
206                <?php if($getwiki_settings['show_retrieved']==true) echo "selected"; ?>>True
207              </option>
208              <option value="0"
209                <?php if($getwiki_settings['show_retrieved']==false) echo "selected"; ?>>False
210              </option>
211            </select>
212          </td>
213        </tr>
214      </table>
215    </fieldset>
216    <input type="submit" name="update_options" value="Update Options" />
217  </form>
218  <br /><br />
219  Notice Shown: <?php echo $getwiki_settings['copyleft']?>
220</div>
221<?php
222}
223//Options Section - End
224
225
226//Hooks Section - Begin
227add_action('admin_menu', 'getwiki_add_page');
228add_action('wp_head', 'wiki_css');
229add_filter('the_content', 'wikify', 2);
230add_filter('the_excerpt', 'wikify', 2);
231//Hooks Section - End
232?>
Note: See TracBrowser for help on using the repository browser.