Wordpress queries and wikipedia

Fragment of a discussion from Project:Support desk
Jump to: navigation, search
Edited by author.
Last edit: 17:04, 27 September 2011
  • Use whatever method you know (or will learn) to get the information from WordPress (ask WordPress support)
  • Use MediaWiki's API on http://en.wikipedia.org/w/api.php to get information about it. Or if you want to just get the article, fetch URLs like: http://en.wikipedia.org/w/index.php?title=Email&action=render (render = just article without header/footer/sidebar etc.).
  • Then in PHP (getWikiIntroParagraph) extract the first one (or two) <p>-tags. And with what you have left, use strip_tags() to get rid of other unwanted html code, and then you have an clear-text excerpt.:

Electronic mail, commonly called email or e-mail, is a method of exchanging digital messages from an author to one or more recipients. Modern email operates across the Internet or other computer networks.

Krinkle01:18, 8 September 2011

Thanks, Krinkle.

If I have more than one tag in wordpress how can I solve this problem that not all of them should be included in an URL like this: http://en.wikipedia.org/w/index.php?title=Email&action=render

And instead of "Email" how do I need to include $tag for example? Like this? http://en.wikipedia.org/w/index.php?title=.$tag.&action=render

Thanks a lot!

85.22.26.14012:19, 12 September 2011
 
Edited by author.
Last edit: 03:26, 17 September 2011

Well, assuming you have an array of tags that you fetched from WordPress (get_the_tags()), something like this:

function getWikiIntroParagraph( $title ) {
 $url = 'http://en.wikipedia.org/w/index.php?' . http_build_query(array( 'title' => $title, 'action' => 'raw' ));
 // Download $url and do your magic extraction
 return $intro;
}
 
$posttags = get_the_tags();
if ($posttags) {
  echo '<ul>';
  foreach($posttags as $tag) {
    echo '<li><strong>' . htmlspecialchars( $tag->name ) . '</strong>: ' . getWikiIntroParagraph( $tag->name ); 
  }
  echo '</ul>';
}
Krinkle19:16, 12 September 2011

I have this right now

<?php
$posttags = get_the_tags();
if ($posttags) {
  foreach($posttags as $tag) {
    echo $tag->name . ' '; 
  }
}
function getWikiIntroParagraph( $title ) {
 $url = 'http://en.wikipedia.org/w/index.php?' . build_http_query(array( 'title' => $title, 'action' => 'raw' ));
 // Download $url and do your magic extraction
 return $intro;
}
 
$posttags = get_the_tags();
if ($posttags) {
  echo '<ul>';
  foreach($posttags as $tag) {
    echo '<li><strong>' . htmlspecialchars( $tag ) . '</strong>: ' . getWikiIntroParagraph( $tag->name ); 
  }
  echo '</ul>';
}
?>

But it says htmlspecialchars() expects parameter 1 to be string, object given on line 41 in single.php and Fatal error: Call to undefined function build_http_query() on 32, same file. Guess it's wrong what I did, isn't it?

79.245.201.14321:24, 13 September 2011
 

@Krinkle Can you give me details on the error for build http query? Solved the htmlspecialchars error wth some help but don't know much about PHP or anything else in programming to solve the build http query problem. Best!

79.245.201.14318:14, 14 September 2011
 

I mistyped build_http_query for http_build_query. And $tag should be $tag-name, just like the others. See my updated example

Krinkle03:28, 17 September 2011

Thanks. Returns now just

-Elements (e.g. Tag names) as a list without wikipedia excerpt. Any idea? Now looks like this:
  • Tag1:
  • Tag2:
Now looks like this:
function getWikiIntroParagraph( $title ) {
 $url = 'http://en.wikipedia.org/w/index.php?' . http_build_query(array( 'title' => $title, 'action' => 'raw' )) . $tag . '&action=render';
 // Download $url and do your magic extraction
 return $intro;
}
 
$posttags = get_the_tags();
if ($posttags) {
  echo '<ul>';
  foreach($posttags as $tag) {
    echo '<li><strong>' . htmlspecialchars( $tag->name ) . '</strong>: ' . getWikiIntroParagraph( $tag->name ); 
  }
  echo '</ul>';
}

Didn't get any other result, just a list of the tag names without an excerpt. If I put $url instead of $url it shows the the url in plain text. Guess I need to define the $intro, right? Hav no idea how to do so.

85.22.20.409:59, 27 September 2011
 

I'm not going to write it for you. In the previous comments I explained how $intro would be defined (obviously it's not defined right now, that's up to you) where it reads " // Download $url and do your magic extraction".

Then in PHP (getWikiIntroParagraph) extract the first one (or two) <p>-tags. And with what you have left, use strip_tags() to get rid of other unwanted html code, and then you have an clear-text excerpt.

Krinkle17:03, 27 September 2011

I know, but I do need further explanation or better the parts I need to use for what is left to do. I am not into php so need to be pointed to each function or whatever I need to have, you know? I just know it is not defined but cannot think about a function or whatever to get what I want. Is $intro the only thing what's left? If so, what do I need to make run? If not, what else and what do I need? As I said I am not into php and need more info. Sorry.


//EDIT

I guess it's only $intro I need to define right now. Do I have to make it within function get WikiIntroParagraph or before / after that? As far as I understand this function extracts anything from the page. Do I have to use truncate to get a summary or is there something else I can use?

79.245.215.17603:36, 28 September 2011
 

I have tried something but doesn't function. Do have any idea(s)? http://pastebin.com/fkLBeViY

85.22.0.20909:51, 30 September 2011
 
 
 
 
Personal tools

Variants
Actions
Navigation
Support
Download
Development
Communication
Toolbox