Sommaire


NewsZ is now passed at version 0.8.x
Many changes are made respect this version: new version browse subcategories and may be a true news aggregator, now
Go to latest version: NewsZ latest

NewsZ Mediawiki extension

version 0.7.3-beta
update  2011/01/02
Changelog:
 - Valid XHTML 1.0 Transitional
version 0.7.2-beta
update  2011/01/01
Changelog:
 - improved 'search Image in article' procedure
 - detect if PHP is as CGI or is as module
version 0.7.1-beta
update  2010/11/27
Changelog:
- search articles only in NS_MAIN

Warning

Warning: although several sanitizer features was added to code, this extension is not yet tested in public writable wikis.

What can this extension do?

Allows users to create frames and tables with lists of News per Category, limited to n elements. With thumbnails preview!

This extension search the first tag "Image:" in article and uses it as thumbnails preview; if the article not contains images, then uses a default "Logo-category_name.png" as thumbnail.

You can chose several types of previews: simple text, icons in colums, icons with text preview.

To mark the text inside article for preview, simply use this syntax:

<!--NewsZ--> ... here the text article preview ... <!--EndZ-->

You can hide a preview text in article:

<!--NewsZ ... here the hidden text for article preview ... EndZ-->

You can also hide a icon preview in every article:

<!-- [[Image:my_hidden_logo.png]] -->

Usage

{{#newsz:My_news_category|begin from|news counter|cols or chars| class="" style="" ...}}

where:

"My_news_category" = a Category (Note: better replace spaces with underscore)
"begin from" = 0-->last inserted, 1-->previous, etc.
"news counter" = number of articles in table
"cols or chars" = 0 --> list article without thumbnails
                  1 to 15 --> n. of cols in preview per table
                  >15 --> number of char in text article preview
last field accepts list of styles

example:

{{#newsz:World_news|0|10|140| style="font-size:14px; width:50%;"}}

Download instructions

Please cut and paste the code found below and place it in $IP/extensions/ExtensionName/ExtensionName.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

Installation

To install this extension, add the following to LocalSettings.php:

require_once( "$IP/extensions/NewsZ/NewsZ.php");
//Add global custom thumbnail size here (default size is "90x72px"):
$wgNewsZthumbsize='120x96px';

Speed up the news refresh

Mediawiki makes intensive use of server-side cache. To speed up news refresh you can invalidate cache in Localsettings.php:

//$wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
$wgCacheEpoch = gmdate( 'YmdHis' );

Code

<?php
# NewsZ mediawiki extension Copyright (C) 2010 Fabio Zorba <zoros3000@gmail.com>
# derivate from News.php Copyright (C) 2009 Erich Steiger <me@erichsteiger.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# http://www.gnu.org/copyleft/gpl.html

if( !defined( 'MEDIAWIKI' ) )
  die( -1 );
 
$wgExtensionFunctions[] = 'wfNewsZ';
$wgHooks['LanguageGetMagic'][]       = 'wfNewsZ_Magic';
 
$wgExtensionCredits['parserhook'][] = array(
		'name' => 'NewsZ',
	'description' => 'Allows users to create frames and tables with lists of News per Category, limited to n elements. With thumbnails preview!',
	'author' => 'Fabio Zorba',
	'url' => 'http://www.mediawiki.org/wiki/Extension:NewsZ',
	'version' => '0.7.3 (beta) 20110102',
	'type' => 'parserhook'
);
 
function wfNewsZ_Magic( &$magicWords, $langCode ) {
	# Add the magic word
	# The first array element is case sensitive, in this case it is not case sensitive
	# All remaining elements are synonyms for our parser function
	$magicWords['newsz'] = array( 0, 'newsz' );
	# unless we return true, other parser functions extensions won't get loaded.
	return true;
}
 
function wfNewsZ() {
	global $wgParser;
	$wgParser->setFunctionHook( 'newsz', 'fzRenderNewsList' );
}
 
function fzShortenText($text, $chars) {
	$text = $text." ";
	$text = substr($text,0,$chars);
	$text = substr($text,0,strrpos($text,' '));
//	$text = $text."...";
	return $text;
}
 
function formatDate($date) {
	$y = substr($date,2,2);
	$m = substr($date,5,2);
	$d = substr($date,8,2);
	return $d . "/" .  $m . "/" . $y;
}
 
function fzFindFirstImage($page_content) {
 
	$pos = stripos($page_content, 'Image');
 
	if ($pos === false) { return false; }
	else {
		//check if open brackets [[ exists
		$open_brackets_pos = strpos($page_content, '[[', $pos-6);
		if ($open_brackets_pos === false) { return false; }
		if ($open_brackets_pos > $pos)     { return false; }
 
		$end_string = strpos($page_content, ']]', $pos+6);
		$image_string=substr  ($page_content,$pos+6,$end_string-$pos-6);
 
		//sanitizer!
		$image_string = Sanitizer::encodeAttribute($image_string);
 
		trim($image_string);
 
		if (strlen($image_string) < 5) { return false; }
 
		$end_string = strpos($image_string, '|');
		if ($end_string === false) { return $image_string; }
 
		$image_string=substr  ($image_string,0,$end_string);
		trim($image_string);
 
		if ((strlen($image_string) < 5) || (strlen($image_string) > 127)) { return false; }
		else { return $image_string; }
	}
}
 
function fzGetArticleAbstract($page_content) {
 
	$pos = strpos($page_content, '<!--NewsZ');
 
	if ($pos === false) { return false; }
	else {
		$end_string = strpos($page_content, 'EndZ-->', $pos+9);
		if ($pos === false) { return false; }
 
		$abstract_string=substr  ($page_content,$pos+9,$end_string-$pos-9);
 
		$abstract_string=str_replace("<!--","",$abstract_string);
		$abstract_string=str_replace( "-->","",$abstract_string);
 
		trim($abstract_string);
 
		//16 chars minimum for abstract
		if (strlen($abstract_string) < 16) { return false; }
 
		//Sanitizer!
		$abstract_string = Sanitizer::safeEncodeAttribute($abstract_string);
 
		return $abstract_string;
	}
}
 
function fzURLencode($URLtoEncode) {
	global $wgScriptPath;
 
	//detect if PHP is as CGI or as module
	$index_path='/index.php/';
	if (substr(php_sapi_name(), 0, 3) == 'cgi') { $index_path = '/index.php?title='; }
 
	$urlEncoded = htmlspecialchars ($wgScriptPath . $index_path . $URLtoEncode);
 
	return $urlEncoded;
}
 
 
function fzRenderNewsList(&$parser, $category, $firstpage, $countpages, $newscols, $divstyle) {
	global $wgNewsZthumbsize;
 
	$dbr = wfGetDB( DB_SLAVE );
 
	$firstpage = is_numeric($firstpage) ? $firstpage : 0;
	$countpages = is_numeric($countpages) ? $countpages : 5;
	$newscols = is_numeric($newscols) ? $newscols : 0;
 
	//Sanitizer!
	$divstyle=str_replace("<","",$divstyle);
	$divstyle=str_replace(">","",$divstyle);
	$category=str_replace(" ","_",$category);
	if ( $firstpage  > 10000 ) { $firstpage=0; }
	if ( $countpages > 100 )   { $countpages=100; }
	if ( $newscols   > 1000 )  { $newscols=1000; }
 
	$output='';
 
	$thumbsize='90x72px';
	if ($wgNewsZthumbsize != null) { $thumbsize=$wgNewsZthumbsize; }
 
	$newscounter = $newscols;
 
	list( $page, $categorylinks, $revision, $text) = $dbr->tableNamesN( 'page', 'categorylinks', 'revision', 'text');
	$sql = "select * from (
		     SELECT page_id, page_namespace, page_title,
		     (select old_text FROM $text JOIN $revision ON rev_text_id = old_id
		     WHERE rev_id = (select max(rev_id) from $revision WHERE rev_page = page_id)) as old_text,
		     (select rev_timestamp FROM $revision WHERE rev_id = (select min(rev_id) from $revision
		     WHERE rev_page = page_id)) as rev_timestamp,
		     STR_TO_DATE(substr((select rev_timestamp FROM $revision WHERE rev_id = (select min(rev_id) from $revision
		     WHERE rev_page = page_id)),1,8), '%Y%m%d') as articleDate
		FROM $page
		JOIN $categorylinks ON page_id = cl_from 
	       WHERE cl_to =" . $dbr->addQuotes($category) . ") V
	       ORDER BY REV_timestamp desc
	       LIMIT " .  $firstpage  . "," . $countpages ;
 
	$res = $dbr->query( $sql, 'news' );
	if ($dbr->numRows($res) <= 0) {
		return '<table id="' . $category . '" ' . $divstyle . '><tr><td> No News in "' . $category . '"</td></tr></table>';
	}
 
	$output .= '<table id="' . $category . '" ' . $divstyle . '>' ."\n";
 
	while ( $row = $dbr->fetchObject( $res ) ) {
 
		if ($row->page_namespace == 0) {
 
		if (( $newscounter == $newscols ) || ( $newscols > 15 )) { $output .= "<tr>\n"; }
 
		//find first image in article (if exists)
		$content=$row->old_text;
		$image_name=fzFindFirstImage($content);
 
		$timestamp=formatDate($row->articleDate);
 
		if ($image_name == false ) {
			$logo='[[Image:Logo-' . $category . '.png|center|' . $thumbsize .'|link=' . $row->page_title . ']]';
		}
		else {
			$logo='[[Image:' . $image_name . '|center|' . $thumbsize . '|link=' . $row->page_title . ']]';
		}
 
		$logo=$parser->replaceInternalLinks($logo);
 
		if ($newscols > 15) {
			$content=fzGetArticleAbstract($content);
			if ($content === false ) {
				$content=$row->page_title;
				$content=str_replace("_"," ",$content);
			}
			else {
				$content=fzShortenText($content, $newscols);
			}
				$output .= '<td style="text-align:center">' . $logo .'</td>' .
				'<td><a href="' . fzURLencode ($row->page_title) .
				'" target="_blank">' . $content . '</a>&nbsp;' . $timestamp . '</td>';
		}
		else {
	 	$content="==='''" . $row->page_title . "'''===";
 
		$content=$parser->doHeadings($content);
		$content=$parser->doAllQuotes($content);
		$content=str_replace("_"," ",$content);
 
		$content='<a href="' . fzURLencode ($row->page_title) .'" target="_blank">' . $content .'</a>';
 
		if ( $newscols == 0 ) {
 
			$content=$row->page_title;
			$content=str_replace("_"," ",$content);
 
			$output .= '<td align="left" valign="center" style="text-align:left">' . $timestamp;
			$output .= ' - <a href="' . fzURLencode ($row->page_title) .'" target="_blank">'
			. $content . '</a>';
 
		}
		else { $output .= '<td style="text-align:center">' . $logo . ' ' . $content . ' ' . $timestamp . '</td>'; }
		}
 
		if ( $newscounter > 0 ) { $newscounter=$newscounter-1; }
 
		if (( $newscounter == 0 ) || ( $newscols > 15 )) {
		$newscounter=$newscols;
		$output .= "</tr>\n";
		}
		}
	}
	$output .= "</table>";
 
	return array($output, 'noparse' => true, 'isHTML' => true, 'noargs' => true);
 
}
?>

This extension is publied at Mediawiki: http://www.mediawiki.org/wiki/Extension:NewsZ

Récupérée de « http://www.zoros.org/wiki/index.php?title=Old_NewsZ_mediawiki_extension_with_thumbnails_preview »