r83871 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r83870‎ | r83871 | r83872 >
Date:05:15, 14 March 2011
Author:bawolff
Status:ok
Tags:
Comment:
Several fixes:
*Output a news:publication tag in the sitemap format. This is required by the spec.
*Fix the notcategories feature. This is needed for the use case that the wikinewsies want this for.
*Remove the googlenewssitemap_toofewcats and googlenewssitemap_noincludecats as they are not used (and not likely to be)
*Make the title of the rss/atom feeds outputed configurable as a system message
*Some minor misc fixes.
Modified paths:
  • /trunk/extensions/GoogleNewsSitemap/GoogleNewsSitemap.i18n.php (modified) (history)
  • /trunk/extensions/GoogleNewsSitemap/GoogleNewsSitemap_body.php (modified) (history)
  • /trunk/extensions/GoogleNewsSitemap/SitemapFeed.php (modified) (history)

Diff [purge]

Index: trunk/extensions/GoogleNewsSitemap/GoogleNewsSitemap_body.php
@@ -6,13 +6,13 @@
77 **
88 * Simple feed using Atom/RSS coupled to DynamicPageList category searching.
99 *
10 - * To use: http://wiki.url/Special:GoogleNewsSitemap/[paramter=value][...]
 10+ * To use: http://wiki.url/Special:GoogleNewsSitemap?[paramter=value][&parameter2=value]&...
1111 *
1212 * Implemented parameters are marked with an @
1313 **
1414 * Parameters
15 - * * category = string ; default = Published
16 - * * notcategory = string ; default = null
 15+ * * categories = string ; default = Published
 16+ * * notcategories = string ; default = null
1717 * * namespace = string ; default = null
1818 * * count = integer ; default = $wgDPLmaxResultCount = 50
1919 * * order = string ; default = descending
@@ -27,11 +27,14 @@
2828
2929 /**
3030 * Script default values - correctly spelt, naming standard.
 31+ * @todo These should be configurable. Perhaps be $wg globals (?)
3132 **/
3233 var $wgDPlminCategories = 1; // Minimum number of categories to look for
3334 var $wgDPlmaxCategories = 6; // Maximum number of categories to look for
3435 var $wgDPLmaxResultCount = 50; // Maximum number of results to allow
3536
 37+ var $fallbackCategory = 'Published';
 38+
3639 /**
3740 * @var array Parameters array
3841 **/
@@ -62,15 +65,19 @@
6366
6467 // Check to make sure that feed type is supported.
6568 if ( FeedUtils::checkFeedOutput( $this->params['feed'] ) ) {
66 - // TODO: should feed title be a message.
6769 $feed = new $wgFeedClasses[ $this->params['feed'] ](
68 - $wgSitename . " [$wgLanguageCode] "
69 - . $wgContLang->uc( $this->params['feed'] ) . ' feed',
 70+ wfMsgExt( 'googlenewssitemap_feedtitle',
 71+ array( 'parsemag', 'content' ),
 72+ $wgContLang->getLanguageName( $wgLanguageCode ),
 73+ $wgContLang->uc( $this->params['feed'] ),
 74+ $wgLanguageCode
 75+ ),
7076 wfMsgExt( 'tagline', 'parsemag' ),
7177 Title::newMainPage()->getFullUrl()
7278 );
7379 } else {
74 - // Can't really do anything if wrong feed type.
 80+ // FeedUtils outputs an error if wrong feed type.
 81+ // So nothing else to do at this point
7582 return;
7683 }
7784
@@ -170,28 +177,24 @@
171178 $currentTableNumber++;
172179 }
173180
174 - // exclusion categories disabled pending discussion on whether they are necessary
175 - /*
176181 for ( $i = 0; $i < $this->params['notCatCount']; $i++ ) {
177 - // echo "notCategory parameter $i<br />\n";
178 - $sqlSelectFrom .= ' LEFT OUTER JOIN ' . $dbr->tableName( 'categorylinks' );
179 - $sqlSelectFrom .= ' AS c' . ( $currentTableNumber + 1 ) . ' ON page_id = c' . ( $currentTableNumber + 1 );
180 - $sqlSelectFrom .= '.cl_from AND c' . ( $currentTableNumber + 1 );
181 - $sqlSelectFrom .= '.cl_to=' . $dbr->addQuotes( $this->notCategories[$i]->getDBkey() );
182 -
183 - $conditions .= ' AND c' . ( $currentTableNumber + 1 ) . '.cl_to IS NULL';
184 -
 182+ $joins["$categorylinks AS c$currentTableNumber"] = array( 'LEFT OUTER JOIN',
 183+ array( "page_id = c{$currentTableNumber}.cl_from",
 184+ "c{$currentTableNumber}.cl_to={$dbr->addQuotes( $this->notCategories[$i]->getDBKey() ) }"
 185+ )
 186+ );
 187+ $tables[] = "$categorylinks AS c$currentTableNumber";
 188+ $conditions[] = "c{$currentTableNumber}.cl_to IS NULL";
185189 $currentTableNumber++;
186190 }
187 - */
188191
189 - if ( 'descending' == $this->params['order'] ) {
 192+ if ( $this->params['order'] === 'descending' ) {
190193 $sortOrder = 'DESC';
191194 } else {
192195 $sortOrder = 'ASC';
193196 }
194197
195 - if ( 'lastedit' == $this->params['orderMethod'] ) {
 198+ if ( $this->params['orderMethod'] === 'lastedit' ) {
196199 $options['ORDER BY'] = 'page_touched ' . $sortOrder;
197200 } else {
198201 $options['ORDER BY'] = 'c1.cl_timestamp ' . $sortOrder;
@@ -201,8 +204,7 @@
202205 // earlier validation logic ensures this is a reasonable number
203206 $options['LIMIT'] = $this->params['count'];
204207
205 - // return $dbr->query( $sqlSelectFrom . $conditions );
206 - return $dbr->select( $tables, $fields, $conditions, '', $options, $joins );
 208+ return $dbr->select( $tables, $fields, $conditions, __METHOD__, $options, $joins );
207209 }
208210
209211 /**
@@ -214,14 +216,8 @@
215217
216218 $this->params = array();
217219
218 - $category = $wgRequest->getText( 'category', 'Published' );
219 - $category = explode( "|", $category );
220 - foreach ( $category as $catName ) {
221 - $catTitle = Title::newFromText( $catName, NS_CATEGORY );
222 - if ( $catTitle ) {
223 - $this->categories[] = $catTitle;
224 - }
225 - }
 220+ $this->categories = $this->getCatRequestArray( 'categories', $this->fallbackCategory, $this->wgDPlmaxCategories );
 221+ $this->notCategories = $this->getCatRequestArray( 'notcategories', '', $this->wgDPlmaxCategories );
226222
227223 // FIXME:notcats
228224 // $this->notCategories[] = $wgRequest->getArray('notcategory');
@@ -246,11 +242,11 @@
247243 $totalCatCount = $this->params['catCount'] + $this->params['notCatCount'];
248244
249245 if ( ( $this->params['catCount'] < 1 && !$this->params['nameSpace'] )
250 - || ( $totalCatCount < $this->wgDPlminCategories ) )
 246+ || ( $totalCatCount < 1 ) )
251247 {
252 - $feed = Title::newFromText( 'Published', NS_CATEGORY );
253 - if ( is_object( $feed ) ) {
254 - $this->categories[] = $feed;
 248+ $fallBack = Title::newFromText( $this->fallbackCategory, NS_CATEGORY );
 249+ if ( $fallBack ) {
 250+ $this->categories[] = $fallBack;
255251 $this->params['catCount'] = count( $this->categories );
256252 } else {
257253 throw new MWException( "Default fallback category is not a valid title!" );
@@ -269,8 +265,34 @@
270266 }
271267
272268 /**
 269+ * Turn a pipe-seperated list from a url parameter into an array.
 270+ * Verifying each element would be a valid title in Category namespace.
 271+ * @param String $name Parameter to retrieve from web reqeust.
 272+ * @param String $default
 273+ * @param Integer $max Maximuin size of resulting array.
 274+ * @return Array of Title objects. The Titles passed in the parameter $name.
 275+ */
 276+ private function getCatRequestArray( $name, $default, $max ) {
 277+ global $wgRequest;
 278+
 279+ $value = $wgRequest->getText( $name, $default );
 280+ $arr = explode( "|", $value, $max + 2 );
 281+ $res = array();
 282+ foreach ( $arr as $name ) {
 283+ $catTitle = Title::newFromText( $name, NS_CATEGORY );
 284+ if ( $catTitle ) {
 285+ $res[] = $catTitle;
 286+ }
 287+ }
 288+ return $res;
 289+ }
 290+
 291+ /**
 292+ * Given a title, figure out what keywords. Use the message googlenewssitemap_categorymap
 293+ * to map local categories to Google News Keywords.
 294+ * @see http://www.google.com/support/news_pub/bin/answer.py?answer=116037
273295 * @param Title $title
274 - * @return string
 296+ * @return string Comma seperated list of keywords
275297 */
276298 function getKeywords ( $title ) {
277299 $cats = $title->getParentCategories();
Index: trunk/extensions/GoogleNewsSitemap/GoogleNewsSitemap.i18n.php
@@ -18,8 +18,7 @@
1919 'googlenewssitemap-desc' => 'Outputs an Atom/RSS feed as a Google News Sitemap',
2020 'googlenewssitemap_categorymap' => '', # Default empty. List of categories to map to keywords. Do not translate.
2121 'googlenewssitemap_toomanycats' => 'Error: Too many categories!',
22 - 'googlenewssitemap_toofewcats' => 'Error: Too few categories!',
23 - 'googlenewssitemap_noincludecats' => 'Error: You need to include at least one category, or specify a namespace!',
 22+ 'googlenewssitemap_feedtitle' => '$1 {{SITENAME}} $2 feed.'
2423 );
2524
2625 /** Message documentation (Message documentation)
@@ -28,7 +27,12 @@
2928 */
3029 $messages['qqq'] = array(
3130 'googlenewssitemap-desc' => '{{desc}}',
 31+ 'googlenewssitemap_toomanycats' => 'Error given when maximum amount of categories specified is exceeded. Default max is 6.',
3232 'googlenewssitemap_badfeedobject' => '<code>$feed</code> is a variable, leave it unchanged.',
 33+ 'googlenewssitemap_feedtitle' => 'Title for the RSS/ATOM feeds produced (does not appear in sitemap XML documents).
 34+*$1 is language name (like English)
 35+*$2 is feed type (RSS or ATOM)
 36+*$3 is language code (like en)'
3337 );
3438
3539 /** Afrikaans (Afrikaans)
Index: trunk/extensions/GoogleNewsSitemap/SitemapFeed.php
@@ -3,11 +3,37 @@
44
55 class SitemapFeed extends ChannelFeed {
66 private $writer;
 7+ private $publicationName;
 8+ private $publicationLang;
79
810 function __construct() {
 11+ global $wgSitename, $wgLanguageCode;
 12+
913 $this->writer = new XMLWriter();
 14+ $this->publicationName = $wgSitename;
 15+ $this->publicationLang = $wgLanguageCode;
1016 }
1117
 18+ /**
 19+ * Set the publication language code. Only used if different from
 20+ * $wgLanguageCode, which could happen if google disagrees with us
 21+ * on say what code zh gets.
 22+ * @param String $lang Language code (like en)
 23+ */
 24+ function setPublicationLang( $lang ) {
 25+ $this->publicationLang = $lang;
 26+ }
 27+
 28+ /**
 29+ * Set the publication name. Normally $wgSitename, but could
 30+ * need to be changed, if Google gives the publication a different
 31+ * name then $wgSitename.
 32+ * @param String $name The name of the publication
 33+ */
 34+ function setPublicationName( $name ) {
 35+ $this->publicationName = $name;
 36+ }
 37+
1238 function contentType() {
1339 return 'application/xml';
1440 }
@@ -55,6 +81,15 @@
5682 $this->writer->text( $item->getTitle() );
5783 $this->writer->endElement();
5884
 85+ $this->writer->startElement( "news:publication" );
 86+ $this->writer->startElement( "news:name" );
 87+ $this->writer->text( $this->publicationName );
 88+ $this->writer->endElement();
 89+ $this->writer->startElement( "news:language" );
 90+ $this->writer->text( $this->publicationLang );
 91+ $this->writer->endElement();
 92+ $this->writer->endElement();
 93+
5994 if ( $item->getKeywords() ) {
6095 $this->writer->startElement( "news:keywords" );
6196 $this->writer->text( $item->getKeywords() );

Status & tagging log