r76001 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r76000‎ | r76001 | r76002 >
Date:13:33, 4 November 2010
Author:demon
Status:ok (Comments)
Tags:
Comment:
Add tag cloud thingie to the tag listing. Ranks by number of uses of each keyword.
Modified paths:
  • /trunk/extensions/CodeReview/CodeReview.php (modified) (history)
  • /trunk/extensions/CodeReview/backend/CodeRepository.php (modified) (history)
  • /trunk/extensions/CodeReview/codereview.css (modified) (history)
  • /trunk/extensions/CodeReview/ui/CodeTagListView.php (modified) (history)
  • /trunk/extensions/CodeReview/ui/WordCloud.php (added) (history)

Diff [purge]

Index: trunk/extensions/CodeReview/CodeReview.php
@@ -73,8 +73,8 @@
7474 $wgAutoloadClasses['SpecialCode'] = $dir . 'ui/SpecialCode.php';
7575 $wgAutoloadClasses['CodeView'] = $dir . 'ui/SpecialCode.php';
7676 $wgAutoloadClasses['SpecialRepoAdmin'] = $dir . 'ui/SpecialRepoAdmin.php';
 77+$wgAutoloadClasses['WordCloud'] = $dir . 'ui/WordCloud.php';
7778
78 -
7979 $wgSpecialPages['Code'] = 'SpecialCode';
8080 $wgSpecialPageGroups['Code'] = 'developer';
8181 $wgSpecialPages['RepoAdmin'] = 'SpecialRepoAdmin';
Index: trunk/extensions/CodeReview/codereview.css
@@ -121,3 +121,48 @@
122122 .mw-codereview-live td {
123123 border-color: #33cc99 !important;
124124 }
 125+
 126+.mw-wordcloud-size-1, .mw-wordcloud-size-2,
 127+.mw-wordcloud-size-3, .mw-wordcloud-size-4,
 128+.mw-wordcloud-size-5, .mw-wordcloud-size-6,
 129+.mw-wordcloud-size-7, .mw-wordcloud-size-8,
 130+.mw-wordcloud-size-0 {
 131+ padding: 4px 4px 4px 4px;
 132+ letter-spacing: 3px;
 133+}
 134+.mw-wordcloud-size-1 {
 135+ color: #000;
 136+ font-size: 2.4em;
 137+}
 138+.mw-wordcloud-size-2 {
 139+ color: #333;
 140+ font-size:2.2em;
 141+}
 142+.mw-wordcloud-size-3 {
 143+ color: #666;
 144+ font-size: 2.0em;
 145+}
 146+.mw-wordcloud-size-4 {
 147+ color: #999;
 148+ font-size: 1.0em;
 149+}
 150+.mw-wordcloud-size-5 {
 151+ color: #aaa;
 152+ font-size: 1.6em;
 153+}
 154+.mw-wordcloud-size-6 {
 155+ color: #bbb;
 156+ font-size: 1.4em;
 157+}
 158+.mw-wordcloud-size-7 {
 159+ color: #ccc;
 160+ font-size: 1.2em;
 161+}
 162+.mw-wordcloud-size-8 {
 163+ color: #ddd;
 164+ font-size: .8em;
 165+}
 166+mw-wordcloud-size-0 {
 167+ color: #ccc;
 168+ font-size: .6em;
 169+}
Index: trunk/extensions/CodeReview/backend/CodeRepository.php
@@ -153,7 +153,7 @@
154154 );
155155 $tags = array();
156156 foreach( $res as $row ) {
157 - $tags[] = $row->ct_tag;
 157+ $tags[$row->ct_tag] = $row->revs;
158158 }
159159 $wgMemc->set( $key, $tags, 3600 * 3 );
160160 return $tags;
Index: trunk/extensions/CodeReview/ui/CodeTagListView.php
@@ -9,12 +9,17 @@
1010
1111 function execute() {
1212 global $wgOut;
13 - $tags = $this->mRepo->getTagList();
1413 $name = $this->mRepo->getName();
15 - $text = '';
16 - foreach ( $tags as $tag ) {
17 - $text .= "* [[Special:Code/$name/tag/$tag|$tag]]\n";
18 - }
19 - $wgOut->addWikiText( $text );
 14+ $list = $this->mRepo->getTagList();
 15+
 16+ $tc = new WordCloud( $list, array( $this, 'linkCallback' ) );
 17+ $wgOut->addHTML( $tc->showCloud() );
2018 }
 19+
 20+ public function linkCallback( $tag, $weight ) {
 21+ $query = $this->mRepo->getName() . '/tag/' . $tag;
 22+ return Html::element( 'a', array(
 23+ 'href' => SpecialPage::getTitleFor( 'Code', $query )->getFullURL(),
 24+ 'class' => 'plainlinks mw-wordcloud-size-' . $weight ), $tag );
 25+ }
