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