In light of religious terrorist attacks in Paris, please see Category:blasphemy!

Freedom Porn:free technology/SequentialLinks

From Freedom Porn
Jump to: navigation, search

SequentialLinks is a MediaWiki extension, which allows the generation for the links to the first, previous, next, last pages of the series. It is currently used on this site in the Template:gallery image to do just that.

Licence

Available under the terms of GNU General Public License 3.0

Produced in the year 2013.
GPLv3
For more information please see Freedom Porn:copyrights.


Useage

It adds the {{#sequential:}} function that can give you the links that you want, it takes the following named parameters:

  • get — one of the following: first, previous, next, last (case insensitive), defaults to next
  • namespace — which namespace do you wish to operate in, defaults to the namespace of the current page
  • current — what is the title of the current page, defaults to the title of the current page q;-)=
  • base — what is the base name of the files (the beginning of the file name that is unique for this sequence, but doesn't change within it), it will try to guess by taking the part of the name until the first digit

Installation

Copy the #Code into the appropriately named files in your extensions directory and add the following line into LocalSettings.php:

require_once( "{$IP}/extensions/SequentialLinks/SequentialLinks.php" );

Code

SequentialLinks.php

<?php
/**
 * @file
 * @author VolodyA! V Anarhist
 * @ingroup Extensions
 * @comment Allows one to gain a link to next, previous, first, last file in the sequence if properly named.
 * @copyright VolodyA! V Anarhist
 * @license GPL 3.0
 
 
**/
 
$dir=dirname( __FILE__ );
 
$wgExtensionCredits['parserhook'][] = array
(
   'path' => __FILE__,
   'name' => 'SequentialLinks',
   'descriptionmsg' => 'sequentiallinks-description',
   'version' => '0.9.0',
   'author' => '[http://www.freedomporn.org/smut/User:VolodyA!_V_Anarhist VolodyA! V Anarhist]',
   'url' => 'http://www.freedomporn.org/smut/Freedom_Porn:free_technology/SequentialLinks',
);
 
// Setting up the language
$wgExtensionMessagesFiles['SequentialLinks'] = "{$dir}/SequentialLinks.i18n.php";
 
// The object
require_once("${dir}/SequentialLinks_body.php");
$object=new SequentialLinks; // The worst name possible, but it's suitable for this task
 
// Setting up the startup hook
 
$wgHooks['ParserFirstCallInit'][] = array( $object, 'setupParsers' );

SequentialLinks_body.php

<?php
/**
 * @file
 * @author VolodyA! V Anarhist
 * @ingroup Extensions
 * @comment Allows one to gain a link to next, previous, first, last file in the sequence if properly named.
 * @copyright VolodyA! V Anarhist
 * @license GPL 3.0
 
 
**/
 
/**
 * Converts an array of values in form [0] => "name=value" into a real
 * associative array in form [name] => value
 * From: http://www.mediawiki.org/wiki/Manual:Parser_functions
 *
 * @param array string $options
 * @return array $results
 */
function extractOptions( array $options )
{
	$results = array();
 
	foreach ( $options as $option )
	{
	        $pair = explode( '=', $option, 2 );
	        if ( count( $pair ) == 2 )
		{
	                $name = strtolower( trim( $pair[0] ) );
	                $value = trim( $pair[1] );
	                $results[$name] = $value;
	        }
	}
 
	return $results;
}
 
function sanitiseOptions( array $options )
{
	global $wgTitle, $wgContLang;
 
	$result=array();
	// get
	if( array_key_exists( 'get', $options ) )
	{
		$result['get']=strtolower( $options['get'] );
	}
	else if( array_key_exists( 'which', $options ) )
	{
		$result['get']=strtolower( $options['which'] );
	}
	else
	{
		$result['get']='next';
	}
 
	// namespace
	if( array_key_exists( 'namespace', $options ) )
	{
		$result['namespace']=$wgContLang->getNsIndex( $options['namespace']);
	}
	else if( array_key_exists( 'ns', $options ) )
	{
		$result['namespace']=$wgContLang->getNsIndex( $options['ns'] );
	}
	else
	{
		$result['namespace']=$wgTitle->getNamespace();
	}
 
	//current
	if( array_key_exists( 'current', $options ) )
	{
		$result['current']=Title::newFromText( $options['current'] )->getDBkey();
	}
	else if( array_key_exists( 'title', $options ) )
	{
		$result['current']=Title::newFromText( $options['title'] )->getDBkey();
	}
	else
	{
		$result['current']=$wgTitle->getDBkey();
	}
 
	// base
	if( array_key_exists( 'base', $options ) && !is_null($options['base']) && $options['base']!='' )
	{
		$result['base']= Title::newFromText( $options['base'] )->getDBkey();
	}
	else
	{
		if( ereg("^([^0-9])+", $result['current'], $match ) )
		{
			$result['base']= $match[0];
		}
		else
		{
			$result['base']='';
		}
	}
 
	return $result;
}
 
class SequentialLinks
{
 
	public function setupParsers( &$parser )
	{
		$parser->setFunctionHook( 'sequential', array( $this, 'getLinkText') );
		return true;
	}
 
	public function getLinkText( )
	{
		global $wgTitle, $wgContLang;
 
		$args = func_get_args();
		$parser=array_shift($args); // we get the $parser leave the rest
		$options=sanitiseOptions ( extractOptions( $args ) );
 
		if($options['get']=='next')
		{
			$sel = array (
				'op'=>'>',
				'or'=>''
			);
		}
		else if($options['get']=='previous')
		{
			$sel = array (
				'op'=>'<',
				'or'=>' DESC'
			);
		}
		else if($options['get']=='first')
		{
			$sel = array (
				'op'=>'<',
				'or'=>''
			);
		}
		else if($options['get']=='last')
		{
			$sel = array (
				'op'=>'>',
				'or'=>' DESC'
			);
		}
		else
		{
			return false;
		}
 
		$dbr = wfGetDB( DB_SLAVE );
		$res = $dbr->select (
			'page',
			array( 'page_namespace', 'page_title' ),
			array (
				'page_is_redirect' => 0,
				'page_namespace' => $options['namespace'],
				'page_title LIKE '.$dbr->addQuotes($options['base'].'%'),
				'page_title '.$sel['op'].$dbr->addQuotes($options['current']),
			),
			__METHOD__, // method name, would be getLink()
			array(
				'ORDER BY' => 'page_title'.$sel['or'],
 
				'LIMIT' => 1
			)
		);
 
 
		if($dbr->numRows($res)) // if anything was found
		{
			$row = $dbr->fetchObject( $res );
			$dbr->freeResult( $res );
 
			$link = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
 
			return $link;
		}
 
		return false;
	}
}

SequentialLinks.i18n.php

<?php
/**
 * @file
 * @author VolodyA! V Anarhist
 * @ingroup Extensions
 * @comment Allows one to gain a link to next, previous, first, last file in the sequence if properly named.
 * @copyright VolodyA! V Anarhist
 * @license GPL 3.0
 
 
**/
 
$messages=array();
$magicWords = array();
 
$messages['en'] = array (
	'sequentiallinks-description' => 'Allows one to gain a link to next, previous, first, last file in the sequence if properly named.',
	'sequentiallinks-next' => 'Next',
	'sequentiallinks-previous' => 'Previous',
	'sequentiallinks-first' => 'First',
	'sequentiallinks-last' => 'Last',
);
 
$magicWords['en'] = array (
	'sequential' => array(0, 'sequential'),
);

See also