2126 }
Index: trunk/extensions/CodeReview/ui/WordCloud.php
@@ -0,0 +1,144 @@
 2+<?php
 3+/**
 4+ * wordCloud Copyright 2007 Derek Harvey
 5+ * www.lotsofcode.com
 6+ *
 7+ * This file is part of wordCloud.
 8+ *
 9+ * wordCloud is free software; you can redistribute it and/or modify
 10+ * it under the terms of the GNU General Public License as published by
 11+ * the Free Software Foundation; either version 2 of the License, or
 12+ * (at your option) any later version.
 13+ *
 14+ * wordCloud is distributed in the hope that it will be useful,
 15+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 17+ * GNU General Public License for more details.
 18+ *
 19+ * You should have received a copy of the GNU General Public License
 20+ * along with wordCloud; if not, write to the Free Software
 21+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 22+ *
 23+ * ---
 24+ * Adapted for use with MediaWiki, cleaned up coding style, etc on 2010-11-03
 25+ */
 26+class WordCloud {
 27+ /**
 28+ * Nice big array of words and their weights
 29+ * @var Array
 30+ */
 31+ private $wordsArray = array();
 32+
 33+ /**
 34+ * An optional callback to format the text before outputting
 35+ */
 36+ private $callback;
 37+
 38+ /*
 39+ * Constructor
 40+ *
 41+ * @param $words Array of word => rank pairs
 42+ */
 43+ public function __construct( Array $words = array(), $callback = null ) {
 44+ foreach( $words as $word => $rank ) {
 45+ $this->addWord( $word, $rank );
 46+ }
 47+ $this->callback = $callback ? $callback : array( $this, 'defaultTextCallback' );
 48+ }
 49+
 50+ /**
 51+ * Assign word to array
 52+ *
 53+ * @param $word String The word to add
 54+ * @param $value Int the weight to give it
 55+ */
 56+ public function addWord( $word, $value = 1 ) {
 57+ $word = strtolower( $word );
 58+ if( array_key_exists( $word, $this->wordsArray ) ) {
 59+ $this->wordsArray[$word] += $value;
 60+ } else {
 61+ $this->wordsArray[$word] = $value;
 62+ }
 63+ }
 64+
 65+ /**
 66+ * Calculate size of words array
 67+ *
 68+ * @return Int
 69+ */
 70+ public function getCloudSize() {
 71+ return array_sum( $this->wordsArray );
 72+ }
 73+
 74+ /**
 75+ * Create the HTML code for each word and apply font size.
 76+ *
 77+ * @return String
 78+ */
 79+ public function showCloud() {
 80+ $this->shuffleCloud();
 81+ $max = max( $this->wordsArray );
 82+ if( is_array( $this->wordsArray ) ) {
 83+ $return = '';
 84+ foreach ( $this->wordsArray as $word => $popularity ) {
 85+ $sizeRange = $this->getClassFromPercent( ( $popularity / $max ) * 100 );
 86+ $return .= call_user_func_array( $this->callback, array( $word, $sizeRange ) );
 87+ }
 88+ return '<div class="mw-wordcloud">' . $return . '</div>';
 89+ }
 90+ }
 91+
 92+ /**
 93+ * Default text callback for word display
 94+ */
 95+ public function defaultTextCallback( $word, $sizeRange ) {
 96+ return "<span class=\"mw-wordcloud-size-$sizeRange\"> &nbsp; {$word} &nbsp; </span>";
 97+ }
 98+
 99+ /**
 100+ * Shuffle associated names in array
 101+ */
 102+ private function shuffleCloud() {
 103+ $keys = array_keys( $this->wordsArray );
 104+
 105+ shuffle( $keys );
 106+
 107+ if( count( $keys ) && is_array( $keys ) ) {
 108+ $tmpArray = $this->wordsArray;
 109+ $this->wordsArray = array();
 110+ foreach( $keys as $key => $value ) {
 111+ $this->wordsArray[$value] = $tmpArray[$value];
 112+ }
 113+ }
 114+ }
 115+
 116+ /**
 117+ * Get the class range using a percentage
 118+ *
 119+ * @return Int
 120+ */
 121+ private function getClassFromPercent( $percent ) {
 122+ if ( $percent >= 99 ) {
 123+ $class = 1;
 124+ } elseif( $percent >= 70 ) {
 125+ $class = 2;
 126+ } elseif( $percent >= 60 ) {
 127+ $class = 3;
 128+ } elseif( $percent >= 50 ) {
 129+ $class = 4;
 130+ } elseif( $percent >= 40 ) {
 131+ $class = 5;
 132+ } elseif( $percent >= 30 ) {
 133+ $class = 6;
 134+ } elseif( $percent >= 20 ) {
 135+ $class = 7;
 136+ } elseif( $percent >= 10 ) {
 137+ $class = 8;
 138+ } elseif( $percent >= 5 ) {
 139+ $class = 9;
 140+ } else {
 141+ $class = 0;
 142+ }
 143+ return $class;
 144+ }
 145+}
Property changes on: trunk/extensions/CodeReview/ui/WordCloud.php
___________________________________________________________________
Added: svn:eol-style
1146 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r76083Followup r76001, use less elseifdemon12:33, 5 November 2010

Comments

#Comment by Nikerabbit (talk | contribs)   14:09, 4 November 2010

+ return "   {$word}   "; Should use CSS instead of  s. How about escaping?

#Comment by Nikerabbit (talk | contribs)   14:17, 4 November 2010

Omg I fell into the didn't preview trap. Anyway ^demon fixed that in r76005.

#Comment by Hashar (talk | contribs)   12:24, 5 November 2010

The getClassFromPercent() could probably be written with less 'elseif' :o)

#Comment by 😂 (talk | contribs)   12:25, 5 November 2010

This code was worse :p I just made a first pass at improving it.

#Comment by Hashar (talk | contribs)   20:51, 5 November 2010

You might want to add a freshness factor into it. For each X months of inactivity, it should get a Y percent malus.

#Comment by 😂 (talk | contribs)   01:55, 6 November 2010

Yeah I'm not feeling like putting much more time in this.

Status & tagging log