r98321 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r98320‎ | r98321 | r98322 >
Date:16:01, 28 September 2011
Author:dasch
Status:resolved (Comments)
Tags:
Comment:
New Extension CreditTab
Modified paths:
  • /trunk/extensions/CreditTab/CreditTab.i18n.php (added) (history)
  • /trunk/extensions/CreditTab/CreditTab.php (added) (history)
  • /trunk/extensions/CreditTab/README (added) (history)

Diff [purge]

Index: trunk/extensions/CreditTab/README
@@ -0,0 +1,8 @@
 2+Options
 3+Default the Tab only shows up in ContentNamespace
 4+
 5+Show in all Namespaces
 6+$wgCreditTabNamespaces=true;
 7+
 8+Show in certain Namespaces, example:
 9+$wgCreditTabNamespaces=array(0,14);
Index: trunk/extensions/CreditTab/CreditTab.i18n.php
@@ -0,0 +1,24 @@
 2+<?php
 3+
 4+/**
 5+ * Messages file for the InputBox extension
 6+ *
 7+ * @addtogroup Extensions
 8+ */
 9+
 10+/**
 11+ * Get all extension messages
 12+ *
 13+ * @return array
 14+ */
 15+$messages = array();
 16+
 17+$messages['en'] = array(
 18+ 'credits-desc' => 'Adds a link to Credits',
 19+ 'credits-tab' => 'Credits',
 20+);
 21+
 22+$messages['de'] = array(
 23+ 'credits-desc' => 'Fügt Link zu Autoren hinzu',
 24+ 'credits-tab' => 'Autoren',
 25+);
\ No newline at end of file
Index: trunk/extensions/CreditTab/CreditTab.php
@@ -0,0 +1,109 @@
 2+<?php
 3+/**
 4+* @addtogroup Extensions
 5+*/
 6+// Check environment
 7+if ( !defined( 'MEDIAWIKI' ) ) {
 8+ echo( "This is an extension to the MediaWiki package and cannot be run standalone.\n" );
 9+ die( -1 );
 10+}
 11+
 12+/* Configuration */
 13+
 14+// Credits
 15+$wgExtensionCredits['other'][] = array(
 16+ 'path' => __FILE__,
 17+ 'name' => 'CreditTab',
 18+ 'author' => '[http://www.dasch-tour.de DaSch]',
 19+ 'description' => 'Adds a link to Credits',
 20+ 'version' => '1.2.0',
 21+ 'descriptionmsg' => 'credits-desc',
 22+ 'url' => 'http://www.mediawiki.org/wiki/Extension:CreditTab',
 23+);
 24+$dir = dirname( __FILE__ ) . '/';
 25+
 26+// Internationalization
 27+$wgExtensionMessagesFiles['CreditTab'] = $dir . 'CreditTab.i18n.php';
 28+
 29+$wgHooks['ParserBeforeTidy'][] = 'addAuthorHeadLink';
 30+$wgHooks['SkinTemplateNavigation'][] = 'displayTab';
 31+
 32+function addAuthorHeadLink ( &$parser, &$text ) {
 33+ global $wgTitle;
 34+ $parser->mOutput->addHeadItem('<link rel="author" type="text/html" title="' . wfMsg('credits-tab') . '" href="' . $wgTitle->getLocalURL( 'action=credits' ) . '" />');
 35+ return true;
 36+}
 37+function displayTab( $obj, &$links ) {
 38+ // the old '$content_actions' array is thankfully just a
 39+ // sub-array of this one
 40+ $views_links = $links['views'];
 41+ showCredits( $obj, $views_links );
 42+ $links['views'] = $views_links;
 43+ return true;
 44+ }
 45+function showCredits( $obj, &$content_actions ) {
 46+ global $wgRequest, $wgCreditTabNamespaces;
 47+ if ( method_exists ( $obj, 'getTitle' ) ) {
 48+ $title = $obj->getTitle();
 49+ } else {
 50+ $title = $obj->mTitle;
 51+ }
 52+ wfLoadExtensionMessages('CreditTab');
 53+ $ctNamespace = $title->getNamespace();
 54+ $ctInsert=false;
 55+ if (count($wgCreditTabNamespaces)>0) {
 56+ if (in_array($ctNamespace, $wgCreditTabNamespaces)) {
 57+ $ctInsert=true;
 58+ }
 59+ if (is_bool($wgCreditTabNamespaces)) {
 60+ $ctInsert=$wgCreditTabNamespaces;
 61+ }
 62+ }
 63+ else {
 64+ if ($title->isContentPage()) {
 65+ $ctInsert=true;
 66+ }
 67+ else {
 68+ $ctInsert=false;
 69+ }
 70+ }
 71+
 72+ $class_name = ( $wgRequest->getVal( 'action' ) == 'credits' ) ? 'selected' : '';
 73+ if( $title->exists() && $ctInsert) {
 74+ $credit_tab = array(
 75+ 'class' => $class_name,
 76+ 'text' => wfMsg('credits-tab'),
 77+ 'href' => $title->getLocalURL( 'action=credits' ),
 78+ );
 79+ // find the location of the 'edit' tab, and add
 80+ // 'edit with form' right before it.
 81+ // this is a "key-safe" splice - it preserves
 82+ // both the keys and the values of the array,
 83+ // by editing them separately and then
 84+ // rebuilding the array.
 85+ // based on the example at
 86+ // http://us2.php.net/manual/en/function.array-splice.php#31234
 87+ $tab_keys = array_keys( $content_actions );
 88+ $tab_values = array_values( $content_actions );
 89+ $edit_tab_location = array_search('history', $tab_keys);
 90+ $edit_tab_location++;
 91+ // If there's no 'edit' tab, look for the 'view source' tab
 92+ // instead.
 93+ if ( $edit_tab_location == null ) {
 94+ $edit_tab_location = array_search( 'viewsource', $tab_keys );
 95+ }
 96+
 97+ // This should rarely happen, but if there was no edit *or*
 98+ // view source tab, set the location index to -1, so the
 99+ // tab shows up near the end.
 100+ if ( $edit_tab_location == null ) {
 101+ $edit_tab_location = - 1;
 102+ }
 103+ array_splice($tab_keys, $edit_tab_location, 0, 'credits');
 104+ array_splice($tab_values, $edit_tab_location, 0, array($credit_tab));
 105+ $content_actions = array();
 106+ for ($i = 0; $i < count($tab_keys); $i++)
 107+ $content_actions[$tab_keys[$i]] = $tab_values[$i];
 108+ }
 109+ return true;
 110+}

