Cicalese has submitted this change and it was merged.
Change subject: added handling for talk pages; code clean up
......................................................................
added handling for talk pages; code clean up
Change-Id: Ib02ac5b3067652be8b8ab1ce3d9c75d564650bd9
---
M SemanticTitle.class.php
A SemanticTitle.i18n.magic.php
M SemanticTitle.php
M i18n/en.json
M i18n/qqq.json
5 files changed, 307 insertions(+), 191 deletions(-)
Approvals:
HermannSchwaerzler: Looks good to me, but someone else must approve
Cicalese: Verified; Looks good to me, approved
diff --git a/SemanticTitle.class.php b/SemanticTitle.class.php
index 13b8f59..23b53eb 100644
--- a/SemanticTitle.class.php
+++ b/SemanticTitle.class.php
@@ -1,4 +1,4 @@
-<?php // SemanticTitle.class.php //
+<?php
/*
------------------------------------------------------------------------------------------------
@@ -15,189 +15,282 @@
See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public
License along with this
- program. If not, see <https://www.gnu.org/licenses/>.
+ program. If not, see <https://www.gnu.org/licenses/>.
------------------------------------------------------------------------------------------------
*/
-if ( ! defined( 'MEDIAWIKI' ) ) {
- die( 'Not an entry point.' );
-}; // if
-
class SemanticTitle {
+ /**
+ * @since 2.0
+ *
+ * @param Parser &$parser
+ */
+ public static function setup( &$parser ) {
- // Returns semantic title text for the specified title.
- // May be used by other extensions to get semantic title for specified
page.
- static public function getText( Title $title ) {
+ if ( !isset( $GLOBALS['wgSemanticTitleProperties'] ) ) {
+ $GLOBALS['wgSemanticTitleProperties'] = array();
+ }
- global $wgSemanticTitleProperties;
+ if ( !isset( $GLOBALS['wgSemanticTitleHideSubtitle'] ) ) {
+ $GLOBALS['wgSemanticTitleHideSubtitle'] = false;
+ }
- // TODO: Cache for semantic titles?
+ $parser->setFunctionHook( 'semantic-title',
+ __CLASS__ . '::hookSemanticTitle' );
- $ns = $title->getNamespace();
- if ( array_key_exists( $ns, $wgSemanticTitleProperties ) && !
$title->isRedirect() ) {
- $label = $wgSemanticTitleProperties[ $ns ];
- $store = smwfGetStore();
+ return true;
+
+ }
+
+
+ /**
+ * Handle links. Implements LinkBegin hook of Linker class.
+ * If a link is customized by a user (e. g. [[Target|Text]]) it should
+ * remain intact. Let us assume a link is not customized if its text is
+ * the prefixed or (to support semantic queries) non-prefixed title of
+ * the target page.
+ *
+ * @since 1.0
+ *
+ * @param $dummy
+ * @param Title $target
+ * @param &$text
+ * @param &$customAttribs
+ * @param &$query
+ * @param &$options
+ * @param &$ret
+ */
+ public static function onLinkBegin( $dummy, Title $target, &$text,
+ &$customAttribs, &$query, &$options, &$ret ) {
+
+ $instance = new self( $target );
+
+ if ( isset( $text ) ) {
+
+ if ( !is_string( $text ) ) {
+
+ return true;
+
+ }
+
+ $title = Title::newFromText( $text );
+
+ if ( is_null( $title ) ) {
+
+ return true;
+
+ }
+
+ if ( $title->getText() != $target->getText() ) {
+
+ return true;
+
+ }
+
+ if ( $title->getSubjectNsText() ==
$target->getSubjectNsText() ||
+ $title->getSubjectNsText() == '' ) {
+
+ $instance->getSemanticTitle( $text );
+
+ }
+
+ } else {
+
+ $instance->getSemanticTitle( $text );
+
+ }
+
+ return true;
+
+ }
+
+ /**
+ * Handle self links.
+ *
+ * @since 1.3
+ *
+ * @param Title $nt
+ * @param &$html
+ * @param &$trail
+ * @param &$prefix
+ */
+ public static function onSelfLinkBegin( Title $nt, &$html, &$trail,
+ &$prefix, &$ret ) {
+
+ $instance = new self( $nt );
+
+ $instance->getSemanticTitle( $html );
+
+ return true;
+
+ }
+
+ /**
+ * Handle page title.
+ *
+ * @since 1.0
+ *
+ * @param OutputPage &$out
+ * @param Skin &$sk
+ */
+ static function onBeforePageDisplay( OutputPage &$out, Skin &$sk ) {
+
+ $title = $out->getTitle();
+ if ( ! $title instanceof Title ) {
+
+ return true;
+
+ }
+
+ // remove fragment (LiquidThreads interaction)
+ $title = Title::newFromText( $title->getPrefixedText() );
+
+ $instance = new self( $title );
+
+ $semanticTitle = null;
+ $instance->getSemanticTitle( $semanticTitle );
+
+ if ( is_null( $semanticTitle ) ) {
+
+ return true;
+
+ }
+
+ $out->setPageTitle( $semanticTitle );
+
+ $instance->setSubtitle( $out );
+
+ return true;
+
+ }
+
+ /**
+ * Handle #semantic-title parser function.
+ *
+ * @since 1.0
+ *
+ * @param $parser
+ * @param $pagename
+ */
+ public static function hookSemanticTitle( $parser, $pagename ) {
+
+ $title = Title::newFromText( $pagename );
+
+ if ( is_null( $title ) ) {
+
+ return $pagename;
+ }
+
+ $instance = new self( $title );
+
+ $instance->getSemanticTitle( $pagename );
+
+ return $pagename;
+
+ }
+
+ private $title;
+ private $isTalk;
+ private $semanticTitleProperties;
+ private $hideSubtitle;
+
+ /**
+ * @since 2.0
+ *
+ * @param Title $title
+ */
+ public function __construct( Title $title ) {
+ if ( $title->isTalkPage() ) {
+ $this->title = $title->getSubjectPage();
+ $this->isTalk = true;
+ } else {
+ $this->title = $title;
+ $this->isTalk = false;
+ }
+ $this->semanticTitleProperties =
$GLOBALS['wgSemanticTitleProperties'];
+ $this->hideSubtitle = $GLOBALS['wgSemanticTitleHideSubtitle'];
+ }
+
+ /**
+ * Returns semantic title text for the specified title.
+ *
+ * @since 2.0
+ *
+ * @param Title $title
+ */
+ public function getSemanticTitle( &$semanticTitle ) {
+
+ $namespace = $this->title->getNamespace();
+
+ if ( array_key_exists( $namespace,
$this->semanticTitleProperties ) &&
+ ! $this->title->isRedirect() ) {
+
+ $label = $this->semanticTitleProperties[ $namespace ];
+
+ $store = \SMW\StoreFactory::getStore();
+
if ( class_exists( 'SMWDataItem' ) ) {
- $subj = SMWDIWikiPage::newFromTitle( $title );
+ $subj = SMWDIWikiPage::newFromTitle(
$this->title );
} else {
- $subj = $title;
- }; # if
+ $subj = $this->title;
+ }
+
$data = $store->getSemanticData( $subj );
$property = SMWDIProperty::newFromUserLabel( $label );
$values = $data->getPropertyValues( $property );
+
if ( count( $values ) > 0 ) {
+
$value = array_shift( $values );
+
if ( $value->getDIType() ==
SMWDataItem::TYPE_STRING ||
$value->getDIType() ==
SMWDataItem::TYPE_BLOB ) {
+
$name = $value->getString();
+
if ( $name != '' ) {
- return htmlentities($name,
ENT_QUOTES);
- }; // if
- }; // if
- }; // if
- }; // if
- return false;
+ $semanticTitle =
htmlspecialchars( $name, ENT_QUOTES );
- } // function getText
+ if ( $this->isTalk ) {
+ $semanticTitle =
+ wfMessage(
'semantictitle-talkpagetitle',
+ $semanticTitle
)->plain();
- // Handle links.
- static public function onLinkBegin( $skin, $target, &$text,
&$customAttribs, &$query, &$options, &$ret ) {
- /*
- If a link is customized by a user (e. g.
[[Target|Text]]) it should remain intact.
- Let us assume a link is not customized if its text is
the prefixed title of target page
- or (to support semantic queries) the non-prefixed title
of the target page.
- However, simple check `$target->getPrefixedText() ==
$text' fails if prefix in text
- is an alias or canonical. So more complicated check
should be done:
- `Title::newFromText( $text )->getPrefixedText() ==
$target->getPrefixedtext()'.
- */
- $semantic = false;
- if ( isset( $text ) ) {
- // Hmm... Sometimes `$text' is not a string but an
object of class `Message'...
- if ( is_string( $text ) ) {
- $title = Title::newFromText( $text );
- if ( $title != null &&
- ( $title->getText() ==
$target->getText() &&
- ( ( $title->getSubjectNsText() ==
$target->getSubjectNsText() ||
- $title->getSubjectNsText() == '' ) ) )
) {
- $semantic = self::getText( $target );
- }; // if
- }; // if
- } else {
- $semantic = self::getText( $target );
- }; // if
- if ( $semantic !== false ) {
- $text = $semantic;
- }; // if
- return true;
- } // function onLinkBegin
+ }
+ }
- // Handle self links.
- static public function onSelfLinkBegin( $nt, &$html, &$trail, &$prefix,
&$ret ) {
- $html = self::getText( $nt );
- return true;
- } // function onSelfLinkBegin
+ }
- // Handle page title.
- static function onBeforePageDisplay( &$out, &$sk ) {
+ }
- if ( ! $out->isArticle() ) {
- return true;
- }; // if
- $title = $out->getTitle();
- if ( ! $title instanceof Title ) {
- return true;
- }; // if
+ }
- // remove fragment (LiquidThreads interaction)
- $title = Title::newFromText($title->getPrefixedText());
+ }
- $semantic = self::getText( $title );
- if ( $semantic === false ) {
- return true;
- }; // if
+ /**
+ * Shows original page title in subtitle if allowed by configuration
+ *
+ * @since 2.0
+ *
+ * @param OutputPage out
+ */
+ public function setSubtitle( OutputPage $out ) {
- $out->setPageTitle( $semantic );
- global $wgSemanticTitleHideSubtitle;
- if ( !isset( $wgSemanticTitleHideSubtitle ) ||
- !$wgSemanticTitleHideSubtitle ) {
+ if ( ! $this->hideSubtitle ) {
+
+ if ( $this->isTalk ) {
+ $pagename =
$this->title->getTalkPage()->getPrefixedText();
+ } else {
+ $pagename = $this->title->getPrefixedText();
+ }
$out->setSubtitle(
- $title->getPrefixedText() .
+ $pagename .
( $out->getSubtitle() == '' ? '' : ' ' .
$out->getSubtitle() )
);
+
}
-
- return true;
-
- } // function onBeforePageDisplay
-
-
- static function onLanguageGetMagic( &$magicWords, $langCode ) {
- // 0 means the magic word is *not* case sensitive.
- // 1 means the magic word *is* case sensitive.
- $magicWords[ 'semantic-title' ] = array( 1, 'semantic-title' );
- return true;
- } // function onLanguageGetMagic
-
-
- static function onParserFirstCallInit( &$parser ) {
- $parser->setFunctionHook( 'semantic-title', __CLASS__ .
'::hookSemanticTitle' );
- return true;
- } // function onParserFirstCallInit
-
-
- // This hook is not enabled yet.
- static public function onSMWStore_updateDataAfter( SMWStore $smw_store,
SMWSemanticData $data ) {
-
- global $wgSemanticTitleProperties;
-
- $subject = $data->getSubject();
- if ( ! $subject->getDIType() == SMWDataItem::TYPE_WIKIPAGE ) {
- return true;
- }; // if
- $ns = $subject->getNamespace();
- if ( ! isset( $wgSemanticTitleProperties[ $ns ] ) ) {
- return true;
- }; // if
- $name = $wgSemanticTitleProperties[ $ns ];
- $props = $data->getProperties();
- if ( ! isset( $props[ $name ] ) ) {
- return true;
- }; // if
- $values = $data->getPropertyValues( $props[ $name ] );
- if ( count( $values ) == 0 ) {
- return true;
- }; // if
- $value = array_shift( $values );
- if ( $value->getDIType() != SMWDataItem::TYPE_STRING ) {
- return true;
- }; // if
- $semantic = $value->getString();
- $title = $subject->getTitle();
-
- // Oops. I need parser to set default sort key. :-(
- // $parser->setDefaultSort( $semantic );
-
- return true;
-
- } // function onSMWStore_updateDataAfter
-
-
- static public function hookSemanticTitle( $parser, $arg ) {
- $res = $arg;
- $title = Title::newFromText( $arg );
- if ( $title != null ) {
- $semantic = self::getText( $title );
- if ( $semantic !== false ) {
- $res = $semantic;
- }; // if
- }
- return $res;
- } // function hookSemanticTitle
-
-
-} // class SemanticTitle
-
-// end of file //
+ }
+}
diff --git a/SemanticTitle.i18n.magic.php b/SemanticTitle.i18n.magic.php
new file mode 100644
index 0000000..c9b2d57
--- /dev/null
+++ b/SemanticTitle.i18n.magic.php
@@ -0,0 +1,26 @@
+<?php
+
+/*
+
------------------------------------------------------------------------------------------------
+ SemanticTitle, a MediaWiki extension for setting visible page title to
value of a semantic
+ property.
+ Copyright (C) 2012 Van de Bugger.
+
+ This program is free software: you can redistribute it and/or modify it
under the terms
+ of the GNU Affero General Public License as published by the Free
Software Foundation,
+ either version 3 of the License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY;
+ without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
+ See the GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public
License along with this
+ program. If not, see <https://www.gnu.org/licenses/>.
+
------------------------------------------------------------------------------------------------
+*/
+
+$magicWords = array();
+
+$magicWords['en'] = array(
+ 'semantic-title' => array( 0, 'semantic-title' )
+);
diff --git a/SemanticTitle.php b/SemanticTitle.php
index 0196b65..eb4704e 100644
--- a/SemanticTitle.php
+++ b/SemanticTitle.php
@@ -15,44 +15,39 @@
See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public
License along with this
- program. If not, see <https://www.gnu.org/licenses/>.
+ program. If not, see <https://www.gnu.org/licenses/>.
------------------------------------------------------------------------------------------------
*/
if ( ! defined( 'MEDIAWIKI' ) ) {
- die( 'Not an entry point.' );
-}; // if
+ die( 'This is an extension to the MediaWiki package and cannot be run
standalone' );
+}
-global $wgAutoloadClasses;
-$wgAutoloadClasses[ 'SemanticTitle' ] = __DIR__ . '/SemanticTitle.class.php';
-
-global $wgHooks;
-$wgHooks[ 'BeforePageDisplay' ][] =
'SemanticTitle::onBeforePageDisplay';
-$wgHooks[ 'LanguageGetMagic' ][] =
'SemanticTitle::onLanguageGetMagic';
-$wgHooks[ 'LinkBegin' ][] = 'SemanticTitle::onLinkBegin';
-$wgHooks[ 'SelfLinkBegin' ][] = 'SemanticTitle::onSelfLinkBegin';
-$wgHooks[ 'ParserFirstCallInit' ][] =
'SemanticTitle::onParserFirstCallInit';
-// Not yet implemented.
-//~ $wgHooks[ 'SMWStore::updateDataAfter' ][] =
'SemanticTitle::onSMWStore_updateDataAfter';
-
-global $wgMessagesDirs;
-$wgMessagesDirs['SemanticTitle'] = __DIR__ . '/i18n';
-global $wgExtensionMessagesFiles;
-$wgExtensionMessagesFiles[ 'SemanticTitle' ] = __DIR__ .
'/SemanticTitle.i18n.php';
-
-global $wgSemanticTitleProperties;
-$wgSemanticTitleProperties = array();
-
-global $wgExtensionCredits;
-$wgExtensionCredits[ 'semantic' ][] = array(
- 'path' => __FILE__,
- 'name' => 'SemanticTitle',
+$GLOBALS['wgExtensionCredits']['semantic'][] = array(
+ 'path' => __FILE__,
+ 'name' => 'Semantic Title',
'license' => 'AGPLv3',
- 'version' => '1.3',
- 'author' => array(
+ 'version' => '2.0',
+ 'author' => array(
'[https://www.mediawiki.org/wiki/User:Van_de_Bugger Van de
Bugger]',
'[https://www.mediawiki.org/wiki/User:Cindy.cicalese Cindy
Cicalese]'
),
- 'url' => 'https://www.mediawiki.org/wiki/Extension:SemanticTitle',
- 'descriptionmsg' => 'semantictitle-desc',
+ 'descriptionmsg' => 'semantictitle-desc',
+ 'url' => 'https://www.mediawiki.org/wiki/Extension:SemanticTitle',
);
+
+$GLOBALS['wgAutoloadClasses']['SemanticTitle'] =
+ __DIR__ . '/SemanticTitle.class.php';
+
+$GLOBALS['wgHooks']['ParserFirstCallInit'][] = 'SemanticTitle::setup';
+$GLOBALS['wgHooks']['BeforePageDisplay'][] =
+ 'SemanticTitle::onBeforePageDisplay';
+$GLOBALS['wgHooks']['LinkBegin' ][] = 'SemanticTitle::onLinkBegin';
+$GLOBALS['wgHooks']['SelfLinkBegin'][] = 'SemanticTitle::onSelfLinkBegin';
+
+$GLOBALS['wgMessagesDirs']['SemanticTitle'] = __DIR__ . '/i18n';
+$GLOBALS['wgExtensionMessagesFiles']['SemanticTitle'] =
+ __DIR__ . '/SemanticTitle.i18n.php';
+
+$GLOBALS['wgExtensionMessagesFiles']['SemanticTitleMagic'] =
+ __DIR__ . '/SemanticTitle.i18n.magic.php';
diff --git a/i18n/en.json b/i18n/en.json
index d2464b7..d25afdd 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -1,8 +1,9 @@
{
- "@metadata": {
- "authors": [
- "Van de Bugger"
- ]
- },
- "semantictitle-desc": "Sets visible page title to value of a semantic
property"
+ "@metadata": {
+ "authors": [
+ "Van de Bugger"
+ ]
+ },
+ "semantictitle-desc": "Sets visible page title to value of a semantic
property",
+ "semantictitle-talkpagetitle": "Discuss $1"
}
diff --git a/i18n/qqq.json b/i18n/qqq.json
index ca40f1e..5101468 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -5,5 +5,6 @@
"Shirayuki"
]
},
- "semantictitle-desc": "{{desc|name=Semantic
Title|url=http://www.mediawiki.org/wiki/Extension:SemanticTitle}}"
+ "semantictitle-desc": "{{desc|name=Semantic
Title|url=http://www.mediawiki.org/wiki/Extension:SemanticTitle}}",
+ "semantictitle-talkpagetitle": "Format string for the title of a talk
page; $1 is the semantic title of the subject page corresponding to the talk
page."
}
--
To view, visit https://gerrit.wikimedia.org/r/169646
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ib02ac5b3067652be8b8ab1ce3d9c75d564650bd9
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/SemanticTitle
Gerrit-Branch: master
Gerrit-Owner: Cicalese <[email protected]>
Gerrit-Reviewer: Cicalese <[email protected]>
Gerrit-Reviewer: HermannSchwaerzler <[email protected]>
Gerrit-Reviewer: Siebrand <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits