r87150 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r87149‎ | r87150 | r87151 >
Date:23:33, 29 April 2011
Author:reedy
Status:ok (Comments)
Tags:
Comment:
* Get a list of all subscribed hooks, and those subscribers

Essentially what SpecialVersion will give you if wgSpecialVersionShowHooks is true
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/api/ApiQuerySiteinfo.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiQuerySiteinfo.php
@@ -94,6 +94,9 @@
9595 case 'functionhooks':
9696 $fit = $this->appendFunctionHooks( $p );
9797 break;
 98+ case 'showhooks':
 99+ $fit = $this->appendSubscribedHooks( $p );
 100+ break;
98101 default:
99102 ApiBase::dieDebug( __METHOD__, "Unknown prop=$p" );
100103 }
@@ -498,6 +501,26 @@
499502 return "<{$item}>";
500503 }
501504
 505+ public function appendSubscribedHooks( $property ) {
 506+ global $wgHooks;
 507+ $myWgHooks = $wgHooks;
 508+ ksort( $myWgHooks );
 509+
 510+ $data = array();
 511+ foreach ( $myWgHooks as $hook => $hooks ) {
 512+ $arr = array(
 513+ 'name' => $hook,
 514+ 'subscribers' => $hooks,
 515+ );
 516+
 517+ $this->getResult()->setIndexedTagName( $arr['subscribers'], 's' );
 518+ $data[] = $arr;
 519+ }
 520+
 521+ $this->getResult()->setIndexedTagName( $data, 'hook' );
 522+ return $this->getResult()->addValue( 'query', $property, $data );
 523+ }
 524+
502525 public function getCacheMode( $params ) {
503526 return 'public';
504527 }
@@ -524,6 +547,7 @@
525548 'skins',
526549 'extensiontags',
527550 'functionhooks',
 551+ 'showhooks',
528552 )
529553 ),
530554 'filteriw' => array(
@@ -557,6 +581,7 @@
558582 ' skins - Returns a list of all enabled skins',
559583 ' extensiontags - Returns a list of parser extension tags',
560584 ' functionhooks - Returns a list of parser function hooks',
 585+ ' showhooks - Returns a list of all subscribed hooks (contents of $wgHooks)'
561586 ),
562587 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
563588 'showalldb' => 'List all database servers, not just the one lagging the most',
Index: trunk/phase3/RELEASE-NOTES
@@ -366,6 +366,7 @@
367367 * (bug 28238) paraminfo: output both limits for multi param
368368 * (bug 27179) API: List of extension tags through meta=siteinfo
369369 * Get a list of function hooks through meta=siteinfo
 370+* Get a list of all subscribed hooks, and those subscribers
370371
371372 === Languages updated in 1.18 ===
372373

Follow-up revisions

RevisionCommit summaryAuthorDate
r87169Followup r87150...reedy16:52, 30 April 2011

Comments

#Comment by Duplicatebug (talk | contribs)   09:16, 30 April 2011

>Essentially what SpecialVersion will give you if wgSpecialVersionShowHooks is true

Should $wgSpecialVersionShowHooks not check also on the api? It is disabled by default, so this revision introduce a way around that config.

#Comment by Dantman (talk | contribs)   09:20, 30 April 2011

Agreed, that config option was added because some people considered it undesireable for a list of registered hooks to be exposed to spammers or people trying to break the wiki who could use it to figure out things about how the wiki was configured.

#Comment by Reedy (talk | contribs)   13:30, 30 April 2011

I'm not sure I quite agree. MediaWiki doesn't have any way of preventing listing of all extension on a wiki, so you can find out the extensions installed, and work it from there. Which has very little to do with "how the wiki was configured"

Unless someone has written a custom spam plugin, (which will happen but be VERY rare), most of them are open source, and you can see some of the "configuration" if you try to signup, or edit as anon.

I did think about that when adding it, but decided it seemed useless, and the option seemed more like additional info some people, but not all would want to see

#Comment by Reedy (talk | contribs)   13:55, 30 April 2011

i.e I'm suggesting that argument is just security through obscurity

#Comment by Catrope (talk | contribs)   16:36, 30 April 2011
+				'subscribers' => $hooks,

Note that the elements of $hooks need not be strings, they may also be objects, or arrays like array( $object, 'method' ) or a bunch of other array formats. See Manual:Hooks#Writing an event handler.

#Comment by Reedy (talk | contribs)   16:46, 30 April 2011
			foreach ( $myWgHooks as $hook => $hooks ) {
				$ret .= "<tr>
						<td>$hook</td>
						<td>" . $this->listToText( $hooks ) . "</td>
					</tr>\n";
			}
<snip>
/**
	 * Convert an array of items into a list for display.
	 *
	 * @param $list Array of elements to display
	 * @param $sort Boolean: whether to sort the items in $list
	 *
	 * @return String
	 */
	function listToText( $list, $sort = true ) {
		$cnt = count( $list );

		if ( $cnt == 1 ) {
			// Enforce always returning a string
			return (string)self::arrayToString( $list[0] );
		} elseif ( $cnt == 0 ) {
			return '';
		} else {
			global $wgLang;
			if ( $sort ) {
				sort( $list );
			}
			return $wgLang->listToText( array_map( array( __CLASS__, 'arrayToString' ), $list ) );
		}
	}

	/**
	 * Convert an array or object to a string for display.
	 *
	 * @param $list Mixed: will convert an array to string if given and return
	 *              the paramater unaltered otherwise
	 *
	 * @return Mixed
	 */
	static function arrayToString( $list ) {
		if( is_array( $list ) && count( $list ) == 1 ) {
			$list = $list[0];
		}
		if( is_object( $list ) ) {
			$class = get_class( $list );
			return "($class)";
		} elseif ( !is_array( $list ) ) {
			return $list;
		} else {
			if( is_object( $list[0] ) ) {
				$class = get_class( $list[0] );
			} else {
				$class = $list[0];
			}
			return "($class, {$list[1]})";
		}
	}

I suppose I could array_map on the items, and check types, if not a string push it through SpecialVersion::arrayToString()

#Comment by Catrope (talk | contribs)   16:49, 30 April 2011

Sounds good. In fact, arrayToString() will take strings just fine (is_array fails, is_object fails !is_array succeeds) so you could just map it unconditionally.

Status & tagging log