Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
WikiReference
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 7
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 getStorageRow
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 fromStorageRow
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 toStorageRow
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 makeTitle
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTargetIdentifier
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Flow\Model;
4
5use Flow\Exception\InvalidInputException;
6use MediaWiki\Title\Title;
7use MediaWiki\WikiMap\WikiMap;
8
9class WikiReference extends Reference {
10    public const TYPE_FILE = 'file';
11    public const TYPE_TEMPLATE = 'template';
12    public const TYPE_CATEGORY = 'category';
13
14    /** @var Title */
15    protected $target;
16
17    /**
18     * @param UUID $id Id of the reference
19     * @param string $wiki Wiki ID of the reference source
20     * @param UUID $srcWorkflow ID of the source Workflow
21     * @param Title $srcTitle Title of the Workflow from which this reference comes.
22     * @param string $objectType Output of getRevisionType for the AbstractRevision that this
23     *   reference comes from.
24     * @param UUID $objectId Unique identifier for the revisioned object containing the reference.
25     * @param string $type Type of reference
26     * @param Title $targetTitle Title of the reference's target.
27     */
28    public function __construct(
29        UUID $id,
30        $wiki,
31        UUID $srcWorkflow,
32        Title $srcTitle,
33        $objectType,
34        UUID $objectId,
35        $type,
36        Title $targetTitle
37    ) {
38        $this->target = $targetTitle;
39
40        $this->validTypes = array_merge( $this->validTypes,
41            [
42                self::TYPE_FILE,
43                self::TYPE_TEMPLATE,
44                self::TYPE_CATEGORY,
45            ]
46        );
47
48        parent::__construct( $id, $wiki, $srcWorkflow, $srcTitle, $objectType, $objectId, $type );
49    }
50
51    /**
52     * Gets the storage row for this WikiReference
53     *
54     * @return array
55     */
56    public function getStorageRow() {
57        return parent::getStorageRow() + [
58            'ref_target_namespace' => $this->target->getNamespace(),
59            'ref_target_title' => $this->target->getDBkey(),
60        ];
61    }
62
63    /**
64     * Instantiates a WikiReference object from a storage row.
65     *
66     * @param array $row
67     * @return WikiReference
68     */
69    public static function fromStorageRow( $row ) {
70        // TODO: Remove this UUID::create() call when the field is populated
71        // everywhere relevant.
72        $id = isset( $row['ref_id'] ) ? UUID::create( $row['ref_id'] ) : UUID::create();
73        $workflow = UUID::create( $row['ref_src_workflow_id'] );
74        $objectType = $row['ref_src_object_type'];
75        $objectId = UUID::create( $row['ref_src_object_id'] );
76        $srcTitle = self::makeTitle( $row['ref_src_namespace'], $row['ref_src_title'] );
77        $targetTitle = self::makeTitle( $row['ref_target_namespace'], $row['ref_target_title'] );
78        $type = $row['ref_type'];
79        $wiki = $row['ref_src_wiki'];
80
81        return new WikiReference(
82            $id, $wiki, $workflow, $srcTitle, $objectType, $objectId, $type, $targetTitle
83        );
84    }
85
86    /**
87     * Gets the storage row from an object.
88     * Helper for BasicObjectMapper.
89     * @param WikiReference $object
90     * @return array
91     */
92    public static function toStorageRow( WikiReference $object ) {
93        return $object->getStorageRow();
94    }
95
96    /**
97     * Gets a Title given a namespace number and title text
98     *
99     * Many loaded references typically point to the same Title, so we cache those
100     * instead of generating a bunch of duplicate title classes.
101     *
102     * @param int $namespace Namespace number
103     * @param string $title Title text
104     * @return Title|null
105     */
106    public static function makeTitle( $namespace, $title ) {
107        try {
108            return Workflow::getFromTitleCache( WikiMap::getCurrentWikiId(), $namespace, $title );
109        } catch ( InvalidInputException $e ) {
110            // duplicate Title::makeTitleSafe which returns null on failure,
111            // but only for InvalidInputException
112
113            wfDebugLog( 'Flow', __METHOD__ . ": Invalid title.  Namespace: $namespace, Title text: $title" );
114
115            return null;
116        }
117    }
118
119    public function getTitle() {
120        return $this->target;
121    }
122
123    public function getTargetIdentifier() {
124        return 'title:' . $this->getTitle()->getPrefixedDBkey();
125    }
126}