http://www.mediawiki.org/wiki/Special:Code/MediaWiki/65342
Revision: 65342
Author: siebrand
Date: 2010-04-20 21:19:10 +0000 (Tue, 20 Apr 2010)
Log Message:
-----------
* support plural for $1 in 'categoryintersection-maxcategories'
* stylize.php, trailing whitespace removed, indentation updated
* add FIXME for schema to be updated through hook
Modified Paths:
--------------
trunk/extensions/CategoryIntersection/CategoryIntersection.alias.php
trunk/extensions/CategoryIntersection/CategoryIntersection.i18n.php
trunk/extensions/CategoryIntersection/CategoryIntersection.php
trunk/extensions/CategoryIntersection/CategoryIntersection_body.php
Modified: trunk/extensions/CategoryIntersection/CategoryIntersection.alias.php
===================================================================
--- trunk/extensions/CategoryIntersection/CategoryIntersection.alias.php
2010-04-20 21:11:00 UTC (rev 65341)
+++ trunk/extensions/CategoryIntersection/CategoryIntersection.alias.php
2010-04-20 21:19:10 UTC (rev 65342)
@@ -10,7 +10,7 @@
/** English */
$aliases['en'] = array(
- 'CategoryIntersection'=> array( 'Category intersection',
'CategoryIntersection' ),
+ 'CategoryIntersection' => array( 'Category intersection',
'CategoryIntersection' ),
);
/** Arabic (العربية) */
Modified: trunk/extensions/CategoryIntersection/CategoryIntersection.i18n.php
===================================================================
--- trunk/extensions/CategoryIntersection/CategoryIntersection.i18n.php
2010-04-20 21:11:00 UTC (rev 65341)
+++ trunk/extensions/CategoryIntersection/CategoryIntersection.i18n.php
2010-04-20 21:19:10 UTC (rev 65342)
@@ -24,6 +24,8 @@
*/
$messages['qqq'] = array(
'categoryintersection-desc' => 'Shown in [[Special:Version]]. Do not
translate links.',
+ 'categoryintersection-maxcategories' => 'Parameters:
+* $1 is the maximum allowed number of intersecting categories. Supports
plural.',
);
/** Afrikaans (Afrikaans)
Modified: trunk/extensions/CategoryIntersection/CategoryIntersection.php
===================================================================
--- trunk/extensions/CategoryIntersection/CategoryIntersection.php
2010-04-20 21:11:00 UTC (rev 65341)
+++ trunk/extensions/CategoryIntersection/CategoryIntersection.php
2010-04-20 21:19:10 UTC (rev 65342)
@@ -6,6 +6,7 @@
* @copyright (c) 2008 by Magnus Manske
* @license Released under GPL
+ // FIXME: creation of table should be done through hook.
SQL for creating categoryintersections table:
CREATE TABLE `categoryintersections` (
@@ -17,12 +18,12 @@
**/
# Alert the user that this is not a valid entry point to MediaWiki if they try
to access the skin file directly.
-if (!defined('MEDIAWIKI')) {
+if ( !defined( 'MEDIAWIKI' ) ) {
echo <<<EOT
To install my extension, put the following line in LocalSettings.php:
require_once("\$IP/extensions/CategoryIntersection/CategoryIntersection.php");
EOT;
- exit(1);
+ exit( 1 );
}
$wgExtensionCredits['other'][] = array(
@@ -33,7 +34,7 @@
'descriptionmsg' => 'categoryintersection-desc',
);
-$dir = dirname(__FILE__) . '/';
+$dir = dirname( __FILE__ ) . '/';
$wgHooks['LinksUpdate'][] = 'CategoryIntersectionLinksUpdate';
$wgHooks['ArticleDelete'][] = 'CategoryIntersectionArticleDelete';
@@ -43,23 +44,23 @@
$wgExtensionAliasesFiles['CategoryIntersection'] = $dir .
'CategoryIntersection.alias.php';
$wgSpecialPages['CategoryIntersection'] = 'CategoryIntersection'; # Let
MediaWiki know about your new special page.
-function CategoryIntersectionGetHashValues ($categories) {
- sort ($categories);
+function CategoryIntersectionGetHashValues ( $categories ) {
+ sort ( $categories );
$hash = array ();
$hv = array ();
- foreach ($categories AS $k => $c1) {
- foreach ($categories AS $c2) {
- if ($c1 == $c2) continue;
- if ($c1 < $c2) $key = $c1 . '|' . $c2;
+ foreach ( $categories AS $k => $c1 ) {
+ foreach ( $categories AS $c2 ) {
+ if ( $c1 == $c2 ) continue;
+ if ( $c1 < $c2 ) $key = $c1 . '|' . $c2;
else $key = $c2 . '|' . $c1;
- if (isset ($hash[$key])) continue; // This combination
was already done
- $m = md5 ($key);
+ if ( isset ( $hash[$key] ) ) continue; // This
combination was already done
+ $m = md5 ( $key );
$m = hexdec ( substr ( $m , 0 , 8 ) ) ;
- if (isset ($hv[$m])) continue; // This hash value is
already in there, prevent unique index conflict
+ if ( isset ( $hv[$m] ) ) continue; // This hash value
is already in there, prevent unique index conflict
$hash[$key] = $m;
$hv[$m] = 1;
}
- unset ($categories[$k]); // No more combinations with this
+ unset ( $categories[$k] ); // No more combinations with this
}
return $hash;
}
@@ -68,16 +69,15 @@
* Updates the category intersection table for a page.
* Called by LinksUpdate hook.
*/
-function CategoryIntersectionLinksUpdate (&$linksUpdate) {
-
+function CategoryIntersectionLinksUpdate ( &$linksUpdate ) {
// Get categories
$categories = $linksUpdate->mCategories; // The keys of this array are
the categories of this page, without cateogry prefix, ucfirst, underscores
- $categories = array_keys ($categories);
- $hash = CategoryIntersectionGetHashValues ($categories);
+ $categories = array_keys ( $categories );
+ $hash = CategoryIntersectionGetHashValues ( $categories );
// Prepare new hash values for table insertion
$arr = array ();
- foreach ($hash AS $k => $v) {
+ foreach ( $hash AS $k => $v ) {
$arr[] = array (
'ci_page' => $linksUpdate->mId ,
'ci_hash' => $v
@@ -85,13 +85,13 @@
}
// Update hash table
- $linksUpdate->dumbTableUpdate ('categoryintersections', $arr,
'ci_page');
+ $linksUpdate->dumbTableUpdate ( 'categoryintersections', $arr,
'ci_page' );
return true; // My work here is done
}
-function CategoryIntersectionArticleDelete (&$article, &$user, &$reason) {
- $dbw = wfGetDB( DB_MASTER );
- $dbw->delete ( 'categoryintersections' , array ( "ci_page" =>
$article->getID() ) ) ;
- return true ;
+function CategoryIntersectionArticleDelete ( &$article, &$user, &$reason ) {
+ $dbw = wfGetDB( DB_MASTER );
+ $dbw->delete ( 'categoryintersections' , array ( "ci_page" =>
$article->getID() ) ) ;
+ return true ;
}
Modified: trunk/extensions/CategoryIntersection/CategoryIntersection_body.php
===================================================================
--- trunk/extensions/CategoryIntersection/CategoryIntersection_body.php
2010-04-20 21:11:00 UTC (rev 65341)
+++ trunk/extensions/CategoryIntersection/CategoryIntersection_body.php
2010-04-20 21:19:10 UTC (rev 65342)
@@ -8,8 +8,8 @@
# Constructor
function CategoryIntersection() {
- SpecialPage::SpecialPage("CategoryIntersection");
- wfLoadExtensionMessages('CategoryIntersection');
+ SpecialPage::SpecialPage( "CategoryIntersection" );
+ wfLoadExtensionMessages( 'CategoryIntersection' );
# Limits
$this->max_categories = 5;
@@ -19,23 +19,23 @@
}
# Here plays the music
- function execute($par) {
+ function execute( $par ) {
global $wgRequest, $wgOut;
$this->setHeaders();
# Get request data
- $lines = $wgRequest->getText('lines');
- $doit = $wgRequest->getText('doit');
+ $lines = $wgRequest->getText( 'lines' );
+ $doit = $wgRequest->getText( 'doit' );
- if ($doit == '') {
+ if ( $doit == '' ) {
$output = $this->getForm();
} else {
- $output = $this->run($lines);
+ $output = $this->run( $lines );
}
# Output
- $wgOut->addHTML($output);
+ $wgOut->addHTML( $output );
}
# Generate the submission form
@@ -43,93 +43,99 @@
$ret = '';
$ret .= "<form method='post'>";
$ret .= "<textarea name='lines' rows='10' cols='50'
style='width:100%'></textarea><br />";
- $ret .= "<input type='submit' name='doit' value='" .
wfMsgHtml('categoryintersection-doit') . "' />";
+ $ret .= "<input type='submit' name='doit' value='" . wfMsgHtml(
'categoryintersection-doit' ) . "' />";
$ret .= "</form>";
return $ret;
}
# Do the actual work
- function run($lines) {
+ function run( $lines ) {
global $wgOut;
$fname = 'CategoryIntersection::run';
- $dbr = wfGetDB (DB_SLAVE);
- $table_categoryintersections = $dbr->tableName
('categoryintersections');
- $table_categorylinks = $dbr->tableName ('categorylinks');
+ $dbr = wfGetDB ( DB_SLAVE );
+ $table_categoryintersections = $dbr->tableName (
'categoryintersections' );
+ $table_categorylinks = $dbr->tableName ( 'categorylinks' );
# Parse list of categories
- $lines = explode ("\n", $lines);
+ $lines = explode ( "\n", $lines );
$arr = array();
- foreach ($lines AS $l) {
- $l = trim ($l);
- if ($l == '') continue;
- $t = Title::newFromText ($l);
+ foreach ( $lines AS $l ) {
+ $l = trim ( $l );
+ if ( $l == '' ) continue;
+ $t = Title::newFromText ( $l );
$arr[] = $t->getDBkey();
}
- if (count ($arr) > $this->max_categories) {
- return
wfMsg('categoryintersection-maxcategories',$this->max_categories);
+ if ( count ( $arr ) > $this->max_categories ) {
+ return wfMsgExt( 'categoryintersection-maxcategories',
'parsemag', $this->max_categories );
}
# Generate hash values for all combinations
- $hashes = CategoryIntersectionGetHashValues ($arr);
-
+ $hashes = CategoryIntersectionGetHashValues ( $arr );
+
# Generate (sub)query chain
# TODO : Do we really need all combinations?
$query = "";
- foreach ($hashes AS $hash) {
+ foreach ( $hashes AS $hash ) {
$q2 = "SELECT ci_page FROM
{$table_categoryintersections} WHERE ci_hash = \"{$hash}\""; # FIXME :
table/field name
- if ($query != "") $q2 .= " AND ci_page IN ({$query})";
+ if ( $query != "" ) $q2 .= " AND ci_page IN ({$query})";
$query = $q2;
}
$query .= " LIMIT " . $this->max_hash_results; # Max number of
hash results
# This is safe, as the only parameters used are hash values
generated by CategoryIntersectionGetHashValues()
- $res = $dbr->query($query, $fname);
+ $res = $dbr->query( $query, $fname );
- if (!$res) {
+ if ( !$res ) {
return '';
}
# page_ids will contain the /candidates/ for results. Remember:
Hashes are not necessarily unique!
$page_ids = array ();
- while ($row = $dbr->fetchObject($res)) {
+ while ( $row = $dbr->fetchObject( $res ) ) {
$page_ids[] = $row->ci_page;
}
# Now check which of these are real - or don't
$titles = array ();
- if ($this->second_check) {
- $carr = count ($arr);
- foreach ($page_ids AS $id) {
- # This is safe; $arr contains only DB keys generated by Title; $id is
a number from the last query
- $query = "SELECT count(cl_to) AS x FROM {$table_categorylinks} WHERE
cl_from = {$id} AND cl_to IN (\"" . implode ("\",\"", $arr) . "\") LIMIT $carr";
- $res = $dbr->query($query, $fname);
- if (!$res) continue;
- $row = $dbr->fetchObject($res);
- $count = $row->x;
- if ($count < $carr) continue; # This is not the article you are
looking for...
- $titles[] = Title::newFromID ($id);
- if (count ($titles) >= $this->max_real_results) break;
- }
- } else {
- foreach ($page_ids AS $id) {
- $titles[] = Title::newFromID ( $id ) ;
- }
- }
+ if ( $this->second_check ) {
+ $carr = count ( $arr );
+ foreach ( $page_ids AS $id ) {
+ # This is safe; $arr contains only DB keys
generated by Title; $id is a number from the last query
+ $query = "SELECT count(cl_to) AS x FROM
{$table_categorylinks} WHERE cl_from = {$id} AND cl_to IN (\"" . implode (
"\",\"", $arr ) . "\") LIMIT $carr";
+ $res = $dbr->query( $query, $fname );
+ if ( !$res ) {
+ continue;
+ }
+ $row = $dbr->fetchObject( $res );
+ $count = $row->x;
+ if ( $count < $carr ) {
+ continue; # This is not the article you
are looking for...
+ }
+ $titles[] = Title::newFromID ( $id );
+ if ( count ( $titles ) >=
$this->max_real_results ) {
+ break;
+ }
+ }
+ } else {
+ foreach ( $page_ids AS $id ) {
+ $titles[] = Title::newFromID ( $id ) ;
+ }
+ }
# Generate title list in wiki markup
$wiki = '';
- foreach ($titles AS $t) {
+ foreach ( $titles AS $t ) {
$ft = $t->getFullText();
$wiki .= "# [[:{$ft}|{$ft}]]\n";
}
- $wgOut->addWikiText ($wiki);
+ $wgOut->addWikiText ( $wiki );
# Final message
global $wgLang;
- $count = $wgLang->formatNum( count($titles) );
- return '<hr/>' . wfMsgExt('categoryintersection-results',
'parse', $count);
+ $count = $wgLang->formatNum( count( $titles ) );
+ return '<hr/>' . wfMsgExt( 'categoryintersection-results',
'parse', $count );
}
}
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs