r65342 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r65341‎ | r65342 | r65343 >
Date:21:19, 20 April 2010
Author:siebrand
Status:deferred
Tags:
Comment:
* 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 (modified) (history)
  • /trunk/extensions/CategoryIntersection/CategoryIntersection.i18n.php (modified) (history)
  • /trunk/extensions/CategoryIntersection/CategoryIntersection.php (modified) (history)
  • /trunk/extensions/CategoryIntersection/CategoryIntersection_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CategoryIntersection/CategoryIntersection.alias.php
@@ -10,7 +10,7 @@
1111
1212 /** English */
1313 $aliases['en'] = array(
14 - 'CategoryIntersection'=> array( 'Category intersection', 'CategoryIntersection' ),
 14+ 'CategoryIntersection' => array( 'Category intersection', 'CategoryIntersection' ),
1515 );
1616
1717 /** Arabic (العربية) */
Index: trunk/extensions/CategoryIntersection/CategoryIntersection_body.php
@@ -8,8 +8,8 @@
99
1010 # Constructor
1111 function CategoryIntersection() {
12 - SpecialPage::SpecialPage("CategoryIntersection");
13 - wfLoadExtensionMessages('CategoryIntersection');
 12+ SpecialPage::SpecialPage( "CategoryIntersection" );
 13+ wfLoadExtensionMessages( 'CategoryIntersection' );
1414
1515 # Limits
1616 $this->max_categories = 5;
@@ -19,23 +19,23 @@
2020 }
2121
2222 # Here plays the music
23 - function execute($par) {
 23+ function execute( $par ) {
2424 global $wgRequest, $wgOut;
2525
2626 $this->setHeaders();
2727
2828 # Get request data
29 - $lines = $wgRequest->getText('lines');
30 - $doit = $wgRequest->getText('doit');
 29+ $lines = $wgRequest->getText( 'lines' );
 30+ $doit = $wgRequest->getText( 'doit' );
3131
32 - if ($doit == '') {
 32+ if ( $doit == '' ) {
3333 $output = $this->getForm();
3434 } else {
35 - $output = $this->run($lines);
 35+ $output = $this->run( $lines );
3636 }
3737
3838 # Output
39 - $wgOut->addHTML($output);
 39+ $wgOut->addHTML( $output );
4040 }
4141
4242 # Generate the submission form
@@ -43,93 +43,99 @@
4444 $ret = '';
4545 $ret .= "<form method='post'>";
4646 $ret .= "<textarea name='lines' rows='10' cols='50' style='width:100%'></textarea><br />";
47 - $ret .= "<input type='submit' name='doit' value='" . wfMsgHtml('categoryintersection-doit') . "' />";
 47+ $ret .= "<input type='submit' name='doit' value='" . wfMsgHtml( 'categoryintersection-doit' ) . "' />";
4848 $ret .= "</form>";
4949 return $ret;
5050 }
5151
5252 # Do the actual work
53 - function run($lines) {
 53+ function run( $lines ) {
5454 global $wgOut;
5555
5656 $fname = 'CategoryIntersection::run';
5757
58 - $dbr = wfGetDB (DB_SLAVE);
59 - $table_categoryintersections = $dbr->tableName ('categoryintersections');
60 - $table_categorylinks = $dbr->tableName ('categorylinks');
 58+ $dbr = wfGetDB ( DB_SLAVE );
 59+ $table_categoryintersections = $dbr->tableName ( 'categoryintersections' );
 60+ $table_categorylinks = $dbr->tableName ( 'categorylinks' );
6161
6262 # Parse list of categories
63 - $lines = explode ("\n", $lines);
 63+ $lines = explode ( "\n", $lines );
6464 $arr = array();
65 - foreach ($lines AS $l) {
66 - $l = trim ($l);
67 - if ($l == '') continue;
68 - $t = Title::newFromText ($l);
 65+ foreach ( $lines AS $l ) {
 66+ $l = trim ( $l );
 67+ if ( $l == '' ) continue;
 68+ $t = Title::newFromText ( $l );
6969 $arr[] = $t->getDBkey();
7070 }
7171
72 - if (count ($arr) > $this->max_categories) {
73 - return wfMsg('categoryintersection-maxcategories',$this->max_categories);
 72+ if ( count ( $arr ) > $this->max_categories ) {
 73+ return wfMsgExt( 'categoryintersection-maxcategories', 'parsemag', $this->max_categories );
7474 }
7575
7676 # Generate hash values for all combinations
77 - $hashes = CategoryIntersectionGetHashValues ($arr);
78 -
 77+ $hashes = CategoryIntersectionGetHashValues ( $arr );
 78+
7979 # Generate (sub)query chain
8080 # TODO : Do we really need all combinations?
8181 $query = "";
82 - foreach ($hashes AS $hash) {
 82+ foreach ( $hashes AS $hash ) {
8383 $q2 = "SELECT ci_page FROM {$table_categoryintersections} WHERE ci_hash = \"{$hash}\""; # FIXME : table/field name
84 - if ($query != "") $q2 .= " AND ci_page IN ({$query})";
 84+ if ( $query != "" ) $q2 .= " AND ci_page IN ({$query})";
8585 $query = $q2;
8686 }
8787 $query .= " LIMIT " . $this->max_hash_results; # Max number of hash results
8888
8989 # This is safe, as the only parameters used are hash values generated by CategoryIntersectionGetHashValues()
90 - $res = $dbr->query($query, $fname);
 90+ $res = $dbr->query( $query, $fname );
9191
92 - if (!$res) {
 92+ if ( !$res ) {
9393 return '';
9494 }
9595
9696 # page_ids will contain the /candidates/ for results. Remember: Hashes are not necessarily unique!
9797 $page_ids = array ();
98 - while ($row = $dbr->fetchObject($res)) {
 98+ while ( $row = $dbr->fetchObject( $res ) ) {
9999 $page_ids[] = $row->ci_page;
100100 }
101101
102102 # Now check which of these are real - or don't
103103 $titles = array ();
104 - if ($this->second_check) {
105 - $carr = count ($arr);
106 - foreach ($page_ids AS $id) {
107 - # This is safe; $arr contains only DB keys generated by Title; $id is a number from the last query
108 - $query = "SELECT count(cl_to) AS x FROM {$table_categorylinks} WHERE cl_from = {$id} AND cl_to IN (\"" . implode ("\",\"", $arr) . "\") LIMIT $carr";
109 - $res = $dbr->query($query, $fname);
110 - if (!$res) continue;
111 - $row = $dbr->fetchObject($res);
112 - $count = $row->x;
113 - if ($count < $carr) continue; # This is not the article you are looking for...
114 - $titles[] = Title::newFromID ($id);
115 - if (count ($titles) >= $this->max_real_results) break;
116 - }
117 - } else {
118 - foreach ($page_ids AS $id) {
119 - $titles[] = Title::newFromID ( $id ) ;
120 - }
121 - }
 104+ if ( $this->second_check ) {
 105+ $carr = count ( $arr );
 106+ foreach ( $page_ids AS $id ) {
 107+ # This is safe; $arr contains only DB keys generated by Title; $id is a number from the last query
 108+ $query = "SELECT count(cl_to) AS x FROM {$table_categorylinks} WHERE cl_from = {$id} AND cl_to IN (\"" . implode ( "\",\"", $arr ) . "\") LIMIT $carr";
 109+ $res = $dbr->query( $query, $fname );
 110+ if ( !$res ) {
 111+ continue;
 112+ }
 113+ $row = $dbr->fetchObject( $res );
 114+ $count = $row->x;
 115+ if ( $count < $carr ) {
 116+ continue; # This is not the article you are looking for...
 117+ }
 118+ $titles[] = Title::newFromID ( $id );
 119+ if ( count ( $titles ) >= $this->max_real_results ) {
 120+ break;
 121+ }
 122+ }
 123+ } else {
 124+ foreach ( $page_ids AS $id ) {
 125+ $titles[] = Title::newFromID ( $id ) ;
 126+ }
 127+ }
122128
123129 # Generate title list in wiki markup
124130 $wiki = '';
125 - foreach ($titles AS $t) {
 131+ foreach ( $titles AS $t ) {
126132 $ft = $t->getFullText();
127133 $wiki .= "# [[:{$ft}|{$ft}]]\n";
128134 }
129 - $wgOut->addWikiText ($wiki);
 135+ $wgOut->addWikiText ( $wiki );
130136
131137 # Final message
132138 global $wgLang;
133 - $count = $wgLang->formatNum( count($titles) );
134 - return '<hr/>' . wfMsgExt('categoryintersection-results', 'parse', $count);
 139+ $count = $wgLang->formatNum( count( $titles ) );
 140+ return '<hr/>' . wfMsgExt( 'categoryintersection-results', 'parse', $count );
135141 }
136142 }
Index: trunk/extensions/CategoryIntersection/CategoryIntersection.i18n.php
@@ -24,6 +24,8 @@
2525 */
2626 $messages['qqq'] = array(
2727 'categoryintersection-desc' => 'Shown in [[Special:Version]]. Do not translate links.',
 28+ 'categoryintersection-maxcategories' => 'Parameters:
 29+* $1 is the maximum allowed number of intersecting categories. Supports plural.',
2830 );
2931
3032 /** Afrikaans (Afrikaans)
Index: trunk/extensions/CategoryIntersection/CategoryIntersection.php
@@ -6,6 +6,7 @@
77 * @copyright (c) 2008 by Magnus Manske
88 * @license Released under GPL
99
 10+ // FIXME: creation of table should be done through hook.
1011 SQL for creating categoryintersections table:
1112
1213 CREATE TABLE `categoryintersections` (
@@ -17,12 +18,12 @@
1819 **/
1920
2021 # Alert the user that this is not a valid entry point to MediaWiki if they try to access the skin file directly.
21 -if (!defined('MEDIAWIKI')) {
 22+if ( !defined( 'MEDIAWIKI' ) ) {
2223 echo <<<EOT
2324 To install my extension, put the following line in LocalSettings.php:
2425 require_once("\$IP/extensions/CategoryIntersection/CategoryIntersection.php");
2526 EOT;
26 - exit(1);
 27+ exit( 1 );
2728 }
2829
2930 $wgExtensionCredits['other'][] = array(
@@ -33,7 +34,7 @@
3435 'descriptionmsg' => 'categoryintersection-desc',
3536 );
3637
37 -$dir = dirname(__FILE__) . '/';
 38+$dir = dirname( __FILE__ ) . '/';
3839
3940 $wgHooks['LinksUpdate'][] = 'CategoryIntersectionLinksUpdate';
4041 $wgHooks['ArticleDelete'][] = 'CategoryIntersectionArticleDelete';
@@ -43,23 +44,23 @@
4445 $wgExtensionAliasesFiles['CategoryIntersection'] = $dir . 'CategoryIntersection.alias.php';
4546 $wgSpecialPages['CategoryIntersection'] = 'CategoryIntersection'; # Let MediaWiki know about your new special page.
4647
47 -function CategoryIntersectionGetHashValues ($categories) {
48 - sort ($categories);
 48+function CategoryIntersectionGetHashValues ( $categories ) {
 49+ sort ( $categories );
4950 $hash = array ();
5051 $hv = array ();
51 - foreach ($categories AS $k => $c1) {
52 - foreach ($categories AS $c2) {
53 - if ($c1 == $c2) continue;
54 - if ($c1 < $c2) $key = $c1 . '|' . $c2;
 52+ foreach ( $categories AS $k => $c1 ) {
 53+ foreach ( $categories AS $c2 ) {
 54+ if ( $c1 == $c2 ) continue;
 55+ if ( $c1 < $c2 ) $key = $c1 . '|' . $c2;
5556 else $key = $c2 . '|' . $c1;
56 - if (isset ($hash[$key])) continue; // This combination was already done
57 - $m = md5 ($key);
 57+ if ( isset ( $hash[$key] ) ) continue; // This combination was already done
 58+ $m = md5 ( $key );
5859 $m = hexdec ( substr ( $m , 0 , 8 ) ) ;
59 - if (isset ($hv[$m])) continue; // This hash value is already in there, prevent unique index conflict
 60+ if ( isset ( $hv[$m] ) ) continue; // This hash value is already in there, prevent unique index conflict
6061 $hash[$key] = $m;
6162 $hv[$m] = 1;
6263 }
63 - unset ($categories[$k]); // No more combinations with this
 64+ unset ( $categories[$k] ); // No more combinations with this
6465 }
6566 return $hash;
6667 }
@@ -68,16 +69,15 @@
6970 * Updates the category intersection table for a page.
7071 * Called by LinksUpdate hook.
7172 */
72 -function CategoryIntersectionLinksUpdate (&$linksUpdate) {
73 -
 73+function CategoryIntersectionLinksUpdate ( &$linksUpdate ) {
7474 // Get categories
7575 $categories = $linksUpdate->mCategories; // The keys of this array are the categories of this page, without cateogry prefix, ucfirst, underscores
76 - $categories = array_keys ($categories);
77 - $hash = CategoryIntersectionGetHashValues ($categories);
 76+ $categories = array_keys ( $categories );
 77+ $hash = CategoryIntersectionGetHashValues ( $categories );
7878
7979 // Prepare new hash values for table insertion
8080 $arr = array ();
81 - foreach ($hash AS $k => $v) {
 81+ foreach ( $hash AS $k => $v ) {
8282 $arr[] = array (
8383 'ci_page' => $linksUpdate->mId ,
8484 'ci_hash' => $v
@@ -85,13 +85,13 @@
8686 }
8787
8888 // Update hash table
89 - $linksUpdate->dumbTableUpdate ('categoryintersections', $arr, 'ci_page');
 89+ $linksUpdate->dumbTableUpdate ( 'categoryintersections', $arr, 'ci_page' );
9090
9191 return true; // My work here is done
9292 }
9393
94 -function CategoryIntersectionArticleDelete (&$article, &$user, &$reason) {
95 - $dbw = wfGetDB( DB_MASTER );
96 - $dbw->delete ( 'categoryintersections' , array ( "ci_page" => $article->getID() ) ) ;
97 - return true ;
 94+function CategoryIntersectionArticleDelete ( &$article, &$user, &$reason ) {
 95+ $dbw = wfGetDB( DB_MASTER );
 96+ $dbw->delete ( 'categoryintersections' , array ( "ci_page" => $article->getID() ) ) ;
 97+ return true ;
9898 }

Status & tagging log