r106628 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106627‎ | r106628 | r106629 >
Date:06:42, 19 December 2011
Author:bawolff
Status:ok
Tags:
Comment:
New extension. Add parser function to do {{#ifpageincategory:some category|do something|do something else}}
Modified paths:
  • /trunk/extensions/PageInCat (added) (history)
  • /trunk/extensions/PageInCat/PageInCat.i18n.magic.php (added) (history)
  • /trunk/extensions/PageInCat/PageInCat.i18n.php (added) (history)
  • /trunk/extensions/PageInCat/PageInCat.php (added) (history)
  • /trunk/extensions/PageInCat/PageInCat_body.php (added) (history)

Diff [purge]

Index: trunk/extensions/PageInCat/PageInCat.i18n.magic.php
@@ -0,0 +1,6 @@
 2+<?php
 3+$magicWords = array();
 4+
 5+$magicWords['en'] = array(
 6+ 'pageincat' => array( 0, 'incat', 'ifpageincat', 'incategory', 'ifpageincategory' ),
 7+);
Property changes on: trunk/extensions/PageInCat/PageInCat.i18n.magic.php
___________________________________________________________________
Added: svn:eol-style
18 + native
Index: trunk/extensions/PageInCat/PageInCat_body.php
@@ -0,0 +1,111 @@
 2+<?php
 3+class PageInCat {
 4+
 5+ /**
 6+ * Register the parser hook.
 7+ * @param $parser Parser
 8+ */
 9+ public static function register( Parser $parser ) {
 10+ $parser->setFunctionHook( 'pageincat', 'PageInCat::render', Parser::SFH_OBJECT_ARGS );
 11+ return true;
 12+ }
 13+
 14+ /**
 15+ * Check if in category.
 16+ * Based on #if from ParserFunctions extension.
 17+ */
 18+ public static function render( $parser, $frame, $args ) {
 19+ $catText = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
 20+
 21+ // Must specify that content varries with what gets inserted in db on save.
 22+ $parser->getOutput()->setFlag( 'vary-revision' );
 23+
 24+ if ( self::inCat( $parser->getTitle(), $catText, $parser ) ) {
 25+ return isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : '';
 26+ } else {
 27+ return isset( $args[2] ) ? trim( $frame->expand( $args[2] ) ) : '';
 28+ }
 29+ }
 30+
 31+ /**
 32+ * check if $page belongs to $category
 33+ * @param $page Title current page
 34+ * @param $category String category to check (not a title object!)
 35+ * @param $parser Parser
 36+ * @return boolean If $page is a member of $category
 37+ */
 38+ private static function inCat( Title $page, $category, Parser $parser ) {
 39+ if ( $category === '' ) return false;
 40+ $catTitle = Title::makeTitleSafe(
 41+ NS_CATEGORY,
 42+ $category
 43+ );
 44+ if ( !$catTitle ) return false;
 45+ $catDBkey = $catTitle->getDBkey();
 46+
 47+ $pageId = $page->getArticleId();
 48+ if ( !$pageId ) return false; // page hasn't yet been saved (preview)
 49+
 50+ if ( !isset( $parser->pageInCat_cache ) ) {
 51+ $parser->pageInCat_cache = array();
 52+ } else {
 53+ if ( isset( $parser->pageInCat_cache[$catDBkey] ) ) {
 54+ # been there done that, return cached value
 55+ return $parser->pageInCat_cache[$catDBkey];
 56+ }
 57+ }
 58+
 59+ if ( !$parser->incrementExpensiveFunctionCount() ) {
 60+ # expensive function limit reached.
 61+ return false;
 62+ }
 63+
 64+ if ( self::inCatCheckDb( $pageId, $catDBkey ) ) {
 65+ $parser->pageInCat_cache[$catDBkey] = true;
 66+ return true;
 67+ } /* else if false */
 68+
 69+ $parser->pageInCat_cache[$catDBkey] = false;
 70+ return false;
 71+ }
 72+
 73+ /**
 74+ * Actually chech it in DB.
 75+ * @param $pageId int page_id of current page (Already verified to not be 0)
 76+ * @param $catDBkey String the db key of category we're checking.
 77+ * @return boolean if the current page belongs to the category.
 78+ */
 79+ private static function inCatCheckDb( $pageId, $catDBkey ) {
 80+ $dbr = wfGetDB( DB_SLAVE );
 81+ // This will be false if page not in cat
 82+ // Since 0 rows returned in that case.
 83+ $res = $dbr->selectField(
 84+ 'categorylinks',
 85+ 'cl_from',
 86+ array(
 87+ 'cl_to' => $catDBkey,
 88+ 'cl_from' => $pageId,
 89+ ),
 90+ __METHOD__
 91+ );
 92+ return $res !== false;
 93+ }
 94+
 95+ /**
 96+ * ClearState hook, so we don't carry cached entries into
 97+ * different parses.
 98+ *
 99+ * Originally I had this all stored in a static member
 100+ * variable of this class (aka self::$catCache[$pageId][$catDBkey] )
 101+ * but changed to approach based on how ParserFunctions extension
 102+ * does some things, because I was concerned that it might be possible
 103+ * for MW to parse something, save the result, then parse the same thing
 104+ * again in same run (doesn't happen currently, but doesn't seem unimaginable
 105+ * that it could).
 106+ * @param $parser Parser
 107+ */
 108+ public static function clearState( Parser $parser ) {
 109+ $parser->pageInCat_cache = array();
 110+ return true;
 111+ }
 112+}
Property changes on: trunk/extensions/PageInCat/PageInCat_body.php
___________________________________________________________________
Added: svn:eol-style
1113 + native
Index: trunk/extensions/PageInCat/PageInCat.i18n.php
@@ -0,0 +1,17 @@
 2+<?php
 3+/**
 4+ * Internationalisation file for extension PageInCat
 5+ *
 6+ * @file
 7+ * @ingroup Extensions
 8+ */
 9+
 10+$messages = array();
 11+
 12+$messages['en'] = array(
 13+ 'pageincat-desc' => 'Adds a parser function <code><nowiki>{{#incat:...}}</nowiki></code> to determine if the current page is in a specified category.',
 14+);
 15+
 16+$messages['qqq'] = array(
 17+ 'pagesincat-desc' => '{{desc}}',
 18+);
Property changes on: trunk/extensions/PageInCat/PageInCat.i18n.php
___________________________________________________________________
Added: svn:eol-style
119 + native
Index: trunk/extensions/PageInCat/PageInCat.php
@@ -0,0 +1,47 @@
 2+<?php
 3+/**
 4+ * Extension to add parserfunction {{#incat:foo|yes|no}}
 5+ * Note, this might give wrong results on preview, but should work once page is saved.
 6+ * @author Brian Wolff <bawolff+ext _at_ gmail _dot_ com>
 7+ *
 8+ * Copyright © Brian Wolff 2011.
 9+ *
 10+ * This program is free software; you can redistribute it and/or
 11+ * modify it under the terms of the GNU General Public License
 12+ * as published by the Free Software Foundation; either version 2
 13+ * of the License, or (at your option) any later version.
 14+ *
 15+ * This program is distributed in the hope that it will be useful,
 16+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 18+ * GNU General Public License for more details.
 19+ *
 20+ * You should have received a copy of the GNU General Public License
 21+ * along with this program; if not, write to the Free Software
 22+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 23+ *
 24+ */
 25+
 26+if ( !defined( 'MEDIAWIKI' ) ) {
 27+ die( 'This file is a MediaWiki extension, it is not a valid entry point' );
 28+}
 29+
 30+
 31+$wgExtensionCredits['parserhook'][] = array(
 32+ 'path' => __FILE__,
 33+ 'name' => 'PageInCat',
 34+ 'descriptionmsg' => 'pageincat-desc',
 35+ 'version' => 1,
 36+ 'url' => 'https://mediawiki.org/wiki/Extension:PageInCat',
 37+ 'author' => '[https://mediawiki.org/wiki/User:Bawolff Brian Wolff]',
 38+);
 39+
 40+$dir = dirname(__FILE__) . '/';
 41+
 42+$wgExtensionMessagesFiles['PageInCat'] = $dir . 'PageInCat.i18n.php';
 43+$wgExtensionMessagesFiles['PageInCatMagic'] = $dir . 'PageInCat.i18n.magic.php';
 44+
 45+$wgAutoloadClasses['PageInCat'] = $dir . 'PageInCat_body.php';
 46+
 47+$wgHooks['ParserFirstCallInit'][] = 'PageInCat::register';
 48+$wgHooks['ParserClearState'][] = 'PageInCat::clearState';
Property changes on: trunk/extensions/PageInCat/PageInCat.php
___________________________________________________________________
Added: svn:eol-style
149 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r106631r106628: Consistency tweaks in preparation for adding extension to translatew...raymond09:54, 19 December 2011
r106632r106628: Adding extension to translatewiki.netraymond09:55, 19 December 2011

Status & tagging log