Topic on Project:Support desk

Getting article content using Title ?

5
Anyhowputx (talkcontribs)

Hello all,

I would like to ask how do I get the article content using the Title class and display the content on my main page.


Currently, I have to first retrieve the recently created articles then get the content of those title. I am using newest page extension (https://www.mediawiki.org/wiki/Extension:Newest_Pages) to do so. However, I tried editing the file but fail because I am echo-ing out the content instead of displaying on the main page.

$article = new Article( Title::newFromText( $title ) ) ;
echo $article->getContent();

How should I do that? I am thinking of parser function. But I am not sure if I am on the right track.

Thanks for all assistance and time!

88.130.101.155 (talkcontribs)

What you say basically is right: $article->getContent(); will contain the actual article text.

I guess that you are somehow editing this file here, right: https://git.wikimedia.org/blob/mediawiki%2Fextensions%2FNewestPages/1547d9f6891e2c5831d315d6d6d3ed2e4301a882/NewestPages.page.php?

I have not yet tested, how exactly that extension is working, but reading the code, I guess that inside function execute(), where the code deals with $wgOut, that this would be where you can somehow add the actual article text (instead of letting the code create the link, which it would normally create). Maybe something like

$article = new Article( Title::newFromText( $title ) );
$articleText = $article->getContent();
$wgOut->addWikiText( $articleText );

works the way you want...

2401:7400:C800:32AC:A9CE:A9EF:A862:5EA8 (talkcontribs)

Hello there,

Thanks for giving me the direction to start. Yes you are right, I am editing the NewestPages.page.php file

I have created a new function to retrieve the content and it works fine

<?php
function showArticleText ($row)
	{
		$titleText = Title::newFromText($row->page_title);
		if (!$titleText) return '<h1>Could not retrieve this Article Page.</h1>';
		$page = $titleText->getFullText();
		$article = new Article( $titleText );
		$articleText = $article->getContent();
		$limit = 100;
		if (strlen($articleText) > $limit) {
		 $words = str_word_count($articleText, 2);
		 $pos = array_keys($words);
		 $articleText = html_cutArticle($articleText, $limit). '<br/>' . '<br/>' . '[[' . $titleText . '|(Read more)]]';
		 // return the changed text
		 return $articleText . "<br/>";
	} else {
		echo "<br/>";
		 return  $articleText . "<br/>";	
	}
?>

and also added the

$wgOut->addHTML( $this->showArticleText( $row) );

in the execute function.

Right now, I am trying to implement a "Read More" function if the article is > 100 words. However, I tried to do the above but it is giving me TitlePage instead. I can't even click on that.

I am guessing because no parser function was used? How should I implement that? Or are there any other methods to implement it?

Really appreciate your assistance and time!! Have a good day!

Anyhowputx (talkcontribs)

Hi,

I have used your method and it works (partially). However, it is giving me weird characters as shown here (http://i60.tinypic.com/eslrhy.png).

Any idea how to fixed it?

Please advise. Thank You so much!

Anyhowputx (talkcontribs)
Reply to "Getting article content using Title ?"