r88455 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r88454‎ | r88455 | r88456 >
Date:15:34, 20 May 2011
Author:ashley
Status:deferred
Tags:
Comment:
EditSimilar: documentation update, coding style tweaks, etc.
Modified paths:
  • /trunk/extensions/EditSimilar/EditSimilar.class.php (modified) (history)
  • /trunk/extensions/EditSimilar/EditSimilar.php (modified) (history)

Diff [purge]

Index: trunk/extensions/EditSimilar/EditSimilar.class.php
@@ -1,61 +1,109 @@
22 <?php
3 -/*
4 - How this extension works:
5 - - upon save, the script searches for articles that are similar
6 - right now, I have assumed the following criteria:
7 - * articles that need attention
8 - * articles similar in category to the one we edited
9 - * if no similar articles were found, we're taking results straight from categories that need attention
10 - * number of articles in result is limited
 3+/**
 4+ * How this extension works:
 5+ * - upon save, the script searches for articles that are similar
 6+ * right now, I have assumed the following criteria:
 7+ * ** articles that need attention
 8+ * ** articles similar in category to the one we edited
 9+ * ** if no similar articles were found, we're taking results straight from
 10+ * categories that need attention
 11+ * ** number of articles in result is limited
 12+ *
 13+ * IMPORTANT NOTE: This extension REQUIRES the article
 14+ * MediaWiki:EditSimilar-Categories to exist on your wiki in order to run.
 15+ * If this article is nonexistent, the extension will disable itself.
 16+ *
 17+ * Format of the article is as follows:
 18+ * * Chosen Stub Category 1
 19+ * * Chosen Stub Category 2
 20+ * etc. (separated by stars)
 21+ *
 22+ * Insert '-' if you want to disable the extension without blanking the
 23+ * commanding article.
 24+ *
 25+ * @file
 26+ */
1127
12 - IMPORTANT NOTE: This extension REQUIRES the article MediaWiki:EditSimilar-Categories to exist on your
13 - wiki in order to run. If this article is nonexistent, the extension will disable itself.
14 - Format of the article is as follows:
15 - * Chosen Stub Category 1
16 - * Chosen Stub Category 2
17 - etc. (separated by stars)
 28+class EditSimilar {
 29+ var $mBaseArticle; // the article from which we hail in our quest for similiarities, this is its title
1830
19 - insert '-' if you want to disable the extension without blanking the commanding article
 31+ /**
 32+ * @var String: how do we mark articles that need attention? Currently, by
 33+ * category only
 34+ */
 35+ var $mMarkerType;
2036
21 -*/
 37+ /**
 38+ * @var Array: the marker array (for now it contains categories)
 39+ */
 40+ var $mAttentionMarkers;
2241
23 -class EditSimilar {
24 - var $mBaseArticle; // the article from which we hail in our quest for similiarities, this is its title
25 - var $mMarkerType; // how do we mark articles that need attention? currently, by category only
26 - var $mAttentionMarkers; // the marker array (for now it contains categories)
27 - var $mMatchType; // how do we match articles as a secondary
28 - var $mPoolLimit; // limit up the pool of 'stubs' to choose from
29 - var $mBaseCategories; // extracted categories that this saved article is in
30 - var $mSimilarArticles; // to differentiate between really similar results or just needing attention
 42+ /**
 43+ * @var Integer: limit up the pool of 'stubs' to choose from, controlled
 44+ * via the $wgEditSimilarMaxResultsPool global variable
 45+ */
 46+ var $mPoolLimit;
3147
32 - // constructor
33 - function __construct( $article, $markertype = 'category' ) {
 48+ /**
 49+ * @var Array: array of extracted categories that this saved article is in
 50+ */
 51+ var $mBaseCategories;
 52+
 53+ /**
 54+ * @var Boolean: to differentiate between really similar results or just
 55+ * needing attention
 56+ */
 57+ var $mSimilarArticles;
 58+
 59+ /**
 60+ * Constructor
 61+ *
 62+ * @param $article Integer: article ID number
 63+ * @param $markerType String: always 'category'
 64+ */
 65+ public function __construct( $article, $markerType = 'category' ) {
3466 global $wgEditSimilarMaxResultsPool;
3567 $this->mBaseArticle = $article;
36 - $this->mMarkerType = $markertype;
 68+ $this->mMarkerType = $markerType;
3769 $this->mAttentionMarkers = $this->getStubCategories();
3870 $this->mPoolLimit = $wgEditSimilarMaxResultsPool;
3971 $this->mBaseCategories = $this->getBaseCategories();
4072 $this->mSimilarArticles = true;
4173 }
4274
43 - // fetch categories marked as 'stub categories'
 75+ /**
 76+ * Fetch categories marked as 'stub categories', controlled via the
 77+ * MediaWiki:EditSimilar-Categories interface message.
 78+ *
 79+ * @return Array|Boolean: array of category names on success, false on
 80+ * failure (if MediaWiki:EditSimilar-Categories is
 81+ * empty or contains -)
 82+ */
4483 function getStubCategories() {
45 - $stub_categories = wfMsgForContent( 'EditSimilar-Categories' );
46 - if ( ( '&lt;EditSimilar-Categories&gt;' == $stub_categories ) || ( '' == $stub_categories ) || ( '-' == $stub_categories ) ) {
 84+ $stubCategories = wfMsgForContent( 'EditSimilar-Categories' );
 85+ if (
 86+ ( '&lt;EditSimilar-Categories&gt;' == $stubCategories ) ||
 87+ ( '' == $stubCategories ) || ( '-' == $stubCategories )
 88+ )
 89+ {
4790 return false;
4891 } else {
49 - $lines = preg_split( "/\*/", $stub_categories );
50 - $normalised_lines = array();
 92+ $lines = preg_split( '/\*/', $stubCategories );
 93+ $normalisedLines = array();
5194 array_shift( $lines );
5295 foreach ( $lines as $line ) {
53 - $normalised_lines[] = str_replace( ' ', '_', trim( $line ) );
 96+ $normalisedLines[] = str_replace( ' ', '_', trim( $line ) );
5497 }
55 - return $normalised_lines;
 98+ return $normalisedLines;
5699 }
57100 }
58101
59 - // this is the main function that returns articles we deem similar or worth showing
 102+ /**
 103+ * Main function that returns articles we deem similar or worth showing
 104+ *
 105+ * @return Array|Boolean: array of article names on success, false on
 106+ * failure
 107+ */
60108 function getSimilarArticles() {
61109 global $wgUser, $wgEditSimilarMaxResultsToDisplay;
62110
@@ -66,8 +114,15 @@
67115 $articles = array();
68116 $x = 0;
69117
70 - while ( ( count( $articles ) < $wgEditSimilarMaxResultsToDisplay ) && ( $x < count( $this->mAttentionMarkers ) ) ) {
71 - $articles = array_merge( $articles, $this->getResults( $this->mAttentionMarkers[$x] ) );
 118+ while (
 119+ ( count( $articles ) < $wgEditSimilarMaxResultsToDisplay ) &&
 120+ ( $x < count( $this->mAttentionMarkers ) )
 121+ )
 122+ {
 123+ $articles = array_merge(
 124+ $articles,
 125+ $this->getResults( $this->mAttentionMarkers[$x] )
 126+ );
72127 if ( !empty( $articles ) ) {
73128 $articles = array_unique( $articles );
74129 }
@@ -84,42 +139,51 @@
85140 $this->mSimilarArticles = false;
86141 }
87142
88 - if ( 1 == count( $articles ) ) { // in this case, array_rand returns a single element, not an array
 143+ if ( count( $articles ) == 1 ) { // in this case, array_rand returns a single element, not an array
89144 $rand_articles = array( 0 );
90145 } else {
91 - $rand_articles = array_rand( $articles, min( $wgEditSimilarMaxResultsToDisplay, count( $articles ) ) );
 146+ $rand_articles = array_rand(
 147+ $articles,
 148+ min( $wgEditSimilarMaxResultsToDisplay, count( $articles ) )
 149+ );
92150 }
 151+
93152 $sk = $wgUser->getSkin();
94 - $skinname = get_class( $sk );
95 - $skinname = strtolower( substr( $skinname, 4 ) );
96 - $real_rand_values = array();
 153+ $realRandValues = array();
 154+
97155 if ( empty( $rand_articles ) ) {
98156 return false;
99157 }
100158
101 - $translated_titles = array();
 159+ $translatedTitles = array();
102160 foreach ( $rand_articles as $r_key => $rand_article_key ) {
103 - $translated_titles[] = $articles [$rand_article_key];
 161+ $translatedTitles[] = $articles[$rand_article_key];
104162 }
105 - $translated_titles = $this->idsToTitles( $translated_titles );
 163+ $translatedTitles = $this->idsToTitles( $translatedTitles );
106164
107 - foreach ( $translated_titles as $link_title ) {
108 - $article_link = $sk->makeKnownLinkObj( $link_title );
109 - $real_rand_values[] = $article_link;
 165+ foreach ( $translatedTitles as $linkTitle ) {
 166+ $articleLink = $sk->makeKnownLinkObj( $linkTitle );
 167+ $realRandValues[] = $articleLink;
110168 }
111169
112 - return $real_rand_values;
 170+ return $realRandValues;
113171 }
114172
115 - // extract all categories our base article is in
 173+ /**
 174+ * Extract all categories our base article is in
 175+ *
 176+ * @return Array|Boolean: array of category names on success, false on
 177+ * failure
 178+ */
116179 function getBaseCategories() {
117180 global $wgEditSimilarMaxResultsToDisplay;
 181+
118182 if ( empty( $this->mAttentionMarkers ) || !$this->mAttentionMarkers ) {
119183 return false;
120184 }
121185
122186 $dbr = wfGetDB( DB_SLAVE );
123 - $result_array = array();
 187+ $resultArray = array();
124188 $res = $dbr->select(
125189 array( 'categorylinks' ),
126190 array( 'cl_to' ),
@@ -130,127 +194,166 @@
131195 'USE_INDEX' => 'cl_from'
132196 )
133197 );
134 - while ( $x = $dbr->fetchObject( $res ) ) {
 198+
 199+ foreach( $res as $x ) {
135200 if ( !in_array( $x->cl_to, $this->mAttentionMarkers ) ) {
136 - $result_array[] = $x->cl_to;
 201+ $resultArray[] = $x->cl_to;
137202 }
138203 }
139204
140 - if ( !empty( $result_array ) ) {
141 - return $result_array;
 205+ if ( !empty( $resultArray ) ) {
 206+ return $resultArray;
142207 } else {
143208 return false;
144209 }
145210 }
146211
147 - /*
148 - latest addition: if we got no results at all (indicating that:
149 - A - the article had no categories,
150 - B - the article had no relevant results for its categories)
151 -
152 - this is to ensure we can get always (well, almost - if "marker" categories get no results, it's dead in the water anyway)
153 - some results
154 - */
 212+ /**
 213+ * Latest addition: if we got no results at all (indicating that:
 214+ * A - the article had no categories,
 215+ * B - the article had no relevant results for its categories)
 216+ *
 217+ * This is to ensure we can get always (well, almost - if "marker"
 218+ * categories get no results, it's dead in the water anyway) some results.
 219+ *
 220+ * @return Array: array of category names
 221+ */
155222 function getAdditionalCheck() {
156223 $dbr = wfGetDB( DB_SLAVE );
157224
158 - $fixed_names = array();
 225+ $fixedNames = array();
159226 foreach ( $this->mAttentionMarkers as $category ) {
160 - $fixed_names[] = $dbr->addQuotes( $category );
 227+ $fixedNames[] = $dbr->addQuotes( $category );
161228 }
162 - $stringed_names = implode( ",", $fixed_names );
 229+ $stringedNames = implode( ',', $fixedNames );
163230
164231 $res = $dbr->select(
165232 'categorylinks',
166233 array( 'cl_from' ),
167 - array( "cl_to IN ($stringed_names)" ),
 234+ array( "cl_to IN ($stringedNames)" ),
168235 __METHOD__
169236 );
170237
171 - $result_array = array();
172 - while ( $x = $dbr->fetchObject( $res ) ) {
 238+ $resultArray = array();
 239+ foreach( $res as $x ) {
173240 if ( $this->mBaseArticle != $x->cl_from ) {
174 - $result_array[] = $x->cl_from;
 241+ $resultArray[] = $x->cl_from;
175242 }
176243 }
177 - $dbr->freeResult( $res );
178244
179 - return $result_array;
 245+ return $resultArray;
180246 }
181247
182 - // one function to turn result ids into titles in one query rather than multiple ones
183 - function idsToTitles( $id_array ) {
 248+ /**
 249+ * Turn result IDs into Title objects in one query rather than multiple
 250+ * ones.
 251+ *
 252+ * @param $idArray Array: array of page ID numbers
 253+ * @return Array: array of Title objects
 254+ */
 255+ function idsToTitles( $idArray ) {
184256 global $wgContentNamespaces;
 257+
185258 $dbr = wfGetDB( DB_SLAVE );
186 - $stringed_names = implode( ",", $id_array );
 259+ $stringedNames = implode( ',', $idArray );
187260 $res = $dbr->select(
188261 'page',
189262 array( 'page_namespace', 'page_title' ),
190 - array( "page_id IN ($stringed_names)" ),
 263+ array( "page_id IN ($stringedNames)" ),
191264 __METHOD__
192265 );
193266
194 - $result_array = array();
 267+ $resultArray = array();
195268
196269 // so for now, to speed things up, just discard results from other namespaces (and subpages)
197 - while ( ( $x = $dbr->fetchObject( $res ) )
198 - && ( in_array( $x->page_namespace, $wgContentNamespaces ) )
199 - && false === strpos( $x->page_title, '/' ) ) {
200 - $result_array[] = Title::makeTitle( $x->page_namespace, $x->page_title );
 270+ while (
 271+ ( $x = $dbr->fetchObject( $res ) ) &&
 272+ ( in_array( $x->page_namespace, $wgContentNamespaces ) ) &&
 273+ strpos( $x->page_title, '/' ) === false
 274+ )
 275+ {
 276+ $resultArray[] = Title::makeTitle(
 277+ $x->page_namespace,
 278+ $x->page_title
 279+ );
201280 }
202281
203282 $dbr->freeResult( $res );
204 - return $result_array;
 283+ return $resultArray;
205284 }
206285
207 - // get categories from the 'stub' or 'attention needed' category
208 - function getResults( $marker_category ) {
 286+ /**
 287+ * Get categories from the 'stub' or 'attention needed' category
 288+ *
 289+ * @param $markerCategory String: category name
 290+ * @return Array: array of category names
 291+ */
 292+ function getResults( $markerCategory ) {
209293 $dbr = wfGetDB( DB_SLAVE );
210 - $title = Title::makeTitle( NS_CATEGORY, $marker_category );
211 - $result_array = array();
 294+ $title = Title::makeTitle( NS_CATEGORY, $markerCategory );
 295+ $resultArray = array();
212296
213297 if ( empty( $this->mBaseCategories ) ) {
214 - return $result_array;
 298+ return $resultArray;
215299 }
216300
 301+ // @todo CHECKME: is it possible to make this query use MediaWiki's
 302+ // Database functions? If so, rewrite it!
217303 $query = "SELECT c1.cl_from
218304 FROM {$dbr->tableName( 'categorylinks' )} AS c1, {$dbr->tableName( 'categorylinks' )} AS c2
219305 WHERE c1.cl_from = c2.cl_from
220306 AND c1.cl_to = " . $dbr->addQuotes( $title->getDBkey() ) . "
221307 AND c2.cl_to IN (";
222308
223 - $fixed_names = array();
 309+ $fixedNames = array();
224310 foreach ( $this->mBaseCategories as $category ) {
225 - $fixed_names[] = $dbr->addQuotes( $category );
 311+ $fixedNames[] = $dbr->addQuotes( $category );
226312 }
227 - $stringed_names = implode( ",", $fixed_names );
228 - $query .= $stringed_names . ")";
 313+ $stringed_names = implode( ',', $fixedNames );
 314+ $query .= $stringed_names . ')';
229315
230316 $res = $dbr->query( $query, __METHOD__ );
231 - while ( $x = $dbr->fetchObject( $res ) ) {
 317+ foreach( $res as $x ) {
232318 if ( $this->mBaseArticle != $x->cl_from ) {
233 - $result_array[] = $x->cl_from;
 319+ $resultArray[] = $x->cl_from;
234320 }
235321 }
236 - $dbr->freeResult( $res );
237322
238 - return $result_array;
 323+ return $resultArray;
239324 }
240325
241 - // message box wrapper
242 - static public function showMessage( $text ) {
 326+ /**
 327+ * Message box wrapper
 328+ *
 329+ * @param $text String: message to show
 330+ */
 331+ public static function showMessage( $text ) {
243332 global $wgOut, $wgUser, $wgScript, $wgScriptPath;
 333+
244334 $wgOut->addExtensionStyle( $wgScriptPath . '/extensions/EditSimilar/EditSimilar.css' );
 335+
 336+ // If the user is logged in, give them a link to their preferences in
 337+ // case if they want to disable EditSimilar suggestions
245338 if ( $wgUser->isLoggedIn() ) {
246 - $link = '<div class="editsimilar_dismiss">[<span class="plainlinks"><a href="' . $wgScript . '?title=Special:Preferences#prefsection-4" id="editsimilar_preferences">' . wfMsg( 'editsimilar-link-disable' ) . '</a></span>]</div><div style="display:block">&#160;</div>';
 339+ $link = '<div class="editsimilar_dismiss">[<span class="plainlinks"><a href="' .
 340+ $wgScript . '?title=Special:Preferences#prefsection-4" id="editsimilar_preferences">' .
 341+ wfMsg( 'editsimilar-link-disable' ) .
 342+ '</a></span>]</div><div style="display:block">&#160;</div>';
247343 } else {
248344 $link = '';
249345 }
250 - $wgOut->addHTML( '<div id="editsimilar_links" class="usermessage editsimilar"><div>' . $text . '</div>' . $link . '</div>' );
 346+ $wgOut->addHTML(
 347+ '<div id="editsimilar_links" class="usermessage editsimilar"><div>' .
 348+ $text . '</div>' . $link . '</div>'
 349+ );
251350 }
252351
253 - // this is for determining whether to display the message or not
254 - static public function checkCounter() {
 352+ /**
 353+ * For determining whether to display the message or not
 354+ *
 355+ * @return Boolean: true to show the message, false to not show it
 356+ */
 357+ public static function checkCounter() {
255358 global $wgEditSimilarCounterValue;
256359 if ( isset( $_SESSION['ES_counter'] ) ) {
257360 $_SESSION['ES_counter']--;
Index: trunk/extensions/EditSimilar/EditSimilar.php
@@ -8,10 +8,12 @@
99 * @author Łukasz Garczewski (TOR) <tor@wikia-inc.com>
1010 * @copyright Copyright © 2008, Wikia Inc.
1111 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 12+ * @link http://www.mediawiki.org/wiki/Extension:EditSimilar Documentation
1213 */
1314
14 -if ( !defined( 'MEDIAWIKI' ) )
 15+if ( !defined( 'MEDIAWIKI' ) ) {
1516 die( "This is not a valid entry point.\n" );
 17+}
1618
1719 // Internationalization file
1820 $dir = dirname( __FILE__ ) . '/';
@@ -42,28 +44,46 @@
4345 'descriptionmsg' => 'editsimilar-desc',
4446 );
4547
46 -// check if we had the extension enabled at all and if this is in a content namespace
 48+/**
 49+ * Check if we had the extension enabled at all and if the current page is in a
 50+ * content namespace.
 51+ *
 52+ * @param $article Object: Article object
 53+ * @return Boolean: true
 54+ */
4755 function wfEditSimilarCheck( $article ) {
4856 global $wgUser, $wgContentNamespaces;
4957
5058 $namespace = $article->getTitle()->getNamespace();
51 - if ( ( 1 == $wgUser->getOption( 'edit-similar', 1 ) ) && ( in_array( $namespace, $wgContentNamespaces ) ) ) {
 59+ if (
 60+ ( $wgUser->getOption( 'edit-similar', 1 ) == 1 ) &&
 61+ ( in_array( $namespace, $wgContentNamespaces ) )
 62+ )
 63+ {
5264 $_SESSION['ES_saved'] = 'yes';
5365 }
5466 return true;
5567 }
5668
57 -// view message depending on settings and the relevancy of the results
 69+/**
 70+ * Show a message, depending on settings and the relevancy of the results.
 71+ *
 72+ * @param $out Object: OutputPage instance
 73+ * @return Boolean: true
 74+ */
5875 function wfEditSimilarViewMesg( &$out ) {
5976 global $wgUser, $wgEditSimilarAlwaysShowThanks;
6077
61 -
62 -
63 - if ( !empty( $_SESSION['ES_saved'] ) && ( 1 == $wgUser->getOption( 'edit-similar', 1 ) ) && $out->isArticle() ) {
 78+ if (
 79+ !empty( $_SESSION['ES_saved'] ) &&
 80+ ( $wgUser->getOption( 'edit-similar', 1 ) == 1 ) &&
 81+ $out->isArticle()
 82+ )
 83+ {
6484 if ( EditSimilar::checkCounter() ) {
6585 $message_text = '';
6686 $title = $out->getTitle();
67 - $article_title = $title->getText();
 87+ $articleTitle = $title->getText();
6888 // here we'll populate the similar articles and links
6989 $instance = new EditSimilar( $title->getArticleId(), 'category' );
7090 $similarities = $instance->getSimilarArticles();
@@ -72,14 +92,14 @@
7393 global $wgLang;
7494
7595 if ( $instance->mSimilarArticles ) {
76 - $message_text = wfMsgExt(
 96+ $messageText = wfMsgExt(
7797 'editsimilar-thanks',
7898 array( 'parsemag' ),
7999 $wgLang->listToText( $similarities ),
80100 count( $similarities )
81101 );
82102 } else { // the articles we found were rather just articles needing attention
83 - $message_text = wfMsgExt(
 103+ $messageText = wfMsgExt(
84104 'editsimilar-thanks-notsimilar',
85105 array( 'parsemag' ),
86106 $wgLang->listToText( $similarities ),
@@ -88,12 +108,12 @@
89109 }
90110 } else {
91111 if ( $wgUser->isLoggedIn() && !empty( $wgEditSimilarAlwaysShowThanks ) ) {
92 - $message_text = wfMsg( 'editsimilar-thankyou', $wgUser->getName() );
 112+ $messageText = wfMsg( 'editsimilar-thankyou', $wgUser->getName() );
93113 }
94114 }
95115
96 - if ( '' != $message_text ) {
97 - EditSimilar::showMessage( $message_text, $article_title );
 116+ if ( $messageText != '' ) {
 117+ EditSimilar::showMessage( $messageText, $articleTitle );
98118 }
99119 }
100120 // display that only once
@@ -103,15 +123,14 @@
104124 }
105125
106126 /**
107 - * Adds the new toggle to Special:Preferences for enabling EditSimilar extension on a per-user basis
 127+ * Adds the new toggle to Special:Preferences for enabling EditSimilar
 128+ * extension on a per-user basis.
108129 *
109130 * @param $user User object
110131 * @param $preferences Preferences object
111 - * @return true
 132+ * @return Boolean: true
112133 */
113134 function wfEditSimilarToggle( $user, &$preferences ) {
114 -
115 -
116135 $preferences['edit-similar'] = array(
117136 'type' => 'toggle',
118137 'section' => 'editing',

Status & tagging log