Code Duplication    Length = 161-177 lines in 2 locations

includes/parser/Preprocessor_DOM.php 1 location

@@ 1560-1720 (lines=161) @@
1557
 * @ingroup Parser
1558
 */
1559
// @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
1560
class PPTemplateFrame_DOM extends PPFrame_DOM {
1561
	// @codingStandardsIgnoreEnd
1562
1563
	public $numberedArgs, $namedArgs;
1564
1565
	/**
1566
	 * @var PPFrame_DOM
1567
	 */
1568
	public $parent;
1569
	public $numberedExpansionCache, $namedExpansionCache;
1570
1571
	/**
1572
	 * @param Preprocessor $preprocessor
1573
	 * @param bool|PPFrame_DOM $parent
1574
	 * @param array $numberedArgs
1575
	 * @param array $namedArgs
1576
	 * @param bool|Title $title
1577
	 */
1578
	public function __construct( $preprocessor, $parent = false, $numberedArgs = [],
1579
		$namedArgs = [], $title = false
1580
	) {
1581
		parent::__construct( $preprocessor );
1582
1583
		$this->parent = $parent;
1584
		$this->numberedArgs = $numberedArgs;
1585
		$this->namedArgs = $namedArgs;
1586
		$this->title = $title;
1587
		$pdbk = $title ? $title->getPrefixedDBkey() : false;
1588
		$this->titleCache = $parent->titleCache;
1589
		$this->titleCache[] = $pdbk;
1590
		$this->loopCheckHash = /*clone*/ $parent->loopCheckHash;
1591
		if ( $pdbk !== false ) {
1592
			$this->loopCheckHash[$pdbk] = true;
1593
		}
1594
		$this->depth = $parent->depth + 1;
1595
		$this->numberedExpansionCache = $this->namedExpansionCache = [];
1596
	}
1597
1598
	public function __toString() {
1599
		$s = 'tplframe{';
1600
		$first = true;
1601
		$args = $this->numberedArgs + $this->namedArgs;
1602
		foreach ( $args as $name => $value ) {
1603
			if ( $first ) {
1604
				$first = false;
1605
			} else {
1606
				$s .= ', ';
1607
			}
1608
			$s .= "\"$name\":\"" .
1609
				str_replace( '"', '\\"', $value->ownerDocument->saveXML( $value ) ) . '"';
1610
		}
1611
		$s .= '}';
1612
		return $s;
1613
	}
1614
1615
	/**
1616
	 * @throws MWException
1617
	 * @param string|int $key
1618
	 * @param string|PPNode_DOM|DOMDocument $root
1619
	 * @param int $flags
1620
	 * @return string
1621
	 */
1622
	public function cachedExpand( $key, $root, $flags = 0 ) {
1623
		if ( isset( $this->parent->childExpansionCache[$key] ) ) {
1624
			return $this->parent->childExpansionCache[$key];
1625
		}
1626
		$retval = $this->expand( $root, $flags );
1627
		if ( !$this->isVolatile() ) {
1628
			$this->parent->childExpansionCache[$key] = $retval;
1629
		}
1630
		return $retval;
1631
	}
1632
1633
	/**
1634
	 * Returns true if there are no arguments in this frame
1635
	 *
1636
	 * @return bool
1637
	 */
1638
	public function isEmpty() {
1639
		return !count( $this->numberedArgs ) && !count( $this->namedArgs );
1640
	}
1641
1642
	public function getArguments() {
1643
		$arguments = [];
1644
		foreach ( array_merge(
1645
				array_keys( $this->numberedArgs ),
1646
				array_keys( $this->namedArgs ) ) as $key ) {
1647
			$arguments[$key] = $this->getArgument( $key );
1648
		}
1649
		return $arguments;
1650
	}
1651
1652
	public function getNumberedArguments() {
1653
		$arguments = [];
1654
		foreach ( array_keys( $this->numberedArgs ) as $key ) {
1655
			$arguments[$key] = $this->getArgument( $key );
1656
		}
1657
		return $arguments;
1658
	}
1659
1660
	public function getNamedArguments() {
1661
		$arguments = [];
1662
		foreach ( array_keys( $this->namedArgs ) as $key ) {
1663
			$arguments[$key] = $this->getArgument( $key );
1664
		}
1665
		return $arguments;
1666
	}
1667
1668
	/**
1669
	 * @param int $index
1670
	 * @return string|bool
1671
	 */
1672
	public function getNumberedArgument( $index ) {
1673
		if ( !isset( $this->numberedArgs[$index] ) ) {
1674
			return false;
1675
		}
1676
		if ( !isset( $this->numberedExpansionCache[$index] ) ) {
1677
			# No trimming for unnamed arguments
1678
			$this->numberedExpansionCache[$index] = $this->parent->expand(
1679
				$this->numberedArgs[$index],
1680
				PPFrame::STRIP_COMMENTS
1681
			);
1682
		}
1683
		return $this->numberedExpansionCache[$index];
1684
	}
1685
1686
	/**
1687
	 * @param string $name
1688
	 * @return string|bool
1689
	 */
1690
	public function getNamedArgument( $name ) {
1691
		if ( !isset( $this->namedArgs[$name] ) ) {
1692
			return false;
1693
		}
1694
		if ( !isset( $this->namedExpansionCache[$name] ) ) {
1695
			# Trim named arguments post-expand, for backwards compatibility
1696
			$this->namedExpansionCache[$name] = trim(
1697
				$this->parent->expand( $this->namedArgs[$name], PPFrame::STRIP_COMMENTS ) );
1698
		}
1699
		return $this->namedExpansionCache[$name];
1700
	}
1701
1702
	/**
1703
	 * @param int|string $name
1704
	 * @return string|bool
1705
	 */
1706
	public function getArgument( $name ) {
1707
		$text = $this->getNumberedArgument( $name );
1708
		if ( $text === false ) {
1709
			$text = $this->getNamedArgument( $name );
1710
		}
1711
		return $text;
1712
	}
1713
1714
	/**
1715
	 * Return true if the frame is a template frame
1716
	 *
1717
	 * @return bool
1718
	 */
1719
	public function isTemplate() {
1720
		return true;
1721
	}
1722
1723
	public function setVolatile( $flag = true ) {

includes/parser/Preprocessor_Hash.php 1 location

@@ 1387-1563 (lines=177) @@
1384
 * @ingroup Parser
1385
 */
1386
// @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
1387
class PPTemplateFrame_Hash extends PPFrame_Hash {
1388
	// @codingStandardsIgnoreEnd
1389
1390
	public $numberedArgs, $namedArgs, $parent;
1391
	public $numberedExpansionCache, $namedExpansionCache;
1392
1393
	/**
1394
	 * @param Preprocessor $preprocessor
1395
	 * @param bool|PPFrame $parent
1396
	 * @param array $numberedArgs
1397
	 * @param array $namedArgs
1398
	 * @param bool|Title $title
1399
	 */
1400
	public function __construct( $preprocessor, $parent = false, $numberedArgs = [],
1401
		$namedArgs = [], $title = false
1402
	) {
1403
		parent::__construct( $preprocessor );
1404
1405
		$this->parent = $parent;
1406
		$this->numberedArgs = $numberedArgs;
1407
		$this->namedArgs = $namedArgs;
1408
		$this->title = $title;
1409
		$pdbk = $title ? $title->getPrefixedDBkey() : false;
1410
		$this->titleCache = $parent->titleCache;
1411
		$this->titleCache[] = $pdbk;
1412
		$this->loopCheckHash = /*clone*/ $parent->loopCheckHash;
1413
		if ( $pdbk !== false ) {
1414
			$this->loopCheckHash[$pdbk] = true;
1415
		}
1416
		$this->depth = $parent->depth + 1;
1417
		$this->numberedExpansionCache = $this->namedExpansionCache = [];
1418
	}
1419
1420
	public function __toString() {
1421
		$s = 'tplframe{';
1422
		$first = true;
1423
		$args = $this->numberedArgs + $this->namedArgs;
1424
		foreach ( $args as $name => $value ) {
1425
			if ( $first ) {
1426
				$first = false;
1427
			} else {
1428
				$s .= ', ';
1429
			}
1430
			$s .= "\"$name\":\"" .
1431
				str_replace( '"', '\\"', $value->__toString() ) . '"';
1432
		}
1433
		$s .= '}';
1434
		return $s;
1435
	}
1436
1437
	/**
1438
	 * @throws MWException
1439
	 * @param string|int $key
1440
	 * @param string|PPNode $root
1441
	 * @param int $flags
1442
	 * @return string
1443
	 */
1444
	public function cachedExpand( $key, $root, $flags = 0 ) {
1445
		if ( isset( $this->parent->childExpansionCache[$key] ) ) {
1446
			return $this->parent->childExpansionCache[$key];
1447
		}
1448
		$retval = $this->expand( $root, $flags );
1449
		if ( !$this->isVolatile() ) {
1450
			$this->parent->childExpansionCache[$key] = $retval;
1451
		}
1452
		return $retval;
1453
	}
1454
1455
	/**
1456
	 * Returns true if there are no arguments in this frame
1457
	 *
1458
	 * @return bool
1459
	 */
1460
	public function isEmpty() {
1461
		return !count( $this->numberedArgs ) && !count( $this->namedArgs );
1462
	}
1463
1464
	/**
1465
	 * @return array
1466
	 */
1467
	public function getArguments() {
1468
		$arguments = [];
1469
		foreach ( array_merge(
1470
				array_keys( $this->numberedArgs ),
1471
				array_keys( $this->namedArgs ) ) as $key ) {
1472
			$arguments[$key] = $this->getArgument( $key );
1473
		}
1474
		return $arguments;
1475
	}
1476
1477
	/**
1478
	 * @return array
1479
	 */
1480
	public function getNumberedArguments() {
1481
		$arguments = [];
1482
		foreach ( array_keys( $this->numberedArgs ) as $key ) {
1483
			$arguments[$key] = $this->getArgument( $key );
1484
		}
1485
		return $arguments;
1486
	}
1487
1488
	/**
1489
	 * @return array
1490
	 */
1491
	public function getNamedArguments() {
1492
		$arguments = [];
1493
		foreach ( array_keys( $this->namedArgs ) as $key ) {
1494
			$arguments[$key] = $this->getArgument( $key );
1495
		}
1496
		return $arguments;
1497
	}
1498
1499
	/**
1500
	 * @param int $index
1501
	 * @return string|bool
1502
	 */
1503
	public function getNumberedArgument( $index ) {
1504
		if ( !isset( $this->numberedArgs[$index] ) ) {
1505
			return false;
1506
		}
1507
		if ( !isset( $this->numberedExpansionCache[$index] ) ) {
1508
			# No trimming for unnamed arguments
1509
			$this->numberedExpansionCache[$index] = $this->parent->expand(
1510
				$this->numberedArgs[$index],
1511
				PPFrame::STRIP_COMMENTS
1512
			);
1513
		}
1514
		return $this->numberedExpansionCache[$index];
1515
	}
1516
1517
	/**
1518
	 * @param string $name
1519
	 * @return string|bool
1520
	 */
1521
	public function getNamedArgument( $name ) {
1522
		if ( !isset( $this->namedArgs[$name] ) ) {
1523
			return false;
1524
		}
1525
		if ( !isset( $this->namedExpansionCache[$name] ) ) {
1526
			# Trim named arguments post-expand, for backwards compatibility
1527
			$this->namedExpansionCache[$name] = trim(
1528
				$this->parent->expand( $this->namedArgs[$name], PPFrame::STRIP_COMMENTS ) );
1529
		}
1530
		return $this->namedExpansionCache[$name];
1531
	}
1532
1533
	/**
1534
	 * @param int|string $name
1535
	 * @return string|bool
1536
	 */
1537
	public function getArgument( $name ) {
1538
		$text = $this->getNumberedArgument( $name );
1539
		if ( $text === false ) {
1540
			$text = $this->getNamedArgument( $name );
1541
		}
1542
		return $text;
1543
	}
1544
1545
	/**
1546
	 * Return true if the frame is a template frame
1547
	 *
1548
	 * @return bool
1549
	 */
1550
	public function isTemplate() {
1551
		return true;
1552
	}
1553
1554
	public function setVolatile( $flag = true ) {
1555
		parent::setVolatile( $flag );
1556
		$this->parent->setVolatile( $flag );
1557
	}
1558
1559
	public function setTTL( $ttl ) {
1560
		parent::setTTL( $ttl );
1561
		$this->parent->setTTL( $ttl );
1562
	}
1563
}
1564
1565
/**
1566
 * Expansion frame with custom arguments