Follow-up revisions

RevisionCommit summaryAuthorDate
r98334fixes for r98321...dasch17:21, 28 September 2011
r98370changes made on advice from r98321 thanks to IAlex and Krinkledasch21:25, 28 September 2011
r98371changes made on advice from r98321 thanks to IAlex and Krinkledasch21:25, 28 September 2011
r98402r98321: Sort languages: 1. 'en, 2. 'qqq', 3. all other by alphabet...raymond07:18, 29 September 2011

Comments

#Comment by Reedy (talk | contribs)   16:04, 28 September 2011

Missing svn:eol-style native

Setup ppSubversion/auto-props[[ if you haven't already

#Comment by DaSch (talk | contribs)   16:05, 28 September 2011

I have set this, but somehow Eclipse does not take it like it's described

#Comment by Siebrand (talk | contribs)   16:42, 28 September 2011

header for i18n file has incorrect extension Mentioned. Please add message documentation for the newly added messages. Thanks.

#Comment by IAlex (talk | contribs)   20:10, 28 September 2011

Shouldn't the BeforePageDisplay hook be used instead of ParserBeforeTidy to add the <link> tag?

#Comment by DaSch (talk | contribs)   20:29, 28 September 2011

What's the difference?

#Comment by IAlex (talk | contribs)   20:40, 28 September 2011

With BeforePageDisplay you work with the OutputPage object which is much more convenient than a Parser object for this kind of things.

#Comment by Krinkle (talk | contribs)   20:44, 28 September 2011

Also, OutputPage has a method getTitle(), please use $out->getTitle() instead of the global $wgTitle.

#Comment by DaSch (talk | contribs)   20:46, 28 September 2011

yes I saw this also in my Extension:RelationLinks. I have to rebuild them a bit. At the moment I stuck at the point that somehow I don't get the correct Suppage with the namespace for a subpage :(

#Comment by Krinkle (talk | contribs)   20:48, 28 September 2011
+	$parser->mOutput->addHeadItem('<link rel="author" type="text/html" title="' . wfMsg('credits-tab') . '" href="' . $wgTitle->getLocalURL( 'action=credits' ) . '" />');

There's a slight html escaping issue here with the above. Use htmlspecialchars() to escape the value put between title="". Or use Html::element('link', array( 'text' => wfMsg( 'credits-tab'))) which does it automatically. Or, ideally, use the following:

$out->addLink( array(
  'rel' => 'author',
  'type' => 'text/html',
  'title' => wfMsg('credits-tab'),
  'href' => $out->getTitle()->getLocalURL( 'action=credits' ),
) );
#Comment by DaSch (talk | contribs)   21:26, 28 September 2011

thanks, slowly I'm getting into it :)

#Comment by Siebrand (talk | contribs)   23:30, 28 September 2011

Hook methods should go into a separate class file.

Status & tagging log