Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
CargoAttach
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
132
0.00% covered (danger)
0.00%
0 / 1
 run
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
132
1<?php
2/**
3 * Class for the #cargo_attach parser function.
4 *
5 * @author Yaron Koren
6 * @ingroup Cargo
7 */
8
9use MediaWiki\Title\Title;
10
11class CargoAttach {
12
13    /**
14     * Handles the #cargo_attach parser function.
15     *
16     * @param Parser $parser
17     * @return string Wikitext
18     */
19    public static function run( Parser $parser ) {
20        if ( !$parser->getTitle() || $parser->getTitle()->getNamespace() != NS_TEMPLATE ) {
21            return CargoUtils::formatError( "Error: #cargo_attach must be called from a template page." );
22        }
23
24        $params = func_get_args();
25        array_shift( $params ); // we already know the $parser...
26
27        $tableName = null;
28        foreach ( $params as $param ) {
29            $parts = explode( '=', $param, 2 );
30
31            if ( count( $parts ) != 2 ) {
32                continue;
33            }
34            $key = trim( $parts[0] );
35            $value = trim( $parts[1] );
36            if ( $key == '_table' || $key == 'table' ) {
37                $tableName = $value;
38            }
39        }
40
41        // Validate table name.
42        if ( $tableName == '' ) {
43            return CargoUtils::formatError( wfMessage( "cargo-notable" )->parse() );
44        }
45
46        $dbw = CargoUtils::getMainDBForWrite();
47        $res = $dbw->select( 'cargo_tables', 'COUNT(*) AS total', [ 'main_table' => $tableName ], __METHOD__ );
48        $row = $res->fetchRow();
49        if ( $row && $row['total'] == 0 ) {
50            return CargoUtils::formatError( "Error: The specified table, \"$tableName\", does not exist." );
51        }
52
53        $parserOutput = $parser->getOutput();
54        $parserOutput->setPageProperty( 'CargoAttachedTable', $tableName );
55
56        // Link to the Special:ViewTable page for this table, and to the template that
57        // declares it.
58        $declaringTemplateID = CargoUtils::getTemplateIDForDBTable( $tableName );
59        $declaringTemplateTitle = Title::newFromID( $declaringTemplateID );
60        if ( $declaringTemplateTitle !== null ) {
61            $declaringTemplateLink = '[[' . $declaringTemplateTitle->getFullText() . '|' .
62                $declaringTemplateTitle->getText() . ']]';
63            $text = wfMessage( 'cargo-addsrows', $tableName, $declaringTemplateLink )->text();
64        } else {
65            $text = wfMessage( 'cargo-addsrows-missingdeclare', $tableName )->text();
66        }
67        $ct = SpecialPage::getTitleFor( 'CargoTables' );
68        $pageName = $ct->getPrefixedText() . "/$tableName";
69        $viewTableMsg = wfMessage( 'cargo-cargotables-viewtablelink' )->parse();
70        $text .= " [[$pageName|$viewTableMsg]].";
71
72        return $text;
73    }
74
75}