http://www.mediawiki.org/wiki/Special:Code/MediaWiki/65355

Revision: 65355
Author:   bawolff
Date:     2010-04-21 05:33:51 +0000 (Wed, 21 Apr 2010)

Log Message:
-----------
Some fixes to Category Intersection extension (I hope original author 
doesn't mind)

*Make it fail more gracefully on invalid input from user
*make new table creation happen from hook instead of just telling people 
to create table in comments.

Modified Paths:
--------------
    trunk/extensions/CategoryIntersection/CategoryIntersection.i18n.php
    trunk/extensions/CategoryIntersection/CategoryIntersection.php
    trunk/extensions/CategoryIntersection/CategoryIntersection_body.php

Added Paths:
-----------
    trunk/extensions/CategoryIntersection/CategoryIntersection.sql

Modified: trunk/extensions/CategoryIntersection/CategoryIntersection.i18n.php
===================================================================
--- trunk/extensions/CategoryIntersection/CategoryIntersection.i18n.php 
2010-04-21 02:37:08 UTC (rev 65354)
+++ trunk/extensions/CategoryIntersection/CategoryIntersection.i18n.php 
2010-04-21 05:33:51 UTC (rev 65355)
@@ -16,6 +16,7 @@
        'categoryintersection-desc'          => 'Maintains a table with hash 
values for [[Special:CategoryIntersection|category intersections]] within a 
page',
        'categoryintersection-doit'          => 'List pages in all these 
categories',
        'categoryintersection-maxcategories' => 'The maximum allowed number of 
intersecting categories is $1.',
+       'categoryintersection-mincategories' => 'You need to include at least 
two categories.',
        'categoryintersection-results'       => 'The search returned $1 
{{PLURAL:$1|result|results}}.',
 );
 

Modified: trunk/extensions/CategoryIntersection/CategoryIntersection.php
===================================================================
--- trunk/extensions/CategoryIntersection/CategoryIntersection.php      
2010-04-21 02:37:08 UTC (rev 65354)
+++ trunk/extensions/CategoryIntersection/CategoryIntersection.php      
2010-04-21 05:33:51 UTC (rev 65355)
@@ -6,22 +6,17 @@
  * @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` (
-    `ci_page` int(10) unsigned NOT NULL,
-    `ci_hash` int(10) unsigned NOT NULL,
-    PRIMARY KEY  (`ci_hash`,`ci_page`)
-  ) ;
-
 **/
 
 # 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' ) ) {
        echo <<<EOT
 To install my extension, put the following line in LocalSettings.php:
+<br/>
 require_once("\$IP/extensions/CategoryIntersection/CategoryIntersection.php");
+<br/>
+Then run the update.php maintenance script, followed by the refreshLinks.php 
maintenance script.
 EOT;
        exit( 1 );
 }
@@ -95,3 +90,16 @@
        $dbw->delete ( 'categoryintersections' , array ( "ci_page" => 
$article->getID() ) ) ;
        return true ;
 }
+
+# new tables needed (based on how ReaderFeedback extension does it)
+$wgHooks['LoadExtensionSchemaUpdates'][] = 
'efCategoryIntersectionSchemaUpdates';
+
+function efCategoryIntersectionSchemaUpdates() {
+        global $wgDBtype, $wgExtNewTables;
+        $base = dirname( __FILE__ );
+        if ( $wgDBtype == 'mysql' ) {
+                $wgExtNewTables[] = array( 'categoryintersections', 
"$base/CategoryIntersection.sql" );
+        }
+        return true;
+}
+

Added: trunk/extensions/CategoryIntersection/CategoryIntersection.sql
===================================================================
--- trunk/extensions/CategoryIntersection/CategoryIntersection.sql              
                (rev 0)
+++ trunk/extensions/CategoryIntersection/CategoryIntersection.sql      
2010-04-21 05:33:51 UTC (rev 65355)
@@ -0,0 +1,14 @@
+-- CategoryIntersection sql
+-- replace stuff between /* and */ with apropriate stuff
+-- or just run the update.php maintence script
+
+CREATE TABLE IF NOT EXISTS /*_*/categoryintersections (
+ `ci_page` int(10) unsigned NOT NULL,
+ `ci_hash` int(10) unsigned NOT NULL,
+ PRIMARY KEY  (`ci_hash`,`ci_page`)
+) /*$wgDBTableOptions*/;
+
+CREATE INDEX /*i*/ci_page ON /*_*/categoryintersections (ci_page);
+
+
+


Property changes on: 
trunk/extensions/CategoryIntersection/CategoryIntersection.sql
___________________________________________________________________
Added: svn:eol-style
   + native

Modified: trunk/extensions/CategoryIntersection/CategoryIntersection_body.php
===================================================================
--- trunk/extensions/CategoryIntersection/CategoryIntersection_body.php 
2010-04-21 02:37:08 UTC (rev 65354)
+++ trunk/extensions/CategoryIntersection/CategoryIntersection_body.php 
2010-04-21 05:33:51 UTC (rev 65355)
@@ -43,7 +43,7 @@
                $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;
        }
@@ -65,16 +65,29 @@
                        $l = trim ( $l );
                        if ( $l == '' ) continue;
                        $t = Title::newFromText ( $l );
-                       $arr[] = $t->getDBkey();
+                       if ( $t ) { // in case of invalid input
+                               $arr[] = $t->getDBkey();
+                       }
                }
 
-               if ( count ( $arr ) > $this->max_categories ) {
+               $numb_categories = count( $arr );
+               if ( $numb_categories > $this->max_categories ) {
                        return wfMsgExt( 'categoryintersection-maxcategories', 
'parsemag', $this->max_categories );
                }
 
+               if ( $numb_categories < 2 ) {
+                       return wfMsgExt( 'categoryintersection-mincategories', 
'parsemag' );
+               }
+
                # Generate hash values for all combinations
                $hashes = CategoryIntersectionGetHashValues ( $arr );
 
+               if ( empty( $hashes ) ) {
+                       // Could potentially happen if user tries to do the 
+                       // intersection of a category with itself.
+                       return wfMsgExt( 'categoryintersection-mincategories', 
'parsemag' );
+               }
+
                # Generate (sub)query chain
                # TODO : Do we really need all combinations?
                $query = "";



_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to