r62468 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r62467‎ | r62468 | r62469 >
Date:16:32, 14 February 2010
Author:ialex
Status:ok
Tags:
Comment:
Changes to Watchers extension:
* Added new right 'watchers-list', users having it will see the complete list of watching users, even if $wgWatchersLimit is set
* Changed "article" parameter to "page", now using the page name instead of its id, so that it works for non-existing pages
* Allow to pass the "page" parameter as subpage paramters to make links easier
* Optimised a bit to not run "user list" query if no user are watching the page
* Now using $wgOut->(add|wrap)WikiMsg() instead of $wgOut->addHTML( wfMsg( ... ) )
* Removed unused $wgWatchersAddCache
* Removed a PHP4-ism
* Changed $fname to __METHOD__
* Spacing tweaks
Modified paths:
  • /trunk/extensions/Watchers/Watchers.i18n.php (modified) (history)
  • /trunk/extensions/Watchers/Watchers.php (modified) (history)
  • /trunk/extensions/Watchers/Watchers_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Watchers/Watchers_body.php
@@ -21,82 +21,82 @@
2222 /**
2323 * Renders the special page
2424 */
25 - function execute( $parameters ) {
26 - global $wgOut , $wgRequest , $wgUser , $wgWatchersLimit;
 25+ function execute( $par ) {
 26+ global $wgOut, $wgRequest, $wgUser, $wgWatchersLimit;
2727
2828 wfLoadExtensionMessages( 'Watchers' );
2929
30 - $out = "";
31 - $fname = "wfWatchersExtension::execute";
32 - $sk =&$wgUser->getSkin();
33 - $id = $wgRequest->getInt ( 'article' , 0 );
34 - $title = Title::newFromID ( $id );
 30+ $this->setHeaders();
3531
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;
 32+ $page = $wgRequest->getVal( 'page', $par );
 33+ if ( $page !== null ) {
 34+ $title = Title::newFromText( $page );
 35+ if ( !$title instanceof Title ) {
 36+ $wgOut->addWikiMsg( 'watchers-error-invalid-page', $page );
 37+ return;
 38+ }
 39+ } else {
 40+ # b/c
 41+ $id = $wgRequest->getInt( 'article', 0 );
 42+ $title = Title::newFromID( $id );
 43+
 44+ # Check for valid title
 45+ if ( $id == 0 || $title->getArticleID() <= 0 ) {
 46+ $wgOut->addWikiMsg( 'watchers_error_article' );
 47+ return;
 48+ }
4249 }
4350
4451 $dbr = wfGetDB( DB_SLAVE );
45 - $conds = array (
46 - 'wl_namespace' => $title->getNamespace() ,
47 - 'wl_title' => $title->getDBkey() ,
 52+ $conds = array(
 53+ 'wl_namespace' => $title->getNamespace(),
 54+ 'wl_title' => $title->getDBkey(),
4855 );
4956
50 - $watcherscountquery = $dbr->select(
 57+ $watcherscount = $dbr->selectField(
5158 /* FROM */ 'watchlist',
5259 /* SELECT */ 'count(wl_user) AS num',
5360 /* WHERE */ $conds,
54 - $fname
 61+ __METHOD__
5562 );
5663
57 - $o = $dbr->fetchObject( $watcherscountquery );
58 - $watcherscount = $o->num;
 64+ if ( $wgUser->isAllowed( 'watchers-list' ) ) {
 65+ if ( $watcherscount == 0 ) {
 66+ $wgOut->addWikiMsg( 'watchers_noone_watches', $title->getPrefixedText() );
 67+ } else {
 68+ $wgOut->wrapWikiMsg( '<h2>$1</h2>', array( 'watchers_header', $title->getPrefixedText(), $watcherscount ) );
5969
60 - $link1 = $sk->makeLinkObj( $title );
61 - $out .= "<h2>" . wfMsgExt( 'watchers_header' , array( 'parsemag' ), $link1, $watcherscount ) . "</h2>";
 70+ $res = $dbr->select(
 71+ /* FROM */ 'watchlist',
 72+ /* SELECT */ 'wl_user',
 73+ /* WHERE */ $conds,
 74+ __METHOD__
 75+ );
6276
63 - if ( $wgWatchersLimit != null ) {
 77+ $sk = $wgUser->getSkin();
6478
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";
 79+ $wgOut->addHTML( "<ol>\n" );
 80+ foreach ( $res AS $row ) {
 81+ $u = User::newFromID( $row->wl_user );
 82+ $link = $sk->link( $u->getUserPage(), htmlspecialchars( $u->getName() ) );
 83+ $wgOut->addHTML( "<li>" . $link . "</li>\n" );
 84+ }
 85+ $wgOut->addHTML( "</ol>\n" );
6986 }
7087 } 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>";
 88+ if ( $wgWatchersLimit === null ) {
 89+ if ( $watcherscount == 0 ) {
 90+ $wgOut->addWikiMsg( 'watchers_noone_watches', $title->getPrefixedText() );
 91+ } else {
 92+ $wgOut->addWikiMsg( 'watchers-num', $watcherscount, $title->getPrefixedText() );
 93+ }
8594 } 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";
 95+ if ( $watcherscount >= $wgWatchersLimit ) {
 96+ $wgOut->addWikiMsg( 'watchers_x_or_more', $wgWatchersLimit, $title->getPrefixedText() );
 97+ } else {
 98+ $wgOut->addWikiMsg( 'watchers_less_than_x', $wgWatchersLimit, $title->getPrefixedText() );
9399 }
94 - $out .= "</ol>";
95100 }
96101 }
97 -
98 - $dbr->freeResult( $watcherscountquery );
99 -
100 - $this->setHeaders();
101 - $wgOut->addHTML( $out );
102102 }
103103 }
Index: trunk/extensions/Watchers/Watchers.i18n.php
@@ -2,8 +2,9 @@
33 /**
44 * Internationalisation file for extension Watchers.
55 *
6 - * @addtogroup Extensions
7 -*/
 6+ * @file
 7+ * @ingroup Extensions
 8+ */
89
910 $messages = array();
1011
@@ -12,10 +13,13 @@
1314 'watchers-desc' => 'Shows [[Special:Watchers|which users have a page on their watchlist]]',
1415 'watchers_link_title' => 'Who watches this page?',
1516 '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.',
 17+ 'watchers-error-invalid-page' => "'''Error:''' \"$1\" is an invald page title.",
 18+ 'watchers_header' => '{{PLURAL:$2|User who is|Users who are}} watching "[[:$1]]"',
 19+ 'watchers_noone_watches' => 'No one watches the page [[:$1]].',
 20+ 'watchers_x_or_more' => '$1 or more {{PLURAL:$1|users|users}} have the page [[:$2]] on their watchlist.',
 21+ 'watchers_less_than_x' => 'Fewer than $1 {{PLURAL:$1|users|users}} have the page [[:$2]] on their watchlist.',
 22+ 'watchers-num' => '$1 {{PLURAL:$1|user has|users have}} the page [[:$2]] on {{PLURAL:$1|its|their}} watchlist.',
 23+ 'right-watchers-list' => 'List users watching a specific page on [[Special:Watchers]]',
2024 );
2125
2226 /** Message documentation (Message documentation)
Index: trunk/extensions/Watchers/Watchers.php
@@ -7,7 +7,7 @@
88 Add
99 include_once ( "extensions/Watchers/Watchers.php" ) ;
1010 to your LocalSettings.php. It should be (one of) the first extension(s) to include, as it adds to the toolbox in the sidebar.
11 -Otherextensions might add a new box there, putting the "Watchers" link in the wrong box.
 11+Other extensions might add a new box there, putting the "Watchers" link in the wrong box.
1212
1313 After inclusion in LocalSettings.php, you can set $wgWatchersLimit
1414 to a number to anonymize results ("X or more" / "Fewer than X" people watching this page)
@@ -16,7 +16,7 @@
1717 $wgExtensionCredits['other'][] = array(
1818 'path' => __FILE__,
1919 'name' => 'Watchers',
20 - 'version' => '0.2',
 20+ 'version' => '0.3',
2121 'author' => 'Magnus Manske',
2222 'url' => 'http://www.mediawiki.org/wiki/Extension:Watchers',
2323 'description' => 'An extension to show who is watching a page.',
@@ -27,11 +27,19 @@
2828 $wgExtensionAliasesFiles['Watchers'] = $dir . 'Watchers.alias.php';
2929 $wgExtensionMessagesFiles['Watchers'] = $dir . 'Watchers.i18n.php';
3030 $wgAutoloadClasses['SpecialWatchers'] = $dir . 'Watchers_body.php';
 31+
 32+$wgAvailableRights[] = 'watchers-list';
 33+$wgGroupPermissions['sysop']['watchers-list'] = true;
 34+
3135 $wgSpecialPages['Watchers'] = 'SpecialWatchers';
 36+
3237 $wgHooks['SkinTemplateToolboxEnd'][] = 'wfWatchersExtensionAfterToolbox';
3338
34 -$wgWatchersLimit = null; # Set this to a number to anonymize results ("X or more" / "Less that X" people watching this page)
35 -$wgWatchersAddCache = false ; # Internal use
 39+/**
 40+ * Set this to a number to anonymize results ("X or more" / "Less that X" people watching this page)
 41+ * This does not affect users having watchers-list right
 42+ */
 43+$wgWatchersLimit = null;
3644
3745 /**
3846 * Display link in toolbox
@@ -52,9 +60,9 @@
5361
5462 echo '<li id="t-watchers"><a href="' ;
5563 $nt = SpecialPage::getTitleFor( 'Watchers' );
56 - echo $nt->escapeLocalURL ( 'article=' . $wgTitle->getArticleID() );
 64+ echo $nt->escapeLocalURL( 'page=' . $wgTitle->getPrefixedDBkey() );
5765 echo '">';
58 - echo wfMsg('watchers_link_title');
 66+ echo wfMsgHtml( 'watchers_link_title' );
5967 echo "</a></li>\n";
6068
6169 return true;

Follow-up revisions

RevisionCommit summaryAuthorDate
r62471Fix typo from r62468ialex17:11, 14 February 2010

Status & tagging log