Advertisement
Guest User

renophaston

a guest
Sep 28th, 2009
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.31 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @author Jean-Lou Dupont
  4.  * @package PageAfterAndBefore
  5.  * @version 1.0.7
  6.  * @Id $Id: PageAfterAndBefore.body.php 812 2008-01-03 01:42:46Z jeanlou.dupont $
  7. */
  8. //<source lang=php>
  9. class PageAfterAndBefore
  10. {
  11.     const thisName = 'PageAfterAndBefore';
  12.     const thisType = 'other';
  13.    
  14.     public function mg_pagebefore( &$parser )
  15.     {
  16.         $params = StubManager::processArgList( func_get_args(), true );
  17.         $this->setupParams($params);
  18.  
  19.         $res = $this->getPages( $params['namespace'], $params['title'], 'desc',$params['category'] );
  20.  
  21.         return (isset($res[0])) ? $res[0]:null;
  22.     }
  23.     public function mg_pageafter( &$parser )
  24.     {
  25.         $params = StubManager::processArgList( func_get_args(), true );
  26.         $this->setupParams($params);
  27.  
  28.         $res = $this->getPages( $params['namespace'], $params['title'], 'asc',$params['category'] );       
  29.  
  30.         return (isset($res[0])) ? $res[0]:null;
  31.     }
  32.     public function mg_firstpage( &$parser )
  33.     // If 'namespace' is not supplied, defaults to current page's namespace
  34.     {
  35.         $params = StubManager::processArgList( func_get_args(), true );
  36.         $this->setupParams($params);
  37.        
  38.         $res = $this->getPages( $params['namespace'], '' , 'asc', $params['category'] );
  39.  
  40.         if (!isset($res[0]))
  41.             return null;
  42.  
  43.         // filter out if requested and currentpage==firstpage
  44.         $currentpage = $this->getCurrentPage( $ns, $title );
  45.         if ( ($this->filterCurrent( $params )) && ( $res[0] == $currentpage))
  46.             return '';
  47.            
  48.         return $res[0];
  49.     }
  50.     public function mg_lastpage( &$parser )
  51.     // If 'namespace' is not supplied, defaults to current page's namespace
  52.     {
  53.         $params = StubManager::processArgList( func_get_args(), true );
  54.         $this->setupParams($params);
  55.  
  56.         $res = $this->getPages( $params['namespace'], '' , 'desc', $params['category'] );      
  57.  
  58.         if (!isset($res[0]))
  59.             return null;
  60.  
  61.         // filter out if requested and currentpage==lastpage
  62.         $currentpage = $this->getCurrentPage( $ns, $title );
  63.         if ( ($this->filterCurrent( $params )) && ( $res[0] == $currentpage))
  64.             return '';
  65.  
  66.         return $res[0];
  67.     }
  68.     /**
  69.      * Verifies the 'filtercurrent' parameter
  70.      */
  71.     protected function filterCurrent( &$params )
  72.     {
  73.         // true by default.
  74.         if ( !isset( $params['filtercurrent'] ))
  75.             return true;
  76.            
  77.         $f = strtolower( $params['filtercurrent'] );
  78.         return ( ($f=='y') || ($f=='yes') || ($f=='1')) ? true:false;
  79.     }
  80.     private function setupParams( &$params )
  81.     {
  82.         $this->getCurrentPage( $d_ns_name, $d_title );
  83.  
  84.         $template = array(
  85.             array( 'key' => 'context',       'index' => '0', 'default' => 'context0' ),
  86.             array( 'key' => 'namespace',     'index' => '1', 'default' => "{$d_ns_name}" ),
  87.             array( 'key' => 'title',         'index' => '2', 'default' => "{$d_title}" ),
  88.             array( 'key' => 'category',      'index' => '3', 'default' => '' ),
  89.             array( 'key' => 'filtercurrent', 'index' => '4', 'default' => 'yes' ),
  90.             #array( 'key' => '', 'index' => '', 'default' => '' ),
  91.         );
  92.         StubManager::initParams( $params, $template );
  93.     }
  94.     /**
  95.      * Returns the current title name in database format.
  96.      */
  97.     public function getCurrentPage( &$ns, &$title )
  98.     {
  99.         global $wgTitle;
  100.        
  101.         $ns_num = $wgTitle->getNamespace();
  102.  
  103.         $ns = ( $ns_num == NS_MAIN ) ? '':MWNamespace::getCanonicalName( $ns_num );
  104.         $title  = $wgTitle->getDBkey();
  105.        
  106.         return (empty( $ns )) ? $title:$ns.':'.$title ;
  107.     }
  108.     public function getPages( $namespace, $titlename, $dir='asc', $category = null, $limit=2 )
  109.     {
  110.         $orderDir = ($dir=="asc")      ? "ASC" : "DESC";
  111.         $cmpDir   = ($orderDir=='ASC') ? "1"   : "-1";
  112.         $where = "";
  113.         $cat = null;
  114.         $pages = array();
  115.                        
  116.         $dbr      =& wfGetDB( DB_SLAVE );
  117.         $page     = $dbr->tableName( 'page' );
  118.         $catlinks = $dbr->tableName( 'categorylinks' );
  119.  
  120.         if (!empty($titlename))
  121.         {
  122.             if (!empty($namespace))
  123.                 $namespace.=':';
  124.  
  125.             $title   =  Title::newFromText( $namespace.$titlename );
  126.             if (!is_object($title))
  127.                 return null;
  128.                
  129.             $ns = $title->getNamespace();
  130.             $unescaped_key = $title->getDBkey();
  131.            
  132.             // fix for apostrophes in title generating database access error
  133.             $key = $dbr->strencode( $unescaped_key );
  134.            
  135.             if ($ns !== NS_MAIN)
  136.                 $namespace = MWNamespace::getCanonicalName( $ns );
  137.             else
  138.                 $namespace ='';
  139.                
  140.             $where = "AND STRCMP({$page}.page_title,'{$key}')={$cmpDir}";
  141.         }
  142.         else
  143.         {
  144.             if (!empty($namespace))
  145.                 $ns = MWNamespace::getCanonicalIndex( strtolower( $namespace ) );
  146.             else
  147.                 $ns = NS_MAIN;
  148.         }
  149.         // If a category is specified.
  150.         if (!empty($category))
  151.         {
  152.             // fix for apostrophes in title generating database access error           
  153.             $category = $dbr->strencode( $category );
  154.            
  155.             $where .= " AND {$catlinks}.cl_to = '{$category}' AND {$catlinks}.cl_from = page_id";
  156.             $cat = ", {$catlinks}";
  157.         }
  158.                
  159.         $query = "SELECT page_namespace, page_title, page_id FROM {$page} {$cat} WHERE {$page}.page_namespace = {$ns} {$where} ORDER BY {$page}.page_title {$orderDir} LIMIT {$limit}";
  160.         $results = $dbr->query( $query );
  161.         $count   = $dbr->numRows( $results );
  162.        
  163.         if ($ns !== NS_MAIN)
  164.             $namespace = MWNamespace::getCanonicalName( $ns );
  165.         else
  166.             $namespace ='';
  167.        
  168.         if (!empty( $namespace ))
  169.             $namespace .= ':';
  170.        
  171.         if ($count>=1)
  172.             while( $row = $dbr->fetchObject( $results ) )
  173.                 $pages[] = $namespace.$row->page_title;
  174.  
  175.         $dbr->freeResult( $results );
  176.         return $pages;
  177.     }
  178.  
  179. } // end class 
  180. //</source>
  181.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement