r40788 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r40787‎ | r40788 | r40789 >
Date:16:58, 13 September 2008
Author:siebrand
Status:old
Tags:
Comment:
Refactored a bit
* Add aliases file and localised special page name in toolbox link
* plural support for 3 messages
* using more generic toolbox hook (not only for Monobook)
* split off special page to separate class
* delay message loading
* remove wfWatchersAddCache(); didn't appear to do anything
* special page unlisted
* update extension credits, add descriptionmsg
Modified paths:
  • /trunk/extensions/Watchers/Watchers.alias.php (added) (history)
  • /trunk/extensions/Watchers/Watchers.i18n.php (modified) (history)
  • /trunk/extensions/Watchers/Watchers.php (modified) (history)
  • /trunk/extensions/Watchers/Watchers_body.php (added) (history)
  • /trunk/extensions/Watchers/Watchers_body.php (added) (history)

Diff [purge]

Index: trunk/extensions/Watchers/Watchers_body.php
@@ -0,0 +1,102 @@
 2+<?php
 3+if ( ! defined( 'MEDIAWIKI' ) )
 4+ die();
 5+/**
 6+ * Implements special page Watchers
 7+ *
 8+ * @author Magnus Manske
 9+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 10+ */
 11+
 12+class SpecialWatchers extends UnlistedSpecialPage {
 13+
 14+ /**
 15+ * Constructor
 16+ */
 17+ function __construct() {
 18+ parent::__construct( 'Watchers' );
 19+ $this->includable( true );
 20+ }
 21+
 22+ /**
 23+ * Renders the special page
 24+ */
 25+ function execute( $parameters ) {
 26+ global $wgOut , $wgRequest , $wgUser , $wgWatchersLimit;
 27+
 28+ wfLoadExtensionMessages( 'Watchers' );
 29+
 30+ $out = "";
 31+ $fname = "wfWatchersExtension::execute";
 32+ $sk =&$wgUser->getSkin();
 33+ $id = $wgRequest->getInt ( 'article' , 0 );
 34+ $title = Title::newFromID ( $id );
 35+
 36+ # Check for valid title
 37+ if ( $id == 0 || $title->getArticleID() <= 0 ) {
 38+ $out = wfMsg ( 'watchers_error_article' );
 39+ $this->setHeaders();
 40+ $wgOut->addHtml( $out );
 41+ return;
 42+ }
 43+
 44+ $dbr =& wfGetDB( DB_SLAVE );
 45+ $conds = array (
 46+ 'wl_namespace' => $title->getNamespace() ,
 47+ 'wl_title' => $title->getDBkey() ,
 48+ );
 49+
 50+ $watcherscountquery = $dbr->select(
 51+ /* FROM */ 'watchlist',
 52+ /* SELECT */ 'count(wl_user) AS num',
 53+ /* WHERE */ $conds,
 54+ $fname
 55+ );
 56+
 57+ $o = $dbr->fetchObject( $watcherscountquery );
 58+ $watcherscount = $o->num;
 59+
 60+ $link1 = $sk->makeLinkObj( $title );
 61+ $out .= "<h2>" . wfMsgExt( 'watchers_header' , array( 'parsemag' ), $link1, $watcherscount ) . "</h2>";
 62+
 63+ if ( $wgWatchersLimit != NULL ) {
 64+
 65+ if ( $watcherscount >= $wgWatchersLimit ) {
 66+ $out .= "<p>" . wfMsgExt( 'watchers_x_or_more', array( 'parsemag' ), $wgWatchersLimit ) . "</p>\n";
 67+ } else {
 68+ $out .= "<p>" . wfMsgExt( 'watchers_less_than_x', array( 'parsemag' ), $wgWatchersLimit ) . "</p>\n";
 69+ }
 70+ } else {
 71+ $res = $dbr->select(
 72+ /* FROM */ 'watchlist',
 73+ /* SELECT */ 'wl_user',
 74+ /* WHERE */ $conds,
 75+ $fname
 76+ );
 77+ $user_ids = array ();
 78+ while ( $o = $dbr->fetchObject( $res ) ) {
 79+ $user_ids[] = $o->wl_user;
 80+ }
 81+ $dbr->freeResult( $res );
 82+
 83+ if ( count ( $user_ids ) == 0 ) {
 84+ $out .= "<p>" . wfMsg ( 'watchers_noone_watches' ) . "</p>";
 85+ } else {
 86+ $out .= "<ol>";
 87+ foreach ( $user_ids AS $uid ) {
 88+ $u = new User;
 89+ $u->setID ( $uid );
 90+ $u->loadFromDatabase();
 91+ $link = $sk->makeLinkObj ( $u->getUserPage() );
 92+ $out .= "<li>" . $link . "</li>\n";
 93+ }
 94+ $out .= "</ol>";
 95+ }
 96+ }
 97+
 98+ $dbr->freeResult( $watcherscountquery );
 99+
 100+ $this->setHeaders();
 101+ $wgOut->addHtml( $out );
 102+ }
 103+}
Property changes on: trunk/extensions/Watchers/Watchers_body.php
___________________________________________________________________
Added: svn:mergeinfo
Added: svn:eol-style
1104 + native
Added: svn:keywords
2105 + Author Date Id Revision
Index: trunk/extensions/Watchers/Watchers.i18n.php
@@ -8,13 +8,14 @@
99 $messages = array();
1010
1111 $messages['en'] = array(
12 - 'watchers' => 'Watchers',
13 - 'watchers_link_title' => 'Who watches this page?',
14 - 'watchers_error_article' => '<b>Error:</b> Page does not exist.',
15 - 'watchers_header' => 'People who are watching "$1"',
16 - 'watchers_noone_watches' => 'Noone watches this page.',
17 - 'watchers_x_or_more' => '$1 or more people are watching this page.',
18 - 'watchers_less_than_x' => 'Less than $1 people watch this page.',
 12+ 'watchers' => 'Watchers',
 13+ 'watchers-desc' => 'Shows [[Special:Watchers|which users have a page on their watchlist]]',
 14+ 'watchers_link_title' => 'Who watches this page?',
 15+ 'watchers_error_article' => "'''Error:''' Page does not exist.",
 16+ 'watchers_header' => '{{PLURAL:$2|User who is|Users who are}} watching "$1"',
 17+ 'watchers_noone_watches' => 'No one watches this page.',
 18+ 'watchers_x_or_more' => '$1 or more {{PLURAL:$1|users|users}} have this page on their watchlist.',
 19+ 'watchers_less_than_x' => 'Fewer than $1 {{PLURAL:$1|users|users}} have this page on their watchlist.',
1920 );
2021
2122 /** Message documentation (Message documentation)
Index: trunk/extensions/Watchers/Watchers.php
@@ -1,4 +1,5 @@
22 <?php
 3+if( !defined( 'MEDIAWIKI' ) ) die();
34 /*
45 Creates a link in the toolbox to a special page showing who watches that page
56
@@ -12,45 +13,26 @@
1314 to a number to anonymize results ("X or more" / "Fewer than X" people watching this page)
1415 */
1516
16 -
17 -if( !defined( 'MEDIAWIKI' ) ) die();
18 -
19 -# Integrating into the MediaWiki environment
20 -
21 -$wgExtensionCredits['Watchers'][] = array(
22 - 'name' => 'Watchers',
23 - 'description' => 'An extension to show who is watching a page.',
24 - 'author' => 'Magnus Manske'
 17+$wgExtensionCredits['other'][] = array(
 18+ 'name' => 'Watchers',
 19+ 'version' => '0.2',
 20+ 'author' => 'Magnus Manske',
 21+ 'url' => 'http://www.mediawiki.org/wiki/Extension:Watchers',
 22+ 'description' => 'An extension to show who is watching a page.',
 23+ 'descriptionmsg' => 'watchers-desc',
2524 );
2625
2726 $dir = dirname(__FILE__) . '/';
 27+$wgExtensionAliasesFiles['Watchers'] = $dir . 'Watchers.alias.php';
2828 $wgExtensionMessagesFiles['Watchers'] = $dir . 'Watchers.i18n.php';
 29+$wgAutoloadClasses['SpecialWatchers'] = $dir . 'Watchers_body.php';
 30+$wgSpecialPages['Watchers'] = 'SpecialWatchers';
 31+$wgHooks['SkinTemplateToolboxEnd'][] = 'wfWatchersExtensionAfterToolbox';
2932
30 -$wgExtensionFunctions[] = 'wfWatchersExtension';
31 -
32 -
33 -# Hooks
34 -$wgHooks['MonoBookTemplateToolboxEnd'][] = 'wfWatchersExtensionAfterToolbox';
35 -
36 -# Global variables
37 -
38 -$wgWatchersLimit = NULL ; # Set this to a number to anonymize results ("X or more" / "Less that X" people watching this page)
 33+$wgWatchersLimit = NULL; # Set this to a number to anonymize results ("X or more" / "Less that X" people watching this page)
3934 $wgWatchersAddCache = false ; # Internal use
4035
41 -
4236 /**
43 - * Text adding function
44 -*/
45 -function wfWatchersAddCache() { # Checked for HTML and MySQL insertion attacks
46 - global $wgWatchersAddCache , $wgUserTogglesEn, $wgDefaultUserOptions;
47 - if( $wgWatchersAddCache ) {
48 - return;
49 - }
50 - wfLoadExtensionMessages( 'Watchers' );
51 - $wgWatchersAddCache = true;
52 -}
53 -
54 -/**
5537 * Display link in toolbox
5638 */
5739 function wfWatchersExtensionAfterToolbox( &$tpl ) { # Checked for HTML and MySQL insertion attacks
@@ -59,125 +41,20 @@
6042 # No talk pages please
6143 return true;
6244 }
 45+
6346 if( $wgTitle->getNamespace() < 0 ) {
6447 # No special pages please
6548 return true;
6649 }
6750
68 - wfWatchersAddCache();
 51+ wfLoadExtensionMessages( 'Watchers' );
6952
70 -
7153 echo '<li id="t-watchers"><a href="' ;
72 - $nt = Title::newFromText ( 'watchers' , NS_SPECIAL ) ;
 54+ $nt = SpecialPage::getTitleFor( 'Watchers' );
7355 echo $nt->escapeLocalURL ( 'article=' . $wgTitle->getArticleID() );
74 - echo '">' ;
 56+ echo '">';
7557 echo wfMsg('watchers_link_title');
76 - echo "</a></li>\n" ;
 58+ echo "</a></li>\n";
7759
7860 return true;
7961 }
80 -
81 -/**
82 - * The special page
83 -*/
84 -function wfWatchersExtension() {
85 - global $IP;
86 - wfWatchersAddCache();
87 -
88 - require_once $IP.'/includes/SpecialPage.php';
89 -
90 - class SpecialWatchers extends SpecialPage {
91 -
92 - /**
93 - * Constructor
94 - */
95 - function SpecialWatchers() {
96 - SpecialPage::SpecialPage( 'Watchers' );
97 - $this->includable( true );
98 - }
99 -
100 - /**
101 - * @see SpecialPage::getDescription
102 - */
103 - function getDescription() {
104 - return wfMsg( 'watchers' );
105 - }
106 -
107 - /**
108 - * Renders the special page
109 - */
110 - function execute( $par = null ) {
111 - global $wgOut , $wgRequest , $wgUser , $wgWatchersLimit ;
112 -
113 - $out = "" ;
114 - $fname = "wfWatchersExtension::execute" ;
115 - $sk =& $wgUser->getSkin() ;
116 - $id = $wgRequest->getInt ( 'article' , 0 ) ;
117 - $title = Title::newFromID ( $id ) ;
118 -
119 - # Check for valid title
120 - if ( $id == 0 || $title->getArticleID() <= 0 ) {
121 - $out = wfMsg ( 'watchers_error_article' ) ;
122 - $this->setHeaders();
123 - $wgOut->addHtml( $out );
124 - return ;
125 - }
126 -
127 - $link1 = $sk->makeLinkObj( $title );
128 - $out .= "<h2>" . wfMsg ( 'watchers_header' , $link1 ) . "</h2>" ;
129 -
130 - $dbr =& wfGetDB( DB_SLAVE );
131 - $conds = array (
132 - 'wl_namespace' => $title->getNamespace() ,
133 - 'wl_title' => $title->getDBkey() ,
134 - ) ;
135 - if ( $wgWatchersLimit != NULL ) {
136 - $res = $dbr->select(
137 - /* FROM */ 'watchlist',
138 - /* SELECT */ 'count(wl_user) AS num',
139 - /* WHERE */ $conds,
140 - $fname
141 - );
142 - $o = $dbr->fetchObject( $res ) ;
143 - if ( $o->num >= $wgWatchersLimit ) {
144 - $out .= "<p>" . wfMsg ( 'watchers_x_or_more' , $wgWatchersLimit ) . "</p>\n" ;
145 - } else {
146 - $out .= "<p>" . wfMsg ( 'watchers_less_than_x' , $wgWatchersLimit ) . "</p>\n" ;
147 - }
148 - } else {
149 - $res = $dbr->select(
150 - /* FROM */ 'watchlist',
151 - /* SELECT */ 'wl_user',
152 - /* WHERE */ $conds,
153 - $fname
154 - );
155 - $user_ids = array () ;
156 - while ( $o = $dbr->fetchObject( $res ) ) {
157 - $user_ids[] = $o->wl_user ;
158 - }
159 - $dbr->freeResult( $res );
160 -
161 - if ( count ( $user_ids ) == 0 ) {
162 - $out .= "<p>" . wfMsg ( 'watchers_noone_watches' ) . "</p>" ;
163 - } else {
164 - $out .= "<ol>" ;
165 - foreach ( $user_ids AS $uid ) {
166 - $u = new User ;
167 - $u->setID ( $uid ) ;
168 - $u->loadFromDatabase() ;
169 - $link = $sk->makeLinkObj ( $u->getUserPage() ) ;
170 - $out .= "<li>" . $link . "</li>\n" ;
171 - }
172 - $out .= "</ol>" ;
173 - }
174 - }
175 -
176 - $this->setHeaders();
177 - $wgOut->addHtml( $out );
178 - }
179 -
180 -
181 - } # end of class
182 -
183 - SpecialPage::addPage( new SpecialWatchers );
184 -}
Index: trunk/extensions/Watchers/Watchers.alias.php
@@ -0,0 +1,22 @@
 2+<?php
 3+/**
 4+ * Aliases for special pages of Watchers extension.
 5+ *
 6+ * @addtogroup Extensions
 7+ */
 8+
 9+$aliases = array();
 10+
 11+/** English
 12+ * @author Magnus Manske
 13+ */
 14+$aliases['en'] = array(
 15+ 'Watchers' => array( 'Watchers' ),
 16+);
 17+
 18+/** English
 19+ * @author Siebrand
 20+ */
 21+$aliases['nl'] = array(
 22+ 'Watchers' => array( 'Volgers' ),
 23+);
Property changes on: trunk/extensions/Watchers/Watchers.alias.php
___________________________________________________________________
Added: svn:eol-style
124 + native
Added: svn:keywords
225 + Id